From bf215ef88e18de54396392ddd9042c880c79ca86 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 6 Mar 2022 14:41:40 +0100 Subject: [PATCH] Add a Hann function for the STFT helper --- src/util.rs | 1 + src/util/window.rs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/util/window.rs diff --git a/src/util.rs b/src/util.rs index ef8f7f67..f15dc5d7 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,7 @@ //! General conversion functions and utilities. mod stft; +pub mod window; pub use stft::StftHelper; diff --git a/src/util/window.rs b/src/util/window.rs new file mode 100644 index 00000000..2abae9e8 --- /dev/null +++ b/src/util/window.rs @@ -0,0 +1,17 @@ +//! Windowing functions, useful in conjuction with [`StftHelper`][super::StftHelper]. + +use std::f32; + +/// A Hann window function. +/// +/// +pub fn hann(size: usize) -> Vec { + // We want to scale `[0, size]` to `[0, pi]`. + let scale = (size as f32 - 1.0).recip() * f32::consts::PI; + (0..size) + .map(|i| { + let sin = (i as f32 * scale).sin(); + sin * sin + }) + .collect() +}