↑↓ move↵ open[] previous / next lesson? all shortcuts
Appearance
Theme
Type size
Reading

Focus mode hides the sidebars. Press f to toggle it anywhere.

Keyboard shortcuts
/
Search
[ ]
Previous / next lesson
t
Jump to the table of contents
r
Review queue
f
Focus mode
?
This panel
Explainer16 min

Digital filters, and what they do to time

FIR versus IIR, Butterworth and its cousins, phase distortion, zero-phase filtering, and the ordering mistakes that quietly wreck analyses.

AssumesWhat the spectrum didAnalog to digitalSpineDecoding / signal processing / ML
On this page16 min
  1. Two families
  2. Butterworth and its cousins
  3. Zero-phase filtering
  4. Ordering mistakes

A removes frequencies you do not want. Every also does something to the frequencies you keep: it delays them, and usually by different amounts, which smears waveforms and shifts peaks. The whole art of filtering biosignals is choosing which distortion you can afford. Let’s design a few and watch what they do to an event-related potential.

Filter designer
This interactive needs JavaScript. If you are reading a printout, the caption describes what it shows.
Figure 1. Design a filter and watch its effect on a test signal. The top panel is the frequency response (gain in green, phase in yellow); the bottom is a test ERP before and after.
Try this
  1. Butterworth low-pass, order 4, cutoff 30 Hz. Read the peak latency shift in the readout. Now order 8. The edge gets sharper and the shift gets larger.
  2. Turn on zero-phase. The shift goes to zero. The peak is a little smaller, because the ’s smoothing is still there, but it is in the right place.
  3. Switch to and vary the taps. The phase is a straight line (constant delay), which the plot shows already compensated. Watch the transition get sharper as taps increase.
  4. Set the test signal to a step and try a high-pass at 1 Hz. That droop after the step is what a high-pass does to a slow potential.

Two families

An computes each output sample as a weighted sum of the last N input samples. Nothing is fed back. Make the weights symmetric and every frequency is delayed by exactly the same amount, (N−1)/2 samples, so waveform shapes are preserved and you can shift the output back by that amount to line it up. The cost is that sharp need many taps: a steep low-pass at 1 Hz on 250 Hz data needs hundreds of weights and a long delay.

Written out, with input xx, output yy, weights bb, and NN taps:

y[n]=∑k=0N−1bk x[n−k]y[n] = \sum_{k=0}^{N-1} b_k, x[n-k]

Symmetric weights (bk=bN−1−kb_k = b_{N-1-k}) give every frequency the same delay of (N−1)/2(N-1)/2 samples, which is why the shape survives.

An feeds its previous outputs back. A handful of coefficients gives a sharp response, which is why every analog and every real-time digital is . The cost is that the delay varies with frequency: low frequencies come through later than high ones near the cutoff. That is , and in Figure 1 you watched it move an peak by tens of milliseconds.

Written out, with feedforward weights bb and feedback weights aa:

y[n]=∑k=0Mbk x[n−k]−∑k=1Pak y[n−k]y[n] = \sum_{k=0}^{M} b_k, x[n-k] - \sum_{k=1}^{P} a_k, y[n-k]

The aa terms feed past outputs back into the sum, and that feedback is what makes the delay vary with frequency.

Butterworth and its cousins

Among designs, has the flattest passband with no ripple, at the cost of a gradual roll-off. Chebyshev rolls off faster but ripples in the passband. Bessel has the most constant delay (the least ) and the gentlest roll-off. Elliptic is the sharpest and the worst behaved in phase. For biosignals the default is Butterworth, and the order is usually 2 to 4: higher orders ring after sharp events, which you can see in Figure 1 by setting the test signal to at order 8.

Zero-phase filtering

Run an forward through the data, then run it backward through the result. The forward pass delays each frequency by some amount; the backward pass delays it by the same amount in the other direction. The delays cancel exactly and the peak lands where it belongs. This is filtfilt, and it is . Two consequences. The magnitude response is applied twice, so a 4th-order behaves like an 8th-order one in attenuation. And it needs the future: it is only possible on recorded data, never in a real-time , which is why that work offline sometimes disappoint online.

In frequency terms, with H(f)H(f) the ’s response, forward-backward filtering gives:

∣Heff(f)∣=∣H(f)∣2,∠Heff(f)=0|H_{eff}(f)| = |H(f)|^2, \quad \angle H_{eff}(f) = 0

The magnitude is squared, so attenuation in decibels doubles; the phase cancels to zero at every frequency.

Predict before you look

You need to detect the P300 peak latency in a real-time speller. Which filter?

A symmetric with its delay subtracted, or a low-order causal whose latency shift you have measured and accept. Filtfilt is impossible in real time. A causal Butterworth shifts the peak by an amount that depends on order and cutoff, which is fine only if you have measured it. The ’s delay is constant and known, so you subtract it and the latency is right, at the cost of a fixed lag of half the length. Real-time systems live with lag; they cannot live with unknown distortion.

Ordering mistakes

after epoching. A low-frequency needs seconds of data to settle. On a one-second it distorts the edges and can add a slope across the whole thing. the continuous data first, always.

High-pass too high. A 1 Hz high-pass on an experiment removes the slow components that some (the late positive, the readiness potential) consist of, and it can create fake early components as an of the droop. For , 0.1 Hz; for oscillations, 1 Hz is fine.

in the wrong place. A after epoching rings across the . A at all, if the hum could have been fixed at the source, hides a problem.

Downsampling without a low-pass. Decimating 1000 Hz data to 250 Hz by taking every fourth sample aliases everything above 125 Hz into your band. Low-pass first, always; ’s resample does this for you.

Decimating 1000 Hz data to 250 Hz moves the Nyquist limit from 500 Hz to 125 Hz:

fN=fs/2f_N = f_s / 2

Everything above the new fNf_N must be filtered out before the rate change, never after.

Filtering the average. the single trials and then average, or average and then ; the result is the same for linear , but filtering the average hides how much each trial was distorted. Do it on the trials so you can see.

Deep dive Reading a frequency response 3 min

The magnitude plot in decibels: 0 dB is unchanged, −3 dB is 70 percent (the conventional cutoff), −20 dB is a tenth, −40 dB is a hundredth. The transition band is the stretch between passband and stopband; a steeper transition needs a higher order or more taps. The phase plot in radians, unwrapped: a straight line is a constant delay (its slope is the delay); a curve is . is the negative slope of the phase and is the delay each frequency’s envelope experiences.

Decibels compress ratios with a logarithm, for amplitudes:

AdB=20log⁡10AA_{dB} = 20 \log_{10} A

So 1/2≈0.7071/\sqrt{2} \approx 0.707 becomes −3-3 dB, 1/101/10 becomes −20-20 dB, and 1/1001/100 becomes −40-40 dB. is the matching time-domain measure:

τg(f)=−12πdϕdf\tau_g(f) = -\frac{1}{2\pi}\frac{d\phi}{df}

Here ϕ\phi is the unwrapped phase in radians; τg\tau_g is the delay each frequency’s envelope experiences, constant for a straight-line phase and varying wherever the phase curves.

Deep dive Designing an FIR filter 3 min

The windowed-sinc method: the ideal low-pass has an impulse response shaped like sin(x)/x, which is infinitely long. Truncate it to N taps and multiply by a smooth window (Hamming, Blackman) to tame the ripples the truncation causes. More taps gives a sharper transition; a wider window gives less ripple but a wider transition. ’s default is exactly this, with the length chosen from the transition width. The interactive’s is a Hamming-windowed sinc.

Deep dive Biquads and why real-time filters are cascades of them 3 min

Any can be broken into second-order sections, “biquads,” each with five coefficients. Cascading them is numerically far better behaved than one high-order , especially on a microcontroller in fixed point. The output=‘sos’ option in SciPy and the chain in the site’s simulator are both biquad cascades. If you write that , write a biquad.

Recall
Why does a symmetric FIR filter preserve waveform shape and an IIR filter not?
A symmetric FIR delays every frequency by the same amount, (N−1)/2 samples, so shapes are preserved and the delay can be subtracted. An IIR's delay varies with frequency, smearing and shifting waveforms.
Recall
What does filtfilt do, what does it cost, and when is it impossible?
It runs the filter forward then backward so phase delays cancel; the magnitude response is applied twice (double the attenuation); it needs the whole recording, so it cannot run in real time.
Recall
Name three filtering order mistakes.
Filtering after epoching (edge distortion); downsampling without a low-pass first (aliasing); using a high-pass too high for slow ERP components (removes or fakes components).
Explain it to your roommate

Explain what this page was about to your roommate in three sentences. No jargon they would not know.