moonbit-hrvkit is a high-performance, target-independent Heart Rate Variability (HRV) analysis toolkit written in MoonBit. It is designed for sports scientists, developers, and bio-hackers interested in recovery monitoring and training status analysis.
The toolkit is completely pure and compatible with all MoonBit backends (wasm-gc, js, and native), requiring no system-specific FFI or runtime imports.
Features
1. RR Interval Cleaning (Ectopic Beat Handling)
Wearable heart rate sensors can suffer from signal drops, muscle noise, and ectopic beats. This library implements medical-grade preprocessing to clean raw RR intervals:
Range Filter: Automatically flags and filters out physiologically implausible beats (default: outside [300, 2000] ms, corresponding to [30, 200] bpm).
Karlsson Local Median Filter: Runs a 5-point local median filter. If any beat deviates from the local median by more than a threshold (default: 20%), it is flagged as ectopic.
Correction Modes:
Remove: Exclude ectopic beats completely from subsequent analysis.
InterpolateLocalMedian: Replace ectopic beats with the local median of surrounding valid beats.
InterpolateLinear: Linearly interpolate the values of ectopic beats using adjacent valid beats.
2. HRV Time-Domain Indicators
Computes standard time-domain metrics widely used in recovery tracking:
Mean RR: The average duration of valid RR intervals (ms).
Mean Heart Rate (HR): The average heart rate (bpm).
SDNN: The standard deviation of normal-to-normal (NN) intervals (ms), reflecting overall autonomic nervous system activity.
RMSSD: The root mean square of successive differences (ms), reflecting parasympathetic (vagal) activity. This is the primary indicator used for daily recovery tracking.
pNN50: The percentage of successive interval differences greater than 50 ms.
pNN(custom): Custom threshold support (e.g. pNN20).
3. Morning baseline and Trend Analysis
For daily recovery tracking, one-off measurements are highly variable. This library computes baseline trends using historical morning measurements:
7-Day Rolling Baseline: Calculates the rolling mean (μ) and standard deviation (σ) of RMSSD over the past 7 measurements.
Normal Range: Computes a personalized normal range defined as μ±0.75σ (standard deviation coefficient is configurable).
Status Classification:
Low: RMSSD is below the normal range (indicates high stress, under-recovery).
High: RMSSD is above the normal range (indicates stable recovery or parasympathetic hyperactivity).
Normal: RMSSD is within the normal range (indicates stable adaptive state).
Recovery Score: A normalized score between 0 and 100 representing where today’s measurement stands relative to the baseline.
4. Custom CSV and JSON formats
Custom CSV Parser & Serializer: A state-machine-based CSV parser supporting comma separators, optional double quotes, and escaped quotes.
JSON Serialization: Complete support for ToJson and FromJson traits on all metrics and trends.
Directory Structure
.
├── moon.mod # Module metadata
├── moon.pkg # Root package config
├── LICENSE # Apache 2.0 License
├── README.md # Project documentation
├── pkg.generated.mbti # Generated public interface definitions
├── hrvkit.mbt # Core data models and configurations
├── cleaning.mbt # RR intervals cleaning and interpolation logic
├── indicators.mbt # SDNN, RMSSD, and pNN computation
├── morning.mbt # Rolling baseline trends and recovery score calculations
├── parser.mbt # CSV state-machine parsing/serialization and domain mapping
├── hrvkit_test.mbt # Unit test suite covering all features
└── cmd
└── main
├── main.mbt # CLI entry point
├── moon.pkg # Executable package config
└── pkg.generated.mbti
Installation & Setup
Make sure you have the latest MoonBit toolchain (v0.10.3 or higher) installed:
moon version --all
Clone the repository and run type check:
moon check
Run unit tests:
moon test
Command Line Interface (CLI) Usage
Run the tool using moon run cmd/main -- [options].
moonbit-hrvkit
moonbit-hrvkitis a high-performance, target-independent Heart Rate Variability (HRV) analysis toolkit written in MoonBit. It is designed for sports scientists, developers, and bio-hackers interested in recovery monitoring and training status analysis.The toolkit is completely pure and compatible with all MoonBit backends (
wasm-gc,js, andnative), requiring no system-specific FFI or runtime imports.Features
1. RR Interval Cleaning (Ectopic Beat Handling)
Wearable heart rate sensors can suffer from signal drops, muscle noise, and ectopic beats. This library implements medical-grade preprocessing to clean raw RR intervals:
[300, 2000]ms, corresponding to[30, 200]bpm).20%), it is flagged as ectopic.Remove: Exclude ectopic beats completely from subsequent analysis.InterpolateLocalMedian: Replace ectopic beats with the local median of surrounding valid beats.InterpolateLinear: Linearly interpolate the values of ectopic beats using adjacent valid beats.2. HRV Time-Domain Indicators
Computes standard time-domain metrics widely used in recovery tracking:
50ms.3. Morning baseline and Trend Analysis
For daily recovery tracking, one-off measurements are highly variable. This library computes baseline trends using historical morning measurements:
Low: RMSSD is below the normal range (indicates high stress, under-recovery).High: RMSSD is above the normal range (indicates stable recovery or parasympathetic hyperactivity).Normal: RMSSD is within the normal range (indicates stable adaptive state).0and100representing where today’s measurement stands relative to the baseline.4. Custom CSV and JSON formats
ToJsonandFromJsontraits on all metrics and trends.Directory Structure
Installation & Setup
Command Line Interface (CLI) Usage
Run the tool using
moon run cmd/main -- [options].Options
--action <clean|metrics|trends>: Select analysis action.--format <json|csv>: Input/Output format (default:csv).--cleaning <remove|median|linear>: Correction method for ectopic beats (default:remove).--data <data_string>: Inline CSV or JSON formatted data.Examples
1. Clean RR Intervals (JSON)
Output:
2. Compute HRV Metrics (CSV)
Output:
3. Compute Morning Trends (CSV)
Output:
License
This project is licensed under the Apache License 2.0. See LICENSE for details.