diff --git a/src/util/window.rs b/src/util/window.rs index 0d8b60de..12423024 100644 --- a/src/util/window.rs +++ b/src/util/window.rs @@ -17,6 +17,18 @@ pub fn hann(size: usize) -> Vec { .collect() } +/// The same as [`hann()`], but filling an existing slice instead. +pub fn hann_in_place(window: &mut [f32]) { + let size = window.len(); + + // We want to scale `[0, size - 1]` to `[0, pi]`. + let scale = (size as f32 - 1.0).recip() * f32::consts::TAU; + for (i, sample) in window.iter_mut().enumerate() { + let cos = (i as f32 * scale).cos(); + *sample = 0.5 - (0.5 * cos) + } +} + /// Multiply a buffer with a window function. #[inline] pub fn multiply_with_window(buffer: &mut [f32], window_function: &[f32]) {