Skip to main content

qualia_audio/features/mel/
mel_to_hz.rs

1//! `mel_to_hz` — mel → Hertz, the inverse of [`super::hz_mel::hz_to_mel`] (HTK).
2
3/// Convert a value on the HTK mel scale back to Hertz.
4///
5/// Inverse of `2595 * log10(1 + hz/700)`, i.e. `hz = 700 * (10^(mel/2595) - 1)`.
6/// Round-trips `hz_to_mel` to within ~1e-3 relative error over the audible band.
7#[inline]
8pub fn mel_to_hz(mel: f32) -> f32 {
9    etalon_signal::mel::mel_to_hz(mel)
10}
11
12#[cfg(test)]
13mod tests {
14    use super::*;
15    use crate::features::mel::hz_mel::hz_to_mel;
16
17    #[test]
18    fn round_trip_within_tolerance() {
19        for &hz in &[50.0f32, 200.0, 440.0, 1000.0, 4000.0, 8000.0, 16000.0] {
20            let back = mel_to_hz(hz_to_mel(hz));
21            let rel = (back - hz).abs() / hz;
22            assert!(rel < 1e-3, "round-trip {hz} -> {back} (rel {rel})");
23        }
24    }
25
26    #[test]
27    fn dc_maps_to_dc() {
28        assert!(mel_to_hz(0.0).abs() < 1e-4);
29    }
30}