numpy.correlate命令的文档说明两个数组的互相关被计算为信号处理的一般定义:
z [k] = sum_n a [n] * conj(v [n + k])
似乎并非如此。看起来相关性被翻转了。这意味着要切换公式最后一个项中的符号
z [k] = sum_n a [n] * conj(v [n-k])
或两个输入向量的顺序错误。给定公式的简单实现是:
x = [1.0, 2.0, 3.0]
y = [0.0, 0.5, 2.0]
y_padded = numpy.append( [0.0, 0.0] , y)
y_padded = numpy.append(y_padded, [0.0, 0.0] )
crosscorr_numpy = numpy.correlate(x, y, mode='full')
crosscorr_self = numpy.zeros(5)
for k in range(5):
for i in range(3):
crosscorr_self[k] += x[i] * y_padded[i+k]
print crosscorr_numpy
print crosscorr_self
您可以很容易地看到生成的向量的顺序错误。当它没有产生我预期的结果时我感到非常困惑,并且非常确定(在与同事讨论之后)这是一个错误。
答案 0 :(得分:0)
您使用的是哪个版本的NumPy?在我的Debian Squeeze框中:
In [1]: import numpy as np
In [2]: np.__version__
Out[2]: '1.4.1'
当我运行你的例子时,我得到:
/usr/lib/pymodules/python2.6/numpy/core/numeric.py:677: DeprecationWarning:
The current behavior of correlate is deprecated for 1.4.0, and will be removed
for NumPy 1.5.0.
The new behavior fits the conventional definition of correlation: inputs are
never swapped, and the second argument is conjugated for complex arrays.
DeprecationWarning)
[ 2. 4.5 7. 1.5 0. ]
[ 0. 1.5 7. 4.5 2. ]
所以你可能对(不正确的)行为是正确的,但它可能已在新版本中得到修复。