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)
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With