Skip to main content

lib_jsl_dsp/discrete/windows/
bartlett.rs

1use super::{extend, len_guard, truncate};
2
3/// The Bartlett window, also known as the triangular window, is defined by a linear tapering of the coefficients from the center to the edges. The coefficients are computed based on the position within the window and the total length, with special handling for even and odd lengths to ensure proper symmetry.
4pub fn bartlett(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 mut w = vec![0.0; m2];
10    let alpha = (m2 as f64 - 1.0) / 2.0;
11    for (n, wn) in w.iter_mut().enumerate() {
12        *wn = 1.0 - ((n as f64 - alpha) / alpha).abs();
13    }
14    truncate(w, needs_trunc)
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    use crate::discrete::windows::test_support::assert_window;
21
22    #[test]
23    fn test_bartlett() {
24        assert_window(include_str!("../test_data/bartlett.csv"), bartlett(7, true));
25    }
26}