Skip to main content

lib_jsl_dsp/discrete/windows/
general_cosine.rs

1//! The general cosine-sum window.
2//!
3//! See the book entry [Window functions](https://jwkunz.github.io/lib_jsl/book/spectral/windows.html#families).
4
5use std::f64::consts::PI;
6
7use super::{extend, len_guard, linspace, truncate};
8
9/// This function generates a general cosine window based on the specified coefficients `a` and symmetry. It is used as a building block for more specific window functions like the Blackman and Nuttall windows, which can be defined as special cases of the general cosine window with specific coefficient values.
10pub fn general_cosine(m: usize, a: &[f64], sym: bool) -> Vec<f64> {
11    if let Some(w) = len_guard(m) {
12        return w;
13    }
14    let (m2, needs_trunc) = extend(m, sym);
15    // ANCHOR: general_cosine
16    let fac = linspace(-PI, PI, m2);
17    let mut w = vec![0.0; m2];
18    for (k, ak) in a.iter().enumerate() {
19        for n in 0..m2 {
20            w[n] += ak * ((k as f64) * fac[n]).cos();
21        }
22    }
23    truncate(w, needs_trunc)
24    // ANCHOR_END: general_cosine
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use crate::discrete::windows::test_support::assert_window;
31
32    #[test]
33    fn test_general_cosine() {
34        assert_window(include_str!("../test_data/general_cosine.csv"), general_cosine(7, &[1.0, 1.942604, 1.340318, 0.440811, 0.043097], true));
35    }
36}