use crate::Vec1;
impl Vec1<f64> {
pub fn hann(n: usize) -> Self {
assert!(n >= 2, "hann: window length must be >= 2");
let mut out = Self::with_capacity(n);
let denom = (n - 1) as f64;
for i in 0..n {
let x = 2.0 * core::f64::consts::PI * i as f64 / denom;
out.push(0.5 * (1.0 - libm::cos(x)));
}
out
}
pub fn hamming(n: usize) -> Self {
assert!(n >= 2, "hamming: window length must be >= 2");
let mut out = Self::with_capacity(n);
let denom = (n - 1) as f64;
for i in 0..n {
let x = 2.0 * core::f64::consts::PI * i as f64 / denom;
out.push(0.54 - 0.46 * libm::cos(x));
}
out
}
pub fn blackman(n: usize) -> Self {
assert!(n >= 2, "blackman: window length must be >= 2");
let mut out = Self::with_capacity(n);
let denom = (n - 1) as f64;
for i in 0..n {
let x = 2.0 * core::f64::consts::PI * i as f64 / denom;
out.push(0.42 - 0.5 * libm::cos(x) + 0.08 * libm::cos(2.0 * x));
}
out
}
pub fn rectangular(n: usize) -> Self {
let mut out = Self::with_capacity(n);
for _ in 0..n {
out.push(1.0);
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hann_basic() {
let w = Vec1::hann(5);
assert_eq!(w.len(), 5);
assert!((w[1] - 0.0).abs() < 1e-10);
assert!((w[3] - 1.0).abs() < 1e-10);
assert!((w[5] - 0.0).abs() < 1e-10);
}
#[test]
fn hamming_basic() {
let w = Vec1::hamming(5);
assert_eq!(w.len(), 5);
assert!((w[1] - 0.08).abs() < 1e-10);
assert!((w[3] - 1.0).abs() < 1e-10);
}
#[test]
fn blackman_basic() {
let w = Vec1::blackman(5);
assert_eq!(w.len(), 5);
assert!((w[1] - 0.0).abs() < 1e-10);
assert!((w[5] - 0.0).abs() < 1e-10);
}
#[test]
fn rectangular_basic() {
let w = Vec1::rectangular(4);
assert_eq!(w.as_slice(), &[1.0, 1.0, 1.0, 1.0]);
}
#[test]
#[should_panic(expected = "window length must be >= 2")]
fn hann_too_short_panics() {
let _ = Vec1::hann(1);
}
}