Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/src/onehz/sleep/hr_fallback.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,17 @@ HrLedWindow? hrLedSleepWindow(
continue;
}
var j = i;
var hole = false;
while (j < n) {
// A HOLE IN THE RECORD ends the run. Two "low" samples three hours apart
// are two observations, not a three-hour low-HR stretch — and the run's
// duration below is WALL CLOCK, so without this the proposed window (the
// one the user is shown and asked to confirm) was inflated by hours of
// data we never had.
if (j > i && tsSec[j] - tsSec[j - 1] > maxSampleGapSec) break;
if (j > i && tsSec[j] - tsSec[j - 1] > maxSampleGapSec) {
hole = true;
break;
}
if (low(j)) {
j++;
continue;
Expand All @@ -134,7 +138,9 @@ HrLedWindow? hrLedSleepWindow(
}
final dur = tsSec[(j - 1).clamp(0, n - 1)] - tsSec[i];
runs.add((start: i, end: j, dur: dur));
i = j + 1;
// After a hole j is the first sample of the next stretch, still a candidate
// run start; every other break stops on a non-low j.
i = hole ? j : j + 1;
}

// 5. Defer a dip still going at the cut, and every dip chained to it the
Expand Down
21 changes: 21 additions & 0 deletions test/onehz/hr_fallback_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,26 @@ void main() {
expect(w!.offsetSec, lessThanOrEqualTo(t0 + 3 * 3600 + 300),
reason: 'pre-fix: t0+12000, the end of the awake stretch');
});

test('a run starting right after a recording hole keeps its first sample', () {
final hr = <double>[];
final ts = <int>[];
const t0 = 1700000000;
var t = t0;
void seg(int secs, double bpm) {
for (var i = 0; i < secs; i++) {
hr.add(bpm);
ts.add(t++);
}
}
seg(3 * 3600, 50);
t += 6 * 60;
final afterHole = t;
seg(4 * 3600, 50);
final w = hrLedSleepWindow(hr, ts,
hrBaseline: List<double>.filled(120, 75));
expect(w, isNotNull);
expect(w!.onsetSec, afterHole);
});
});
}
Loading