Today we go deep. Digital signal processing is not just a preprocessing chore — it encodes fundamental assumptions about your data that propagate silently into every downstream analysis. We will build genuine intuition for what a filter does to a signal in both time and frequency domains, understand why naive downsampling is catastrophic, and dissect the automated bad-channel detection machinery that modern EEG pipelines rely on.
The Mathematics of Filtering
Convolution,
Transfer Functions & the Frequency Domain
A digital filter is an operation that selectively attenuates or passes frequencies in a discrete-time signal. At its core, linear time-invariant (LTI) filtering is implemented as convolution between the input signal x[n] and the filter impulse response h[n]. Convolution in time is equivalent to multiplication in the frequency domain (the Convolution Theorem) — which is why we describe filters using their frequency response H(f).
The Four Classic Filter Types
- Low-pass filter (LPF): Passes frequencies below cutoff fc. Used for anti-aliasing before downsampling and noise removal.
- High-pass filter (HPF): Passes frequencies above fc. Removes slow DC drift and skin-potential artefacts. The most debated filter in EEG (>0.1 Hz vs 1 Hz wars).
- Band-pass filter: HPF + LPF combined. Isolates a frequency band of interest, e.g., 4–8 Hz theta.
- Band-stop / Notch: Attenuates a narrow band — typically 50/60 Hz powerline noise and harmonics.
Two Implementation Families
- FIR (Finite Impulse Response): Always stable, achieves exact linear phase (zero phase shift), but requires more coefficients for steep roll-off. MNE defaults to windowed-sinc FIR design.
- IIR (Infinite Impulse Response): Uses feedback, more computationally efficient with steeper roll-off, but has non-linear phase and can become unstable. Types: Butterworth (maximally flat), Chebyshev (steeper, passband ripple), Elliptic (steepest, ripple in both bands).
Key insight: The frequency response tells you exactly how much each frequency component of your EEG will be amplified or attenuated. A flat passband (0 dB) means faithful reproduction. The transition band between passband and stopband narrows as order increases — but at the cost of temporal ringing.
⚖️ The 0.1 Hz vs. 1.0 Hz Dilemma & The Dual-Filter Pipeline
One of the biggest debates in electrophysiology is setting the high-pass filter cutoff. Why do ERP researchers insist on 0.1 Hz while ICA specialists demand 1.0 Hz?
Preserves slow cortical potentials, late cognitive components (P300, N400, CNV), and prevents severe pre-stimulus baseline ringing and latency shifts (Widmann et al., 2015; Kappenman & Luck, 2010).
Eliminates non-stationary, low-frequency sweating and movement drifts. Winkler et al. (2015) and Dimigen (2020) proved that 1.0 Hz filtering dramatically improves ICA component stability and dipolarity.
★ The Gold-Standard Solution (Dual-Filter Pipeline):
Never compromise your ERP data! Filter a copy of your continuous EEG at 1.0 Hz to fit ICA unmixing weights $\mathbf{W}$. Then, apply those learned spatial weights back onto your 0.1 Hz filtered dataset. You get pristine artifact rejection with zero ERP distortion!
Nyquist Theorem & Aliasing
Why Sampling
Rate Is Not Just a Storage Decision
The Nyquist–Shannon Sampling Theorem (1928/1949) states: a continuous band-limited signal can be perfectly reconstructed from its samples if and only if the sampling rate fs exceeds twice the highest frequency component fmax. The Nyquist frequency is the maximum faithfully representable frequency:
Aliasing occurs when frequencies above the Nyquist limit are present in the signal before sampling. They "fold back" into the representable range and appear as phantom low-frequency signals that are mathematically indistinguishable from genuine neural oscillations. The alias frequency of a signal at frequency $f$ sampled at rate $f_s$ is:
🚀 Interactive Aliasing Demo →
Open the standalone interactive visualizer: drag the sampling-rate slider and watch high-frequency tones fold into the baseband as aliased ghost signals. Anti-aliasing filter toggle included.
The Anti-Aliasing Filter: Your Mandatory Insurance Policy
Before any downsampling step, apply a low-pass filter with cutoff at or below the new Nyquist frequency. This removes energy at frequencies that would alias. Hardware EEG amplifiers include an analog anti-aliasing filter, but its cutoff may not match your downsampling target — a software anti-aliasing filter is still required.
MNE’s raw.resample() automatically applies an anti-aliasing FIR filter before decimating.
But when filtering and downsampling separately, the order is critical: filter first, downsample
second.
Applies a brick-wall lowpass anti-aliasing FIR filter (e.g. at $0.8 \times f_{\text{Nyquist}}$) before sample rate reduction, eliminating all high-frequency powerline and muscle noise before decimation.
Naively takes every 4th sample without prior lowpass filtering. High-frequency 300 Hz muscle spikes fold backwards into the 10–40 Hz theta/alpha/beta bands, irreversibly corrupting your neural data with ghost artifacts!
Concrete EEG disaster: Record at 1024 Hz, downsample directly to 128 Hz without filtering. Muscle EMG energy at 200–400 Hz aliases down to 24–128 Hz — right into your beta and gamma bands. Your "gamma oscillations" are aliased muscle artefacts. This cannot be corrected post-hoc. Irreversible data contamination.
Filter Order & Filter Artefacts
Roll-off,
Ringing, and the Gibbs Phenomenon
The filter order determines how steeply the filter transitions from passband to stopband. A first-order Butterworth HPF rolls off at −20 dB/decade; an eighth-order version rolls off at −160 dB/decade. Higher order sounds better — but it comes with serious costs in EEG analysis.
Trade-offs of Higher Filter Order
- Ringing (Gibbs phenomenon): Any abrupt transition in the frequency domain corresponds to a sinc-like function spreading infinitely in time. A high-order filter applied near a sharp transient creates oscillatory artefacts that spread before and after the event. This is entirely artificial.
- Increased group delay (IIR only): Higher-order IIR filters introduce more frequency-dependent phase delay. Different frequency bands arrive at different times, distorting temporal relationships in the EEG.
- Filter length (FIR): More taps = more temporal smearing. A 3000-tap FIR at 1000 Hz smears 3 seconds of data around every transient.
The Pre-Stimulus Ringing Artefact: A Critical EEG Pitfall
In ERP research, a 1 Hz high-pass filter with steep roll-off and zero-phase design can create a pre-stimulus negative deflection that mimics a genuine neural prediction response. This is pure ringing — a filter artefact. Kappenman & Luck (2010) demonstrated how inappropriate high-pass filtering creates spurious ERP components that have been published as real findings.
Practical Filter Order Guidelines for EEG
- High-pass (drift removal): 0.1–0.5 Hz cutoff. Use gentle roll-off (2nd order Butterworth or long FIR). Avoid 1 Hz+ HPF with steep roll-off in ERP studies.
- Low-pass (anti-alias): 100–200 Hz cutoff before downsampling. Steeper is acceptable here since the transition band is far from signals of interest.
- Notch filter: Very narrow band — avoid IIR notch filters as they introduce severe ringing. Prefer FIR or spectrum interpolation.
- MNE default: FIR with firwin design, Hamming window, order chosen to give −6 dB at the specified cutoff. Conservative and well-motivated.
Rule of thumb (Cohen ANTS): FIR filter length / sampling rate = temporal smearing in seconds. Always check the "filter length" warning MNE prints. If it says "3001 samples at 1000 Hz", your filter smears 3 seconds of data around every transient event.
Causal vs. Acausal Filters
Phase
Distortion, Zero-Phase Filtering & the ERP Trap
A filter is causal if its output at time t depends only on input at times ≤t — it cannot look into the future. A filter is acausal (non-causal) if it uses future samples. In real-time EEG (BCI, neurofeedback) you must use causal filters. In offline analysis, acausal filtering is almost always preferable because it eliminates phase distortion entirely.
| Property | Causal Filter | Acausal (Zero-Phase) |
|---|---|---|
| Phase response | Non-linear phase delay; different frequencies time-shifted differently | Zero phase distortion; peaks preserved at their true latency |
| Pre-stimulus artefact | Only post-stimulus ringing | Ringing spreads symmetrically BEFORE AND AFTER transients |
| Real-time use | Yes — only option for BCI | No — requires the entire signal |
| Temporal precision | Peak latencies distorted (frequency-dependent shift) | Peak latencies preserved; component timing trustworthy |
| MNE default (offline) | — | Yes — MNE applies filtfilt: forward then backward pass |
| Effective order | N | 2N (forward + backward doubles the effective order) |
Zero-Phase Filtering (filtfilt): How MNE Does It
- Apply filter forward in time → correct magnitude response, but introduces phase shift.
- Reverse the output and filter again → phase shift is applied in the opposite direction, cancelling exactly.
- Reverse again → final output has zero phase distortion and doubled effective order.
Consequence: with phase="zero" (MNE default), a 2nd-order Butterworth
becomes a 4th-order zero-phase filter. The steeper effective roll-off means stronger ringing potential around
sharp transients.
When to Choose Causal Filtering
- Real-time BCI / neurofeedback: No alternative exists.
- When pre-stimulus baseline must be truly flat: If zero-phase filtering creates suspicious
pre-stimulus activity, switch to
phase="minimum"and correct for the known phase delay. - Time-frequency analysis at gamma+: Even zero-phase FIR filters introduce substantial time smearing. Morlet wavelets (Day 9) handle this more gracefully via the Heisenberg uncertainty principle.
The pre-stimulus negativity trap: A 1 Hz HPF with zero-phase design applied to data
containing a large P300 will produce a visible negative deflection ~200 ms before the stimulus. This
is pure filter ringing. Reviewers unfamiliar with filtering will mistake it for a pre-stimulus prediction
response. Always use mne.viz.plot_filter() to inspect your filter impulse response before
trusting any ERP component.
Bad Channel Detection
CleanRawData
from ASR Algorithm & Quantitative Criteria
A bad channel is any electrode whose signal no longer faithfully represents the underlying cortical field potential. Bad channels corrupt every downstream step: re-referencing spreads their noise across all channels, ICA decomposes their artefacts into multiple components, and source localization is systematically biased. Early, automated detection is essential.
Taxonomy of Bad Channel Types
- Flat / Dead channels: Amplifier failure, broken lead, dried gel. Signal is near-constant (variance near zero). PSD is dominated by the noise floor.
- High-noise / High-variance channels: Poor electrode-skin contact, high impedance, broken wire. Variance or RMS far exceeds that of neighbouring channels.
- Bridged channels: Conductive gel shorting two electrodes. Signals become near-identical (correlation >0.995). Undermines spatial specificity.
- Intermittently bad channels: Fine for most of the recording but fail during specific periods. Detected on a sliding-window basis by ASR.
- Excessive line noise channels: Power at 50/60 Hz far exceeds surrounding band — usually grounding or shielding failure at that specific electrode.
Quantitative Criteria Used by Automated Algorithms
The CleanRawData Algorithm (EEGLAB / PyPREP Implementation)
CleanRawData is the automated pipeline in EEGLAB, wrapped in Python via PyPREP. It operates in sequential passes:
from pyprep.find_noisy_channels import NoisyChannels # Initialize on MNE Raw object (montage must be set) nd = NoisyChannels(raw, do_detrend=True) # Run all detection criteria sequentially nd.find_bad_by_nan_flat() # Flat / NaN channels nd.find_bad_by_deviation() # Amplitude deviation from robust median nd.find_bad_by_hfnoise() # High-frequency noise ratio nd.find_bad_by_correlation() # Neighbour correlation (windowed) nd.find_bad_by_ransac() # RANSAC spatial prediction error # Retrieve per-criterion results for auditing bads = nd.get_bads(as_dict=True) for criterion, channels in bads.items(): if channels: print(f"Bad by {criterion}: {channels}") # Apply and interpolate raw.info["bads"] = nd.get_bads() raw.interpolate_bads(reset_bads=True)
Why not just visual inspection? Visual inspection of 64–256 channel datasets across hours of recording is impractical and subjective. Two raters marking bad channels from the same data agree on perhaps 70–80%. Automated algorithms provide reproducibility and a documented decision trail. Always do a sanity check after automation — never blindly trust any algorithm.
The 10% rule: If more than ~10–15% of channels are detected as bad, reconsider the recording entirely. Interpolating that many channels creates a spatially over-smoothed dataset. Topographies look clean but spatial resolution is severely compromised. Consider excluding the participant from the study.
RANSAC: Random Sample Consensus
Outlier-Robust
Spatial Prediction for Bad Channel Detection
RANSAC (Random Sample Consensus, Fischler & Bolles 1981) was originally developed for computer vision to fit models to data containing outliers. In EEG, it is repurposed as the gold-standard method for detecting channels whose signal cannot be explained by the surrounding electrode array.
The key insight: good EEG channels are spatially redundant. Activity at any electrode is a weighted spatial mixture of underlying cortical sources also captured by neighbouring electrodes. A good channel should be well-predictable from its neighbours — a bad channel fails this test consistently regardless of which random neighbour subset is used as predictor.
The RANSAC Algorithm for EEG — Step by Step
☝ Tip: Click any channel node directly on the canvas (or use the dropdown) to set it as Target!
RANSAC vs. Simple Threshold Methods
- Variance thresholding alone misses channels with normal overall variance but uncorrelated activity.
- Neighbour correlation alone fails when multiple adjacent channels are all bad — they correlate with each other and appear normal.
- RANSAC is robust to clustered bad channels because it samples from the full array. It is also robust to transient artefacts because of windowed analysis. It can identify channels bad only 40% of the time.
Computational cost: O(n_channels × n_iterations × n_windows) with matrix solve at each step. For 256 channels this can take minutes — acceptable for offline analysis.
🎯 Why RANSAC Outperforms Local Correlation (The Spatial Cluster Trap)
Traditional correlation checks ask: "Does this electrode correlate with its immediate neighbours?" This fails catastrophically when a cluster of adjacent electrodes goes bad together (e.g., Fp1, Fp2, and Fpz shorting due to forehead sweat). Because they are all corrupted by the same noise source, they correlate strongly with each other ($r > 0.85$) and fool local algorithms!
The RANSAC Solution: By drawing random subsets of 25–30 electrodes from the entire scalp montage, RANSAC forces distant channels (parietal/occipital) to predict the frontal site using spherical spline physics ($m=4, \lambda=10^{-5}$). Since the distant channels do not share the localized sweat artifact, their prediction fails repeatedly, successfully exposing the bad cluster!
PyPREP Consensus Parameters: RANSAC precomputes spherical spline matrices for all electrode pairs. An electrode is marked bad if its spatial prediction correlation falls below corr_thresh = 0.75 for more than frac_bad = 0.40 (40%) of data windows across $N=50$ random iterations.
Interpolation of Bad Channels
Spherical
Splines & What You Are Actually Getting
Once bad channels are identified, you either exclude them or interpolate them. Exclusion is conservative but reduces spatial resolution and can break algorithms requiring a complete channel set. Interpolation reconstructs a plausible estimate from spatial neighbours using spherical spline mathematics.
Spherical Spline Interpolation (Perrin et al., 1989)
- Project all electrode locations onto a unit sphere (head approximated as spherical).
- Interpolation is a linear combination of radial basis functions (Green functions of the surface Laplacian) centred at each good electrode location.
- Weights are solved by minimising the squared norm of the surface Laplacian — finding the smoothest function consistent with the observed data.
- The interpolated value at the bad electrode location is read off from the fitted smooth surface.
What Interpolation Gives You — and What It Does Not
- Gives: A spatially smooth estimate consistent with surrounding voltages. Topographic maps look complete; channel can be included in ICA and source localization without gaps.
- Does NOT give: The original unique neural information. The interpolated signal has zero independent information — entirely determined by neighbours. Effective spatial degrees of freedom are reduced by the number of interpolated channels.
Implications for ICA: The Rank Deficiency Problem
Interpolated channels introduce spatial rank deficiency. With 64 channels and 4 interpolated, your data matrix has rank 60. ICA algorithms assuming a full-rank covariance matrix will fail or produce spurious components. Always set the rank parameter explicitly when fitting ICA after interpolation.
- Colour gradient = interpolated potential field on the scalp surface. Warm colors (red/coral) represent positive electric potential (+μV), cool colors (blue/teal) represent negative potential (-μV), and dark navy represents the zero/isoelectric baseline. Spherical spline functions model continuous field flow across the 3D scalp surface.
- Gray ring = bad/missing channel (Fp2), reconstructed entirely via spherical spline spatial interpolation.
- Teal rings = good electrodes driving the spherical spline radial basis functions.
Before or after ICA? Interpolate after ICA. Interpolated channels confuse ICA — it finds a component that perfectly explains the interpolated channel because it is a linear mixture of others by construction. Exclude bads during ICA fitting, then interpolate afterwards.
Max interpolation limit: At >5% of channels, spatial resolution is noticeably reduced. At >10%, seriously reconsider. At >15–20%, the participant data may be scientifically unusable for topographic or source-level analysis.
🧭 The Day 4 Canonical Pipeline Order
1
Import &
Set Montage
2
HP Filter
(0.1+ Hz)
3
LP / Notch
Anti-alias
4
Downsample
(safe now)
5
Detect Bad
Channels
6
ICA / ASR
(Day 5)
7
Interpolate
Bads
8
Re-reference
(Day 5)
Key ordering constraints: Filter BEFORE downsample (Nyquist). Detect bads BEFORE ICA (bads corrupt decomposition). Interpolate AFTER ICA (preserve rank). Re-reference AFTER interpolation (complete channel set required).