A content-addressed (0x00 || sha256(wasm)) WebAssembly codec conforming to CODEC-ABI v0.1 that packs 100% bit-exact lossless clinical ECG rhythm strips — including standard 10-second strips at 250–360 Hz — into a single dense QR code and decodes them into standards-compliant FHIR R4 Bundle resources containing Observation.valueSampledData.
- WASM binary:
ecg-strip.wasm— 42,742 bytes - SHA-256:
b37fa36f53a0e4818c8456be8ee7fe0532806a20fb35772607f38938e6ffb75c - Static audit (
auditWasm): 0 violations, 0 imports, 0 floating-point instructions, 0 SIMD/threads, 0 reachable panics (enforced at link time),#![no_std],#![forbid(unsafe_code)]inecg_strip_core. - Decode speed: 2.5–3.8 ms per 10-second strip (including full canonical re-encode verification across all 4 predictors).
Benchmark on 8 Real Open-Access Human ECG Recordings (PhysioNet)
All 8 strips are real human ECG recordings fetched from PhysioNet under the Open Data Commons Attribution License v1.0 (ODC-By 1.0), verified against each database's cardiologist .atr annotations (scripts/fetch-physionet.ts). Every strip round-trips through shc_encode → pack({ codec: { sha256 } }) → QR numeric digits → parse → runCodec with 0 sample error across all 27,700 samples.
| Strip | PhysioNet Record | Pathology | Lead | Fs | Samples | FHIR JSON | DEFLATE (L9) | Codec Payload (bits/smp) | SHA-256 Card (+35 B) | QR Version-L (bare / viewer) | vs FHIR | vs DEFLATE | Best Predictor |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
nsr | mitdb/100 @ 00:10 | Normal Sinus Rhythm | MLII | 360 Hz | 3,600 | 15,656 B | 3,978 B (>QR max) | 1,824 B (4.05 b/s) | 1,859 B | V32-L / V32-L | 8.42× | 2.14× | P0: delta |
afib | mitdb/201 @ 00:10 | Atrial Fibrillation | MLII | 360 Hz | 3,600 | 15,568 B | 4,044 B (>QR max) | 1,813 B (4.03 b/s) | 1,848 B | V32-L / V32-L | 8.42× | 2.19× | P3: lms3 |
bigeminy | mitdb/106 @ 12:30 | Ventricular Bigeminy | MLII | 360 Hz | 3,600 | 15,457 B | 4,850 B (>QR max) | 2,012 B (4.47 b/s) | 2,047 B | V34-L / V34-L | 7.55× | 2.37× | P2: ar2-damped |
lbbb | mitdb/109 @ 00:00 | Left Bundle Branch Block | MLII | 360 Hz | 3,600 | 16,685 B | 5,331 B (>QR max) | 2,031 B (4.51 b/s) | 2,066 B | V34-L / V34-L | 8.08× | 2.58× | P2: ar2-damped |
paced | mitdb/107 @ 00:00 | Ventricular Paced Rhythm | MLII | 360 Hz | 3,600 | 16,685 B | 5,926 B (>QR max) | 2,093 B (4.65 b/s) | 2,128 B | V34-L / V35-L | 7.84× | 2.78× | P1: linear2 |
vtach | mitdb/207 @ 00:38 | VTach / Ventricular Flutter | MLII | 360 Hz | 3,600 | 14,951 B | 5,369 B (>QR max) | 1,973 B (4.38 b/s) | 2,008 B | V33-L / V34-L | 7.45× | 2.67× | P2: ar2-damped |
av-block | mitdb/231 @ 01:38 | 2nd-Deg AV Block (Mobitz II) | MLII | 360 Hz | 3,600 | 15,428 B | 4,216 B (>QR max) | 1,807 B (4.02 b/s) | 1,842 B | V32-L / V32-L | 8.38× | 2.29× | P0: delta |
stemi | edb/e0103 @ 16:15 | Acute ST-Elevation (+200 µV) | MLIII | 250 Hz | 2,500 | 11,308 B | 2,842 B | 1,371 B (4.39 b/s) | 1,406 B | V27-L / V28-L | 8.04× | 2.02× | P0: delta |
Why this matters: On all seven 10-second 360 Hz MIT-BIH strips, raw DEFLATE of the FHIR Bundle is 3,978–5,926 bytes, exceeding QR Version 40-L maximum capacity (2,953 bytes) — none can fit in a single QR code under generic compression.
ecg-stripfits every 10-second strip into a single QR code even with the 33-byte SHA-256 content-addressed codec reference, 2-byte integrity check, full viewer URL, patient demographics, device metadata, SNOMED CT interpretation, and provenance note.
Input Model (model.d.ts)
export interface EcgStrip {
patient?: { family?: string; given?: string[]; birthDate?: string };
source?: { name: string; version?: string };
recordedAt: string; // FHIR dateTime / instant
sampleRateHz: number; // integer 50..1000 (e.g. 250, 360, 500)
microvoltsPerUnit: number; // integer 1..1000 (e.g. 5 => 0.005 mV/step)
interpretation?: { system?: string; code?: string; display?: string; text?: string };
note?: string;
leads: {
lead: "I"|"II"|"III"|"aVR"|"aVL"|"aVF"|"V1"|"V2"|"V3"|"V4"|"V5"|"V6"|"MLII"|"MLIII";
samples: number[]; // exact signed i16 (-32768..32767), 1..12000/lead, <=24000 total
}[];
}
Bounded Limits
| Dimension | Bound |
|---|---|
sampleRateHz | 50..1000 (integer Hz) |
microvoltsPerUnit | 1..1000 (integer µV/LSB) |
leads | 1..12 unique standard leads |
samples per lead | 1..12000 (equal length across leads; <= 24000 total) |
| Sample value | -32768..32767 (i16) |
| Card payload | <= 4096 bytes (CODEC-ABI limit) |
FHIR R4 Output Mapping (shc_decode)
shc_decode emits deterministic minified UTF-8 JSON with fixed key order:
Bundle(type: "collection")resource:0—Patient(id: "0", withname/birthDatewhen present)resource:1(optional) —Device(id: "1", withdeviceNameand optionalversionwhensourceis present)resource:k— OneObservationper lead:status: "final",category: procedurecode: ISO/IEEE 11073-10101 (urn:iso:std:iso:11073:10101) per-lead MDC code (131329–131336,131389–131392) +text: "ECG Lead <name>"subject: { reference: "resource:0" },effectiveDateTime: recordedAt, optionaldevice: { reference: "resource:1" }valueSampledData:origin: { value: 0, unit: "mV", system: "http://unitsofmeasure.org", code: "mV" }period: pure-integer decimal millisecondsround(1_000_000 / sampleRateHz) / 1000(e.g.,2.778for 360 Hz,4for 250 Hz,2for 500 Hz)factor: pure-integer decimalmicrovoltsPerUnit / 1000mV/unit (e.g.,0.005for 200 adu/mV)dimensions: 1data: space-separated exact integer ADC samples — 100% lossless
- Optional
interpretation(CodeableConcept) andnote(Annotation)
Wire Format & Lossless DSP + Entropy Coding
- Compact Binary Header:
- Byte 0: version bits (
0= v1;1..3→UNSUPPORTED), optional-section flags (patient,source,interpretation,note), reserved bits (0). recordedAt: compact 4-byte little-endian Unix seconds when exact UTCYYYY-MM-DDThh:mm:ssZ(1970–2105), else length-prefixed ASCII.sampleRateHz - 50(ULEB128),microvoltsPerUnit - 1(ULEB128),numLeads(u8),numSamples - 1(ULEB128).- Optional sections:
patient(2-byte packed day-offset forYYYY-MM-DDin 1900–2079),source,interpretation(2-bit code forhttp://snomed.info/sct/http://loinc.org/ literal),note.
- Byte 0: version bits (
- Per-Lead Integer Predictor Selection (4 modes, 2-bit tag):
P0: 1st-order deltax[n-1]P1: 2nd-order linear2*x[n-1] - x[n-2]P2: damped 2nd-order AR(2)(7*x[n-1] - 3*x[n-2] + 2) >> 2P3: 3-tap integer delta-LMS (Q10 fixed-point weights updated with bounded integer gradient step>> 6)- The encoder evaluates all 4 predictors per lead and picks the shortest output (lowest predictor ID breaks ties).
- 6-Context Activity-Adaptive Range Coder:
- Zigzag-mapped residuals are coded in one of 6 contexts selected by an integer EMA of
|residual|(0,<=1,<=3,<=7,<=18,>18). - Each context maintains a 16-symbol adaptive frequency table (
0..14direct,15escape → order-1 Exp-Golomb with adaptive unary prefix probabilities + equiprobable mantissa bits).
- Zigzag-mapped residuals are coded in one of 6 contexts selected by an integer EMA of
- Canonicality:
shc_decodere-encodes the decodedEcgStripand rejects any card whose bytes do not match byte-for-byte (MALFORMED).
Build, Fetch & Test
# Re-fetch and verify the 8 real PhysioNet strips into examples/
bun codecs/ecg-strip/scripts/fetch-physionet.ts
# Rebuild ecg-strip.wasm (requires rustc with wasm32-unknown-unknown target)
bash codecs/ecg-strip/rust/build.sh
# Run full audit, lossless round-trip, canonicality, fuzz, and benchmark suite
bun test codecs/ecg-strip/ecg-strip.test.ts
PhysioNet Attribution
- MIT-BIH Arrhythmia Database (
mitdb, records100,106,107,109,201,207,231): Moody GB, Mark RG. The impact of the MIT-BIH Arrhythmia Database. IEEE Eng in Med and Biol 20(3):45–50 (May–June 2001). DOI:10.13026/C2F305(ODC-By 1.0). - European ST-T Database (
edb, recorde0103): Taddei A, Distante G, Emdin M, Pisani P, Moody GB, Zeelenberg C, Marchesi C. The European ST-T Database: standard for evaluating systems for the analysis of ST-T changes in ambulatory electrocardiography. European Heart Journal 13:1164–1172 (1992). DOI:10.13026/C2D59Z(ODC-By 1.0).