Numpy: normalized cross-correlation (Trac #1714)

Created on 20 Oct 2012  ·  12Comments  ·  Source: numpy/numpy

_Original ticket http://projects.scipy.org/numpy/ticket/1714 on 2011-01-15 by trac user bubla, assigned to unknown._

Hello,
I have made a patch that extends the correlate function so it can compute normalized cross-correlation now
See [http://en.wikipedia.org/wiki/Cross-correlation#Normalized_cross-correlation the Wikipedia article].

I have added documentation and simple doctest too, of course.
The patch is against the latest master Git

01 - Enhancement Patch numpy.core

Most helpful comment

There is an answer at SO that points here: https://stackoverflow.com/a/5639626/5392420

It also has the most relevant code included. As the attachment above is no longer available (at least to me), I thought it could be helpful to have this as a start for implementation.

I can also confirm that - at least in my case - it works to "normalize" the input vectors before using np.correlate like this and reasonable values will be returned within a range of [-1,1]:

a = (a - np.mean(a)) / (np.std(a) * len(a))
b = (b - np.mean(b)) / (np.std(b))
c = np.correlate(a, b, 'full')

All 12 comments

Attachment added by trac user bubla on 2011-01-15: correlate.parch

_@rgommers wrote on 2011-03-29_

Since this is new functionality, you should ask on the mailing list whether this can be included.

A unit test is necessary, doctests are not executed by default in the test suite.

This could be useful.

Can someone verify if this is correct?

I am wondering about what is right in combination with mode='full'.

I happen to need this feature, but am not sure that it is correct except when mode='valid' AND both arrays are the same size. In other words, I think the normalization has to be applied to each window.

any updates on the matter?
Has the patch been tested for all correlate modes?

@VlamV, as there is nothing here, no, I don't believe so. If you have time to fix it for the other modes, I think we could add it.

Any news on this ?

would it not make more sense to divide by the standard deviation of the portion of a that is actually used (in case of mode=same or mode=full)

also would it not make sense to use the bias and ddof options of numpy.corrcoef instead of simply dividing by len(a)

There is an answer at SO that points here: https://stackoverflow.com/a/5639626/5392420

It also has the most relevant code included. As the attachment above is no longer available (at least to me), I thought it could be helpful to have this as a start for implementation.

I can also confirm that - at least in my case - it works to "normalize" the input vectors before using np.correlate like this and reasonable values will be returned within a range of [-1,1]:

a = (a - np.mean(a)) / (np.std(a) * len(a))
b = (b - np.mean(b)) / (np.std(b))
c = np.correlate(a, b, 'full')

The normalization by the portion of signal a that is actually used (see @wilberth's comment) is implemented with numpy methods in the correlate_template function in this repository: https://github.com/trichter/xcorr

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ricardoV94 picture ricardoV94  ·  53Comments

mcarans picture mcarans  ·  267Comments

jakirkham picture jakirkham  ·  55Comments

mrava87 picture mrava87  ·  53Comments

vigna picture vigna  ·  104Comments