Skip to main content

qualia_audio/features/mel/
hz_mel.rs

1//! `hz_to_mel` — Hertz → mel using the HTK convention `2595 * log10(1 + hz/700)`.
2//!
3//! Per-unit conversion delegated to [`etalon_signal::mel::hz_to_mel`].
4//! The inverse lives in the sibling file [`mod@super::mel_to_hz`] (one pub fn per file).
5
6/// Convert a frequency in Hertz to the HTK mel scale.
7///
8/// HTK convention: `mel = 2595 * log10(1 + hz / 700)`.
9///
10/// `hz_to_mel(1000.0) ≈ 1000.0` (the mel scale is anchored so that 1000 Hz maps
11/// to ≈ 1000 mel under HTK). Negative inputs are clamped to 0 Hz.
12#[inline]
13pub fn hz_to_mel(hz: f32) -> f32 {
14    etalon_signal::mel::hz_to_mel(hz)
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn mel_1000hz_is_known_htk_value() {
23        // HTK: 2595*log10(1+1000/700) = 2595*log10(2.428571...) ≈ 1000.02
24        let m = hz_to_mel(1000.0);
25        assert!((m - 1000.02).abs() < 0.1, "hz_to_mel(1000)={m}");
26    }
27
28    #[test]
29    fn mel_is_monotonic_increasing() {
30        assert!(hz_to_mel(0.0) < hz_to_mel(100.0));
31        assert!(hz_to_mel(100.0) < hz_to_mel(1000.0));
32        assert!(hz_to_mel(1000.0) < hz_to_mel(8000.0));
33    }
34
35    #[test]
36    fn mel_zero_at_dc() {
37        assert!(hz_to_mel(0.0).abs() < 1e-6);
38        // Negative clamps to DC.
39        assert!(hz_to_mel(-50.0).abs() < 1e-6);
40    }
41}