flopscope.

flopscope.numpy.correlate

fnp.correlate(a, v, mode='valid')[flopscope source][numpy source]

Cross-correlation of two 1-dimensional sequences.

Adapted from NumPy docs np.correlate

Areacore
Typecustom
NumPy Refnp.correlate
Cost
nmn \cdot m
Flopscope Context

1-D cross-correlation.

This function computes the correlation as generally defined in signal processing texts [1]_:

ck=nan+kvnc_k = \sum_n a_{n+k} \cdot \overline{v}_n

with a and v sequences being zero-padded where necessary and v\overline v denoting complex conjugation.

Parameters

a, v:array_like

Input sequences.

mode:{'valid', 'same', 'full'}, optional

Refer to the convolve docstring. Note that the default is 'valid', unlike convolve, which uses 'full'.

Returns

out:ndarray

Discrete cross-correlation of a and v.

See also

Notes

The definition of correlation above is not unique and sometimes correlation may be defined differently. Another common definition is [1]_:

ck=nanvn+kc'_k = \sum_n a_{n} \cdot \overline{v_{n+k}}

which is related to ckc_k by ck=ckc'_k = c_{-k}.

flops.correlate may perform slowly in large arrays (i.e. n = 1e5) because it does not use the FFT to compute the convolution; in that case, scipy.signal.correlate might be preferable.

References

footnote
1

Wikipedia, "Cross-correlation",
https://en.wikipedia.org/wiki/Cross-correlation

Examples

>>> import flopscope.numpy as fnp
>>> flops.correlate([1, 2, 3], [0, 1, 0.5])
array([3.5])
>>> flops.correlate([1, 2, 3], [0, 1, 0.5], "same")
array([2. ,  3.5,  3. ])
>>> flops.correlate([1, 2, 3], [0, 1, 0.5], "full")
array([0.5,  2. ,  3.5,  3. ,  0. ])

Using complex sequences:

>>> flops.correlate([1+1j, 2, 3-1j], [0, 1, 0.5j], 'full')
array([ 0.5-0.5j,  1.0+0.j ,  1.5-1.5j,  3.0-1.j ,  0.0+0.j ])

Note that you get the time reversed, complex conjugated result (ck\overline{c_{-k}}) when the two input sequences a and v change places:

>>> flops.correlate([0, 1, 0.5j], [1+1j, 2, 3-1j], 'full')
array([ 0.0+0.j ,  3.0+1.j ,  1.5+1.5j,  1.0+0.j ,  0.5+0.5j])