Skip to main content

harmos_storage/signal/spectrum/
mod.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12#[cfg(feature = "fft")]
13mod bounded;
14mod decibel;
15#[cfg(feature = "fft")]
16mod fourier;
17mod harmonic;
18mod indicator;
19#[cfg(feature = "fft")]
20mod spilled;
21mod taper;
22
23/* === Re-Exports === */
24
25#[cfg(feature = "fft")]
26pub use bounded::fft_in_place;
27pub use decibel::{Decibel, Scale};
28#[cfg(feature = "fft")]
29pub use fourier::{Estimate, Welch, reconstruct, transform};
30pub use harmonic::{fundamental, harmonics, peaks, thd};
31pub use indicator::{band_energy, band_rms, centroid, occupied_bandwidth, papr, rolloff};
32#[cfg(feature = "fft")]
33pub use spilled::FftBuffer;
34pub use taper::{Band, Taper};
35
36/* === Imports === */
37
38// Standard Library Imports
39
40// Third Party Imports
41
42// Local Imports
43
44/* === Constants === */
45
46/* === Macros === */
47
48/* === Type Aliases === */
49
50/* === Structs === */
51
52#[derive(Clone, Debug, PartialEq)]
53pub struct Spectrum {
54    pub step: f64,
55    pub real: Vec<f64>,
56    pub imag: Vec<f64>,
57}
58
59/* === Enums === */
60
61/* === Traits === */
62
63/* === Implementations === */
64
65impl Spectrum {
66    #[must_use]
67    pub fn rate(&self) -> f64 {
68        match self.real.len() {
69            0 | 1 => 0.0,
70            bins => self.step * ((bins - 1) * 2) as f64,
71        }
72    }
73}
74
75/* === Public Functions === */
76
77#[must_use]
78pub fn magnitude(real: &[f64], imag: &[f64]) -> Vec<f64> {
79    real.iter().zip(imag).map(|(real, imag)| real.hypot(*imag)).collect()
80}
81
82#[must_use]
83pub fn phase(real: &[f64], imag: &[f64]) -> Vec<f64> {
84    real.iter().zip(imag).map(|(real, imag)| imag.atan2(*real)).collect()
85}
86
87#[must_use]
88pub fn bins(step: f64, count: usize) -> Vec<f64> {
89    (0..count).map(|index| index as f64 * step).collect()
90}
91
92/* === Private Functions === */
93
94/* === Tests === */