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

Jac*_*oge 5 python numpy

我有一个矩阵 M1 ,其中每一行都是一个与时间相关的信号。

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

我想要一个列向量 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))
Run Code Online (Sandbox Code Playgroud)

打印:

[[ 1.   0.4  1.   0.4]
 [ 0.4  1.   0.4  1. ]
 [ 1.   0.4  1.   0.4]
 [ 0.4  1.   0.4  1. ]]
Run Code Online (Sandbox Code Playgroud)

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

有人可以帮忙吗?

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

代码上下文:

有 256 行(信号),我在“主信号”上运行 200 个样本的滑动窗口,该信号的长度为 10k 个样本。所以 M1 和 M2 都是 256 行 x 200 列。对于错误的 10k 样本,我们深表歉意。这就是信号的总长度。通过使用与滑动模板的相关性,我尝试找到模板最匹配的偏移量。实际上,我正在寻找 256 通道侵入性心电图(或者更确切地说,医生所说的电描记图)中的 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)))
Run Code Online (Sandbox Code Playgroud)

Jac*_*oge 0

我认为是这样的:(如有错误,请指正!)

import numpy as np

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

M2 = np.array ([
    [10, 20, 33, 40],
    [20, 35, 15, 40]
])

v = np.diagonal (np.corrcoef (M1, M2), M1.shape [0])

print (v)
Run Code Online (Sandbox Code Playgroud)

哪个打印:

[ 0.99411402  0.96131896]
Run Code Online (Sandbox Code Playgroud)

由于它只有一维,我可以将其视为列向量......