core/health/hrv_run.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file hrv_run.c | ||
| 3 | * @brief Turns the beat gaps the watch reports into heart rate variability, as RMSSD in ms. | ||
| 4 | * | ||
| 5 | * @ingroup lib_core | ||
| 6 | */ | ||
| 7 | #include "health/hrv_run.h" | ||
| 8 | |||
| 9 | #include <string.h> | ||
| 10 | |||
| 11 | /** | ||
| 12 | * @brief The square root of a whole number, rounded down. | ||
| 13 | * | ||
| 14 | * The watch has no hardware floating point, so this walks the bits instead of calling sqrt. | ||
| 15 | * | ||
| 16 | * @param value The number to take the root of. | ||
| 17 | * @return The largest whole number whose square is no more than value. | ||
| 18 | */ | ||
| 19 | 12 | static uint32_t root_floor(uint32_t value) | |
| 20 | { | ||
| 21 | 12 | uint32_t root = 0; | |
| 22 | 12 | uint32_t bit = 1u << 30; | |
| 23 | |||
| 24 |
2/2✓ Branch 4 → 3 taken 145 times.
✓ Branch 4 → 5 taken 12 times.
|
157 | while (bit > value) |
| 25 | { | ||
| 26 | 145 | bit >>= 2; | |
| 27 | } | ||
| 28 | |||
| 29 | 12 | uint32_t rest = value; | |
| 30 |
2/2✓ Branch 10 → 6 taken 47 times.
✓ Branch 10 → 11 taken 12 times.
|
59 | while (bit != 0) |
| 31 | { | ||
| 32 |
2/2✓ Branch 6 → 7 taken 24 times.
✓ Branch 6 → 8 taken 23 times.
|
47 | if (rest >= root + bit) |
| 33 | { | ||
| 34 | 24 | rest -= root + bit; | |
| 35 | 24 | root = (root >> 1) + bit; | |
| 36 | } | ||
| 37 | else | ||
| 38 | { | ||
| 39 | 23 | root >>= 1; | |
| 40 | } | ||
| 41 | 47 | bit >>= 2; | |
| 42 | } | ||
| 43 | |||
| 44 | 12 | return root; | |
| 45 | } | ||
| 46 | |||
| 47 | 29 | void hrv_run_reset(HrvRun *run) | |
| 48 | { | ||
| 49 | 29 | memset(run, 0, sizeof(*run)); | |
| 50 | 29 | } | |
| 51 | |||
| 52 | 13 | int hrv_run_finish(HrvRun *run) | |
| 53 | { | ||
| 54 | 13 | int result = -1; | |
| 55 | |||
| 56 |
2/2✓ Branch 2 → 3 taken 12 times.
✓ Branch 2 → 7 taken 1 time.
|
13 | if (run->diffs >= HRV_DIFFS_MIN) |
| 57 | { | ||
| 58 | // the root of the mean rounded down is the root of the whole mean rounded down, so only | ||
| 59 | // the round up needs the fraction. it goes up when the mean reaches root and a half | ||
| 60 | // squared, which times 4 and the count stays in whole numbers. rounding the mean first | ||
| 61 | // instead would round twice and read 1 ms low on a mean just past the halfway mark. | ||
| 62 | // the sums stay well inside 32 bits, since a kept difference is at most 400 ms | ||
| 63 | 12 | uint32_t root = root_floor(run->sum_sq / run->diffs); | |
| 64 | 12 | uint32_t halfway = (2 * root + 1) * (2 * root + 1); | |
| 65 |
2/2✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 10 times.
|
12 | if (halfway * run->diffs <= 4 * run->sum_sq) |
| 66 | { | ||
| 67 | 2 | root++; | |
| 68 | } | ||
| 69 | 12 | result = (int)root; | |
| 70 | } | ||
| 71 | |||
| 72 | 13 | uint16_t diffs = run->diffs; | |
| 73 | 13 | hrv_run_reset(run); | |
| 74 | 13 | run->finished_diffs = diffs; | |
| 75 | 13 | return result; | |
| 76 | } | ||
| 77 | |||
| 78 | 402 | bool hrv_run_quiet(const HrvRun *run, uint32_t now_sec) | |
| 79 | { | ||
| 80 | // a clock set back since the last gap reads as no time passed, rather than wrapping round to | ||
| 81 | // a pause of over a century | ||
| 82 |
6/6✓ Branch 2 → 3 taken 386 times.
✓ Branch 2 → 6 taken 16 times.
✓ Branch 3 → 4 taken 385 times.
✓ Branch 3 → 6 taken 1 time.
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 383 times.
|
402 | return run->last_at != 0 && now_sec >= run->last_at && now_sec - run->last_at >= HRV_QUIET_SEC; |
| 83 | } | ||
| 84 | |||
| 85 | 398 | int hrv_run_push(HrvRun *run, uint16_t ppi_ms, uint32_t now_sec) | |
| 86 | { | ||
| 87 | 398 | int finished = -1; | |
| 88 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 397 times.
|
398 | if (hrv_run_quiet(run, now_sec)) |
| 89 | { | ||
| 90 | 1 | finished = hrv_run_finish(run); | |
| 91 | } | ||
| 92 | |||
| 93 | 398 | run->last_at = now_sec; | |
| 94 | |||
| 95 | // a 0 is the watch saying it has no gap for this beat, which is dropped like any other bad one. | ||
| 96 | // taking the watch off shows up as the gaps stopping, which the quiet check ends the run on | ||
| 97 |
4/4✓ Branch 5 → 6 taken 396 times.
✓ Branch 5 → 7 taken 2 times.
✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 8 taken 394 times.
|
398 | if (ppi_ms < HRV_PPI_MIN_MS || ppi_ms > HRV_PPI_MAX_MS) |
| 98 | { | ||
| 99 | // a difference is only worth anything between neighbouring beats, and this gap stood | ||
| 100 | // between the last good one and the next, so the chain starts over after it | ||
| 101 | 4 | run->last_ms = 0; | |
| 102 | 4 | return finished; | |
| 103 | } | ||
| 104 | |||
| 105 |
2/2✓ Branch 8 → 9 taken 20 times.
✓ Branch 8 → 10 taken 374 times.
|
394 | if (run->last_ms == 0) |
| 106 | { | ||
| 107 | 20 | run->last_ms = ppi_ms; | |
| 108 | 20 | return finished; | |
| 109 | } | ||
| 110 | |||
| 111 |
2/2✓ Branch 10 → 11 taken 188 times.
✓ Branch 10 → 12 taken 186 times.
|
374 | uint32_t diff = ppi_ms > run->last_ms ? ppi_ms - run->last_ms : run->last_ms - ppi_ms; |
| 112 | |||
| 113 |
2/2✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 15 taken 373 times.
|
374 | if (diff * 100 > (uint32_t)run->last_ms * HRV_JUMP_PCT) |
| 114 | { | ||
| 115 | // a missed beat reads as one gap twice as long, and an extra one as two short ones. it is | ||
| 116 | // dropped and the chain starts over after it, which also lets a heart rate that really | ||
| 117 | // moved carry on from its new pace, since the next gap is taken as it comes | ||
| 118 | 1 | run->last_ms = 0; | |
| 119 | 1 | return finished; | |
| 120 | } | ||
| 121 | |||
| 122 | 373 | run->sum_sq += diff * diff; | |
| 123 | 373 | run->diffs++; | |
| 124 | 373 | run->last_ms = ppi_ms; | |
| 125 | |||
| 126 | // a gap that finished a quiet run returned above as the first beat of the next, so only one | ||
| 127 | // run can finish here. the gap that fills a run also starts the next one, since its neighbour | ||
| 128 | // after it is still a real difference | ||
| 129 |
2/2✓ Branch 15 → 16 taken 2 times.
✓ Branch 15 → 18 taken 371 times.
|
373 | if (run->diffs >= HRV_DIFFS_FULL) |
| 130 | { | ||
| 131 | 2 | int full = hrv_run_finish(run); | |
| 132 | 2 | run->last_ms = ppi_ms; | |
| 133 | 2 | run->last_at = now_sec; | |
| 134 | 2 | return full; | |
| 135 | } | ||
| 136 | |||
| 137 | 371 | return finished; | |
| 138 | } | ||
| 139 | |||
| 140 | 10 | bool hrv_reading_stale(uint32_t measured_at, uint32_t now_sec, uint16_t period_sec) | |
| 141 | { | ||
| 142 | 10 | uint32_t after = (uint32_t)period_sec * HRV_STALE_PERIODS; | |
| 143 |
2/2✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 4 taken 2 times.
|
10 | if (after < HRV_STALE_MIN_SEC) |
| 144 | { | ||
| 145 | 8 | after = HRV_STALE_MIN_SEC; | |
| 146 | } | ||
| 147 | |||
| 148 | // a clock set back since the reading reads as no time passed, like the quiet check | ||
| 149 |
4/4✓ Branch 4 → 5 taken 9 times.
✓ Branch 4 → 7 taken 1 time.
✓ Branch 5 → 6 taken 5 times.
✓ Branch 5 → 7 taken 4 times.
|
10 | return now_sec > measured_at && now_sec - measured_at > after; |
| 150 | } | ||
| 151 | |||
| 152 | 4 | bool hrv_should_ask(bool sampling, uint32_t gap_at, uint32_t asked_at, uint32_t now_sec, uint16_t period_sec) | |
| 153 | { | ||
| 154 |
4/4✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 5 taken 2 times.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1 time.
|
4 | bool lapsed = !sampling || hrv_reading_stale(gap_at, now_sec, period_sec); |
| 155 | |||
| 156 |
4/4✓ Branch 7 → 8 taken 3 times.
✓ Branch 7 → 11 taken 1 time.
✓ Branch 9 → 10 taken 2 times.
✓ Branch 9 → 11 taken 1 time.
|
4 | return lapsed && hrv_reading_stale(asked_at, now_sec, period_sec); |
| 157 | } | ||
| 158 | |||
| 159 | 3 | bool hrv_hold_over(uint32_t held_since, uint32_t now_sec) | |
| 160 | { | ||
| 161 |
4/4✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 5 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1 time.
|
3 | return now_sec >= held_since && now_sec - held_since >= HRV_HOLD_MAX_SEC; |
| 162 | } | ||
| 163 | |||
| 164 | // the settle is held to HRV_SETTLE_GAPS_MAX, so a face cannot leave out more than 20 gaps even for a | ||
| 165 | // sensor that settles slower. a burst is held for HRV_HOLD_MAX_SEC at most, and a run needs | ||
| 166 | // HRV_DIFFS_MIN differences, which takes that many gaps plus one after the settle. at the slowest heart | ||
| 167 | // rate kept, 30 a minute, a gap lands every 2 seconds, so a settle of 20 has the run's 20th difference | ||
| 168 | // in by 80 seconds with five beats spare. a longer settle would let a slow heart's hold run out before | ||
| 169 | // its run gave a reading, so that wearer would never see one | ||
| 170 | _Static_assert((HRV_SETTLE_GAPS_MAX + HRV_DIFFS_MIN) * HRV_PPI_MAX_MS / 1000 <= HRV_HOLD_MAX_SEC, | ||
| 171 | "the longest settle must leave a run at 30 bpm time to finish inside one hold"); | ||
| 172 | |||
| 173 | 5 | uint8_t hrv_settle_gaps(uint16_t asked) | |
| 174 | { | ||
| 175 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 4 times.
|
5 | if (asked == 0) |
| 176 | { | ||
| 177 | 1 | return HRV_SETTLE_GAPS; | |
| 178 | } | ||
| 179 | |||
| 180 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 3 times.
|
4 | return asked < HRV_SETTLE_GAPS_MAX ? (uint8_t)asked : HRV_SETTLE_GAPS_MAX; |
| 181 | } | ||
| 182 |