scipy.signal.correlation_lags¶
-
scipy.signal.
correlation_lags
(in1_len, in2_len, mode='full')[source]¶ Calculates the lag / displacement indices array for 1D cross-correlation.
- Parameters
- in1_sizeint
First input size.
- in2_sizeint
Second input size.
- modestr {‘full’, ‘valid’, ‘same’}, optional
A string indicating the size of the output. See the documentation
correlate
for more information.
- Returns
- lagsarray
Returns an array containing cross-correlation lag/displacement indices. Indices can be indexed with the np.argmax of the correlation to return the lag/displacement.
See also
correlate
Compute the N-dimensional cross-correlation.
Notes
Cross-correlation for continuous functions \(f\) and \(g\) is defined as:
\[\left ( f\star g \right )\left ( \tau \right ) \triangleq \int_{t_0}^{t_0 +T} \overline{f\left ( t \right )}g\left ( t+\tau \right )dt\]Where \(\tau\) is defined as the displacement, also known as the lag.
Cross correlation for discrete functions \(f\) and \(g\) is defined as:
\[\left ( f\star g \right )\left [ n \right ] \triangleq \sum_{-\infty}^{\infty} \overline{f\left [ m \right ]}g\left [ m+n \right ]\]Where \(n\) is the lag.
Examples
Cross-correlation of a signal with its time-delayed self.
>>> from scipy import signal >>> from numpy.random import default_rng >>> rng = default_rng() >>> x = rng.standard_normal(1000) >>> y = np.concatenate([rng.standard_normal(100), x]) >>> correlation = signal.correlate(x, y, mode="full") >>> lags = signal.correlation_lags(x.size, y.size, mode="full") >>> lag = lags[np.argmax(correlation)]