Bench modeSteps, parts, and safety only. Big type for a phone at the bench.

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

You are skimming: the title, the first figure, and the short version. Switch to Read in the header for the full page, or Deep to open every deep dive.

A removes frequencies you do not want. Every filter 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 filter’s smoothing is still there, but it is in the right place.
  3. Switch to FIR 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 filters need many taps: a steep low-pass at 1 Hz on 250 Hz data needs hundreds of weights and a long delay.

An feeds its previous outputs back. A handful of coefficients gives a sharp response, which is why every analog filter and every real-time digital filter is IIR. 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 ERP peak by tens of milliseconds.

Butterworth and its cousins

Among IIR 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 phase distortion) 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 spikes at order 8.

Zero-phase filtering

Run an IIR filter 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 filter 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 BCI, which is why decoders that work offline sometimes disappoint online.

Predict before you look

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

A symmetric FIR with its delay subtracted, or a low-order causal IIR 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 FIR’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 filter length. Real-time systems live with lag; they cannot live with unknown distortion.

Ordering mistakes

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

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

Notch in the wrong place. A notch after epoching rings across the epoch. A notch 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; MNE’s resample does this for you.

Filtering the average. Filter the single trials and then average, or average and then filter; the result is the same for linear filters, 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 phase distortion. is the negative slope of the phase and is the delay each frequency’s envelope experiences.

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. MNE’s default filter is exactly this, with the length chosen from the transition width. The interactive’s FIR is a Hamming-windowed sinc.

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

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