Skip to main content

qualia_audio/features/window/
hamming.rs

1//! Hamming window (periodic / DFT-even convention).
2
3use crate::types::AudioError;
4
5/// Fill `out` with a Hamming window of length `out.len()`.
6///
7/// Periodic convention `w[n] = a0 − a1·cos(2πn/N)` with `a0 = 0.54`,
8/// `a1 = 0.46` and denominator `N = out.len()`. Unlike Hann it does not reach
9/// zero at the edges (`w[0] = a0 − a1 = 0.08`), which is the defining property
10/// of the Hamming taper.
11///
12/// Returns [`AudioError::InvalidParameter`] if `out` is empty.
13#[inline]
14pub fn hamming_window(out: &mut [f32]) -> Result<(), AudioError> {
15    etalon_signal::window::hamming_window(out).map_err(|_| AudioError::InvalidParameter)
16}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21
22    #[test]
23    fn edge_and_centre_values() {
24        let mut w = vec![0.0f32; 64];
25        hamming_window(&mut w).unwrap();
26        assert!((w[0] - 0.08).abs() < 1e-4, "w[0] = {}", w[0]);
27        assert!((w[32] - 1.0).abs() < 1e-4, "centre = {}", w[32]);
28    }
29
30    #[test]
31    fn rejects_empty() {
32        let mut w: [f32; 0] = [];
33        assert_eq!(hamming_window(&mut w), Err(AudioError::InvalidParameter));
34    }
35}