Skip to main content

qualia_audio/features/pitch/
confidence.rs

1//! Map a YIN aperiodicity (the CMND value at the selected lag) to a voicing
2//! confidence in `[0, 1]`, delegated to [`etalon_signal::pitch::yin_pitch_confidence`].
3
4/// Voicing confidence from the CMND minimum `aperiodicity`.
5#[inline]
6pub fn pitch_confidence(aperiodicity: f32) -> f32 {
7    etalon_signal::pitch::yin_pitch_confidence(aperiodicity)
8}
9
10#[cfg(test)]
11mod tests {
12    use super::*;
13
14    #[test]
15    fn clean_tone_is_confident() {
16        assert!(pitch_confidence(0.01) > 0.95, "{}", pitch_confidence(0.01));
17        assert!(pitch_confidence(0.001) > 0.99);
18    }
19
20    #[test]
21    fn noise_is_unconfident() {
22        assert!(pitch_confidence(0.8) < 0.5, "{}", pitch_confidence(0.8));
23        assert!(pitch_confidence(1.0) <= 0.0 + 1e-6);
24    }
25
26    #[test]
27    fn monotonic_decreasing() {
28        let mut prev = pitch_confidence(0.0);
29        for i in 1..=10 {
30            let c = pitch_confidence(i as f32 / 10.0);
31            assert!(c <= prev + 1e-6, "not monotone at {i}: {c} > {prev}");
32            prev = c;
33        }
34    }
35
36    #[test]
37    fn non_finite_is_zero() {
38        assert_eq!(pitch_confidence(f32::NAN), 0.0);
39        assert_eq!(pitch_confidence(f32::INFINITY), 0.0);
40    }
41}