fast-hrv
Heart rate variability (HRV) from ECG, in Rust: from the raw signal to R peaks, artifact handling and
54 HRV metrics, offline or packet by packet, with results that match NeuroKit2 where the methods are the
same.
Cleaning, R peaks, Kubios correction and all HRV metrics on a 512 Hz synthetic ECG, one thread,
NeuroKit2 0.3.0 vs fast-hrv 0.10.0, Intel i7-9700K. R peaks within 2 samples, RMSSD/SDNN/SampEn equal to < 1e-14.
Reproduce with tools/neurokit_benchmark.py.
What it does
flowchart LR
A[raw ECG] --> B[clean<br/>zero-phase or causal]
B --> C[R peaks<br/>two detectors + bSQI]
C --> D[artifacts<br/>Kubios or exclusion]
D --> E[HRV metrics]
E --> F[window report<br/>valid / warning / suppressed]
G[signal quality] -.-> F
H[breathing rate<br/>from ECG] -.-> F
I[accelerometer<br/>activity, posture] -.-> F
| Stage |
In short |
Details |
| Cleaning |
NeuroKit2-identical zero-phase filter, or a causal streaming filter (optional notch) whose packet-by-packet output equals the whole-record output bit for bit |
detection.md |
| R peaks |
NeuroKit2 and Elgendi detectors, sub-sample refinement, choice of fiducial point, windowed processing of long records, a streaming detector with the batch output |
detection.md |
| Signal quality |
flat line, saturation, kSQI, pSQI, basSQI, bSQI, template correlation, RR plausibility |
sqi module |
| Artifacts |
Kubios correction (NeuroKit2-identical) with a per-beat record of what changed, or an exclusion mode that drops intervals around ectopic beats instead of moving them; gap-aware segments |
artifacts.md |
| Metrics |
time domain, frequency domain (Welch, Burg AR, Lomb–Scargle, NeuroKit2-compatible), Poincaré, entropy, DFA, asymmetry, fragmentation; smoothness-priors detrending |
metrics.md |
| Context |
breathing rate from ECG and an HF band centred on it, accelerometer activity and posture |
metrics.md |
| Personal baseline |
rolling mean / median / CV over calendar days, robust z, smallest worthwhile change, EWMA, CUSUM (statistics only; thresholds stay in the application) |
metrics.md |
| Window rules |
minimum and recommended length for every metric, a per-window verdict with reasons, and coverage-weighted aggregation of short windows |
metrics.md |
How this compares with NeuroKit2, Kubios, pyHRV, hrv-analysis, systole, RHRV and HeartPy, feature by
feature: feature matrix.
Plain scalar f64 code with no heavy dependencies: filters, FFT and splines are part of the crate, and nothing
assumes a particular CPU (no AVX, no target-cpu=native).
Quick start
From R-peak sample indices:
let rri = fast_hrv::rri_from_peaks(&peaks, 512); // R-peak indices → NN intervals (ms)
let r = fast_hrv::hrv(&rri);
println!("SDNN {:.1} ms RMSSD {:.1} ms LF/HF {:.2} SampEn {:.2}",
r.time.sdnn, r.time.rmssd, r.frequency.lf_hf, r.nonlinear.sampen);
From raw ECG, with sub-sample peaks and artifact correction:
use fast_hrv::detect::{ElgendiConfig, RefineConfig, elgendi_peaks, refine_peaks};
let clean = fast_hrv::ecg_clean_zero_phase(&raw, 512.0, &fast_hrv::EcgFilterConfig::default())?;
let cand = elgendi_peaks(&clean, 512.0, &ElgendiConfig::default())?;
let peaks = refine_peaks(&clean, 512.0, &cand, &RefineConfig::default())?;
let fixed = peaks.fix_kubios(512.0, &fast_hrv::KubiosParams::default())?;
let r = fast_hrv::hrv(&fixed.rri_ms(512.0));
Live, 32 samples at a time (the peaks equal the batch result bit for bit):
use fast_hrv::stream::{StreamConfig, StreamDetector, StreamPeaks};
let mut filt = fast_hrv::EcgStreamFilter::new(512.0, &fast_hrv::EcgFilterConfig::default())?;
let mut det = StreamDetector::new(512.0, 0, StreamConfig::default())?;
let mut out = StreamPeaks::default();
for packet in packets {
det.push(&filt.process(packet), &mut out)?; // out.primary: confirmed peaks
}
det.flush(&mut out)?;
A runnable version with gaps is in examples/streaming.rs.
Which numbers can be shown
A metric computed on a window is not automatically meaningful: the window may be too short for it, too
many beats may have been corrected, or slow breathing may have moved the respiratory peak into the LF band.
window_report_rri gives each metric a verdict and the reasons
(examples/window_report.rs, 60 s, two corrected beats in a row, breathing 7 /min):
window 60 s, artifact ratio 2.8 %
mean_hr 70.79 valid
rmssd 19.41 warning consecutive_corrections
sdnn 31.97 warning below_recommended_length
hf 2.90 warning below_strict_length, breathing_in_lf_band
lf_hf NaN suppressed not_computed, too_short, breathing_in_lf_band
sampen 0.18 suppressed too_short
The length limits come from the metric catalogue (METRICS, metric_info("lf_hf")), with their sources.
hrv_with_preset(rri, Preset::KubiosLike) runs a named set of processing choices and returns a
Provenance record (library version, preset, detrending, frequency method, intervals used) to store next to
the values.
Accuracy
- Against NeuroKit2 (same input): cleaning, R peaks, Kubios correction, time-domain and nonlinear
metrics are identical to ≤ 3.3e-14 relative;
frequency_domain_neurokit reproduces nk.hrv_frequency LF/HF
to ≤ 3e-13. The default frequency method follows Task Force / Kubios practice and differs from NeuroKit2 by
a few percent. Full table: accuracy.md.
- On annotated real ECG (MIT-BIH Arrhythmia, 18 records, live path): median RMSSD error 0.34 % against
the annotated beats (mean 1.6 %, worst 11.8 %) with the default low-amplitude rescue, which fixes detections on
P waves;
Fiducial::ADAPTIVE brings the mean to 1.2 % and fixes the large
errors on bundle branch blocks (detection.md).
- Not claimed: agreement with a reference device (chest strap, Holter) in the field. Matching NeuroKit2
shows the methods are implemented as specified, not that a wearable recording is measured correctly.
cargo test --release runs the full suite (over 200 tests), including the NeuroKit2 and SciPy parity checks and stream = batch
equivalence for any packet size.
Python
pip install fast-hrv (wheels for Linux, macOS and Windows, CPython 3.10–3.14), then import fast_hrv_rs.
From a checkout: maturin develop --release.
import fast_hrv_rs as f
f.process_ecg(ecg, 512) # clean, R peaks, Kubios correction, basic stats
m = f.hrv(rr_ms) # every catalogue metric: {name: value}
m, prov = f.hrv_with_preset(rr_ms, "kubios_like") # or "task_force", "neurokit"
w = f.window_report(rr_ms, corrected, 15.0) # w["metrics"]["rmssd"] -> {"value", "verdict", "reasons"}
f.metric_catalogue()["lf_hf"] # min / recommended / strict lengths, artifact limit
f.smoothness_priors(rr_ms, 500.0) # detrended series
live = f.LiveEcg(512, window_s=60, step_s=10) # live: filter, R peaks, HRV windows
for packet in packets: # any size, e.g. 32 samples
out = live.push(packet) # out["peaks"], out["windows"] (verdicts per metric)
Also exclusion and concordance (artifact handling on R-peak positions), the baseline statistics
(rolling, robust_z, swc, ewma, cusum), motion_context (accelerometer), ecg_clean,
find_peaks, fix_peaks, compute_quality, hrv_metrics and single-metric functions. Wheels are tested on
CPython 3.10–3.14.
Features and toolchain
| Feature |
Effect |
serde |
Serialize/Deserialize for report structs |
python |
PyO3 module fast_hrv_rs |
simd |
portable unrolled helpers; no CPU-specific instructions; off by default |
parallel |
reserved, currently no effect |
Rust 1.100.0 (rust-toolchain.toml), MSRV 1.85, edition 2024. Changes: CHANGELOG.md;
releases: docs/releasing.md.
Capacity estimate for a live multi-user pipeline: CAPACITY.md (Turkish).
License
Licensed under either of MIT or Apache-2.0 at your option.