1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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}");
}
}