From 17f3c6cc6c68d91fee29b1ec821609808ef2338f Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Mon, 7 Mar 2022 20:52:37 +0100 Subject: [PATCH] Add an in-place version of the Hann function --- src/util/window.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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]) {