python - 2 个时间相关的多维信号(信号向量)的相关性

标签 python numpy

我有一个矩阵 M1 ,其中每一行都是一个随时间变化的信号。

我还有另一个相同维度的矩阵 M2,其中每一行也是一个时间相关信号,用作"template"以识别第一个矩阵中的信号形状。

结果我想要一个列向量 v,其中 v [i] 是 M1 的第 i 行和 M2 的第 i 行之间的相关性。

我研究了 numpy 的 corrcoef 函数并尝试了以下代码:

import numpy as np

M1 = np.array ([
    [1, 2, 3, 4],
    [2, 3, 1, 4]
])

M2 = np.array ([
    [10, 20, 30, 40],
    [20, 30, 10, 40]
])

print (np.corrcoef (M1, M2))

打印:

[[ 1.   0.4  1.   0.4]
 [ 0.4  1.   0.4  1. ]
 [ 1.   0.4  1.   0.4]
 [ 0.4  1.   0.4  1. ]]

我一直在阅读文档,但对于必须选择此矩阵的哪些条目作为向量 v 的条目,我仍然感到困惑。

有人能帮忙吗?

(我已经研究了几个类似问题的 S.O. 答案,但还没有看到曙光......)

代码上下文:

有 256 行(信号),我在“主信号”上运行了一个包含 200 个样本的滑动窗口,它的长度为 10k 个样本。所以 M1 和 M2 都是 256 行 x 200 列。对于错误的 10k 样本,我们深表歉意。那是总信号长度。通过使用与滑动模板的相关性,我尝试找到模板最匹配的偏移量。实际上,我正在 256 channel 侵入性心电图中寻找 QRS 复合波(或者更确切地说,医生称之为电图)。

    lg.info ('Processor: {}, time: {}, markers: {}'.format (self.key, dt.datetime.now ().time (), len (self.data.markers)))

    # Compute average signal shape over preexisting markers and uses that as a template to find the others.
    # All generated markers will have the width of the widest preexisting one.

    template = np.zeros ((self.data.samples.shape [0], self.bufferWidthSteps))

    # Add intervals that were marked in advance
    nrOfTerms = 0
    maxWidthSteps = 0
    newMarkers = []
    for marker in self.data.markers:
        if marker.key == self.markerKey:

            # Find start and stop sample index    
            startIndex = marker.tSteps - marker.stampWidthSteps // 2
            stopIndex = marker.tSteps + marker.stampWidthSteps // 2

            # Extract relevant slice from samples and add it to template
            template += np.hstack ((self.data.samples [ : , startIndex : stopIndex], np.zeros ((self.data.samples.shape [0], self.bufferWidthSteps - marker.stampWidthSteps))))

            # Adapt nr of added terms to facilitate averaging
            nrOfTerms += 1

            # Remember maximum width of previously marked QRS complexes
            maxWidthSteps = max (maxWidthSteps, marker.stampWidthSteps)
        else:
            # Preexisting markers with non-matching keys are just copied to the new marker list
            # Preexisting markers with a matching key are omitted from the new marker list
            newMarkers.append (marker)

    # Compute average of intervals that were marked in advance
    template = template [ : , 0 : maxWidthSteps] / nrOfTerms
    halfWidthSteps = maxWidthSteps // 2

    # Append markers of intervals that yield an above threshold correlation with the averaged marked intervals
    firstIndex = 0
    stopIndex = self.data.samples.shape [1] - maxWidthSteps
    while firstIndex < stopIndex:
        corr = np.corrcoef (
            template,
            self.data.samples [ : , firstIndex : firstIndex + maxWidthSteps]
        )

        diag = np.diagonal (
            corr,
            template.shape [0]
        )

        meanCorr = np.mean (diag)

        if meanCorr > self.correlationThreshold:
            newMarkers.append ([self.markerFactories [self.markerKey] .make (firstIndex + halfWidthSteps, maxWidthSteps)])

            # Prevent overlapping markers
            firstIndex += maxWidthSteps
        else:
            firstIndex += 5

    self.data.markers = newMarkers

    lg.info ('Processor: {}, time: {}, markers: {}'.format (self.key, dt.datetime.now ().time (), len (self.data.markers)))

最佳答案

基于 this solution为了找到两个 2D 数组之间的相关矩阵,我们可以使用类似的方法来找到相关向量,计算两个数组中相应行之间的相关性。实现看起来像这样 -

def corr2_coeff_rowwise(A,B):
    # Rowwise mean of input arrays & subtract from input arrays themeselves
    A_mA = A - A.mean(1)[:,None]
    B_mB = B - B.mean(1)[:,None]

    # Sum of squares across rows
    ssA = (A_mA**2).sum(1);
    ssB = (B_mB**2).sum(1);

    # Finally get corr coeff
    return np.einsum('ij,ij->i',A_mA,B_mB)/np.sqrt(ssA*ssB)

我们还可以通过在其中引入einsum 魔法来进一步优化该部分以获得ssAssB!

def corr2_coeff_rowwise2(A,B):
    A_mA = A - A.mean(1)[:,None]
    B_mB = B - B.mean(1)[:,None]
    ssA = np.einsum('ij,ij->i',A_mA,A_mA)
    ssB = np.einsum('ij,ij->i',B_mB,B_mB)
    return np.einsum('ij,ij->i',A_mA,B_mB)/np.sqrt(ssA*ssB)

sample 运行-

In [164]: M1 = np.array ([
     ...:     [1, 2, 3, 4],
     ...:     [2, 3, 1, 4.5]
     ...: ])
     ...: 
     ...: M2 = np.array ([
     ...:     [10, 20, 33, 40],
     ...:     [20, 35, 15, 40]
     ...: ])
     ...: 

In [165]: corr2_coeff_rowwise(M1, M2)
Out[165]: array([ 0.99411402,  0.96131896])

In [166]: corr2_coeff_rowwise2(M1, M2)
Out[166]: array([ 0.99411402,  0.96131896])

运行时测试-

In [97]: M1 = np.random.rand(256,200)
    ...: M2 = np.random.rand(256,200)
    ...: 

In [98]: out1 = np.diagonal (np.corrcoef (M1, M2), M1.shape [0])
    ...: out2 = corr2_coeff_rowwise(M1, M2)
    ...: out3 = corr2_coeff_rowwise2(M1, M2)
    ...: 

In [99]: np.allclose(out1, out2)
Out[99]: True

In [100]: np.allclose(out1, out3)
Out[100]: True

In [101]: %timeit np.diagonal (np.corrcoef (M1, M2), M1.shape [0])
     ...: %timeit corr2_coeff_rowwise(M1, M2)
     ...: %timeit corr2_coeff_rowwise2(M1, M2)
     ...: 
100 loops, best of 3: 9.5 ms per loop
1000 loops, best of 3: 554 µs per loop
1000 loops, best of 3: 430 µs per loop

20x+ 使用 einsum 比内置 np.corrcoef 提速!

关于python - 2 个时间相关的多维信号(信号向量)的相关性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41700840/

相关文章:

python - Python 和 Numpy 中自定义 NaN float 的行为

python - 直方图只有一个条

python - tkinter 按钮命令无需单击即可运行功能?

python - 遍历python中的字典列表

python - Paramiko 跳过 channel 接收上的一些数据

python - 从 3D 创建 4D 上对角线数组

python - 如何使用具有大量行的 DataFrame 使线图可读

python - 根据第 0 个列表的 argsort 对嵌套列表中的列表进行排序

python - 在 Python 中获取 HTTP GET 变量

python - 在 Python 中创建一个重写父类(super class)方法的子类