Skip to main content

lib_jsl_dsp/discrete/windows/
barthann.rs

1use std::f64::consts::PI;
2
3use super::{extend, len_guard, truncate};
4
5/// The modified Bartlett-Hann window combines a linear taper with a cosine taper (`0.62 - 0.48|x - 0.5| - 0.38 cos(2*pi*x)` for `x` in `[0, 1]`), giving lower side lobes than the plain Bartlett window.
6pub fn barthann(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 (n, wn) in w.iter_mut().enumerate() {
13        let x = n as f64 / (m2 as f64 - 1.0);
14        *wn = 0.62 - 0.48 * (x - 0.5).abs() - 0.38 * (2.0 * PI * x).cos();
15    }
16    truncate(w, needs_trunc)
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22    use crate::discrete::windows::test_support::assert_window;
23
24    #[test]
25    fn test_barthann() {
26        assert_window(include_str!("../test_data/barthann.csv"), barthann(7, true));
27    }
28}