gspx 0.1.2

Sparse graph signal processing and spectral graph wavelets in Rust
Documentation
//! Analytical filter functions used by `gspx` convolution.
//!
//! These helpers define the scalar spectral responses used throughout the crate
//! and are useful for constructing target responses for [`crate::fit`].

use ndarray::Array1;
use num_complex::Complex64;

/// Analytical lowpass response `1 / (s * lambda + 1)`.
pub fn lowpass(x: &Array1<f64>, scale: f64) -> Array1<f64> {
    x.mapv(|value| 1.0 / (scale * value + 1.0))
}

/// Analytical highpass response `s * lambda / (s * lambda + 1)`.
pub fn highpass(x: &Array1<f64>, scale: f64) -> Array1<f64> {
    x.mapv(|value| (scale * value) / (scale * value + 1.0))
}

/// Analytical bandpass response from repeated low/high compositions.
pub fn bandpass(x: &Array1<f64>, scale: f64, order: usize) -> Array1<f64> {
    let q = 1.0 / scale;
    x.mapv(|value| ((4.0 * q * value) / (value + q).powi(2)).powi(order as i32))
}

/// Complex Morlet-like wavelet sampled at `time`.
pub fn gaussian_wavelet(
    time: &Array1<f64>,
    time_scale: f64,
    center_time: f64,
    w0: f64,
) -> Array1<Complex64> {
    let dt = if time.len() > 1 {
        time[1] - time[0]
    } else {
        1.0
    };
    let norm = (dt / time_scale) * std::f64::consts::PI.powf(-0.25);
    time.mapv(|sample_time| {
        let normalized_time = (sample_time - center_time) / time_scale;
        let gauss = (-0.5 * normalized_time * normalized_time).exp();
        let oscillation = Complex64::new(0.0, w0 * normalized_time).exp()
            - Complex64::new((-0.5 * w0 * w0).exp(), 0.0);
        oscillation * gauss * norm
    })
}