dpnp.correlate

dpnp.correlate(a, v, mode='valid')[source]

Cross-correlation of two 1-dimensional sequences.

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

\[c_k = \sum_n a_{n+k} \cdot \overline{v}_n\]

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

For full documentation refer to numpy.correlate.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}) -- First input array.

  • v ({dpnp.ndarray, usm_ndarray}) -- Second input array.

  • mode ({"valid", "same", "full"}, optional) --

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

    Default: "valid".

Returns:

out -- Discrete cross-correlation of a and v.

Return type:

dpnp.ndarray

Notes

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

\[c'_k = \sum_n a_{n} \cdot \overline{v_{n+k}}\]

which is related to \(c_k\) by \(c'_k = c_{-k}\).

References

See also

dpnp.convolve

Discrete, linear convolution of two one-dimensional sequences.

Examples

>>> import dpnp as np
>>> a = np.array([1, 2, 3], dtype=np.float32)
>>> v = np.array([0, 1, 0.5], dtype=np.float32)
>>> np.correlate(a, v)
array([3.5], dtype=float32)
>>> np.correlate(a, v, "same")
array([2. , 3.5, 3. ], dtype=float32)
>>> np.correlate([a, v, "full")
array([0.5, 2. , 3.5, 3. , 0. ], dtype=float32)

Using complex sequences:

>>> ac = np.array([1+1j, 2, 3-1j], dtype=np.complex64)
>>> vc = np.array([0, 1, 0.5j], dtype=np.complex64)
>>> np.correlate(ac, vc, 'full')
array([0.5-0.5j, 1. +0.j , 1.5-1.5j, 3. -1.j , 0. +0.j ], dtype=complex64)

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

>>> np.correlate(vc, ac, 'full')
array([0. +0.j , 3. +1.j , 1.5+1.5j, 1. +0.j , 0.5+0.5j], dtype=complex64)