Skip to main content

qualia_audio/features/mel/
gfcc.rs

1//! `gfcc` — Gammatone-Frequency Cepstral Coefficients: ERB/gammatone bank → log →
2//! DCT-II.
3//!
4//! Same cepstral core as [`super::mfcc::mfcc`], driven by an ERB (gammatone-ish)
5//! bank built with [`super::erb_bank::build_erb_bank`]. GFCCs are the noise-robust
6//! auditory cousin of MFCCs; here they share the MFCC pipeline (bank → `ln` → DCT-II)
7//! rather than duplicating it.
8
9use crate::features::mel::mfcc::mfcc;
10use crate::types::AudioError;
11
12/// Compute the first `n_coeffs` gammatone cepstral coefficients of one
13/// power-spectrum frame. `erb_weights` is an `n_bands × n_fft_bins` bank from
14/// [`super::erb_bank::build_erb_bank`]. See [`super::mfcc::mfcc`] for buffer
15/// requirements (`out` ≥ `n_coeffs`, `scratch` ≥ `2 * n_bands`).
16pub fn gfcc(
17    power_spectrum: &[f32],
18    erb_weights: &[f32],
19    n_bands: usize,
20    n_coeffs: usize,
21    out: &mut [f32],
22    scratch: &mut [f32],
23) -> Result<(), AudioError> {
24    mfcc(power_spectrum, erb_weights, n_bands, n_coeffs, out, scratch)
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use crate::features::mel::erb_bank::build_erb_bank;
31
32    #[test]
33    fn gfcc_c0_dominates_for_single_tone() {
34        let n_bins = 257;
35        let n_bands = 24;
36        let n_coeffs = 13;
37        let sr = 16_000.0;
38        let mut bank = vec![0.0f32; n_bands * n_bins];
39        build_erb_bank(n_bins, n_bands, sr, 0.0, 8000.0, &mut bank).unwrap();
40
41        let mut power = vec![0.0f32; n_bins];
42        power[64] = 100.0;
43
44        let mut out = vec![0.0f32; n_coeffs];
45        let mut scratch = vec![0.0f32; 2 * n_bands];
46        gfcc(&power, &bank, n_bands, n_coeffs, &mut out, &mut scratch).unwrap();
47
48        let c0 = out[0].abs();
49        for (k, &c) in out.iter().enumerate().skip(1) {
50            assert!(
51                c0 > c.abs(),
52                "c0 {c0} does not dominate coeff {k} ({})",
53                c.abs()
54            );
55        }
56    }
57}