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.
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 filterFilterAn operation that passes some frequencies and attenuates others; every filter also delays or distorts the timing of what it passes. Glossary entry 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.
- 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.
- 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.
- 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.
- 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 FIR filterFIR filterA filter that outputs a weighted sum of recent inputs only; symmetric ones delay every frequency equally, which preserves waveform shape. Glossary entry 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 IIR filterIIR filterA filter that feeds its output back into itself; cheap and sharp, but it delays different frequencies by different amounts. Glossary entry 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 phase distortionPhase distortionDifferent frequencies being delayed by different amounts, which smears and shifts waveforms such as an ERP peak. Glossary entry, and in Figure 1 you watched it move an ERP peak by tens of milliseconds.
Butterworth and its cousins
Among IIR designs, ButterworthButterworth filterThe IIR filter with the flattest possible passband, the default choice when you do not have a reason to choose something else. Glossary entry 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 zero-phase filteringZero-phase filteringRunning a filter forward and then backward through recorded data so the delays cancel; impossible in real time. Glossary entry. 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.
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. Group delayGroup delayHow long a filter delays the envelope of a signal at each frequency; constant for symmetric FIR filters. Glossary entry 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.
Explain what this page was about to your roommate in three sentences. No jargon they would not know.