且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 numpy.correlate 进行自相关?

更新时间:2021-09-12 23:47:16

为了回答你的第一个问题,numpy.correlate(a, v, mode) 正在执行 a 与 v 的反面,并给出由指定模式裁剪的结果.卷积的定义,C(t)=∑ -∞ aivt+i 其中 -∞

To answer your first question, numpy.correlate(a, v, mode) is performing the convolution of a with the reverse of v and giving the results clipped by the specified mode. The definition of convolution, C(t)=∑ -∞ < i < ∞ aivt+i where -∞ < t < ∞, allows for results from -∞ to ∞, but you obviously can't store an infinitely long array. So it has to be clipped, and that is where the mode comes in. There are 3 different modes: full, same, & valid:

  • 完整"模式为​​每个 t 返回结果,其中 av 有一些重叠.
  • 相同"模式返回与最短向量(av)长度相同的结果.
  • "valid" 模式仅在 av 彼此完全重叠时才返回结果.numpy.convolve 的文档代码> 提供了有关模式的更多详细信息.
  • "full" mode returns results for every t where both a and v have some overlap.
  • "same" mode returns a result with the same length as the shortest vector (a or v).
  • "valid" mode returns results only when a and v completely overlap each other. The documentation for numpy.convolve gives more detail on the modes.

对于你的第二个问题,我认为 numpy.correlate 给你自相关,它只是给你更多一点.自相关用于确定信号或函数在特定时间差与其自身的相似程度.在时间差为 0 时,自相关应该最高,因为信号与其自身相同,因此您预计自相关结果数组中的第一个元素将是最大的.但是,相关性不是从时间差 0 开始.它从负时间差开始,接近 0,然后变为正值.也就是说,您期望:

For your second question, I think numpy.correlate is giving you the autocorrelation, it is just giving you a little more as well. The autocorrelation is used to find how similar a signal, or function, is to itself at a certain time difference. At a time difference of 0, the auto-correlation should be the highest because the signal is identical to itself, so you expected that the first element in the autocorrelation result array would be the greatest. However, the correlation is not starting at a time difference of 0. It starts at a negative time difference, closes to 0, and then goes positive. That is, you were expecting:

自相关(a) = ∑ -∞ aivt+i 其中 0

autocorrelation(a) = ∑ -∞ < i < ∞ aivt+i where 0 <= t < ∞

但是你得到的是:

自相关(a) = ∑ -∞ aivt+i 其中 -∞

autocorrelation(a) = ∑ -∞ < i < ∞ aivt+i where -∞ < t < ∞

您需要做的是取相关结果的后半部分,这应该是您正在寻找的自相关.一个简单的python函数来做到这一点:

What you need to do is take the last half of your correlation result, and that should be the autocorrelation you are looking for. A simple python function to do that would be:

def autocorr(x):
    result = numpy.correlate(x, x, mode='full')
    return result[result.size/2:]

当然,您需要进行错误检查以确保 x 实际上是一个一维数组.此外,这种解释在数学上可能不是最严格的.我一直在讨论无穷大,因为卷积的定义使用它们,但这不一定适用于自相关.所以,这个解释的理论部分可能有点不稳定,但希望实际结果会有所帮助.这些 有关自相关的页面非常有用,如果您不介意涉足符号和繁重的概念,可以为您提供更好的理论背景.

You will, of course, need error checking to make sure that x is actually a 1-d array. Also, this explanation probably isn't the most mathematically rigorous. I've been throwing around infinities because the definition of convolution uses them, but that doesn't necessarily apply for autocorrelation. So, the theoretical portion of this explanation may be slightly wonky, but hopefully the practical results are helpful. These pages on autocorrelation are pretty helpful, and can give you a much better theoretical background if you don't mind wading through the notation and heavy concepts.