Skip to main content

FrequencySpectrum

Struct FrequencySpectrum 

Source
pub struct FrequencySpectrum { /* private fields */ }
Expand description

Convenient wrapper around the processed FFT result.

This is the result produced by samples_fft_to_spectrum. It describes each frequency and its corresponding value (magnitude) from the analyzed samples, according to the provided input parameters. The data is scaled/normalized according to the optionally applied scaling function.

Unless frequencies were explicitly excluded, the spectrum covers the full range from the DC component (0 Hz) up to the Nyquist frequency with the frequency resolution derived from the input data.

Implementations§

Source§

impl FrequencySpectrum

Source

pub fn apply_scaling_fn( &mut self, scaling_fn: &SpectrumScalingFunction, ) -> Result<(), SpectrumAnalyzerError>

Applies the function scaling_fn to each element and updates several metrics about the spectrum, such as min and max, afterwards accordingly. It ensures that no value is NaN or Infinity (regarding IEEE-754) after scaling_fn was applied. Otherwise, SpectrumAnalyzerError::ScalingError is returned.

§Parameters
Source

pub const fn average(&self) -> FrequencyValue

Returns the average frequency value of the spectrum.

Source

pub const fn max(&self) -> (Frequency, FrequencyValue)

Returns the maximum (frequency, frequency value)-pair of the spectrum regarding the frequency value.

Source

pub const fn min(&self) -> (Frequency, FrequencyValue)

Returns the minimum (frequency, frequency value)-pair of the spectrum regarding the frequency value.

Source

pub fn range(&self) -> FrequencyValue

Returns FrequencySpectrum::max().1 subtracted by FrequencySpectrum::min().1, i.e. the range of the frequency values (not the frequencies itself, but their values).

Source

pub fn data(&self) -> &[(Frequency, FrequencyValue)]

Returns the underlying sorted data.

Source

pub const fn frequency_resolution(&self) -> Frequency

Returns the frequency resolution of this spectrum.

Source

pub const fn samples_len(&self) -> u32

Returns the number of samples used to obtain this spectrum.

Source

pub fn max_fr(&self) -> Frequency

Getter for the highest frequency that is captured inside this spectrum. Shortcut for spectrum.data()[spectrum.data().len() - 1].0. This corresponds to the crate::limit::FrequencyLimit of the spectrum.

This method could return the Nyquist frequency, if there was no Frequency limit while obtaining the spectrum.

Source

pub fn min_fr(&self) -> Frequency

Getter for the lowest frequency that is captured inside this spectrum. Shortcut for spectrum.data()[0].0. This corresponds to the crate::limit::FrequencyLimit of the spectrum.

This method could return the DC component, see Self::dc_component.

Source

pub fn dc_component(&self) -> Option<FrequencyValue>

Returns the DC Component or also called DC bias which corresponds to the FFT result at index 0 which corresponds to 0Hz. This is only present if the frequencies were not limited to for example 100 <= f <= 10000 when the libraries main function was called.

Note that the unscaled value is N times the mean of the (windowed) samples, not the mean itself. See crate::samples_fft_to_spectrum.

More information: https://dsp.stackexchange.com/questions/12972/discrete-fourier-transform-what-is-the-dc-term-really

Excerpt: As far as practical applications go, the DC or 0 Hz term is not particularly useful. In many cases it will be close to zero, as most signal processing applications will tend to filter out any DC component at the analogue level. In cases where you might be interested it can be calculated directly as an average in the usual way, without resorting to a DFT/FFT. - Paul R.

Source

pub fn freq_val_exact(&self, search_fr: f32) -> Option<FrequencyValue>

Returns the value of the given frequency from the spectrum either exactly or approximated.

If the value is out of bounds, the function returns None.

If search_fr is not exactly given in the spectrum, i.e. due to the Self::frequency_resolution, this function takes the two closest neighbors/points (A, B), put a linear function through them and calculates the point C in the middle. This is done by the private function calculate_y_coord_between_points.

The interpolated value only follows the shape of the spectrum. It is not the value a sine wave of exactly search_fr would have, because such a sine wave leaks into the neighboring bins.

§Parameters
  • search_fr The frequency of that you want the value in the spectrum.
Source

pub fn freq_val_closest( &self, search_fr: f32, ) -> Option<(Frequency, FrequencyValue)>

Returns the frequency closest to parameter search_fr in the spectrum.

If the value is out of bounds, the function returns None.

For example, if the spectrum looks like this:

Vector:    [0]      [1]      [2]      [3]
Frequency  100 Hz   200 Hz   300 Hz   400 Hz
Fr Value   0.0      1.0      0.5      0.1

then get_frequency_value_closest(320) will return (300.0, 0.5).

§Parameters
  • search_fr The frequency of that you want the value in the spectrum.
Source

pub fn to_vec(&self) -> Vec<(f32, f32)>

Returns a sorted Vec with all value pairs as f32.

Trait Implementations§

Source§

impl Debug for FrequencySpectrum

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.