✦ Day 04 · Signal Conditioning

Filtering, Nyquist &
Bad Channel Detection

From convolution kernels and phase distortion to the CleanRawData algorithm, spherical-spline interpolation, and the elegant logic of RANSAC — the rigorous signal processing foundations every EEG analyst must master.

📊 Nyquist Theorem & Aliasing ⚠ The 0.1 vs 1.0 Hz High-Pass Dilemma 🧮 Dual-Filter Pipeline ⌛ Causal vs. Acausal Phase Traps 🧹 CleanRawData / ASR 🎯 RANSAC Spatial Consensus 🌐 Spherical Spline Math

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.


1

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).
$$y[n] = (x * h)[n] = \sum_{k} x[k] \cdot h[n - k]$$
Time-domain convolution $\equiv$ frequency-domain multiplication: $Y(f) = X(f) \cdot H(f)$
Frequency response (magnitude in dB). Note how filter order controls roll-off steepness and transition bandwidth.

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?

🧠 0.1 Hz for ERP Waveforms:

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).

⚡ 1.0 Hz for ICA Decomposition:

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!


2

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:

$$f_s > 2 \cdot f_{\max} \implies f_{\text{Nyquist}} = \frac{f_s}{2}$$
At $250\text{ Hz}$ sampling rate: Nyquist $= 125\text{ Hz}$. Any EEG energy above $125\text{ Hz}$ will alias if not removed first.

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:

$$f_{\text{alias}} = \left| f - \text{round}\left(\frac{f}{f_s}\right) \cdot f_s \right|$$
Example: A $300\text{ Hz}$ tone sampled at $250\text{ Hz} \implies f_{\text{alias}} = |300 - 1 \times 250| = 50\text{ Hz}$ — indistinguishable from gamma!
🎵

🚀 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.

✓ Safe Resampling (`raw.resample`):

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.

❌ Fatal Decimation (`data[:, ::4]`):

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.


3

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.

Ringing Demo: Apply HPF to a step function (simulates large ERP transient)
Red = original step. Blue = filtered output. Pink zone = pre-stimulus artefact zone. Oscillations here are 100% artificial — pure filter ringing growing with order.

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.


4

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

  1. Apply filter forward in time → correct magnitude response, but introduces phase shift.
  2. Reverse the output and filter again → phase shift is applied in the opposite direction, cancelling exactly.
  3. 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.
Gray = original signal with sharp transient. Colored = filtered output. Toggle modes to compare symmetric vs. one-sided ringing and phase delay.

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.


5

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

σ
Variance / RMS deviation: Flag if variance is outside the robust distribution (e.g., z-score > 5 or < −3) of all channels. Flat channels: near-zero variance. Noisy channels: orders-of-magnitude higher. Robust statistics (median, MAD) are preferred over mean/SD to avoid circular dependency on other bad channels.
r
Neighbour correlation: Genuine EEG has strong spatial autocorrelation. Compute max absolute correlation of each channel with any spatial neighbour on a sliding window. If persistently below threshold (default 0.4–0.75), the channel is not tracking the local field. Bridges flagged if correlation > 0.995 between two channels.
HF
High-frequency noise ratio: Power in 50–(fs/2) Hz band vs. power in 1–50 Hz band. Genuine EEG has a 1/f power spectrum; bad channels driven by HF noise have an elevated HF portion. Threshold: ratio > 0.25 flags the channel (configurable).
SNR
RANSAC spatial prediction error: How well can the channel be predicted from a random sample of other channels? If prediction fails consistently across iterations, the channel is anomalous. This is the most powerful criterion and is covered in detail in Section 6.

The CleanRawData Algorithm (EEGLAB / PyPREP Implementation)

CleanRawData is the automated pipeline in EEGLAB, wrapped in Python via PyPREP. It operates in sequential passes:


1
Flat channel removal: Any channel with RMS amplitude below a threshold (default: 5 μV or relative to dataset noise floor) for more than a critical fraction of the recording is immediately marked bad.
2
High-frequency noise detection: Per-channel ratio of power in the 50–(fs/2) Hz band to power in 1–50 Hz. Channels exceeding 0.25 are flagged as noise-dominated — they have more high-frequency power than is typical of scalp EEG.
3
Correlation-based detection: On multiple short windows, compute the maximum absolute correlation of each channel with any of its spatial neighbours. If persistently below threshold (default 0.75), the channel is not tracking the local field and is flagged.
4
RANSAC-based spatial prediction: For each channel, trains a spatial predictor using random subsets of other channels and measures the prediction error. Consistently high error across iterations → anomalous channel. See Section 6 for full walkthrough.
5
Conservative union: Any channel flagged by any criterion is marked bad. Errs on the side of caution: better to interpolate a marginal channel than let its noise corrupt ICA decomposition.
Simulated 8-channel EEG — three channels with different failure modes (animated, 2.5s refresh)
Ch1=Normal • Ch2=Flat/Dead (variance ≈ 0) • Ch3=Normal • Ch4=High Variance (noisy) • Ch5=Normal • Ch6=Normal • Ch7=HF Noise dominated • Ch8=Normal
Python / PyPREP
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.


6

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

1
Segment the data: Divide the continuous recording into short overlapping windows (typically 4–5 seconds). Windowed analysis allows detection of channels that are only intermittently bad — a key advantage over global statistics.
2
For each target channel c, repeat ~50 iterations: Randomly sample a small subset of other channels (e.g., 25 out of 63 remaining). This is the "random sample" step — crucial for robustness since we do not know a priori which other channels are bad.
3
Fit a spatial predictor: Using the selected subset, compute the spherical-spline weights that best map their signals onto the location of channel c. Then predict channel c signal from these weights. The same spherical spline mathematics used for interpolation is used here for prediction.
4
Measure prediction error: Compute the Pearson correlation between the predicted and actual signal of channel c in this window. A good channel correlates highly (r > 0.85) with predictions from almost any random neighbour subset.
5
Aggregate across iterations: Compute the fraction of windows where channel c prediction correlation fell below a threshold (default r = 0.75). If this fraction exceeds 40% of windows, channel c is flagged as bad.
Why "consensus"? Using many random subsets and looking for agreement makes the algorithm robust. Even if some predictor subsets include other bad channels, most will consist of good channels and successfully predict a good target. The consensus vote over iterations is the key insight that gives RANSAC its power and robustness.
RANSAC spatial prediction simulation: green = good channel (predictable), red = bad channel (fails prediction). Target channel shown with glow.
☝ 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.


7

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.
$$V(\mathbf{r}) = \sum_{j=1}^{N} \lambda_j \, G(\mathbf{r} \cdot \mathbf{r}_j) + \mu$$
$V(\mathbf{r}) =$ interpolated potential at location $\mathbf{r}$. $G =$ Green function of surface Laplacian. $\lambda_j =$ fitted weights. $\mu =$ constant monopole offset.

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.

Spherical spline interpolation — reconstructing a missing electrode from the surrounding field
🎨 Topographic Legend & Color Gradient Details:
  • 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).

📚

Further reading

  1. MNE-Python Official Guide: Background on Filtering & Resampling EEG Data 📖
    In-depth tutorial on digital filter design in MNE, cutoff selection, transition bandwidths, zero-phase filtering (filtfilt), and avoiding phase distortion.
  2. Digital Filter Design for Electrophysiological Data (Widmann et al., 2015) 📑
    Landmark paper detailing impulse response, causal vs. acausal filtering, passband ripple, roll-off, and preventing filter-induced ERP latency shifts and phase distortion.
  3. Phases fading away by Rahul Venugopal 📖
    Rahul Venugopal's repository exploring phase dynamics, phase distortion, zero-phase filtering, and signal degradation mitigation techniques in EEG processing.