one-indexed-vec 0.2.0

A one-based (1-indexed) wrapper around Vec<T>: element at position N is accessed via vec[N].
Documentation
//! Window functions for spectral analysis.

use crate::Vec1;

impl Vec1<f64> {
    /// Hann window of length `n`.
    ///
    /// `w[i] = 0.5 · (1 - cos(2π · (i-1) / (n-1)))`.
    ///
    /// ```rust
    /// use one_indexed_vec::Vec1;
    /// let w = Vec1::hann(5);
    /// assert_eq!(w.len(), 5);
    /// assert!((w[1] - 0.0).abs() < 1e-10);
    /// assert!((w[5] - 0.0).abs() < 1e-10);
    /// ```
    pub fn hann(n: usize) -> Self {
        assert!(n >= 2, "hann: window length must be >= 2");
        let mut out = Self::with_capacity(n);
        let denom = (n - 1) as f64;
        for i in 0..n {
            let x = 2.0 * core::f64::consts::PI * i as f64 / denom;
            out.push(0.5 * (1.0 - libm::cos(x)));
        }
        out
    }

    /// Hamming window of length `n`.
    ///
    /// `w[i] = 0.54 - 0.46 · cos(2π · (i-1) / (n-1))`.
    ///
    /// ```rust
    /// use one_indexed_vec::Vec1;
    /// let w = Vec1::hamming(5);
    /// assert_eq!(w.len(), 5);
    /// assert!((w[1] - 0.08).abs() < 1e-10);
    /// ```
    pub fn hamming(n: usize) -> Self {
        assert!(n >= 2, "hamming: window length must be >= 2");
        let mut out = Self::with_capacity(n);
        let denom = (n - 1) as f64;
        for i in 0..n {
            let x = 2.0 * core::f64::consts::PI * i as f64 / denom;
            out.push(0.54 - 0.46 * libm::cos(x));
        }
        out
    }

    /// Blackman window of length `n`.
    ///
    /// `w[i] = 0.42 - 0.5·cos(2πi/(n-1)) + 0.08·cos(4πi/(n-1))`.
    pub fn blackman(n: usize) -> Self {
        assert!(n >= 2, "blackman: window length must be >= 2");
        let mut out = Self::with_capacity(n);
        let denom = (n - 1) as f64;
        for i in 0..n {
            let x = 2.0 * core::f64::consts::PI * i as f64 / denom;
            out.push(0.42 - 0.5 * libm::cos(x) + 0.08 * libm::cos(2.0 * x));
        }
        out
    }

    /// Rectangular (boxcar) window of length `n` — all ones.
    pub fn rectangular(n: usize) -> Self {
        let mut out = Self::with_capacity(n);
        for _ in 0..n {
            out.push(1.0);
        }
        out
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hann_basic() {
        let w = Vec1::hann(5);
        assert_eq!(w.len(), 5);
        assert!((w[1] - 0.0).abs() < 1e-10);
        assert!((w[3] - 1.0).abs() < 1e-10);
        assert!((w[5] - 0.0).abs() < 1e-10);
    }

    #[test]
    fn hamming_basic() {
        let w = Vec1::hamming(5);
        assert_eq!(w.len(), 5);
        assert!((w[1] - 0.08).abs() < 1e-10);
        assert!((w[3] - 1.0).abs() < 1e-10);
    }

    #[test]
    fn blackman_basic() {
        let w = Vec1::blackman(5);
        assert_eq!(w.len(), 5);
        // Endpoints: 0.42 - 0.5 + 0.08 = 0
        assert!((w[1] - 0.0).abs() < 1e-10);
        assert!((w[5] - 0.0).abs() < 1e-10);
    }

    #[test]
    fn rectangular_basic() {
        let w = Vec1::rectangular(4);
        assert_eq!(w.as_slice(), &[1.0, 1.0, 1.0, 1.0]);
    }

    #[test]
    #[should_panic(expected = "window length must be >= 2")]
    fn hann_too_short_panics() {
        let _ = Vec1::hann(1);
    }
}