Skip to main content

qualia_audio/features/window/
blackman_harris.rs

1//! 4-term Blackman–Harris window (periodic / DFT-even convention).
2
3use crate::types::AudioError;
4
5/// Fill `out` with a 4-term Blackman–Harris window of length `out.len()`.
6///
7/// Periodic convention with the standard minimum-side-lobe coefficients
8/// (`≈ −92 dB`):
9/// `w[n] = a0 − a1·cos(2πn/N) + a2·cos(4πn/N) − a3·cos(6πn/N)`
10/// where `a0 = 0.35875`, `a1 = 0.48829`, `a2 = 0.14128`, `a3 = 0.01168` and
11/// `N = out.len()`.
12///
13/// Returns [`AudioError::InvalidParameter`] if `out` is empty.
14#[inline]
15pub fn blackman_harris_window(out: &mut [f32]) -> Result<(), AudioError> {
16    etalon_signal::window::blackman_harris_window(out).map_err(|_| AudioError::InvalidParameter)
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22
23    #[test]
24    fn edge_small_centre_one() {
25        let mut w = vec![0.0f32; 64];
26        blackman_harris_window(&mut w).unwrap();
27        // w[0] = a0 - a1 + a2 - a3 ≈ 6e-5.
28        assert!(w[0].abs() < 1e-3, "w[0] = {}", w[0]);
29        // Centre: a0 + a1 + a2 + a3 = 1.0.
30        assert!((w[32] - 1.0).abs() < 1e-4, "centre = {}", w[32]);
31    }
32
33    #[test]
34    fn rejects_empty() {
35        let mut w: [f32; 0] = [];
36        assert_eq!(
37            blackman_harris_window(&mut w),
38            Err(AudioError::InvalidParameter)
39        );
40    }
41}