alp@vilnius: ~ up since 2019 · Vilnius Download cv.pdf

← ALP-OS

alp ~ $ work heart-rate

Heart rate that fell apart when you walked

A health smartwatch lost the heart rate as soon as you started walking. I built a rig to compare it against a chest strap, then rebuilt the algorithm. Error while walking dropped from 8.2 to 5.5 bpm, and RAM from 6.9 to 2.7 kB.

8.2  to 5.5 bpmheart-rate error while moving

A health smartwatch with ECG, heart rate and SpO2 lost the heart rate as soon as you started walking. Walking is the hard case for a wrist sensor: every step moves it and adds its own rhythm to the signal.

It’s easy to tweak a heart-rate algorithm until it looks good on one walk. To know if it’s really better, you need a reference. So I built two things: a way to measure it, and a better algorithm.

The first bug was a unit

The motion detector compared a threshold of 0.10 against acceleration in the wrong unit. Every pulse was flagged as motion and thrown away.

A way to measure it

I built the whole loop. A BLE service on the watch streams per-beat PPG and heart rate, plus raw PPG at 100 Hz and accelerometer data at 50 Hz. A Kotlin Android app records the watch and a reference chest strap side by side, with activity markers. Python scripts score each session and replay the filters, so I can try a change on recorded data before it goes on the watch.

An algorithm that expects motion

The new estimator runs a DFT on a 25 Hz grid. The accelerometer gives the step rate, and notches at 0.5×, 1× and 2× that rate remove the step and arm-swing peaks. A beat-interval prior and a bounded tracker stop the estimate from jumping around, and clip, jump and artifact-hold gates keep it steady.

The old motion gate, the new estimator, and the loop that proved it Old path: the motion gate compared a threshold of 0.10 against acceleration in the wrong unit, so every pulse was flagged as motion and heart rate was lost while walking. New estimator, one estimate every 2 seconds: the accelerometer at 50 Hz gives the step rate. Raw PPG at 100 Hz goes through a DFT on a 25 Hz grid, then notches at 0.5, 1 and 2 times the step rate remove step and arm-swing peaks. The strongest remaining peak goes to a bounded tracker with a beat-interval prior that cannot jump from 90 to 140 bpm in 2 seconds, then to clip, jump and artifact-hold gates, then out as heart rate. How it was proven: a BLE service on the watch streams per-beat PPG and heart rate, raw PPG at 100 Hz and accelerometer at 50 Hz. A Kotlin Android app records the watch and a reference chest strap side by side with activity markers. Python scores every session, replays the gates and writes golden vectors. The C++ port with an optimized FFT library agrees with the Python twin to a median of 0.03 bpm. It ran in shadow mode next to the old path, then a selector with beat-gap fill switched it live, with other measurement paths unchanged. The loop then starts again with new recordings. OLD PATH · why walking lost heart rate acceleration in the wrong unit motion gate threshold 0.10 every pulse flagged as motion heart rate lost while walking NEW ESTIMATOR · one estimate every 2 s accel 50 Hz step rate raw PPG 100 Hz DFT on a 25 Hz grid step-rate notches at 0.5×, 1×, 2× strongest peak that is left bounded tracker beat-interval prior no 90 → 140 bpm in 2 s gates clip, jump artifact hold heart rate moving error 5.5 bpm The notches cut step rate and arm swing from the spectrum. The strongest peak left is the heart. HOW I PROVED IT · record, score, port, shadow, switch reference chest strap watch · BLE service per-beat PPG and HR raw PPG 100 Hz accel 50 Hz Android app · Kotlin watch + strap side by side activity markers Python scores every session replays the gates writes golden vectors C++ port · FFT library agrees with the Python twin to a median of 0.03 bpm shadow mode runs next to the old path old path still live switched live selector, beat-gap fill other paths unchanged next recordings
The old motion gate, the new estimator, and the loop that proved it

Fitting it on the watch

The MCU was already at 98% RAM. I ported the estimator to C++ with an optimized FFT library and checked it against golden vectors from Python. Before switching it on, I ran it in shadow mode next to the old one. Along the way I also fixed a stack overflow that only happened while the watch was being worn.

Conclusion

Mean absolute error while walking, against the chest strap, went from 8.2 to 5.5 bpm.

Heart-rate error while moving

The estimator’s RAM went from 6.9 kB to 2.7 kB.

Estimator RAM

The algorithm gets the attention, but the chest strap, the recorder app and the scoring scripts are what make 5.5 bpm a measurement and not a guess.

Tags dsp ble wearable