Skip to main content

lib_jsl_dsp/discrete/windows/
parzen.rs

1use super::{extend, len_guard, linspace, truncate};
2
3/// The Parzen window is a piecewise cubic B-spline taper: a cubic polynomial near the center and a cubed linear falloff toward the edges, with strictly non-negative coefficients.
4pub fn parzen(m: usize, sym: bool) -> Vec<f64> {
5    if let Some(w) = len_guard(m) {
6        return w;
7    }
8    let (m2, needs_trunc) = extend(m, sym);
9    let n = linspace(-(m2 as f64 - 1.0) / 2.0, (m2 as f64 - 1.0) / 2.0, m2);
10    let mut w = vec![0.0; m2];
11    for (i, ni) in n.iter().enumerate() {
12        let an = ni.abs();
13        let h = m2 as f64 / 2.0;
14        w[i] = if an <= (m2 as f64 - 1.0) / 4.0 {
15            1.0 - 6.0 * (an / h).powi(2) + 6.0 * (an / h).powi(3)
16        } else {
17            2.0 * (1.0 - an / h).powi(3)
18        };
19    }
20    truncate(w, needs_trunc)
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use crate::discrete::windows::test_support::assert_window;
27
28    #[test]
29    fn test_parzen() {
30        assert_window(include_str!("../test_data/parzen.csv"), parzen(7, true));
31    }
32}