From d0fcc9878e8a5e27c7d75b9ed7379b06f7c6888c Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 8 May 2022 00:47:20 +0200 Subject: [PATCH] Add a Blackman window function --- src/util/window.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/util/window.rs b/src/util/window.rs index f2004dc1..638e8e7c 100644 --- a/src/util/window.rs +++ b/src/util/window.rs @@ -2,6 +2,29 @@ use std::f32; +/// A Blackman window function with the 'standard' coefficients. +/// +/// +pub fn blackman(size: usize) -> Vec { + let mut window = vec![0.0; size]; + blackman_in_place(&mut window); + + window +} + +/// The same as [`blackman()`], but filling an existing slice instead. +pub fn blackman_in_place(window: &mut [f32]) { + let size = window.len(); + + let scale_1 = (2.0 * f32::consts::PI) / (size - 1) as f32; + let scale_2 = scale_1 * 2.0; + for (i, sample) in window.iter_mut().enumerate() { + let cos_1 = (scale_1 * i as f32).cos(); + let cos_2 = (scale_2 * i as f32).cos(); + *sample = 0.42 - (0.5 * cos_1) + (0.08 * cos_2); + } +} + /// A Hann window function. /// ///