Digital low-pass filtering
Remove frequencies above a chosen cutoff from sampled data.
SciPy · Function
Designs an Nth-order digital or analog Butterworth filter. It supports low-pass, high-pass, band-pass, and band-stop designs and returns coefficients as polynomials, zero-pole-gain values, or second-order sections.
butter computes an Nth-order Butterworth filter from its critical frequency or frequencies. It can design digital or analog low-pass, high-pass, band-pass, or band-stop filters, and returns polynomial, zero-pole-gain, or second-order-section coefficients according to output.
scipy.signal.butter(N, Wn, btype='lowpass', analog=False, output='ba', fs=None)| Name | Type | Description |
|---|---|---|
N | int | The filter order. |
Wn | array_like | The critical frequency or frequencies. A scalar is used for low-pass and high-pass filters; a length-2 sequence is used for band-pass and band-stop filters. |
btype | {‘lowpass’, ‘highpass’, ‘bandpass’, ‘bandstop’}, optional | The filter type: 'lowpass', 'highpass', 'bandpass', or 'bandstop'. Default: 'lowpass'. |
analog | bool, optional | If true, design an analog filter; otherwise design a digital filter. Default: False. |
output | {‘ba’, ‘zpk’, ‘sos’}, optional | Coefficient representation: 'ba', 'zpk', or 'sos'. Default: 'ba'. |
fs | float, optional | Sampling frequency of the digital system. |
If output='ba', returns (b, a): numerator and denominator polynomials. If output='zpk', returns (z, p, k): zeros, poles, and system gain. If output='sos', returns an array of second-order sections.
output.btype selects low-pass, high-pass, band-pass, or band-stop behavior.analog=True produces an analog design; the default produces a digital design.fs, critical frequencies use normalized units: 1 corresponds to the Nyquist frequency.fs is supplied, express digital critical frequencies in the same units as the sampling frequency.import numpy as np
from scipy import signal
fs = 1000.0
sos = signal.butter(4, 100.0, btype="lowpass", fs=fs, output="sos")
t = np.arange(0, 1, 1 / fs)
x = np.sin(2 * np.pi * 20 * t) + np.sin(2 * np.pi * 250 * t)
y = signal.sosfilt(sos, x)
print(y.shape, sos.shape)Designs a fourth-order digital low-pass Butterworth filter with a 100 Hz cutoff and applies its numerically stable second-order-section representation.
Remove frequencies above a chosen cutoff from sampled data.
Suppress a narrow frequency range such as mains interference.
Create a continuous-time prototype for analog circuit or control analysis.
Filter with higher-order designs while reducing coefficient-related numerical issues.
Wn represent?Wn is the critical frequency, or the two critical frequencies for band-pass and band-stop designs. For a Butterworth filter, it is the frequency where the gain reaches the passband value divided by 2, commonly called the -3 dB point.
Follow-up: How do digital frequency units change when fs is provided? Without fs, frequencies are normalized to the Nyquist frequency; with fs, Wn uses the same units as fs.
output return?output="ba" returns numerator and denominator polynomial coefficients, output="zpk" returns zeros, poles, and gain, and output="sos" returns second-order sections. The default is ba, but the documentation recommends sos for general-purpose filtering.
Follow-up: Why is sos preferable for difficult designs? Cascaded second-order sections generally avoid the numerical sensitivity that can make high-order transfer-function coefficients unstable or inaccurate.
Set analog=True to design an analog filter; otherwise the result is digital. Digital designs use the sampling frequency when fs is specified, while analog designs interpret Wn as angular frequencies.
Follow-up: What filter types can be selected? btype supports low-pass, high-pass, band-pass, and band-stop designs.
scipy.signal.sosfiltscipy.signal.sosfreqzscipy.signal.freqzscipy.signal.zpk2sos