numpy.correlate是否可能不遵循给定的公式? [英] Is it possible that numpy.correlate does not follow the given formula?

查看:130
本文介绍了numpy.correlate是否可能不遵循给定的公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy.correlate命令的文档说,两个数组的互相关被计算为信号处理的通用定义,方式如下:

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])

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])

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.

推荐答案

您使用的是哪个版本的NumPy?在我的Debian Squeeze框上:

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.

这篇关于numpy.correlate是否可能不遵循给定的公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆