1
0
Fork 0

Add an in-place version of the Hann function

This commit is contained in:
Robbert van der Helm 2022-03-07 20:52:37 +01:00
parent 81308d0c8d
commit 17f3c6cc6c

View file

@ -17,6 +17,18 @@ pub fn hann(size: usize) -> Vec<f32> {
.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]) {