What a Filter Does
A filter is an LTI system designed to keep some frequencies and suppress others. Low-pass filters smooth and anti-alias, high-pass filters remove drift and hum below a cutoff, band-pass filters isolate a channel, and notch filters remove one interfering tone. This lesson sets out the vocabulary of filter specifications and shows the ideal filter, and why it cannot be built exactly.
Types and specifications
| Type | Keeps | Typical use |
|---|---|---|
| low-pass | below the cutoff | smoothing, anti-aliasing, subwoofers |
| high-pass | above | removing DC offset and slow drift, "rumble" filters |
| band-pass | between and | radio channels, ECG (0.5-40 Hz), DTMF tone detection |
| band-stop / notch | all but a band | removing 50/60 Hz mains hum |
A real specification lists the passband (allowed ripple, for example dB), the stopband (required attenuation, for example 60 dB), and the transition band between them. Narrow transitions and deep stopbands cost longer filters.
The ideal low-pass filter
An ideal low-pass filter has frequency response 1 for and 0 above. Its impulse response is a sinc, : infinitely long and non-causal. Truncating it creates ripples (the Gibbs phenomenon in frequency), which is why practical designs taper it.
import numpy as np
fs, fc = 1000.0, 100.0
for half in (10, 50, 200):
n = np.arange(-half, half + 1)
h = 2 * fc / fs * np.sinc(2 * fc / fs * n) # truncated ideal low-pass
H = np.abs(np.fft.rfft(h, 8192))
f = np.fft.rfftfreq(8192, 1 / fs)
M = 2 * half + 1
overshoot = H.max() # largest gain anywhere
stop = 20 * np.log10(H[f > fc + 2 * fs / M].max()) # beyond the transition band
print(f"{M:3d} taps: transition about {2 * fs / M:5.1f} Hz wide peak gain {overshoot:.4f} worst stopband {stop:6.1f} dB") 21 taps: transition about 95.2 Hz wide peak gain 1.1192 worst stopband -29.0 dB
101 taps: transition about 19.8 Hz wide peak gain 1.0941 worst stopband -27.0 dB
401 taps: transition about 5.0 Hz wide peak gain 1.0905 worst stopband -26.5 dB
More taps sharpen the transition, but the overshoot near the cutoff tends to about 9 % (the Gibbs constant 1.0895) and the stopband stays stuck around dB. Windowing fixes that (FIR Filter Design).
Filtering in practice
Removing a mains hum (50 Hz) from a slow signal with a band-stop filter built in the frequency domain:
import numpy as np
fs = 500
t = np.arange(0, 4, 1 / fs)
clean = np.sin(2 * np.pi * 1.25 * t) + 0.5 * np.sin(2 * np.pi * 7 * t)
x = clean + 0.8 * np.sin(2 * np.pi * 50 * t)
X = np.fft.rfft(x)
f = np.fft.rfftfreq(x.size, 1 / fs)
X[np.abs(f - 50) < 1] = 0 # zero the bins near 50 Hz
y = np.fft.irfft(X, x.size)
print("RMS error before:", round(float(np.sqrt(np.mean((x - clean) ** 2))), 4))
print("RMS error after below 1e-12:", float(np.sqrt(np.mean((y - clean) ** 2))) < 1e-12)RMS error before: 0.5657
RMS error after below 1e-12: True
Here every component completes whole cycles in the 4 s frame (so all of them sit exactly on bins), and zeroing bins works perfectly. For streaming data, or hum that drifts, a causal notch filter is used instead (IIR Filters and Feedback).
In software
scipy.signaldesigns filters (firwin,remez,butter,cheby1,ellip,iirnotch) and applies them (lfilter,sosfilt,filtfiltfor zero phase). See SciPy Signal Filters.- Audio equalizers are banks of band-pass and shelving filters; image sharpening is a high-pass filter added back to the image.
Exercises
Exercise 1: Pick the type
An accelerometer signal contains a slow temperature drift (below 0.1 Hz) and vibrations between 5 and 200 Hz. Which filter isolates the vibrations?
Answer
A band-pass filter (or a high-pass at about 1 Hz, if nothing above 200 Hz matters).
Solution
The drift is removed by any high-pass with cutoff well above 0.1 Hz; an upper cutoff above 200 Hz also removes high-frequency noise, making it a band-pass of, say, 1 to 250 Hz.
Exercise 2: Ideal impulse response
What is the impulse response of an ideal high-pass filter with cutoff , in terms of the ideal low-pass ?
Answer
.
Solution
The high-pass frequency response is , and 1 is the transform of . In discrete time this "spectral inversion" gives .
Exercise 3: Low-pass by zeroing bins (code)
Remove everything above 20 Hz from the hum example by zeroing bins, and report the remaining RMS error against clean.
Answer
Essentially 0: the clean signal lies entirely below 20 Hz.
Solution
import numpy as np
fs = 500
t = np.arange(0, 4, 1 / fs)
clean = np.sin(2 * np.pi * 1.25 * t) + 0.5 * np.sin(2 * np.pi * 7 * t)
x = clean + 0.8 * np.sin(2 * np.pi * 50 * t)
X = np.fft.rfft(x)
X[np.fft.rfftfreq(x.size, 1 / fs) > 20] = 0
print(float(np.sqrt(np.mean((np.fft.irfft(X, x.size) - clean) ** 2))) < 1e-10)True
FFT-domain filtering is exact for periodic, on-bin signals like these; for real recordings it causes wrap-around and ringing, which is why time-domain filters are usually preferred.
Test yourself
A filter that removes 50 Hz mains hum but keeps everything else is a…
- low-pass
- high-pass
- notch (band-stop)
- all-pass
The impulse response of an ideal low-pass filter is…
- a rectangle
- a sinc, infinitely long and non-causal
- a decaying exponential
- a delta
Truncating the ideal impulse response causes…
- no change
- ripples near the cutoff (Gibbs) and limited stopband attenuation
- a phase inversion
- aliasing