Skip to main content

qualia_audio/features/window/
blackman.rs

1//! Blackman window (periodic / DFT-even convention).
2
3use crate::types::AudioError;
4
5/// Fill `out` with a Blackman window of length `out.len()`.
6///
7/// Periodic convention using the classic (unexact) coefficients:
8/// `w[n] = a0 − a1·cos(2πn/N) + a2·cos(4πn/N)` with `a0 = 0.42`, `a1 = 0.5`,
9/// `a2 = 0.08` and denominator `N = out.len()`. Lower side lobes than Hann at
10/// the cost of a wider main lobe.
11///
12/// Returns [`AudioError::InvalidParameter`] if `out` is empty.
13#[inline]
14pub fn blackman_window(out: &mut [f32]) -> Result<(), AudioError> {
15    etalon_signal::window::blackman_window(out).map_err(|_| AudioError::InvalidParameter)
16}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21
22    #[test]
23    fn edge_near_zero_centre_one() {
24        let mut w = vec![0.0f32; 64];
25        blackman_window(&mut w).unwrap();
26        // w[0] = a0 - a1 + a2 = 0.0.
27        assert!(w[0].abs() < 1e-4, "w[0] = {}", w[0]);
28        assert!((w[32] - 1.0).abs() < 1e-4, "centre = {}", w[32]);
29    }
30
31    #[test]
32    fn rejects_empty() {
33        let mut w: [f32; 0] = [];
34        assert_eq!(blackman_window(&mut w), Err(AudioError::InvalidParameter));
35    }
36}