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