S1552 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

S15 · Lesson 52 · ECG processing lab

Three stages of an ECG front end in NumPy: a first-order high-pass that removes baseline wander, a second-order notch that removes power-line interference, and a Pan–Tompkins-style detector that finds every heartbeat in a noisy, wandering record

Every bedside monitor and wearable patch runs a version of the same pipeline: strip off the slow wander from breathing and electrode drift, remove what's left of the power line, and find the QRS complexes to count the heart rate. In this lab you'll build each stage in Python and test it against the exact frequency response it should have, then against synthetic ECG records with known beat times, wandering baselines and noise, where a detector must find every beat and nothing else.

Predict

Your detector differentiates the ECG and squares it to emphasize the QRS. Without any smoothing first, what goes wrong on a record with 50 µV of broadband noise?

Name it

Task 1: baseline removal

The analog RC high-pass of lesson 18 becomes, with time step T and τ = 1/(2πf_c),

y[n]=a(y[n−1]+x[n]−x[n−1]),a=ττ+T

Its gain at frequency f is |a(1−z−1)/(1−az−1)| with z=ej2πf/fs, which the tests check at 0.2, 1 and 10 Hz.

Task 2: the notch

Zeros on the unit circle at ±ω₀ = ±2πf₀/f_s remove f₀ completely; poles just inside, at radius r, restore the response elsewhere:

y[n]=g(x[n]−2cos⁡ω0x[n−1]+x[n−2])+2rcos⁡ω0y[n−1]−r2y[n−2]

with g=(1−2rcos⁡ω0+r2)/(2−2cos⁡ω0) for unit gain at DC. The notch's width is about (1−r)fs/π.

Task 3: beat detection

Smooth over about 20 ms, take a central difference, square, average over 150 ms, and find the peaks above a threshold with a 250 ms refractory period; then move each detection to the largest ECG sample within 75 ms. The tests use synthetic beats with P, QRS and T waves, RR intervals from 0.6 to 1.1 s, 0.3 mV of baseline wander and 50 µV of noise.

Worked example

What should task 1 give for a 0.5 Hz corner at 500 samples/s?

Show the solution
  1. Time constant. For a 0.5 Hz corner: τ=0.318s
  2. Filter coefficient. At 500 samples/s the sample interval is 0.002 s: a=ττ+0.002=0.99376
  3. Gain at three frequencies. |H(0.2Hz)|=0.371|H(1Hz)|=0.892|H(10Hz)|=0.996 so breathing-rate wander at 0.2 Hz is cut by almost two-thirds.
  4. Against the analog filter. These match the analog filter closely because the corner is far below the sample rate.

Answer a = 0.99376, with gains of 0.371 at 0.2 Hz, 0.892 at 1 Hz and 0.996 at 10 Hz.

Worked example

What should task 2 give for a 60 Hz notch with r = 0.98 at 500 samples/s?

Show the solution
  1. Normalizing gain. g=0.9807
  2. Gain at the test frequencies. |H(60Hz)|=0|H(58Hz)|=0.780|H(55Hz)|=0.953|H(10Hz)|=0.99994 exactly 0 at 60 Hz.
  3. Notch width. Δf≈0.02×500π=3.2Hz
  4. Choosing r. A larger r narrows it but makes its transient last longer.

Answer g = 0.9807; the gain is exactly 0 at 60 Hz, 0.780 at 58 Hz, 0.953 at 55 Hz and 0.99994 at 10 Hz, and the notch is about 3.2 Hz wide.

Check

Step 1. Write highpass: remove baseline wander with a first-order IIR filter.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 2. Write notch: remove power-line interference with a second-order IIR notch.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 3. Write detect_beats and heart_rate: find every QRS complex.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does the detector square the derivative and then average it, instead of thresholding the ECG itself?

Practice

Keep practicing

3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.