Skip to main content

lib_jsl_dsp/discrete/windows/
triang.rs

1use super::{extend, len_guard, truncate};
2
3/// The triangular window is a linear taper from the center to the edges. Unlike the Bartlett window its end points are non-zero, and it handles even and odd lengths separately to remain symmetric.
4pub fn triang(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: Vec<f64> = (1..=m2.div_ceil(2)).map(|x| x as f64).collect();
10    let mut w = if m2.is_multiple_of(2) {
11        n.iter().map(|x| (2.0 * x - 1.0) / m2 as f64).collect::<Vec<_>>()
12    } else {
13        n.iter().map(|x| 2.0 * x / (m2 as f64 + 1.0)).collect::<Vec<_>>()
14    };
15    let mut tail = w.clone();
16    if m2 % 2 != 0 {
17        tail.pop();
18    }
19    tail.reverse();
20    w.extend(tail);
21    truncate(w, needs_trunc)
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27    use crate::discrete::windows::test_support::assert_window;
28
29    #[test]
30    fn test_triang() {
31        assert_window(include_str!("../test_data/triang.csv"), triang(7, true));
32    }
33}