# The Heart as a Signal

Class 9, Chapter 5: Heart Rate, ECG and Heart Rate Variability. 30 slides, about 36 minutes.

## 1. Title

Slide up while people sit down. Today we put a strap on a chest and follow one heartbeat from the skin to a number. By the end you will know why two devices on one runner can disagree by fifteen beats, and which one to believe.

## 2. What does the sensor actually sense?  (1.5 min)

Chapters five, six and seven are about sensors worn on the body. Here are four rows of the book's table of fifteen. Read across: what the physics delivers, what is computed from it, and what corrupts it. The strap measures the timing of the R wave. Heart rate and heart rate variability are computed from those times. The column to remember is the last one. Error depends far more on the activity and on where the sensor sits than on the brand. So the habit for this week: before you judge a heart-rate number, find out what the device actually sensed.

## 3. One heartbeat as the ECG records it  (1.5 min)

Each heartbeat starts as an electrical event in the sinoatrial node, about once a second at rest. Electrodes on the skin record the sum of that activity as a voltage. One beat has three parts. The small P wave is the upper chambers. The tall, sharp QRS complex is the lower chambers, the ones that pump. The broad T wave is them recovering. Why time the R wave and not the others? It is tall and narrow, so you can place it to within one sample, and it survives a run. [Point at the box.] A strap records one lead: enough to time beats, not to judge a heart. We never interpret rhythms here; concerns go to a clinician.

## 4. Ten seconds of a resting chest strap  (2 min)

This is real. Ten seconds from the book's resting example, recorded by a chest strap at about a hundred and thirty samples a second. You can see every beat: the tall R spikes, and between them the smaller P and T waves. The circles are the R peaks our code will find later today. Heart rate is sixty thousand divided by the RR interval in milliseconds. Eight hundred milliseconds is seventy-five beats a minute. The nine intervals here average nine hundred ninety-four point nine milliseconds, sixty point three beats a minute. Notice they are reciprocals, so equal steps in rate are not equal steps in interval.

## 5. Electrical and optical  (2 min)

Two ways to detect a heartbeat without a needle. Electrically: a strap presses two pads against the skin below the chest muscles and times the R wave itself. Against a clinical Holter recorder in ten people, the course strap got ninety-nine point six percent of intervals right, better than the Holter. Optically: a watch shines light into the skin and reads the pulse of blood in small vessels. That is photoplethysmography. It sees the pulse wave, not the electrical beat, a fraction of a second later, and its rounded top is harder to time. Site matters too: in one study an upper-arm band missed by one point four beats on average, the same maker's wrist watch by six point four.

## 6. A flat 170 on a hill  (2 min)

[Ask the room before you click.] Solid line, the strap. Dashed, a wrist sensor. Dotted, the runner's cadence, a hundred and seventy steps a minute. For three minutes the wrist reads exactly one seventy, flat. What is it measuring? [Take answers, then click.] The steps. Each arm swings eighty-five times a minute and every footstrike jolts the body a hundred and seventy times. When heart rate is near the same rate, the software can lock onto the motion and report it as heart rate. The clue is a flat line at exactly the cadence. A strap cannot do this, because a footstrike is not an electrical event.

## 7. Which device to believe  (1.5 min)

So here is the answer to today's question, in words you can say to an athlete. Two devices fifteen beats apart in the middle of an interval. The strap times electrical events and is rarely fooled by motion. The wrist reads a pulse through moving skin. Believe the strap, unless it is dry or slipping, and then check one thing: is the wrist number flat at the runner's cadence? That is how you would know. Notice the order: a disagreement between devices is a sensor question before it is a question about the body.

## 8. Sampling sets the timing  (1.5 min)

An ECG stream is a list of voltages at a fixed rate. At a hundred and thirty samples a second, the samples are seven point seven milliseconds apart, and an R peak can only be placed on a sample. So every interval is a whole number of samples, and every difference between intervals is a multiple of seven point seven. At sixty beats a minute, one sample late is under one percent of the interval. At a hundred and fifty, about two percent. That sounds like exercise is the hard case. But the differences that heart rate variability is built from at rest are only tens of milliseconds, so that is where timing hurts.

## 9. Finding R peaks: the Pan and Tompkins recipe  (2.5 min)

Most beat detectors follow a design published in nineteen eighty-five by Pan and Tompkins, and the Heart app does too. Follow the right side from top to bottom. The raw ECG. Then conditioned, keeping the frequencies where the QRS lives. Then differentiated and squared, so only the steep slopes of each QRS survive. Then averaged over a hundred and fifty milliseconds, so each beat is one hump that crosses the dashed threshold once. After a beat, ignore anything for two hundred milliseconds, because the heart cannot beat again that soon. Finally move the time to the tallest ECG sample nearby, the R peak itself.

## 10. Listing 5.2: R peaks in twelve lines  (2 min)

Here is the recipe as code. Each line is one step of the last slide, and the comment says which. Band-pass from five to fifteen hertz, differentiate, square, average over a hundred and fifty milliseconds, then find peaks above a threshold with a minimum distance, which is the refractory period. Then refine each beat to the tallest ECG value nearby. On the resting example it finds four hundred seventy-nine beats, and a mean heart rate of fifty-eight. The strap's own firmware reported four hundred seventy-nine intervals too. Before you run it, predict: take out the distance, what happens? [Answer: the count goes up; T waves and noise humps become beats.]

## 11. Two kinds of wrong interval  (1.5 min)

Two things put wrong intervals into a series. An artifact is a measurement error: a missed beat makes one interval twice as long, and a noise spike counted as a beat splits one interval into two short ones. An ectopic beat is real. It starts somewhere other than the usual pacemaker, arrives early, and is followed by a longer pause. These are common in healthy people. Neither belongs in heart rate variability, which is meant to describe the normal rhythm. So every program compares each interval with its neighbors and drops the ones that do not fit. The app and our listing use slightly different rules. Remember that; it matters in two slides.

## 12. Two brakes and an accelerator  (1.5 min)

Even at rest the interval is never constant. Look at the plot: it moves by tens of milliseconds from beat to beat. That is heart rate variability. Why? The pacemaker on its own fires faster than a resting heart beats. Two branches of the nervous system adjust it. The vagal brake slows it within a beat or two, and at rest it dominates. The sympathetic side speeds it up, more slowly. And the vagal brake rises and falls with breathing, so the heart speeds up a little as you breathe in. The fast wiggles you see are mostly breathing.

## 13. RMSSD, SDNN and pNN50 by hand  (3 min)

[Everyone, paper out. Three minutes.] Three measures. RMSSD looks only at the change from one beat to the next, so it measures the fast, vagal part. SDNN is the plain standard deviation, so it sees fast and slow variation together. pNN50 is the share of successive differences larger than fifty milliseconds. Work all three for these five intervals. [Wait, then click.] The differences are minus twenty-two, forty-five, minus thirty-four and twenty-one. RMSSD thirty-two milliseconds. SDNN fifteen point seven if you divide by N, as the app does, seventeen point six by N minus one. No difference over fifty, so zero percent.

## 14. One recording, four computations  (2 min)

The same eight-minute resting recording, computed four ways: the Heart app, our listing, a maintained library called neurokit two, and the intervals the strap's own firmware reported. RMSSD agrees within about three percent. SDNN within a fraction of a millisecond. pNN50 runs from two point one to three point four percent. Why so fragile? It counts differences that cross a hard line at fifty, and only ten to sixteen of four hundred seventy-seven differences are that big. Move one R peak by one sample and a difference can cross the line. When programs disagree, check these four causes, in this order.

## 15. Why RMSSD for athletes?  (2 min)

[Thirty seconds, then hands up for A, B, C, D.] The answer is C. RMSSD is built from successive differences, the fast part of the variation that the vagal brake controls, and a few minutes are enough to estimate it. A is backwards: one missed beat can inflate RMSSD enormously, as you will see in a moment. B: the strap measures R-wave times; RMSSD is computed. D: just look at the table we had, twenty-two against fifty-one. For day-to-day comparison we take its natural log, ln RMSSD, because it is skewed toward high values.

## 16. The instructor's strap: four sessions, one person  (2.5 min)

Now real data from this room: the instructor's own strap, four sessions in June. The same person gives an RMSSD of twenty-two seated in the evening and fifty to sixty asleep. That is not a change in fitness. It is a change in conditions: posture, time of day, breathing, sleep. The two nights differ by about eight milliseconds. Is that a change? We cannot say yet. Two nights is not a baseline. That is why the book's morning protocol fixes the conditions and asks for seven mornings, so you know your own day-to-day noise before you read anything into one number. You will use these files for homework eleven.

## 17. Check the AI: an RMSSD of 311 ms  (2 min)

An AI assistant was given these twelve intervals and wrote the sentence in the box. Find the flaw before I click. [Pause.] Look at three thirty followed by nine twenty. Short, then long, and together about two normal intervals: an ectopic beat and its pause. There are two of these pairs, and their huge differences dominate the sum. Take them out and RMSSD is about four milliseconds, which is low, exactly what you expect a minute after hard exercise. The assistant did the arithmetic correctly on dirty data and then explained a number that should never have existed. Check the beats before the statistics.

## 18. A workout with five efforts  (1.5 min)

Heart rate is the body's fastest-moving measure of effort, but it is not instant. Here is the book's workout example from a strap: a warm-up jog, five hard efforts of one to two minutes with jogging between, and a cool-down. Watch each effort. When work starts, heart rate climbs within seconds, first as the vagal brake lets go, then more slowly. In moderate steady work it levels off. In long steady work it drifts upward as the body heats and loses fluid. When work stops it falls quickly for a minute, then slowly. The drop in the first sixty seconds is a common field measure.

## 19. A first-order model  (2.5 min)

Engineers describe this with a first-order model. Heart rate heads for the level the work demands, fast at first and then more slowly, and tau says how quickly. After one tau it has covered sixty-three percent of the change; after three, ninety-five. Fit to effort three: the rise goes from a hundred forty-two toward a hundred eighty-two with a time constant of twenty-eight seconds, plus or minus one. The fall is slower and much less certain, seventy-nine plus or minus twelve, because the runner kept jogging. The fit you can defend is often shorter than the one you can compute. You will fit one yourself in the homework.

## 20. Before Class 10  (1 min)

Homework ten is due at the start of next class. The core exercises on beats and intervals, including the RR-to-heart-rate conversions and the cadence-lock question. Then your first recording: five minutes of rest and a short effort with the Heart app. Open a clean ten-second window, read off the R peaks, and compute the heart rate by hand. Compare with the app. Start your seven mornings too; they become your baseline. Bring a strap and a laptop with a browser that has Bluetooth. And read sections five seven to five ten: zones, load, and how good the number is.
