Skip to main content

lib_jsl_dsp/discrete/windows/
cosine.rs

1use std::f64::consts::PI;
2
3use super::{extend, len_guard, truncate};
4
5/// The cosine window is defined by a specific mathematical formula that involves the sine function. The coefficients are computed based on the position within the window and the total length, with special handling for symmetric and periodic windows to ensure proper shape and spectral properties.
6pub fn cosine(m: usize, sym: bool) -> Vec<f64> {
7    if let Some(w) = len_guard(m) {
8        return w;
9    }
10    let (m2, needs_trunc) = extend(m, sym);
11    let mut w = vec![0.0; m2];
12    for (i, wn) in w.iter_mut().enumerate() {
13        *wn = (PI / m2 as f64 * (i as f64 + 0.5)).sin();
14    }
15    truncate(w, needs_trunc)
16}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21    use crate::discrete::windows::test_support::assert_window;
22
23    #[test]
24    fn test_cosine() {
25        assert_window(include_str!("../test_data/cosine.csv"), cosine(7, true));
26    }
27}