filtering Level 1Level 2 pf-filter-epoched-data

Filtering short epochs

Symptom. Edge artifacts inside the analysis window.

Symptom

ERPs or time-frequency maps with deflections that ramp toward the edges of the epoch, a baseline that is not flat, or a low-frequency component whose amplitude depends on the epoch length rather than on the condition. The problem is worst for high-pass filters and for low-frequency time-frequency estimates, and it grows as epochs get shorter.

Cause

A filter needs samples on both sides of every point it computes. In a short epoch those samples do not exist near the edges, so the implementation pads (with zeros, a mirror image, or a constant); the padded values are not data, and the filter’s response to the padding leaks into the epoch by the length of its impulse response. For a high-pass with a low cutoff that length can exceed the whole epoch, so every sample is affected. Each epoch also starts and ends at an arbitrary phase of ongoing activity, so the edge discontinuity differs from trial to trial and does not average out cleanly.

Detect

  • Compare the ERP computed from epochs cut before filtering against the ERP from data filtered continuously and epoched afterwards; any difference is edge effect.
  • Compute the impulse-response length of the filter (FIR: the number of taps; IIR: the settling time) and compare it with the epoch length.
  • Plot the average of the pre-stimulus baseline across trials: a systematic slope or curvature near the epoch start is a sign of edge contamination.
  • For time-frequency maps, shade the region within one wavelet (or filter) length of each edge and check that no result lies in it (pf-tf-edge-effects).

Fix

  • Filter continuous data, then epoch (the canonical order in L2.8).
  • If data are only available as epochs, use epochs long enough that the analysis window lies more than one impulse-response length from each edge, and state the padding method.
  • Choose the mildest filter that serves the goal; a lower-order or wider-transition-band filter has a shorter impulse response.
  • For real-time or single-epoch processing, use causal filters and accept the group delay rather than zero-phase filtering with padding.

Example

Top: two versions of a two-second band-passed EEG epoch overlaid; they agree in the middle and diverge towards both ends. Bottom: their difference, near zero in the centre and growing towards the two edges, with the whole epoch shaded because the filter is longer than the epoch.

ds-eegbci S001 R01, O1, a 2-s epoch from 20 s. Filtering the continuous run and then cutting the epoch, versus cutting first and filtering the 2-s epoch (MNE’s default 1–30 Hz FIR, 529 taps = 3.31 s, longer than the epoch; MNE warns that distortion is likely). Padding invents data at the edges: the error reaches 26 µV at the edges and 1.5 µV in the middle half-second. Filter before epoching, or use epochs much longer than the filter. Generated by data/scripts/make_figures.py (ODC-By 1.0).