我有一个矩阵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))
哪个打印:
^{pr2}$我一直在阅读文档,但是我仍然不明白这个矩阵中的哪些条目必须作为向量v的条目
有人能帮忙吗?在
(我已经研究过几个类似问题的S.O.答案,但还没有看到曙光……)
代码上下文:
有256行(信号),我在“主信号”上运行了200个样本的滑动窗口,主信号的长度为10k个样本。所以M1和M2都是256行x200列。很抱歉,错误的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)))
由于对纽比阵法的魔法了解不够,我只会挑出一行,把每一行分别喂给科科科夫
为了np.数组列输出
^{pr2}$我相信使用numpy广播/索引功能会更好
我想是这样的:(如果错误,请纠正!)
哪个打印:
^{pr2}$因为它只有一个维度,所以我可以把它看作一个列向量。。。在
在^{} 寻找两个
2D
数组之间的相关矩阵的基础上,我们可以有一个相似的查找相关向量的方法来计算两个数组中相应行之间的相关性。实现应该是这样的-我们还可以通过在其中引入}!在
^{pr2}$einsum
魔法来进一步优化部件,得到ssA
和{样本运行-
运行时测试-
20x+
在内置的np.corrcoef
上使用einsum
加速!在相关问题 更多 >
编程相关推荐