Skip to main content

lib_jsl_dsp/discrete/windows/
lanczos.rs

1use crate::discrete::sinc::sinc;
2
3use super::{extend, len_guard, truncate};
4
5/// The Lanczos window is the central lobe of the sinc function, `sinc(2n/(M-1) - 1)`, giving a smooth taper that is zero-crossing at the edges.
6pub fn lanczos(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 = 2.0 * n as f64 / (m2 as f64 - 1.0) - 1.0;
14        *wn = sinc(x);
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_lanczos() {
26        assert_window(include_str!("../test_data/lanczos.csv"), lanczos(7, true));
27    }
28}