Skip to main content

transform

Function transform 

Source
pub fn transform(samples: &[f64], rate: f64, taper: Taper) -> Spectrum
Expand description

Transforms real samples into one single-window spectrum.

rate is the sampling rate in Hz, and the answer covers 0..=rate / 2 in samples.len() / 2 + 1 bins. The taper’s coherent gain is divided back out and every bin but DC and Nyquist is doubled, so a tone landing on a bin reads its own amplitude whichever taper measured it — which is the property reconstruct inverts exactly.

An empty input answers an empty spectrum; there is no error to handle.

use harmos_signal::{Taper, transform};

let rate = 1000.0;
let samples: Vec<f64> = (0..1000)
    .map(|i| 3.0 * (std::f64::consts::TAU * 50.0 * f64::from(i) / rate).sin())
    .collect();

let spectrum = transform(&samples, rate, Taper::Rectangular);
let magnitude = harmos_signal::magnitude(&spectrum.real, &spectrum.imag);

// 50 Hz at a 1 Hz bin step is bin 50, and it reads the amplitude it was given.
assert!((magnitude[50] - 3.0).abs() < 1e-9);