use std::f32;
pub fn blackman(size: usize) -> Vec<f32> {
let mut window = vec![0.0; size];
blackman_in_place(&mut window);
window
}
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);
}
}
pub fn hann(size: usize) -> Vec<f32> {
let mut window = vec![0.0; size];
hann_in_place(&mut window);
window
}
pub fn hann_in_place(window: &mut [f32]) {
let size = window.len();
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)
}
}
#[inline]
pub fn multiply_with_window(buffer: &mut [f32], window_function: &[f32]) {
for (sample, window_sample) in buffer.iter_mut().zip(window_function) {
*sample *= window_sample;
}
}