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() +}