简体   繁体   English

信号的互相关/相似度-计算时滞

[英]Cross correlation / similarity of signals - calculate time lag

I have two signals which I want to compare in terms of similarity. 我有两个信号要比较相似性。 One is smaller (by time) than the other one. (按时间)一个比另一个小。 If I use correlation to find the highest similarity it tells me that the highest values is at an value where I would'nt expect it. 如果我使用相关性来找到最高的相似度,它会告诉我最高的值是我不期望的值。

Could anyone give me a hint if I am just thinking "wrong" or is correlation the wrong tool for that kind of a problem? 如果我只是在想“错误”或相关性是解决此类问题的错误工具,谁能给我提示吗?

My setup: 我的设置:

import numpy
import matplotlib.pyplot as plt

signal_a = numpy.array([10, 20, 10, 30, 20, 10, 28, 22, 10])
signal_b = numpy.array([28, 22])
correlations = numpy.correlate(signal_a, signal_b, mode = "full")

print(correlations)
plt.plot(correlations)

Outputs this chart and correlations array 输出此图表和相关性数组

The highest correlation of [28, 22] is calculated at the position [..., 30, 20, ...]. [28,22]的最高相关性是在位置[...,30,20,...]处计算的。 I understand the formula and why it is 1280. But I am actually looking for [..., 28, 22, ...] as it is exactly (at that case) what I am looking for (Signal B). 我知道公式以及为什么是1280。但是我实际上是在寻找[...,28,22,...],因为(在这种情况下)正是我要寻找的(信号B)。

Is correlation the right thing to do? 关联正确吗? I have found so many sources where correlation gets used to detect similarity. 我发现了很多使用相关性检测相似性的资源。 Shouldn't the same values be more similar than any other ones? 相同的值不应该比其他任何值更相似吗?

Instead of looking into correlation you might look into difference in values to detect similarity. 除了查看相关性之外,您还可以查看值的差异来检测相似性。 You could for example pick every 2 elements in a (if b has length 2) and look at the absolute values of the differences: 例如,您可以选择a中的每2个元素(如果b的长度为2),然后查看差异的绝对值:

 import numpy as np
 import matplotlib.pyplot as plt

 signal_a = np.array([10, 20, 10, 30, 20, 10, 28, 22, 10])
 signal_b = np.array([28, 22])
 N2 = len(signal_b)

 diffs = []
 for i in range(len(signal_a) - len(signal_b) + 1):
      diff_ab = signal_a[i:i+N2] - signal_b
      diffs.append(sum(abs(diff_ab)))

 print(diffs)
 plt.plot(diffs)

And find a minimum in the diffs array. 并在diffs数组中找到最小值。 Instead of abs() you could use the squared value of the difference as well. 代替abs(),您也可以使用差异的平方值。

One possible solution to your problem is Mean Squared Error (MSE) . 解决您的问题的一种可能方法是均方误差(MSE) Given two signals a and b of same dimensions, MSE is the average value of the element-wise squares of the difference between a and b . 给定两个a相同尺寸的信号ab ,MSE是ab之间的差的元素方平方的平均值。 The code would look like follows (based on this ): 该代码看起来像以下(基于 ):

import numpy as np
import matplotlib.pyplot as plt

a = np.array([10, 20, 10, 30, 20, 10, 28, 22, 10])
b = np.array([28, 22])
mse = np.ndarray((len(a) - len(b) + 1))

for i in range(c.size):
    mse[i] = np.square(np.subtract(a[i:i+len(b)],b)).mean()

print(mse.argmin())
plt.plot(mse)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM