Chuyển đến nội dung

Bài 33 · Applications

ECG and Power-Line Denoising

Clean a synthetic ECG with a 50 Hz notch, a baseline-wander high-pass and zero-phase filtering, then detect heartbeats and estimate the heart rate in Python.

A biomedical signal is a perfect test of filter design. An electrocardiogram (ECG) records the electrical activity of the heart as a small voltage (about 1 mV) riding on much larger disturbances. Three disturbances appear in nearly every recording: power-line interference at 50 or 60 Hz, baseline wander from breathing and electrode movement (below about 0.5 Hz), and muscle noise at higher frequencies. This lesson builds a simulated ECG, adds those problems, and removes them with the filters you know.

There is no real patient data here: the ECG is synthetic and the lesson is about signal processing, not diagnosis.

A synthetic ECG

One heartbeat is a sum of five Gaussian bumps called P, Q, R, S and T. Repeating the beat every second gives a signal of 60 beats per minute.

Python Generating a test ECG

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The noisy trace wanders by about a millivolt, as large as the R peak itself. A simple threshold would fail.

The three-filter recipe

ECG cleanup chain

High-pass 0.5 Hz Removes baseline wander
Notch 50 Hz Removes mains hum
Low-pass 40 Hz Trims muscle noise

Use zero-phase filtering so QRS shapes stay symmetric.

1 / 2

Phase matters in the ECG: a nonlinear phase filter distorts the shape of the QRS complex and the ST segment, which clinicians read carefully. That is why offline analysis uses zero-phase filtering (sosfiltfilt, forward then backward). Real-time monitors that cannot look ahead use linear-phase FIRs or carefully designed IIRs.

Python Filtering the ECG

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The RMS error falls from 0.42 mV to about 0.08 mV and the R peak is unchanged. The notch removes the 50 Hz line without touching the nearby QRS energy because its Q is high (narrow). Use 60 Hz in countries with a 60 Hz mains supply.

Detecting the heartbeat

The R wave is the tallest, sharpest part of the beat. A standard detector (inspired by the Pan-Tompkins algorithm) band-passes around 5-15 Hz, differentiates, squares and integrates to emphasize the QRS, then finds peaks with a minimum spacing.

Python QRS detection and heart rate

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Every beat in the record is found (35; the 36th R wave in the list falls just after the end of the 30 s record), the detections are within a few milliseconds of the true R waves, and the heart rate comes out at the true 70.6 beats per minute.

What the adaptive alternative offers

A fixed notch removes interference only at a fixed frequency. If the mains frequency drifts (for example on generators) or if the interference is not sinusoidal, use an adaptive canceller with a reference sensor, or adapt the notch frequency.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why is zero-phase filtering preferred for offline ECG analysis?

    Forward-backward filtering cancels the phase response so waveform shapes are preserved, which matters for clinical interpretation.
  2. 2. A high-pass filter at about 0.5 Hz is used to remove

    Baseline wander is very slow drift below about 0.5 Hz; a gentle high-pass removes it without touching the heartbeat.
  3. 3. Why is the notch filter given a high Q?

    A narrow notch removes the 50 or 60 Hz line while leaving neighboring ECG frequencies almost untouched.
  4. 4. What is the purpose of squaring in a QRS detector?

    Squaring the derivative makes the signal non-negative and emphasizes the steep QRS slopes relative to smaller waves.

Summary

  • ECGs are corrupted by mains hum, baseline wander and muscle noise; each has its own filter.
  • Use a high-pass near 0.5 Hz, a high-Q notch at 50/60 Hz and a low-pass near 40 Hz, applied with zero-phase filtering offline.
  • A band-pass, derivative, squaring and integration chain highlights QRS complexes for peak detection and heart-rate estimation.
  • Fixed notches fail for drifting interference; adaptive cancellers do not.

Next: AM and FM Demodulation

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.