Signal filtering
Apply a finite impulse response or smoothing kernel to sampled data.
SciPy · Function
Convolve two N-dimensional arrays using selectable output sizing and computation methods. The result is a requested subset of their discrete linear convolution.
The function performs discrete linear convolution between two N-dimensional array-like inputs. Choose the output extent with mode, then choose direct, FFT-based, or automatic computation with method; the returned array contains the requested portion of the convolution.
scipy.signal.convolve(in1, in2, mode='full', method='auto')| Name | Type | Description |
|---|---|---|
in1 | array_like | First input array-like. |
in2 | array_like | Second input array-like; it should have the same number of dimensions as in1. |
mode | str {‘full’, ‘valid’, ‘same’}, optional | Output-size selector: full, valid, or same. |
method | str {‘auto’, ‘direct’, ‘fft’}, optional | Convolution algorithm selector: auto, direct, or fft. |
An N-dimensional array containing a subset of the discrete linear convolution of in1 with in2.
import numpy as np
from scipy.signal import convolve
signal = np.array([1, 2, 3])
kernel = np.array([1, 1])
result = convolve(signal, kernel, mode="same")
print(result)Convolves a one-dimensional signal with a two-element kernel and returns an output matching the signal's shape.
Apply a finite impulse response or smoothing kernel to sampled data.
Combine multidimensional arrays when the complete or cropped linear convolution is needed.
Use automatic algorithm selection for general-purpose convolution workloads.
It computes the discrete linear convolution of two N-dimensional array-like inputs. The mode argument controls whether the result is full, valid, or the same size as the first input.
Follow-up: How would you select the computation strategy? Use direct for straightforward computation, fft for FFT-based convolution, or auto to let SciPy choose.
full returns the complete discrete linear convolution. valid excludes values dependent on zero-padding and imposes a size relationship between the inputs; same returns an output shaped like in1 and centered relative to full.
Follow-up: What constraint matters for multidimensional valid convolution? One input must be at least as large as the other in every dimension.
method selects the algorithm: direct uses the definition directly, fft uses FFT convolution, and auto estimates which is faster. The function returns an N-dimensional array containing the selected convolution subset.
Follow-up: What numerical caveat applies to FFT convolution? NaN or inf values can propagate through the FFT and generate NaN output, so direct computation may be safer for such inputs.
scipy.signal.fftconvolvescipy.signal.oaconvolvescipy.signal.correlatescipy.signal.convolve2d