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
1-D cross-correlation.
This function computes the correlation as generally defined in signal processing texts [1]_:
with a and v sequences being zero-padded where necessary and denoting complex conjugation.
Parameters
Returns
- out:ndarray
Discrete cross-correlation of
aandv.
See also
- we.flops.convolve Discrete, linear convolution of two one-dimensional sequences.
- scipy.signal.correlate uses FFT which has superior performance on large arrays.
Notes
The definition of correlation above is not unique and sometimes correlation may be defined differently. Another common definition is [1]_:
which is related to by .
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
1
Wikipedia, "Cross-correlation",
https://en.wikipedia.org/wiki/Cross-correlationExamples
>>> 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 () 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])