lasprs 0.14.0

Library for Acoustic Signal Processing (Rust edition, with optional Python bindings via pyo3)
use crate::{config::*, filter::ZPKModel};
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter, EnumMessage};
/// Sound level frequency weighting type (A, C, Z)
#[derive(Copy, Display, Debug, EnumMessage, Default, Clone, PartialEq, EnumIter)]
#[cfg_attr(
    feature = "python-bindings",
    gen_stub_pyclass_enum,
    pyclass(eq, eq_int, from_py_object)
)]
pub enum FreqWeighting {
    /// A-weighting
    A,
    /// C-weighting
    C,
    /// Z-weighting, or no weighting
    #[default]
    Z,
}
impl FreqWeighting {
    #[inline]
    /// Calculate the frequency weighting for the given frequency array,
    /// corresponding to the frequency weighting. So it is the magnitude of the
    /// frequency response corresponding to the A or C weighting filter. This is
    /// linear frequency weighting, to correct power spectra with it, please use
    /// the square of these weights.
    ///
    /// # Arguments
    /// * `freq` - The frequency array to calculate the weighting for.
    ///
    /// # Returns
    /// An array of weights corresponding to the frequency array.
    pub fn linearweight(&self, freq: &[Flt]) -> Array1<Flt> {
        use crate::filter::TransferFunction;
        let fw = ZPKModel::freqWeightingFilter(*self);
        fw.tf(1.0.try_into().unwrap(), freq).map(|g| Cflt::abs(*g))
    }

    /// Calculate the power weighting for the given frequency array,
    /// corresponding to the frequency weighting. So it is the magnitude squared of the
    /// frequency response corresponding to the A or C weighting filter.
    ///
    /// # Arguments
    /// * `freq` - The frequency array to calculate the weighting for.
    ///
    /// # Returns
    /// An array of power weights corresponding to the frequency array.
    pub fn powerweight(&self, freq: &[Flt]) -> Array1<Flt> {
        use crate::filter::TransferFunction;
        let fw = ZPKModel::freqWeightingFilter(*self);
        fw.tf(1.0.try_into().unwrap(), freq).map(Cflt::norm_sqr)
    }
}

#[cfg(feature = "python-bindings")]
#[cfg_attr(feature = "python-bindings", gen_stub_pymethods, pymethods)]
impl FreqWeighting {
    #[staticmethod]
    fn all() -> Vec<Self> {
        Self::iter().collect()
    }
    fn __str__(&self) -> String {
        format!("{self}-weighting")
    }
    fn letter(&self) -> String {
        format!("{self}")
    }
    #[staticmethod]
    #[pyo3(name = "default")]
    fn default_py() -> Self {
        Self::default()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test() {
        let a = FreqWeighting::A;
        let c = FreqWeighting::C;
        let z = FreqWeighting::Z;
        println!("A-weighting: {a}");
        println!("C-weighting: {c}");
        println!("Z-weighting: {z}");
    }
}