packages

signal::spectral::psd

On this page

Power spectral density by averaged modified periodograms (Welch 1967): windowed, detrended, overlapping segments, each a real FFT, their squared magnitudes averaged. Variance falls with the segment count; resolution is fs / nperseg.

Structs

SpectrumSource

pub struct Spectrum

A one-sided spectrum: p[k] is the power at frequency f[k].

  • pub f: Vector<f64>§

    Source
  • pub p: Vector<f64>§

    Source
  • pub fn peak(self) -> f64§

    Source

    The frequency of the largest bin.

    nabi
    use core::array::Array;
    
    let x = Array::from_fn([1000], |i: [usize; 1]| -> f64 { (2.0 * f64::PI * 50.0 * (i[0] as f64) / 1000.0).sin() });
    let s = try signal::spectral::welch(x, fs: 1000.0);
    println(s.peak()); // 50.78.. (the nearest bin, 1000 / 256 apart)
    

Enums

ScalingSource

pub enum Scaling

What the estimate is scaled to.

  • Density§

    Source

    Power per unit frequency (V^2 / Hz): integrates to the variance.

  • Power per bin (V^2): a sinusoid of amplitude A on a bin reads A^2 / 2.

Functions

welchSource

pub fn welch(x: some RealSamples, fs: f64 = 1.0, window: Window = Window::Hann, nperseg: usize = 256, noverlap: usize? = none, nfft: usize? = none, detrend: Detrend? = Detrend::Constant, scaling: Scaling = Scaling::Density) throws (SignalError) -> Spectrum

Welch's power spectral density estimate of x sampled at fs: segments of nperseg samples (256 by default, at most the signal length), overlapping by noverlap (half by default), each detrended (Detrend::Constant by default, none for no detrending), windowed (periodic Hann by default) and zero-padded to nfft. As SciPy's welch.

nabi
use core::array::Array;
use signal::spectral::{welch, Scaling};

let x = Array::from_fn([4096], |i: [usize; 1]| -> f64 { 2.0 * (f64::PI * (i[0] as f64) / 4.0).sin() });
let s = try welch(x, fs: 8.0, nperseg: 64, scaling: Scaling::Spectrum);
println(s.peak()); // 1.0

periodogramSource

pub fn periodogram(x: some RealSamples, fs: f64 = 1.0, window: Window = Window::Boxcar, nfft: usize? = none, detrend: Detrend? = Detrend::Constant, scaling: Scaling = Scaling::Density) throws (SignalError) -> Spectrum

The periodogram of x: one segment, the whole signal, no window by default. Its bins are noisy (each is chi-squared with 2 degrees of freedom); welch trades resolution for variance.

nabi
use core::array::Vector;
use signal::spectral::{periodogram, Scaling};

let x: Vector<f64> = [1, -1, 1, -1];
let s = try periodogram(x, scaling: Scaling::Spectrum);
println(s.p); // [0.0, 0.0, 1.0]