apopt.comCBE 493R Data-Driven Athletics

Course / Tutorials / Week 5 / CS-6

Case study CS-6 · Week 5 · about 60 min · Core

From a chest-strap recording to a recovery number

Check a chest strap's beat intervals against its own ECG, clean two nights of sleep with a written artifact rule, compute a nightly RMSSD with an honest interval, and find out what two nights can and cannot say about a baseline band.

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

Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser. Helpful first: Q-13 Find the beats in ten seconds of ECG, Q-14 Heart rate from beat intervals, Q-15 One night's RMSSD.

The decision

The instructor wants a morning answer to one question: was last night's recovery normal for me, or below my usual range? If it is below, he would make the day's session easy; if it is normal, he would train as planned. Answering needs two things: a trustworthy number for one night, and a band of usual values to compare it with. The case file has two nights.

Predict first

Before any code: how many nights (or mornings) of heart rate variability do you think you need before you can say "tonight is below your usual range"?

The raw data

Data dictionary
FieldMeaningUnitRangeMissing
t_stime of the beat from the start of the recordings0–21 820none
rr_msinterval since the previous beat as timed by the strapms254–11 568 (raw)none
hr_bpmthe strap's displayed heart rate where it was written on a beat rowbeats/min0–128empty; 0 when contact was lost
time (excerpt)sample timens since 1970-01-01 UTC—none
ecg (excerpt)single-lead ECGmV−1.29 to 2.29none
rr (excerpt)strap interval on the rows where one was writtenms1129–1544empty on other rows

What is wrong with it: the strap writes some intervals that no resting heart produces (110 on the first night, from 254 ms to 11.6 s), and on the first night it counted noise as beats for about six minutes near hour 4.1. Its interval rows are written to the file about three seconds after the beat they describe. The hr_bpm column is filled on only some rows and is 0 while the strap had no skin contact. Two nights are all there is: the case file has no morning readings.

Step by step

Step 1 · Gather

The instructor slept in a Polar H10 chest strap on two nights in June 2026, 26 and 28 June. The strap wrote two things: its raw ECG, about 130 samples a second, and the interval between beats it timed itself (the RR interval). The whole-night ECG files are large, so this study uses the strap's beat files for the nights, one row per beat, and a three-minute ECG excerpt from the middle of the second night to check those beats against the raw signal. Everything here is training information about one healthy adult, not medical advice.

PythonStep 1 · Open the two nights

Type a prediction to enable Run.

Show the expected result without running
2026-06-26: 18027 beats over 6.06 h; columns ['t_s', 'rr_ms', 'hr_bpm']
2026-06-28: 17082 beats over 6.01 h; columns ['t_s', 'rr_ms', 'hr_bpm']

Before trusting the strap's intervals for a whole night, check them against the ECG it recorded at the same time. Find the R peaks in the three-minute excerpt (the method of Q-13), then line up your intervals with the ones the strap wrote.

PythonStep 1 · Check the strap's intervals against its own ECG

Type a prediction to enable Run.

Show the expected result without running
133 intervals from the ECG, 136 from the strap; best shift 5 rows
strap rows written 3.1 s after the beat
difference: median 2.0 ms, largest 7.1 ms

Lined up, the two agree to within one ECG sample (7.7 ms) on every beat. The strap's intervals are written to the file a few seconds late, which does not matter for HRV but would matter if you joined them to another clock. From here on the study uses the strap's own intervals: they are the same beats, given to the millisecond.

Step 2 · Statistics

A profile of each night first: how many beats, how they are spread, and how many look wrong before any rule is applied.

PythonStep 2 · Profile each night

Type a prediction to enable Run.

Show the expected result without running
     night  beats  hours  median_rr_ms  min_rr_ms  max_rr_ms  outside_300_2000  jumps_over_20pct  mean_hr_bpm
2026-06-26  18027    6.1        1226.0      254.0    11568.0               110               759         49.6
2026-06-28  17082    6.0        1279.0      293.0     2383.0                 5               202         47.4

The first night has nearly four times as many suspect jumps and 110 intervals outside the range a resting heart produces, from 254 ms to over 11 seconds. A mean heart rate of 47 to 50 beats a minute is what you would expect in sleep. The extremes need a picture before a rule.

Step 3 · Visualize

PythonStep 3 · The first night, raw

Show the expected result without runningTwo panels. Top, every RR interval of the first night against hours, mostly 1000 to 1400 ms, with scattered points far above and below. Bottom, twenty minutes around hour 4.1, where the intervals scatter widely and the suspect ones are marked with crosses.

Most of the night is a tidy band between about 1000 and 1400 ms. Near hour 4.1 the intervals collapse to 300 to 600 ms for about six minutes, a heart rate of 100 to 200 a minute, which a sleeping heart does not reach. The strap was most likely counting noise while the sleeper moved and the electrodes lost contact. Some of those noise beats happen to agree with the one before them, so a beat-by-beat rule alone will not catch them all. Those minutes must not feed an HRV number, but the rules must not throw away the good beats around them either.

Step 4 · Cleanse

The rules, written before looking at any HRV number:

  1. An interval outside 300 to 2000 ms is removed.
  2. An interval more than 20% away from the previous recorded interval is removed.
  3. A successive difference is used only when both of its beats were kept.
  4. A five-minute window counts only if it keeps at least 120 beats and at least 80% of its beats, the Heart app's test for a window valid for HRV.

Rule 2 has a trap. Compare with the previous kept interval instead, and one bad beat can become the reference for everything after it.

PythonStep 4 · The previous recorded beat, or the previous kept beat?

Type a prediction to enable Run.

Show the expected result without running
2026-06-26: previous recorded removes   786 (longest run   20); previous kept removes  6242 (longest run 4281)
2026-06-28: previous recorded removes   203 (longest run   10); previous kept removes  4215 (longest run 3960)

The "previous kept" version removes thousands of beats on each night, including a run of more than 4000 good beats on the first: once an odd interval sets the reference, every normal beat after it looks 20% away and is thrown out, for hours. Comparing with the previous recorded beat lets the series recover two beats after an artifact. Now apply rules 1 to 4, write the clean copies and a change log.

PythonStep 4 · Apply the rules, windows and a change log

Show the expected result without running
night               2026-06-26  2026-06-28
rule                                      
1-2 beats removed          786         203
3 differences used       16933       16701
4 windows dropped            3           1

Three windows of the first night fail rule 4, two of them the noisy minutes near hour 4.1, where rule 2 alone had let some noise beats through. One window of the second night fails because it is only the last few minutes. The raw files are untouched; the clean copies mark each beat as kept or not, so anyone can check the rules.

Step 5 · Features: a number per night, and how sure it is

The nightly number is the median RMSSD of the valid windows, and its natural logarithm, ln RMSSD, the form used for day-to-day comparisons (Section 5.5). How precise is one night's median? Resample the night with a bootstrap. Neighbouring windows are alike (sleep comes in cycles), so resample blocks of six windows, half an hour at a time, not single windows.

PythonStep 5 · Nightly RMSSD with a bootstrap interval

Type a prediction to enable Run.

Show the expected result without running
     night  windows  rmssd_ms  ln_rmssd  ci_low_ms  ci_high_ms  lowest_5min_hr
2026-06-26       70     51.78      3.95      41.06       61.90           41.62
2026-06-28       72     58.68      4.07      52.60       63.73           43.34
case file's summary: {'2026-06-26': 50.3, '2026-06-28': 58.7}

The second night matches the case file. The first is 1.5 ms higher, and only rule 4 differs: the case file keeps the three windows of the first night in which only 36 to 70% of the beats survived, and their RMSSD of 41 to 45 ms pulls the median down. A window rule moves the number too. An earlier version of the case file's script also broke rule 3: it took differences between successive kept beats, so a difference could reach across a removed beat, and it reported 58.5 and 61.6 ms, 6.7 and 2.9 ms too high. The same word, RMSSD, and the same rule for beats, gave different numbers: always say how the differences and windows were taken.

Each night's interval is wide, about ±10 ms on the first night: sleep varies within a night, and a nightly median carries that. The two nights differ by 6.9 ms, less than either interval.

Step 5 · A baseline band from two nights?

The decision needs a band: the usual range of this athlete's ln RMSSD, so that a new night can be called "normal for you" or "below your band". Section 5.5 builds it from seven mornings: the mean, and the day-to-day standard deviation as the noise. Here there are two nights. What would a band from two nights be worth?

PythonStep 5 · How well do n nights pin down the day-to-day spread?

Show the expected result without running
two nights: ln RMSSD 3.95 and 4.07; mean 4.01, SD 0.088
not a day-to-day spread: SD of ln RMSSD over all 142 windows = 0.29
 nights  true SD from    to
      2          0.45 31.91
      3          0.52  6.28
      5          0.60  2.87
      7          0.64  2.20
     14          0.72  1.61
     28          0.79  1.36
Left, the two nightly RMSSD values with their bootstrap intervals, overlapping. Right, the 95% range of the true day-to-day standard deviation, as a multiple of the one measured, against the number of nights, very wide at two nights and narrowing past seven.

From two nights the measured spread, 0.088 in ln units, could come from a true day-to-day spread anywhere from about half of it to 32 times it. A band built on that says nothing. With seven nights the range is 0.64 to 2.2 times, still wide but usable; with four weeks, 0.79 to 1.36. That is why Section 5.5 asks for seven mornings at least, and why a review of heart-rate monitoring found that HRV followed running performance only when 3 to 4 days or more were averaged, not from single days (Buchheit, 2014).

So the honest answer to "is last night normal for me?" is: the case file cannot say yet. Two nights give two numbers, 51.8 and 58.7 ms, each uncertain by several milliseconds, and no band. The action is to collect the band, the same way every time.

How good is the number?

The beats. Against the ECG recorded at the same time, the strap's intervals agree within 7.1 ms on every beat of three minutes (median 2.0 ms), so the beat timing is not the weak point.

One night. A block bootstrap over half-hour blocks of five-minute windows gives 95% intervals of 41.1 to 61.9 ms for the first night and 52.6 to 63.7 ms for the second. The two nights differ by 6.9 ms, inside both intervals.

The method. The case file's own summary gives 50.3 and 58.7 ms. The second night matches; the first differs by 1.5 ms because the case file keeps three windows that rule 4 drops. An earlier version of its script took differences across removed beats and gave 58.5 and 61.6 ms, a gap larger than the difference between the nights: state how differences and windows were taken whenever you report the number.

The band. None yet. A standard deviation from two values is almost unconstrained; seven values narrow it to 0.64–2.2 times the measured one.

Action card

Action card
Finding
Last night's heart rate variability (RMSSD) was 58.7 ms; the night before last was 51.8 ms.
Evidence
Median of 72 and 70 clean five-minute windows; 95% intervals 52.6–63.7 and 41.1–61.9 ms.
How sure
Sure of each night's number to several milliseconds; no band yet, so "normal for you" cannot be decided.
Action
Train as planned, and record five minutes on waking for the next seven mornings, same position, same strap, before using HRV to change a session.
Test again
Build the band after seven mornings; then compare each new morning with it.

Track it

Add one row per night: date=2026-06-28, test=hrv-night, metric=rmssd_median_ms, value=58.7, unit=ms, device=polar-h10, protocol_version=sleep-5min-windows-v1 (and a row for ln_rmssd). Keep night recordings and morning recordings as two different tests: they are not the same number.

Open your athlete record

With your own data

Wear the strap overnight or record five minutes on waking with the Heart app, export the beat intervals with times, and replace the two night files. Keep the ECG if you can: Step 1's check needs three minutes of it.

Check the AI

Asked "is my HRV normal?", an assistant wrote:

> I computed RMSSD in five-minute windows for both nights: medians 51.8 and 58.7 ms. Pooling > all 142 windows, the standard deviation of ln RMSSD is 0.29, so your baseline band is > ln RMSSD 4.01 ± 0.29, that is 41 to 74 ms. Both nights fall inside it, so your recovery is > normal.

What is wrong with it?

The band is built from the wrong spread. The 0.29 is how much RMSSD varies from one five-minute window to the next within a night (sleep stages, waking), not how much the nightly number varies from day to day. A band needs one number per night or morning, and at least seven of them. The medians themselves are right.

Check yourself

  1. Why compare each beat with the previous recorded beat rather than the previous kept one?

  2. Why does the bootstrap resample half-hour blocks of windows instead of single windows?

  3. The two nights are 51.8 and 58.7 ms. What can you conclude?

  4. Seven mornings give ln RMSSD with a standard deviation of 0.08. From the table in Step 5, what is the upper end of the 95% range for the true SD?

    ln units

0 of 4 answered · 0 right the first time

Challenge: Seven mornings, one band

The decision in this study could not be made because there was no band. Make one. Use your own mornings, or a classmate's with their written agreement, or the instructor's record if he adds mornings during the term.

  1. Record five minutes on waking for at least seven mornings with the Heart app (same position, normal breathing, before coffee); discard the first minute.
  2. Export the beat intervals and apply this study's four rules, with a change log per morning.
  3. Compute ln RMSSD per morning, the baseline mean and day-to-day SD, and the 95% range of the true SD from Step 5's table.
  4. On morning eight, say whether the reading is inside the band, below it by more than one SD, or not decidable, and what you would do about the day's training.
  5. Compare one morning with a night, if you have both, and explain why they are different tests.

Hand in

How it is judged

Hints

    The solution unlocks after you make an attempt. Comparing your own attempt with it is where the learning happens.

    The same method in another field

    A beat-interval series is a sensor stream with dropouts and spikes, like a flow meter that stutters or a vibration sensor that loses contact. The same choice, compare each reading with the previous recorded one, not the previous accepted one, decides whether one bad reading removes a neighbour or a whole shift of data. See the Machine Learning for Engineers page on cleansing data.

    Acknowledgements

    Recordings by the instructor with a Polar H10 strap, published under his name in the course case file. Not medical advice: heart rate variability here is training information for a healthy adult.

    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.