# [SciPy-user] cross correlation functions

josef.pktd@gmai... josef.pktd@gmai...
Thu Apr 2 08:36:26 CDT 2009

On Thu, Apr 2, 2009 at 2:52 AM, Chris Lee <c.j.lee@tnw.utwente.nl> wrote:
> Thanks,
>
> I think the 2d function is what I want. I have two 1-d arrays and I
> would like a 2-d cross correlation of the two.
>
> Cheers
> Chris
>
> On Apr 1, 2009, at 9:41 PM, josef.pktd@gmail.com wrote:
>
>> On Wed, Apr 1, 2009 at 8:41 AM, Chris Lee <c.j.lee@tnw.utwente.nl>
>> wrote:
>>> Hi All,
>>> I have two arrays of data and I want to perform a cross correlation
>>> on them.
>>> Will correlate(input, weights, output=None, mode='reflect', cval=0.0,
>>> origin=0) do this for me?
>>> Would the structure be something like this (pseudo code):
>>> arr1 = [bunch of data]
>>> arr2 = [bunch of data]
>>> input_arr = append(arr1, arr2)
>>> weigths = zeros(input_arr.shape, float)
>>> weigth[max(arr1.shape)::] = 1
>>> out_mat = correlate(input_arr, weights)
>>> Otherwise, I may have to loop... the prospect is not appealing as
>>> these
>>> arrays are reasonably large.
>>> Cheers
>>> Chris
>>
>> What's the dimension of your arr1, arr2? there are 3 different
>> correlate in numpy/scipy, for 1d numpy.correlate and for 2d
>> scipy.signal.correlate2d might do more directly what you want.
>>
>> Josef

for two 1-d arrays np.correlate should be enough.

I had forgotten to add some references to current implementation to my
previous email, they also show how to
normalize the cross covariance.

an example implementation for masked arrays is in

http://scipy.org/scipy/scikits/browser/trunk/timeseries/scikits/timeseries/lib/avcf.py#L67
http://scipy.org/scipy/scikits/browser/trunk/timeseries/scikits/timeseries/lib/avcf.py#L135

Pierre also had an example for different implementations in
http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg01487.html

matplotlib also has an implementation based on np.correlate

matplotlib.pyplot.acorr
matplotlib.pyplot.xcorr

Josef