5. Filtering
Sculpting signals in time and frequency
Now that we can see the frequency content of a signal, the next question is obvious: can we remove the parts we don't want? That is exactly what filtering does. A filter is a system that modifies a signal's frequency content — attenuating some frequencies while passing others through. It is one of the most fundamental operations in all of signal processing.
Convolution
Almost every useful filter in discrete signal processing is implemented through convolution. Convolution takes two sequences — the input signal x[n] and a short sequence of coefficients called the kernel (or impulse response) h[n] — and produces an output y[n] by sliding the kernel over the input and taking a weighted sum at each position:
y[n] = Σk h[k] · x[n − k]
In plain English: at each output sample position, multiply each kernel coefficient by the corresponding input sample, and add up all the products.
The kernel completely determines what the filter does. A kernel of [⅓, ⅓, ⅓] is a moving average — it smooths out rapid fluctuations, which is the same as attenuating high frequencies. Different kernels produce different frequency responses.
FIR Filters
A Finite Impulse Response (FIR) filter is one whose kernel has a finite number of non-zero coefficients. The output depends only on the current and past input samples — there is no feedback from past outputs. The moving average above is an FIR filter.
FIR filters have two important properties that make them attractive:
- Guaranteed stability: with no feedback, the output can never grow without bound.
- Linear phase: if the coefficients are symmetric (h[k] = h[N−1−k]), the filter delays all frequencies by exactly the same amount. This is crucial when the shape of a waveform matters (audio, ECG signals, images).
The trade-off: achieving a very sharp frequency cutoff requires a long kernel — which means more multiplications per output sample, and more latency.
IIR Filters
An Infinite Impulse Response (IIR) filter adds feedback: the output also depends on previous output samples. This is described by a difference equation:
y[n] = Σk b[k] · x[n−k] − Σj a[j] · y[n−j]
The b coefficients weight past inputs (feedforward), and the a coefficients weight past outputs (feedback).
The feedback means the filter has a kind of memory that decays over time — mathematically, a single impulse at the input produces an output that can ring on indefinitely (though it typically decays exponentially). This is the "infinite impulse response".
IIR advantages: a much sharper frequency cutoff than an FIR with the same number of coefficients — orders of magnitude more efficient. IIR disadvantages: possible instability if the feedback coefficients are chosen poorly, and non-linear phase response. For most real-time audio effects, IIR is the practical choice.
Filter Types
Filters are classified by which frequencies they pass and which they block:
Low-pass: passes frequencies below the cutoff fc, blocks those above. Used to remove noise, smooth signals, or limit bandwidth before downsampling.
High-pass: blocks low frequencies, passes high ones. Used to remove DC offset or low-frequency rumble, isolate high-frequency detail.
Band-pass: passes a band of frequencies between two cutoffs, blocks everything else. Used in radio receivers (tuning to a specific channel), audio equalisers (isolating the midrange), and biomedical sensing (isolating EEG alpha waves).
Band-stop (notch): blocks a band, passes everything else. Used to remove interference at a specific frequency — classically, 50 Hz or 60 Hz mains hum in audio recordings.
Designing a Filter
Three parameters define a filter's behaviour:
Cutoff frequency fc: the boundary between passband and stopband. For a low-pass filter, this is the highest frequency passed. It is usually specified as a fraction of the Nyquist frequency.
Order: the number of coefficients (FIR) or the degree of the feedback polynomial (IIR). Higher order = sharper roll-off in the transition band — frequencies just above the cutoff are attenuated more aggressively. A first-order filter rolls off at 6 dB/octave; a fourth-order at 24 dB/octave.
Roll-off (dB/octave): how quickly the filter attenuation increases past the cutoff. Higher order gives steeper roll-off.
Filtering in the Frequency Domain
Convolution in the time domain is equivalent to multiplication in the frequency domain — this is the convolution theorem. In practice this gives us a fast alternative:
- Compute the DFT of the input signal: X[k]
- Compute the DFT of the filter's impulse response: H[k] (the frequency response)
- Multiply pointwise: Y[k] = H[k] · X[k]
- Compute the inverse DFT: y[n] = IDFT(Y[k])
For long signals, this overlap-add or overlap-save method is often faster than direct convolution because it leverages the FFT's O(N log N) complexity. For real-time streaming, time-domain convolution is typically preferred for short kernels.
This view makes filter design intuitive: draw the frequency response you want (a rectangle passing the desired frequencies), compute its inverse DFT to get the impulse response, and use that as your kernel. Applying a rectangular frequency response produces the ideal sinc kernel — which is exactly why the windowed sinc is the standard FIR design method.
Key Takeaways
- Filtering modifies a signal's frequency content by attenuating some frequencies and passing others.
- Convolution is the core operation: slide a kernel (impulse response) over the input, computing weighted sums.
- FIR filters use only past inputs (feedforward). They are always stable and can have exactly linear phase (equal delay for all frequencies), but require more coefficients for sharp roll-off.
- IIR filters feed past outputs back in. Far more efficient than FIR for a given roll-off sharpness, but can be unstable and have non-linear phase.
- The four filter types — low-pass, high-pass, band-pass, band-stop — cover essentially all practical needs.
- Filter order determines roll-off steepness. Higher order = sharper transition, but more computation and latency.
- The convolution theorem: convolution in time = multiplication in frequency. Frequency-domain filtering is just pointwise multiplication of the signal spectrum by the filter's frequency response.
- For offline processing of long signals, FFT-based (frequency-domain) filtering is often faster than direct convolution.
Try It — Signal Filter
Check the frequency components you want in the input signal, choose a filter type, and move the cutoff slider. The left column shows the input; the right column shows the filtered output. The dashed gray line on the spectrum plots marks the filter's frequency response.