Skip to main content

qualia_vision/biosense/respiration/
respiration_from_motion.rs

1//! Respiratory rate proxy from vertical motion energy band (compat entry).
2//!
3//! Delegates to [`super::respiration_rate_from_motion_trace()`] with default SNR gate.
4
5use super::respiration_rate_from_motion_trace::respiration_rate_from_motion_trace;
6use super::rr_estimate::RR_MIN_SNR_DEFAULT;
7use crate::cv::error::CvError;
8
9/// `vert_motion` per-frame vertical motion scalar. Estimate breaths/min in ~6–30 band.
10///
11/// Returns `(breaths_per_min, confidence)`. Fails closed on short/noisy traces
12/// (default SNR gate inside the spectral estimator).
13pub fn respiration_from_motion(vert_motion: &[f32], fps: f32) -> Result<(f32, f32), CvError> {
14    let e = respiration_rate_from_motion_trace(vert_motion, fps, RR_MIN_SNR_DEFAULT)?;
15    Ok((e.breaths_per_min, e.confidence))
16}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21
22    #[test]
23    fn compat_finds_roughly_15_bpm() {
24        let fps = 30.0f32;
25        let n = 450usize;
26        let f = 15.0 / 60.0;
27        let mut t = vec![0.0f32; n];
28        for i in 0..n {
29            t[i] = (core::f32::consts::TAU * f * i as f32 / fps).sin();
30        }
31        let (bpm, conf) = respiration_from_motion(&t, fps).unwrap();
32        assert!((bpm - 15.0).abs() < 1.5, "bpm={}", bpm);
33        assert!(conf > 0.15, "conf={}", conf);
34    }
35}