Python cross-correlation
I have a couple of 1D arrays (different lengths), for example:
data1 = [0,0,0,1,1,1,0,1,0,0,1]
data2 = [0,1,1,0,1,0,0,1]
I would like to get the maximum 2nd series cross-correlation in python. In Matlab the function xcorr()
will return it OK
I have tried the following 2 methods:
-
numpy.correlate(data1, data2)
-
signal.fftconvolve(data2, data1[::-1], mode='full')
Both methods give me the same values, but the values โโthat I get from python are different from the values โโthat come out of matlab. Python gives me integer values> 1, whereas Matlab gives the actual correlation values โโbetween 0 and 1.
I tried to normalize 2 arrays first (mean / SD), but the cross-correlation values โโI get are in the thousands which don't seem to be correct.
Matlab will also give you the lag value at which the cross-correlation will be greatest. I'm guessing it's easy to do this with indices, but what is the most appropriate way to do this if my arrays contain 10k values?
I would like to mimic a function xcorr(
) that Matlab has any thoughts on how I would do this in python?
source to share