Skip to main content

qualia_audio/features/mel/
bfcc.rs

1//! `bfcc` — Bark-Frequency Cepstral Coefficients: Bark bank → log → DCT-II.
2//!
3//! Same cepstral core as [`super::mfcc::mfcc`], driven by a Bark bank built with
4//! [`super::bark_bank::build_bark_bank`] instead of a mel bank. Kept as a distinct
5//! entry point (perceptually a different frequency warping) that reuses the MFCC
6//! pipeline rather than duplicating it.
7
8use crate::features::mel::mfcc::mfcc;
9use crate::types::AudioError;
10
11/// Compute the first `n_coeffs` Bark cepstral coefficients of one power-spectrum
12/// frame. `bark_weights` is an `n_bands × n_fft_bins` bank from
13/// [`super::bark_bank::build_bark_bank`]. See [`super::mfcc::mfcc`] for buffer
14/// requirements (`out` ≥ `n_coeffs`, `scratch` ≥ `2 * n_bands`); the pipeline is
15/// bank → `ln` → orthonormal DCT-II → first `n_coeffs`.
16pub fn bfcc(
17    power_spectrum: &[f32],
18    bark_weights: &[f32],
19    n_bands: usize,
20    n_coeffs: usize,
21    out: &mut [f32],
22    scratch: &mut [f32],
23) -> Result<(), AudioError> {
24    mfcc(
25        power_spectrum,
26        bark_weights,
27        n_bands,
28        n_coeffs,
29        out,
30        scratch,
31    )
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::features::mel::bark_bank::build_bark_bank;
38
39    #[test]
40    fn bfcc_c0_dominates_for_single_tone() {
41        let n_bins = 257;
42        let n_bands = 24;
43        let n_coeffs = 13;
44        let sr = 16_000.0;
45        let mut bank = vec![0.0f32; n_bands * n_bins];
46        build_bark_bank(n_bins, n_bands, sr, 0.0, 8000.0, &mut bank).unwrap();
47
48        let mut power = vec![0.0f32; n_bins];
49        power[64] = 100.0;
50
51        let mut out = vec![0.0f32; n_coeffs];
52        let mut scratch = vec![0.0f32; 2 * n_bands];
53        bfcc(&power, &bank, n_bands, n_coeffs, &mut out, &mut scratch).unwrap();
54
55        let c0 = out[0].abs();
56        for (k, &c) in out.iter().enumerate().skip(1) {
57            assert!(
58                c0 > c.abs(),
59                "c0 {c0} does not dominate coeff {k} ({})",
60                c.abs()
61            );
62        }
63    }
64}