Remove noise without shifting features in time
Offline signal processing
SciPy · Function
Applies a digital filter forward and backward to produce zero-phase filtering. It supports configurable edge handling through padding or Gustafsson’s method.
filtfilt applies the same linear digital filter once in the forward direction and once in reverse. The reverse pass cancels the filter’s phase shift, while edge behavior is controlled through padding or Gustafsson’s method.
scipy.signal.filtfilt(b, a, x, axis=-1, padtype='odd', padlen=None, method='pad', irlen=None)| Name | Type | Description |
|---|---|---|
b | array_like | Numerator coefficients of the digital filter’s transfer function. |
a | array_like | Denominator coefficients of the digital filter’s transfer function. |
x | array_like | Data array to filter. |
axis | int, optional | Axis along which filtering is performed; defaults to -1. |
padtype | str or None, optional | Edge-extension type for padding; supported choices include 'odd', 'even', 'constant', or None. Defaults to 'odd'. |
padlen | int or None, optional | Number of samples added to both ends along the filtering axis. None uses the documented default. |
method | str, optional | Edge-handling strategy: 'pad' or 'gust'. Defaults to 'pad'. |
irlen | int or None, optional | Effective impulse-response length used with Gustafsson’s method. |
y: ndarray — the filtered output, with the same shape as x.
import numpy as np
from scipy.signal import butter, filtfilt
rng = np.random.default_rng(0)
t = np.linspace(0, 1, 200)
signal = np.sin(2 * np.pi * 5 * t) + 0.25 * rng.normal(size=t.size)
b, a = butter(3, 0.15)
filtered = filtfilt(b, a, signal)
print(filtered.shape)Designs a Butterworth filter and applies it forward and backward to a noisy signal, preserving the input shape.
Offline signal processing
Multidimensional arrays
Edge-sensitive filtering
Long IIR signals
It runs the filter forward and then backward over the data. This produces zero phase shift, but the effective filter order is twice the original.
Follow-up: What happens at the boundaries? The function uses padding by default, or Gustafsson’s method when method='gust', to determine edge behavior.
axis selects the dimension filtered, with -1 as the default. The output has the same shape as the input array.
Follow-up: How can edge padding be disabled? Set padtype=None; alternatively, choose method='gust', which ignores padding arguments.
The default method is 'pad', which extends the signal using the default odd pad type. padlen controls the extension length, while irlen can specify the effective impulse-response length for Gustafsson’s method.
Follow-up: When can irlen improve performance? For long signals and IIR filters, specifying a suitable impulse-response length can reduce computation with method='gust'.
scipy.signal.sosfiltfiltscipy.signal.lfilterscipy.signal.butterscipy.signal.filtfilt