lib_jsl_dsp/discrete/windows/
tukey.rs1use std::f64::consts::PI;
2
3use super::{boxcar::boxcar, extend, hann::hann, len_guard, truncate};
4
5pub fn tukey(m: usize, alpha: f64, sym: bool) -> Vec<f64> {
7 if let Some(w) = len_guard(m) {
8 return w;
9 }
10 if alpha <= 0.0 {
11 return boxcar(m, sym);
12 }
13 if alpha >= 1.0 {
14 return hann(m, sym);
15 }
16 let (m2, needs_trunc) = extend(m, sym);
17 let mut w = vec![0.0; m2];
18 let denom = m2 as f64 - 1.0;
19 for (n, wn) in w.iter_mut().enumerate() {
20 let x = n as f64 / denom;
21 *wn = if x < alpha / 2.0 {
22 0.5 * (1.0 + (2.0 * PI / alpha * (x - alpha / 2.0)).cos())
23 } else if x <= 1.0 - alpha / 2.0 {
24 1.0
25 } else {
26 0.5 * (1.0 + (2.0 * PI / alpha * (x - 1.0 + alpha / 2.0)).cos())
27 };
28 }
29 truncate(w, needs_trunc)
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35 use crate::discrete::windows::test_support::assert_window;
36
37 #[test]
38 fn test_tukey() {
39 assert_window(include_str!("../test_data/tukey.csv"), tukey(7, 0.5, true));
40 }
41}