nlcpy.correlate

nlcpy.correlate(a, v, mode='valid')

Cross-correlation of two 1-dimensional sequences.

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

c_{av}[k] = sum_n a[n+k] * conj(v[n])

with a and v sequences being zero-padded where necessary and conj being the conjugate.

Parameters
a,varray_like

Input sequences.

mode{‘valid’,’same’,’full’}, optional
  • ‘full’ : By default, mode is ‘full’. This returns the convolution at each point of overlap, with an output shape of (N+M-1,). At the end-points of the convolution, the signals do not overlap completely, and boundary effects may be seen.

  • ‘same’ : Mode ‘same’ returns output of length max(M, N). Boundary effects are still visible.

  • ‘valid’: Mode ‘valid’ returns output of length max(M, N) - min(M, N) + 1. The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect.

Returns
outndarray

Discrete, linear convolution of a and v.

Note

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

c'_{av}[k]=sum_n a[n] conj(v[n+k])

which is related to c_{av}[k] by c'_{av}[k] = c_{av}[-k].

Restriction

This function is the wrapper function to utilize numpy.correlate(). Calculations during this function perform on only Vector Host(Linux/x86).

Examples

>>> import nlcpy as vp
>>> vp.correlate([1, 2, 3], [0, 1, 0.5])
array([3.5])
>>> vp.correlate([1, 2, 3], [0, 1, 0.5], "same")
array([2. , 3.5, 3. ])
>>> vp.correlate([1, 2, 3], [0, 1, 0.5], "full")
array([0.5, 2. , 3.5, 3. , 0. ])

Using complex sequences:

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

Note that you get the time reversed, complex conjugated result when the two input sequences change places, i.e., c_{va}[k] = c^{*}_{av}[-k]:

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