Template matching
Compare a measured signal with a reference waveform to locate matching delays.
SciPy · Function
Computes the discrete linear cross-correlation of two N-dimensional arrays. The mode selects the output region, and method selects direct, FFT, or automatic computation.
signal.correlate computes the discrete linear cross-correlation of two N-dimensional arrays. It selects the output region with mode and uses either direct summation, an FFT-based method, or an automatic choice based on an estimated cost.
scipy.signal.correlate(in1, in2, mode='full', method='auto')| Name | Type | Description |
|---|---|---|
in1 | array_like | First input array. |
in2 | array_like | Second input; it must have the same number of dimensions as in1. |
mode | {'full', 'valid', 'same'}, optional | Output selection: full, valid, or same; defaults to full. |
method | {'auto', 'direct', 'fft'}, optional | Computation strategy: direct, fft, or auto; defaults to auto. |
An N-dimensional array containing a subset of the discrete linear cross-correlation of in1 with in2.
import numpy as np
from scipy import signal
x = np.array([1, 2, 3])
template = np.array([1, 1])
result = signal.correlate(x, template, mode="full")
print(result)Computes the full cross-correlation sequence between two one-dimensional numeric arrays.
Compare a measured signal with a reference waveform to locate matching delays.
Measure similarity between sequences as one is shifted relative to the other.
Process multidimensional numerical data where alignment or similarity across offsets matters.
It cross-correlates two N-dimensional arrays and returns an array containing a subset of their discrete linear cross-correlation. The mode controls the output region, while method controls how the computation is performed.
Follow-up: When is fft unsuitable? It is available only for numerical arrays, so non-numeric inputs require direct computation.
full returns the complete discrete linear cross-correlation. same returns an output shaped like in1 and centered relative to the full result, while valid excludes values that depend on zero-padding.
Follow-up: What restriction applies to valid? Either input must be at least as large as the other in every dimension.
direct evaluates the correlation from sums, fft uses the Fast Fourier Transform, and auto estimates which of those approaches is faster; auto is the default.
Follow-up: What practical limitation does fft have? It is available only for numerical arrays.
scipy.signal.convolvescipy.signal.fftconvolvescipy.signal.correlate2dscipy.signal.correlation_lags