v0.3.2
This commit is contained in:
parent
788c68a1d8
commit
35a380b808
8 changed files with 954 additions and 46 deletions
|
|
@ -4,7 +4,7 @@ use std::collections::VecDeque;
|
|||
|
||||
use crate::model::{ArterialShape, CapnoShape, EcgRhythm, Parameters, PulseConduction};
|
||||
|
||||
const SAMPLE_STEP: f64 = 0.01; // 100 Hz
|
||||
const SAMPLE_STEP: f64 = 0.004; // 250 Hz; adequately samples the QRS upstroke
|
||||
const HISTORY_SECONDS: f64 = 10.0;
|
||||
const ART_DELAY: f64 = 0.12; // QRS -> arterial upstroke, seconds (schematic)
|
||||
const PLETH_DELAY: f64 = 0.30; // QRS -> peripheral pleth onset, seconds
|
||||
|
|
@ -54,7 +54,7 @@ pub struct Simulator {
|
|||
impl Simulator {
|
||||
pub fn new(params: &Parameters) -> Self {
|
||||
let mut sim = Self {
|
||||
samples: VecDeque::with_capacity(1_100),
|
||||
samples: VecDeque::with_capacity((HISTORY_SECONDS / SAMPLE_STEP) as usize + 16),
|
||||
time: -8.0,
|
||||
ecg_phase: 0.0,
|
||||
respiratory_phase: 0.0,
|
||||
|
|
@ -68,7 +68,7 @@ impl Simulator {
|
|||
pending_time: 0.0,
|
||||
};
|
||||
// Prepopulate the trace and pulse history, including the initial pulse rate.
|
||||
for _ in 0..800 {
|
||||
for _ in 0..(8.0 / SAMPLE_STEP) as usize {
|
||||
sim.step(params);
|
||||
}
|
||||
sim
|
||||
|
|
@ -143,7 +143,7 @@ impl Simulator {
|
|||
let increment = SAMPLE_STEP * params.heart_rate as f64 / 60.0 / irregularity;
|
||||
let next_phase = self.ecg_phase + increment;
|
||||
if self.ecg_phase < QRS_PHASE && next_phase >= QRS_PHASE {
|
||||
// Interpolate within the 10ms sample so mechanical onsets stay aligned.
|
||||
// Interpolate within the 4 ms sample so mechanical onsets stay aligned.
|
||||
let qrs_time = self.time - SAMPLE_STEP * (next_phase - QRS_PHASE) / increment;
|
||||
self.ecg_beat_number += 1;
|
||||
if params.pulse_conduction.conducts(self.ecg_beat_number) {
|
||||
|
|
@ -230,6 +230,17 @@ fn phase_gaussian(phase: f64, center: f64, width: f64) -> f64 {
|
|||
gaussian(distance, 0.0, width)
|
||||
}
|
||||
|
||||
/// Periodic ECG component with a location and width expressed in seconds,
|
||||
/// not as fractions of the R-R interval. This keeps QRS morphology from
|
||||
/// artificially narrowing when the instructor increases the heart rate.
|
||||
fn ecg_lobe(phase: f64, rr_seconds: f64, offset_seconds: f64, width_seconds: f64) -> f64 {
|
||||
phase_gaussian(
|
||||
phase,
|
||||
QRS_PHASE + offset_seconds / rr_seconds,
|
||||
width_seconds / rr_seconds,
|
||||
)
|
||||
}
|
||||
|
||||
fn ecg_amplitude(phase: f64, time: f64, rhythm: EcgRhythm, heart_rate: i32) -> f64 {
|
||||
if rhythm == EcgRhythm::VentricularFibrillation {
|
||||
// Non-periodic-looking composite oscillation with no organized QRS.
|
||||
|
|
@ -240,26 +251,41 @@ fn ecg_amplitude(phase: f64, time: f64, rhythm: EcgRhythm, heart_rate: i32) -> f
|
|||
if heart_rate <= 0 || rhythm == EcgRhythm::Asystole {
|
||||
return 0.006 * (time * 9.3).sin();
|
||||
}
|
||||
|
||||
// ECG wave durations are measured in *seconds*. Only the P/T placement
|
||||
// and their widths shorten with R-R interval; Q/R/S widths stay fixed.
|
||||
// These synthetic lead-II-like morphologies are illustrative, not
|
||||
// calibrated or validated diagnostic ECGs.
|
||||
let rr_seconds = 60.0 / f64::from(heart_rate);
|
||||
let p_before_qrs = (rr_seconds * 0.26).clamp(0.08, 0.18);
|
||||
let t_after_qrs = (rr_seconds * 0.30).clamp(0.10, 0.29);
|
||||
let p_width = (rr_seconds * 0.045).clamp(0.014, 0.035);
|
||||
let t_width = (rr_seconds * 0.09).clamp(0.022, 0.065);
|
||||
let lobe = |offset, width| ecg_lobe(phase, rr_seconds, offset, width);
|
||||
|
||||
match rhythm {
|
||||
EcgRhythm::Sinus => {
|
||||
0.16 * phase_gaussian(phase, 0.16, 0.030)
|
||||
- 0.19 * phase_gaussian(phase, 0.300, 0.011)
|
||||
+ 1.26 * phase_gaussian(phase, QRS_PHASE, 0.008)
|
||||
- 0.32 * phase_gaussian(phase, 0.363, 0.014)
|
||||
+ 0.32 * phase_gaussian(phase, 0.61, 0.070)
|
||||
// Small P wave, narrow negative Q, prominent R, negative S and T.
|
||||
0.16 * lobe(-p_before_qrs, p_width)
|
||||
- 0.22 * lobe(-0.026, 0.009)
|
||||
+ 1.26 * lobe(0.0, 0.012)
|
||||
- 0.32 * lobe(0.028, 0.012)
|
||||
+ 0.32 * lobe(t_after_qrs, t_width)
|
||||
}
|
||||
EcgRhythm::AtrialFibrillation => {
|
||||
// No organized P waves; preserve the QRS shape under changing HR.
|
||||
0.023 * (time * 39.0).sin()
|
||||
+ 0.014 * (time * 57.0).sin()
|
||||
- 0.19 * phase_gaussian(phase, 0.300, 0.011)
|
||||
+ 1.26 * phase_gaussian(phase, QRS_PHASE, 0.008)
|
||||
- 0.32 * phase_gaussian(phase, 0.363, 0.014)
|
||||
+ 0.28 * phase_gaussian(phase, 0.61, 0.072)
|
||||
- 0.22 * lobe(-0.026, 0.009)
|
||||
+ 1.26 * lobe(0.0, 0.012)
|
||||
- 0.32 * lobe(0.028, 0.012)
|
||||
+ 0.28 * lobe(t_after_qrs, t_width)
|
||||
}
|
||||
EcgRhythm::VentricularTachycardia => {
|
||||
1.06 * phase_gaussian(phase, 0.31, 0.086)
|
||||
- 0.49 * phase_gaussian(phase, 0.46, 0.067)
|
||||
- 0.16 * phase_gaussian(phase, 0.72, 0.10)
|
||||
// Broad monomorphic ventricular complexes, also in milliseconds.
|
||||
1.06 * lobe(0.0, 0.045)
|
||||
- 0.49 * lobe(0.068, 0.030)
|
||||
- 0.16 * lobe(0.13, 0.045)
|
||||
}
|
||||
EcgRhythm::VentricularFibrillation | EcgRhythm::Asystole => unreachable!(),
|
||||
}
|
||||
|
|
@ -472,6 +498,56 @@ mod tests {
|
|||
assert!(sim.samples().iter().any(|s| s.art > 0.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sinus_qrs_peak_height_is_stable_even_at_high_heart_rates() {
|
||||
// The previous phase-based 100 Hz version varied strongly at 189 bpm:
|
||||
// the narrow R peaks frequently fell between sample timestamps.
|
||||
for heart_rate in [60, 75, 120, 189, 240] {
|
||||
let p = Parameters { heart_rate, ..Parameters::default() };
|
||||
let mut sim = blank(&p);
|
||||
let rr = 60.0 / f64::from(heart_rate);
|
||||
let duration = 9.0;
|
||||
while sim.time < duration {
|
||||
sim.step(&p);
|
||||
}
|
||||
|
||||
// Compare maxima in +/-30 ms windows around the exact QRS times.
|
||||
let peaks: Vec<f64> = (1..18)
|
||||
.filter_map(|beat| {
|
||||
let qrs_time = (f64::from(beat) + QRS_PHASE) * rr;
|
||||
if qrs_time > duration - 0.05 { return None; }
|
||||
sim.samples.iter()
|
||||
.filter(|s| (s.time - qrs_time).abs() <= 0.03)
|
||||
.map(|s| s.ecg)
|
||||
.reduce(f64::max)
|
||||
})
|
||||
.collect();
|
||||
assert!(peaks.len() >= 5, "too few captured QRS at {heart_rate} bpm");
|
||||
let minimum = peaks.iter().copied().fold(f64::INFINITY, f64::min);
|
||||
let maximum = peaks.iter().copied().fold(f64::NEG_INFINITY, f64::max);
|
||||
assert!(minimum > 1.1, "QRS peak too small at {heart_rate} bpm: {minimum}");
|
||||
assert!(maximum - minimum < 0.04,
|
||||
"QRS peak varies excessively at {heart_rate} bpm: {minimum}..{maximum}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pulse_pressure_variation_does_not_change_ecg_amplitude() {
|
||||
let baseline = Parameters::default();
|
||||
let with_ppv = Parameters {
|
||||
pulse_pressure_variation: 60,
|
||||
..Parameters::default()
|
||||
};
|
||||
let mut sim_baseline = blank(&baseline);
|
||||
let mut sim_ppv = blank(&with_ppv);
|
||||
for _ in 0..1_000 {
|
||||
sim_baseline.step(&baseline);
|
||||
sim_ppv.step(&with_ppv);
|
||||
assert_eq!(sim_baseline.samples.back().unwrap().ecg,
|
||||
sim_ppv.samples.back().unwrap().ecg);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capnogram_reaches_etco2_plateau() {
|
||||
assert!((capno_amplitude(0.76, CapnoShape::Normal) - 1.0).abs() < 1e-8);
|
||||
|
|
|
|||
Loading…
Reference in a new issue