Python互相关 [英] Python cross correlation
问题描述
我有一对(长度不同)的一维数组,如下所示:
I have a pair of 1D arrays (of different lengths) like the following:
data1 = [0,0,0,1,1,1,0,1,0,0,1]
data2 = [0,1,1,0,1,0,0,1]
我想在python中获得2系列的最大互相关.在matlab中,xcorr()
函数将其返回OK
I would like to get the max cross correlation of the 2 series in python. In matlab, the xcorr()
function will return it OK
我尝试了以下2种方法:
I have tried the following 2 methods:
-
numpy.correlate(data1, data2)
-
signal.fftconvolve(data2, data1[::-1], mode='full')
numpy.correlate(data1, data2)
signal.fftconvolve(data2, data1[::-1], mode='full')
两个方法都给我相同的值,但是我从python中获得的值与从matlab中获得的值不同. Python给我的整数值> 1,而matlab给我的实际相关值在0和1之间.
Both methods give me the same values, but the values I get from python are different from what comes out of matlab. Python gives me integers values > 1, whereas matlab gives actual correlation values between 0 and 1.
我尝试过先对2个数组进行归一化处理(值均值/SD),但是得到的互相关值却成千上万,这似乎并不正确.
I have tried normalizing the 2 arrays first (value-mean/SD), but the cross correlation values I get are in the thousands which doesnt seem correct.
Matlab也将为您提供一个互相关最大的滞后值.我认为使用索引很容易做到这一点,但是如果我的数组包含10的数千个值,最合适的方法是什么?
Matlab will also give you a lag value at which the cross correlation is the greatest. I assume it is easy to do this using indices but whats the most appropriate way of doing this if my arrays contain 10's of thousands of values?
我想模仿matlab的xcorr(
)函数,关于我将如何在python中做到这一点的任何想法?
I would like to mimic the xcorr(
) function that matlab has, any thoughts on how I would do that in python?
推荐答案
numpy.correlate(arr1,arr2,"full")
为我提供与
xcorr(arr1,arr2)
使用matlab
这篇关于Python互相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!