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?

+3


source to share


1 answer


numpy.correlate(arr1,arr2,"full")

      

gave me the same result as



xcorr(arr1,arr2)

      

gives in matlab

+5


source







All Articles