Is it possible that numpy.correlate does not follow the given formula?

妖精的绣舞 提交于 2019-12-11 11:27:53

问题


The documentation of the numpy.correlate command says that the cross correlation of two arrays is computed as the general definition for signal processing in the way:

z[k] = sum_n a[n] * conj(v[n+k])

This does not seem to be the case. It looks like the correlation is flipped. This would mean that either the sign in the last term of the formula is switched

z[k] = sum_n a[n] * conj(v[n-k])

or that the two input vectors are in the wrong order. A simple implementation of the given formula would be:

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

You can easily see that the resulting vector has the wrong order. I was very confused when it did not produce the results I expected and am pretty sure (after discussing it with my colleagues) that this is an error.


回答1:


Which version of NumPy are you using? On my Debian Squeeze box:

In [1]: import numpy as np

In [2]: np.__version__
Out[2]: '1.4.1'

When I run your example, I get:

/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. ]

so you may be right about the (incorrect) behavior, but it probably has been fixed in the new version.



来源:https://stackoverflow.com/questions/12251953/is-it-possible-that-numpy-correlate-does-not-follow-the-given-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!