v0.6.7: fix: Smooth HR transitions

This commit is contained in:
GABRIEL Sebastian 2026-11-12 22:49:12 +01:00
commit 2c0dc760d9
8 changed files with 311 additions and 38 deletions

View file

@ -197,12 +197,11 @@ pub struct Simulator {
respiratory_phase: f64,
ecg_beat_number: u64,
conducted_qrs: VecDeque<ConductedBeat>, // captured QRS with respiratory beat amplitude
rate_qrs: VecDeque<f64>, // captured beats for measured PR; reset after rate changes
rate_qrs: VecDeque<f64>, // captured beats for measured PR; rolling history smooths HR changes
audio_events: VecDeque<BeatEvent>, // consumed by the 100-ms UI audio timer
filtered_pleth: f64,
previous_rhythm: EcgRhythm,
previous_conduction: PulseConduction,
previous_heart_rate: i32,
poor_signal_outages: PoorSignalOutages,
compressions: ChestCompressions,
nibp: NibpMachine,
@ -224,7 +223,6 @@ impl Simulator {
filtered_pleth: 0.0,
previous_rhythm: params.ecg_rhythm,
previous_conduction: params.pulse_conduction,
previous_heart_rate: params.heart_rate,
poor_signal_outages: PoorSignalOutages::new(),
compressions: ChestCompressions::new(),
nibp: NibpMachine::new(),
@ -427,12 +425,12 @@ impl Simulator {
self.previous_rhythm = params.ecg_rhythm;
self.previous_conduction = params.pulse_conduction;
}
if params.heart_rate != self.previous_heart_rate {
// Old captured events remain in the *waveforms* until they pass.
// They must not bias the pulse rate after an HR slider change.
self.rate_qrs.clear();
self.previous_heart_rate = params.heart_rate;
}
// Keep recent mechanically captured beats when only HR changes.
// The rolling three-interval PR calculation then converges naturally
// to the new rate as new pulses arrive. Clearing this history on every
// slider update would briefly make PR unavailable and falsely trigger
// SpO2 NO READING, even though perfusion and the sensor are intact.
// Actual loss of capture is still handled above, immediately.
// Respiratory phase drives both the capnogram and arterial PPV.
let respiratory_rate = params.respiratory_rate.max(0) as f64 / 60.0;
@ -1181,12 +1179,55 @@ mod tests {
assert_eq!(sim.pulse_rate(), Some(75));
p.heart_rate = 189;
sim.step(&p);
// No old 75 bpm PR is displayed while the new beat interval is measured.
assert_eq!(sim.pulse_rate(), None);
for _ in 0..200 { sim.step(&p); }
// A new HR must not invalidate existing mechanical pulse history.
// The displayed PR is smoothed from actual conducted QRS intervals.
assert_eq!(sim.pulse_rate(), Some(75));
assert_eq!(sim.spo2_status(&p), Spo2Status::Available);
for _ in 0..800 {
sim.step(&p);
assert!(sim.pulse_rate().is_some(), "PR must remain available");
assert_eq!(sim.spo2_status(&p), Spo2Status::Available);
assert!(sim.samples.back().unwrap().pleth_valid,
"no artificial gap in the pleth trace during HR adjustment");
}
assert_eq!(sim.pulse_rate(), Some(189));
}
#[test]
fn repeated_heart_rate_slider_moves_do_not_trigger_spo2_no_reading() {
let mut p = Parameters::default();
let mut sim = Simulator::new(&p);
for rate in [90, 105, 120, 140, 110, 80, 75] {
p.heart_rate = rate;
for _ in 0..40 { // only 160 ms between successive slider updates
sim.step(&p);
assert!(sim.pulse_rate().is_some());
assert_eq!(sim.spo2_status(&p), Spo2Status::Available);
assert!(sim.samples.back().unwrap().pleth_valid);
}
}
}
#[test]
fn conducted_two_to_one_pulses_remain_available_during_hr_change() {
let mut p = Parameters { pulse_conduction: PulseConduction::TwoToOne,
..Parameters::default() };
let mut sim = Simulator::new(&p);
assert_eq!(sim.pulse_rate(), Some(38));
p.heart_rate = 150;
for _ in 0..1200 {
sim.step(&p);
assert!(sim.pulse_rate().is_some());
assert_eq!(sim.spo2_status(&p), Spo2Status::Available);
}
assert_eq!(sim.pulse_rate(), Some(75));
// Genuine mechanical capture loss still produces NO READING immediately.
p.pulse_conduction = PulseConduction::None;
sim.step(&p);
assert_eq!(sim.pulse_rate(), None);
assert_eq!(sim.spo2_status(&p), Spo2Status::NoReading);
}
#[test]
fn new_rate_respects_two_to_one_mechanical_capture() {
let mut p = Parameters::default();