TExcelTAD
Home › Fourier Analysis & Signals › What a Filter Does

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 fcf_c smoothing, anti-aliasing, subwoofers
high-pass above fcf_c removing DC offset and slow drift, "rumble" filters
band-pass between f1f_1 and f2f_2 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 ±0.1\pm 0.1 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 ∣f∣<fc\lvert f \rvert < f_c and 0 above. Its impulse response is a sinc, h(t)=2fcsinc⁡(2fct)h(t) = 2f_c \operatorname{sinc}(2f_c t): infinitely long and non-causal. Truncating it creates ripples (the Gibbs phenomenon in frequency), which is why practical designs taper it.

python · try it yourself
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 −27-27 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:

python · try it yourself
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.signal designs filters (firwin, remez, butter, cheby1, ellip, iirnotch) and applies them (lfilter, sosfilt, filtfilt for 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 fcf_c, in terms of the ideal low-pass hLPh_\text{LP}?

Answer

δ(t)−hLP(t)\delta(t) - h_\text{LP}(t).

Solution

The high-pass frequency response is 1−HLP(f)1 - H_\text{LP}(f), and 1 is the transform of δ\delta. In discrete time this "spectral inversion" gives δ[n]−hLP[n]\delta[n] - h_\text{LP}[n].

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
python · try it yourself
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…

  1. low-pass
  2. high-pass
  3. notch (band-stop)
  4. all-pass

The impulse response of an ideal low-pass filter is…

  1. a rectangle
  2. a sinc, infinitely long and non-causal
  3. a decaying exponential
  4. a delta

Truncating the ideal impulse response causes…

  1. no change
  2. ripples near the cutoff (Gibbs) and limited stopband attenuation
  3. a phase inversion
  4. aliasing