Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing cross-correlation function?

In R, I am using ccf or acf to compute the pair-wise cross-correlation function so that I can find out which shift gives me the maximum value. From the looks of it, R gives me a normalized sequence of values. Is there something similar in Python's scipy or am I supposed to do it using the fft module? Currently, I am doing it as follows:

xcorr = lambda x,y : irfft(rfft(x)*rfft(y[::-1])) x = numpy.array([0,0,1,1]) y = numpy.array([1,1,0,0]) print xcorr(x,y) 
like image 312
Legend Avatar asked Aug 09 '11 04:08

Legend


People also ask

What is meant by cross correlation function?

The cross correlation function between two different signals is defined as the measure of similarity or coherence between one signal and the time delayed version of another signal. The cross correlation function is defined separately for energy (or aperiodic) signals and power or periodic signals.

How do you calculate the correlation between two signals?

If x(n), y(n) and z(n) are the samples of the signals, the correlation coefficient between x and y is given by Sigma x(n) * y(n) divided by the root of [Sigma x(n)^2 * y(n)^2], where ' * ' denotes simple multiplication and ^2 denotes squaring. The summation is taken over all the samples of the signals.

How do you calculate cross-correlation in Excel?

To do this for Example 1, press Ctrl-m and select the Cross Correlations data analysis tool from the Time S tab (or the Time Series data analysis tool if you are using the original user interface).


1 Answers

To cross-correlate 1d arrays use numpy.correlate.

For 2d arrays, use scipy.signal.correlate2d.

There is also scipy.stsci.convolve.correlate2d.

There is also matplotlib.pyplot.xcorr which is based on numpy.correlate.

See this post on the SciPy mailing list for some links to different implementations.

Edit: @user333700 added a link to the SciPy ticket for this issue in a comment.

like image 148
agf Avatar answered Sep 30 '22 01:09

agf