viewof bwAmp = Inputs.range([0, 2], { value: 0.8, step: 0.05, label: "Baseline wander (mV)" })
viewof humAmp = Inputs.range([0, 1], { value: 0.3, step: 0.01, label: "Mains noise (mV)" })
viewof humF = Inputs.radio([50, 60], { value: 50, label: "Mains frequency (Hz)" })
viewof emgAmp = Inputs.range([0, 0.2], { value: 0.04, step: 0.005, label: "EMG noise SD (mV)" })5 Digital Filters
A filter is a process that keeps the desired frequency components of a signal and reduces the unwanted ones. It is the most frequently used tool in biosignal processing, but it always has the side effect of “distorting the signal in exchange for removing noise”.
5.1 Types of filters
| Type | Components passed | Use in ECG |
|---|---|---|
| Low-pass (LPF) | Components below the cutoff frequency | Removing EMG and high-frequency noise |
| High-pass (HPF) | Components above the cutoff frequency | Removing baseline wander |
| Band-pass (BPF) | A specific band | Preprocessing for QRS detection (e.g., 5–15 Hz) |
| Band-stop / notch | Everything except a specific frequency | Removing mains noise (50/60 Hz) |
5.1.1 FIR and IIR
| FIR (finite impulse response) | IIR (infinite impulse response) | |
|---|---|---|
| Equation | y[n] = \sum_k b_k x[n-k] (inputs only) | y[n] = \sum_k b_k x[n-k] - \sum_k a_k y[n-k] (also uses past outputs) |
| Phase | Can be linear phase (preserves waveform shape) | Generally nonlinear phase (delay differs by frequency → waveform distortion) |
| Computation | Many coefficients needed for a sharp response | Sharp response with few coefficients |
| Stability | Always stable | Can be unstable depending on design |
| Examples | Moving average | Butterworth, notch |
5.1.2 Phase shift and the “zero-phase filter”
Because an IIR filter has a different delay (phase shift) at each frequency, it changes the shape of the waveform. When processing recorded data (offline processing), the phase shift can be cancelled by applying the same filter forward and backward (zero-phase filter, filtfilt). However, because it uses future data, it cannot be used for real-time processing.
5.2 Recommendations for ECG filters
According to the recommendations of the American Heart Association (AHA) and others (Kligfield et al. 2007):
| Use | High-pass (lower limit) | Low-pass (upper limit) |
|---|---|---|
| Diagnostic ECG | 0.05 Hz (up to 0.67 Hz allowed with a linear-phase filter) | 150 Hz (adults) |
| Monitoring ECG | about 0.5 Hz | about 40 Hz |
The high-pass cutoff is set very low (0.05 Hz) for diagnostic use because a nonlinear-phase high-pass filter distorts the ST segment, leading to errors in judging myocardial ischemia (ST elevation/depression). For monitoring (heart rate and arrhythmia surveillance) the band is narrowed to favor readability, but such recordings cannot be used to assess the ST segment.
5.3 Demo: filtering a noisy ECG
Baseline wander, mains noise, and EMG noise are added to a 10-second synthetic ECG sampled at 500 Hz. All filters are second-order IIR (Butterworth-type HPF/LPF, notch).
5.3.1 The full 10 seconds
5.3.2 Zoom on 2 beats: comparison with the original (noise-free) ECG
5.3.3 Frequency response of the whole filter chain
TipTry it
- Turn off all filters and check how each type of noise looks
- Set baseline wander, mains noise, and EMG noise all to 0, then raise the HPF cutoff 0.05 → 0.5 → 2 Hz and observe how the ST segment (right after the QRS) and the T wave in the zoomed view are distorted. Also look at the error (RMS) (with noise present, the effect of removing baseline wander and the distortion are mixed and cannot be compared)
- Keeping the HPF at 2 Hz, how does the distortion change when you turn on “Zero phase”? Also confirm that even with the LPF at 40 Hz, the position (timing) of the R wave changes when zero phase is turned on/off
- When the LPF is set to 15 Hz, what happens to the R-wave height and the Q and S waves?
- Confirm that when you change the mains frequency to 60 Hz, the notch frequency follows automatically (in real equipment, a wrong regional setting means it cannot be removed)
NoteExplanation
- Baseline wander is removed well, but the higher the cutoff, the more the HPF phase shift produces a downward deflection after the QRS (spurious ST depression), and the shape of the T wave also changes. This is a typical cause of errors in ST-segment assessment.
- Zero phase reduces the asymmetric distortion due to phase shift, but at 2 Hz the low-frequency components of the ECG themselves (such as the T wave) are cut, so amplitude distortion remains. This is why the AHA recommendation allows up to 0.67 Hz only with linear-phase (zero-phase) filtering.
- Because the QRS contains many components around 10–40 Hz, a 15 Hz LPF lowers the R wave, blunts the Q and S waves, and makes the QRS look wider. It can be used as preprocessing for QRS detection but not for waveform diagnosis.
5.4 Moving average and differentiation: the simplest filters
- Moving average: y[n] = \frac{1}{M}\sum_{k=0}^{M-1} x[n-k]. The simplest LPF. An M-point moving average completely removes frequencies at integer multiples of f_s/M (e.g., a 10-point moving average at 500 Hz removes 50 Hz and its multiples)
- Difference (differentiation): y[n] = x[n] - x[n-1]. An HPF that emphasizes higher frequencies more. Used in QRS detection to emphasize steep upstrokes (Chapter 7)
5.5 Noise that frequency filters cannot remove
- Motion artifacts, EMG: because they overlap the ECG band, frequency filtering becomes a trade-off between “cutting the signal along with the noise” and “leaving the noise”
- Countermeasures include adaptive filters (using reference signals such as acceleration), wavelet transforms, independent component analysis, ensemble averaging that superimposes multiple beats, and removal by machine learning
5.6 Summary
- Choose among LPF / HPF / BPF / notch. FIR is linear phase; IIR needs less computation but distorts phase
- For offline processing, a zero-phase filter (filtfilt) prevents phase distortion
- An ECG HPF distorts the ST segment. About 0.05 Hz for diagnostic use and 0.5 Hz for monitoring
- Noise whose band overlaps the signal cannot be fully removed by frequency filters