qualia_vision/biosense/respiration/rr_estimate.rs
1//! Shared respiratory-rate estimate (no training; spectral only).
2
3/// Breaths-per-minute estimate with honest SNR-derived confidence.
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct RrEstimate {
6 /// Breaths per minute (clinical-style rate).
7 pub breaths_per_min: f32,
8 /// Peak / mean-band power ratio in the respiratory DFT band.
9 pub snr: f32,
10 /// Confidence in \[0, 1\], derived from SNR; not calibrated clinical accuracy.
11 pub confidence: f32,
12}
13
14/// Adult-ish respiratory search band: 0.10–0.50 Hz → 6–30 breaths/min.
15pub const RR_F_LO_HZ: f32 = 0.10;
16pub const RR_F_HI_HZ: f32 = 0.50;
17/// Minimum samples for a usable low-frequency peak (~2 s @ 30 fps is still weak; 64 is floor).
18pub const RR_MIN_SAMPLES: usize = 64;
19/// Spectral bins across the RR band.
20pub const RR_SPECTRAL_STEPS: usize = 96;
21/// SNR below this → abstain (fail closed) when a gate is applied.
22pub const RR_MIN_SNR_DEFAULT: f32 = 3.0;
23/// Map SNR → confidence; conf ≈ 1 when snr ≥ this.
24pub const RR_SNR_FOR_FULL_CONF: f32 = 18.0;
25
26#[inline]
27/// SNR to confidence.
28pub fn snr_to_confidence(snr: f32) -> f32 {
29 (snr / RR_SNR_FOR_FULL_CONF).clamp(0.0, 1.0)
30}