[Numpy-discussion] How to limit cross correlation window width in Numpy?

Mansour Moufid mansourmoufid at gmail.com
Thu Jun 18 00:16:04 EDT 2015


There is a simple solution.

The cross-correlation of two arrays of lengths m and n is of length
m + n - 1, where m is usually much larger than n.

If you need to compute the cross-correlation with a bound on the lag
of k, then truncate the longer array to length k - n + 1.

That is,

def _correlate(x, y, maxlag):
    n = y.shape[0]
    return numpy.correlate(x[:maxlag - n + 1], y)

As for the lag array, it is defined as

    -n + 1, ..., 0, ..., m + 1

so truncate it too,

    -n + 1, ..., 0, ..., maxlag - n + 2

By the way, you should truncate to a power of two.


More information about the NumPy-Discussion mailing list