Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

signal.filtfilt

Applies a digital filter forward and backward to produce zero-phase filtering. It supports configurable edge handling through padding or Gustafsson’s method.

How it works

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.

Syntax

scipy.signal.filtfilt(b, a, x, axis=-1, padtype='odd', padlen=None, method='pad', irlen=None)

Parameters

NameTypeDescription
barray_likeNumerator coefficients of the digital filter’s transfer function.
aarray_likeDenominator coefficients of the digital filter’s transfer function.
xarray_likeData array to filter.
axisint, optionalAxis along which filtering is performed; defaults to -1.
padtypestr or None, optionalEdge-extension type for padding; supported choices include 'odd', 'even', 'constant', or None. Defaults to 'odd'.
padlenint or None, optionalNumber of samples added to both ends along the filtering axis. None uses the documented default.
methodstr, optionalEdge-handling strategy: 'pad' or 'gust'. Defaults to 'pad'.
irlenint or None, optionalEffective impulse-response length used with Gustafsson’s method.

Returns

y: ndarray — the filtered output, with the same shape as x.

Key points

  • The function performs forward-backward digital filtering.
  • The result has no phase shift and doubles the original filter order.
  • padtype chooses how the signal is extended at its edges.
  • Odd extension is the default padding mode.
  • method selects conventional padding or Gustafsson edge handling.
  • The returned array preserves the input shape.

Example: Zero-phase filtering of a sampled signal

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.

When to use it

Remove noise without shifting features in time

Offline signal processing

Filter one selected axis of sampled data

Multidimensional arrays

Choose padding or Gustafsson edge treatment

Edge-sensitive filtering

Use impulse-response length to optimize Gustafsson filtering

Long IIR signals

Interview questions

QHow does filtfilt achieve zero phase?

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.

QWhat do axis and the output shape mean?

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.

QHow are signal edges handled?

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.sosfiltfilt
  • scipy.signal.lfilter
  • scipy.signal.butter
  • scipy.signal.filtfilt

Source: filtfilt. Checked 2026-10-15.