Repository navigation
v25: decode the optical samples and the sub-second field #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ import 'constants.dart'; | |
| class R24 { | ||
| final int histVersion; // historical layout version byte @ inner[1] (frame[5]) | ||
| final int tsEpoch; // unix seconds @ inner[7:11] (frame[11:15]) | ||
| final int tsSubsec; // sub-seconds @ inner[11:13] (frame[15:17], v24/v12) | ||
| final int tsSubsec; // sub-seconds @ inner[11:13] (frame[15:17], v24/v12/v25) | ||
| final int counter; // record counter @ inner[3:7] (frame[7:11]) | ||
| final int hr; // heart rate bpm @ inner[17] (frame[21], v24/v12) | ||
|
|
||
|
|
@@ -251,9 +251,13 @@ ByteData _view(Uint8List b) => | |
| b.buffer.asByteData(b.offsetInBytes, b.lengthInBytes); | ||
|
|
||
| /// Decode a v25 historical record. v25 is NOT a 1 Hz biometric record: it is a | ||
| /// raw ~24 Hz PPG waveform, sent in 13–27 s bursts roughly every 20 minutes. | ||
| /// The only field solved in it is the unix second at inner[7:11]; the waveform | ||
| /// itself is left whole in [R24.rawTail]. | ||
| /// compressed optical burst, sent in bursts (up to ~40 records) roughly every 20 | ||
| /// minutes. | ||
| /// Header: counter @ inner[3:7], unix second @ [7:11], sub-seconds (1/32768 s) | ||
| /// @ [11:13] — the same header as v24 — so the timestamp is full resolution. | ||
| /// The waveform is left whole in [R24.rawTail]; `reconstructV25Samples` decodes it: | ||
| /// 25 samples per record, an i32 first sample @ [15:19] followed by 24 i16 | ||
| /// saturating deltas @ [19:67] (see v25_samples.dart). | ||
| /// | ||
| /// So everything else comes back absent, and that is deliberate: | ||
| /// | ||
|
|
@@ -282,7 +286,7 @@ R24? _parseV25(Uint8List inner) { | |
| return R24( | ||
| histVersion: 25, | ||
| tsEpoch: view.getUint32(7, Endian.little), | ||
| tsSubsec: 0, | ||
| tsSubsec: view.getUint16(11, Endian.little), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Decoded timestamps lose sub-seconds When callers obtain v25 timestamps through Include the decoded Also at Prompt for AI agentsIn `lib/src/records.dart` at line 289:
**Decoded timestamps lose sub-seconds**
When callers obtain v25 timestamps through `decodeRecord` rather than `parseR24`, `decodeRecord` assigns `DecodedSample.ts` from `d.tsEpoch`, and `DecodedSample.toMap()` emits only `ts`, discarding `tsSubsec`; callers receive whole-second timestamps.
Include the decoded `tsSubsec` in the timestamp exposed by `decodeRecord` and `DecodedSample.toMap()`.
Also at `lib/src/records.dart:290`.There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ Low · Sub-second documentation omits v25
Update the R24 field comment and README to document v25 sub-second decoding. Prompt for AI agentsIn `lib/src/records.dart` at line 289:
**Sub-second documentation omits v25**
`R24.tsSubsec` is now populated for v25, but its field comment still lists only “v24/v12,” and README.md:101 says `parseR24` decodes “v24/v12 verbatim.” Callers relying on those descriptions miss that v25 timestamps now include the decoded sub-second field.
Update the R24 field comment and README to document v25 sub-second decoding. |
||
| counter: view.getUint32(3, Endian.little), | ||
| hr: 0, | ||
| rrCount: 0, | ||
|
|
@@ -333,8 +337,9 @@ bool _physiologicallyPlausible(List<double> accelG, int hr) { | |
| /// packet-type byte. | ||
| /// | ||
| /// Routing: | ||
| /// - v25 → the 24 Hz PPG-waveform layout ([_parseV25]): timestamp only, with | ||
| /// HR 0 and accelG empty. | ||
| /// - v25 → the optical-burst layout ([_parseV25]): timestamp only, with | ||
| /// HR 0 and accelG empty; the samples come from | ||
| /// `reconstructV25Samples` (v25_samples.dart). | ||
| /// - v24 / v12 → our hardware-validated field map, returned as-is. | ||
| /// - v18 / v9 / v7 → the SAME field map but with the HR byte read at that | ||
| /// version's offset (only the HR offset is independently confirmed for | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // v25_samples.dart — the optical samples of a WHOOP 4 v25 historical record. | ||
| // | ||
| // Same encoding as gen5's v26 window: an i32 first sample @ inner[15:19], then | ||
| // 24 saturating i16 deltas @ inner[19:67], 25 samples in all. So this reuses | ||
| // [reconstructSaturatedDeltaWindow] and its evidence about the tail. | ||
| // | ||
| // How it was checked (one WHOOP 4.0, firmware harvard 41.17.6.0, realtime raw | ||
| // stream on): 739 v25 records have an R11 frame with the same timestamp. In | ||
| // 734 the 25 samples equal R11 slot 1 lane A samples 0, 2, ..., 48 exactly. The | ||
| // other five hold 24 differing deltas, every one the live difference clamped | ||
| // to the i16 range, and every one a rail value. So all records without a rail | ||
| // delta matched, and all five records with one did not. A rail delta is | ||
| // ambiguous (the true difference may be larger), so the sample it reaches and | ||
| // every later absolute value in that record are not trustworthy. The next | ||
| // record starts from its own absolute value. | ||
| // Which LED/photodiode slot 1 lane A is, is not established. | ||
|
|
||
| import 'dart:typed_data'; | ||
|
|
||
| import 'constants.dart'; | ||
| import 'gen5_records.dart' | ||
| show Gen5PpgReconstruction, reconstructSaturatedDeltaWindow; | ||
|
|
||
| /// Samples of one v25 record (~26 Hz), or null when [inner] is not a v25 | ||
| /// historical record of at least 67 bytes. Decode each record on its own: the | ||
| /// first sample is absolute, never carry a sum across records. Use | ||
| /// [Gen5PpgReconstruction.trustedSamples]; do not trust the tail after a rail delta. | ||
| Gen5PpgReconstruction? reconstructV25Samples(Uint8List inner) { | ||
| if (inner.length < 67 || | ||
| inner[0] != PacketType.historicalData || | ||
| inner[1] != 25) { | ||
| return null; | ||
| } | ||
| final view = ByteData.sublistView(inner); | ||
| return reconstructSaturatedDeltaWindow(view.getInt32(15, Endian.little), [ | ||
| for (var k = 0; k < 24; k++) view.getInt16(19 + 2 * k, Endian.little), | ||
| ]); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document the decoder’s full-record input.
rawTailstarts atinner[13], butreconstructV25Samplesexpects the full inner record. A 75-byte record yields a 62-byte tail, which the decoder rejects. Longer tails can also have the wrong type and sample offsets. Tell callers to retain and passinner, or provide a tail-specific decoder. (raw.githubusercontent.com)