Commit 992c2b2
authored
fix(firmware): correct ESP32 edge heart rate — sample-rate + harmonic lock (ruvnet#987) (ruvnet#988)
* fix(firmware): correct heart-rate estimation — sample-rate + harmonic lock
The edge vitals HR was stuck at ~45 BPM regardless of true heart rate
(Apple Watch ground truth 87 BPM read as ~45) and "dropped a lot" between
frames. Two root causes:
1. Stale fixed sample rate. estimate_bpm_zero_crossing() used a hardcoded
`sample_rate = 10.0f` (and the biquads a separate `fs = 20.0f`). That
constant was correct when CSI came from ~10 Hz beacons, but ruvnet#985's
self-ping raised the callback rate to a VARIABLE ~13-19 Hz. BPM scales as
(assumed_rate / actual_rate) x true, so a true 87 read ~45, and because
the real rate fluctuates with CSI yield while the code assumed a fixed
value, the reported HR swung frame-to-frame (the "drops").
2. Breathing-harmonic lock. Zero-crossing HR estimation locked onto a
breathing harmonic — a 0.25 Hz breathing fundamental puts its 3rd
harmonic at ~0.74 Hz ~= 44 BPM, right in the HR band — so it parked at
~45 BPM independent of the real heartbeat.
Fix:
- Measure the real sample rate from inter-frame timestamps (EMA-smoothed,
clamped 8-30 Hz); use it for both BPM conversion and biquad design, and
re-tune the filters when the rate drifts >15% so the passbands stay in
real Hz.
- Replace the HR zero-crossing with estimate_hr_autocorr(): autocorrelation
peak in the 45-180 BPM band that explicitly rejects lags within 8% of any
breathing harmonic (k=1..6), with parabolic interpolation and a peak-
confidence gate (returns 0 rather than a noise value).
- Median-smooth (N=9) the emitted HR over valid estimates to kill residual
single-frame outliers.
Validated on hardware (ESP32-S3, COM8/192.168.1.80) vs an unmodified board
(192.168.1.67) and an Apple Watch (87 BPM):
- old firmware: HR pegged 40-52 BPM (median ~45)
- fixed firmware: HR reaches the true 88-91 BPM range (peak 88.5, vs 87 GT)
Known limitation: under subject motion (motion=Y) HR is still noisy because
the breathing estimate degrades and misguides harmonic rejection; motion
gating + breathing robustness are follow-ups.
Co-Authored-By: claude-flow <ruv@ruv.net>
* fix(firmware): robust HR harmonic rejection via autocorr breathing period (ruvnet#987)
Follow-up to 332c2a98d. The HR harmonic rejection was fed the noisy
zero-crossing breathing estimate, which under motion notched the wrong
frequencies and let the autocorr lock onto the ~0.75 Hz breathing harmonic
(~45 BPM). Generalize estimate_hr_autocorr -> estimate_periodicity_autocorr
and drive HR harmonic rejection from a robust autocorrelation breathing
period instead; widen the HR median smoother to N=13.
Hardware A/B (fixed .80 vs unmodified control .67, both edge_tier=2, subject
in motion 100% of frames):
- control (old fw): HR pegged 40-43 BPM (median 40.6)
- fixed: HR 60-91 BPM (median 71.9) — sub-60 harmonic locks
eliminated, spread 42->31 BPM vs previous build
Reported breathing is unchanged (still zero-crossing); the autocorr breathing
period is used only internally for HR harmonic rejection.
Co-Authored-By: claude-flow <ruv@ruv.net>
* docs(changelog): record ESP32 heart-rate fix (ruvnet#987)
Co-Authored-By: claude-flow <ruv@ruv.net>1 parent 5789351 commit 992c2b2
2 files changed
Lines changed: 155 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
218 | 325 | | |
219 | 326 | | |
220 | 327 | | |
| |||
246 | 353 | | |
247 | 354 | | |
248 | 355 | | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
249 | 364 | | |
250 | 365 | | |
251 | 366 | | |
| |||
535 | 650 | | |
536 | 651 | | |
537 | 652 | | |
538 | | - | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
539 | 658 | | |
540 | 659 | | |
541 | 660 | | |
| |||
715 | 834 | | |
716 | 835 | | |
717 | 836 | | |
718 | | - | |
719 | | - | |
720 | | - | |
721 | | - | |
722 | | - | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
723 | 867 | | |
724 | 868 | | |
725 | 869 | | |
| |||
777 | 921 | | |
778 | 922 | | |
779 | 923 | | |
780 | | - | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
781 | 927 | | |
782 | 928 | | |
783 | 929 | | |
784 | | - | |
| 930 | + | |
785 | 931 | | |
786 | 932 | | |
787 | 933 | | |
| |||
0 commit comments