apopt.comCBE 493R Data-Driven Athletics

Course / Tutorials / Week 5 / Q-13

Quick tutorial Q-13 · Week 5 · about 15 min · Foundation

Find the beats in ten seconds of ECG

Read the clock of a raw chest-strap ECG file, find every R peak in ten seconds with a band-pass, slope and threshold, and check the count against the strap.

  1. 1Gather (used)
  2. 2Statistics
  3. 3Visualize (used)
  4. 4Cleanse
  5. 5Features (used)
  6. 6Balance
  7. 7Scale
  8. 8Split
  9. 9Deploy
  1. Ask
  2. Collect (this week)
  3. Check (this week)
  4. Interpret
  5. Coach
  6. Reassess

Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser.

The decision

Before any heart-rate or HRV number from a strap is used, check that the strap is finding the beats. Ten seconds of raw ECG is enough to see it with your own eyes and count.

The raw data

Try it

A chest strap that streams its raw ECG lets you find the heartbeats yourself. The file is three minutes of the instructor at rest, recorded with a Polar H10 strap: one row per ECG sample, with the strap's own beat-to-beat interval (rr, in milliseconds) on a few rows. You will look at ten seconds of it, find every R peak (the tall spike of each beat), and check your count against the strap.

Open the file and read its clock

PythonOpen the ECG and measure the sample rate

Type a prediction to enable Run.

Show the expected result without running
23379 samples in 180.0 s: 129.88 samples/s, one sample = 7.70 ms
ten-second window: 1298 samples, ECG from -1.20 to 1.77 mV

About 130 samples a second, so an R peak can only be placed to within one sample, 7.7 ms. The code keeps one spare second on each side of the window. A filter has nothing to work with beyond the last sample, so near an edge it goes wrong; without the spare second, a beat that peaked just before 60 s is "found" on the first sample of the window.

Find the R peaks

The recipe is the one in Section 5.4 of the book, after Pan and Tompkins: keep the frequencies where the QRS complex lives (5 to 15 Hz), take the slope, square it so every beat is a positive hump, average over 150 ms, and accept a hump as a beat only if it is tall enough and at least 200 ms after the last one. Then move each beat to the tallest ECG sample nearby.

PythonBand-pass, slope, square, average, peaks

Show the expected result without running
10 beats, 9 intervals: [970, 985, 986, 1009, 1009, 1009, 1016, 978, 962] ms
mean interval 991.5 ms -> 60.5 beats/min

Ten beats and nine intervals between them, 60.5 beats a minute in this stretch. Every interval is a whole number of samples, so the values come in steps of 7.7 ms (970, 978, 985: one sample apart).

Look, and check against the strap

PythonThe beats on the trace, and the strap's own intervals

Show the expected result without running
strap: 10 intervals reported in the same ten seconds, mean 997.8 ms
difference in mean interval: -6.3 ms
Ten seconds of resting ECG in millivolts against time, with a circle on each of the ten detected R peaks.

The strap reports each interval in a packet a little after the beat, so its rows in a ten-second window are not exactly the same beats as yours: it reported 10 intervals here, you measured 9, and the means differ by 6.3 ms, less than one sample and under half a beat a minute. Your detector and the strap agree on the rhythm.

For the athlete: "Your strap is reading your heartbeat cleanly: we found each beat in the raw signal ourselves, and the rhythm matches what the strap reported."

Action card

Action card
Finding
The strap finds the beats; 10 R peaks in ten seconds at rest, 60.5 beats a minute.
Evidence
The detected mean interval (991.5 ms) and the strap's own (997.8 ms) differ by less than one sample.
How sure
High for this clean, resting stretch; a dry or slipping strap needs the same check.
Action
Wet the strap's electrodes before each session and look at ten seconds of ECG once.
Test again
Repeat the check whenever a heart-rate number looks wrong.

With your own data

Record two minutes at rest with the Heart app or a logging app that writes the same five columns, open your file instead, and pick a ten-second window where you sat still.

Check yourself

  1. Why does the detector refuse a second peak within 200 ms of the last one?

  2. At 130 samples a second, how long is one sample, in milliseconds?

    ms

0 of 2 answered · 0 right the first time

References

Built 2026-11-30 with Python packages numpy 2.5.5, pandas 3.1.1, matplotlib 3.11.2, scipy 1.18.1. In the browser the same code runs on Pyodide, so the last digit can differ.