signal
Signal processing over core::array arrays: FFTs of any
length, convolution, windows, FIR and IIR filters, spectral
estimation and peak finding, in pure Nabi.
Modules
convConvolution and correlation, 1-d and 2-d, direct or by FFT.fftDiscrete Fourier transforms of any length: complex, real, n-d, the frequency helpers and the cosine and sine transforms built on them.filterDigital filters: FIR and IIR design, filtering (causal and zero-phase), frequency response, resampling and detrending.opsConvolution and correlation as operators:a ⊛ b(U+229B, circled asterisk) is the full linear convolution anda ⋆ b(U+22C6, star operator) the full cross-correlation, both at the level of*.peaksPeak finding as SciPy'sfind_peaks: local maxima (a flat top counts once, at its middle), then filtered by height, by distance (highest first) and by topographic prominence.spectralSpectral estimation: periodograms averaged over segments (Welch), the short-time Fourier transform and its inverse, spectrograms, and the analytic signal.unitsNumber suffixes for frequencies and times, so a call reads like its spec sheet:butter(4, Band::Lowpass(100hz), fs: 8khz).windowWindow functions, as SciPy'sscipy.signal.windows: symmetric by default (for filter design);sym: falsegives the periodic form (for spectral analysis), the firstmpoints of them + 1window.
Re-exports
conv::{convolve, correlate, convolve2d, correlate2d, Mode, Method}Source
pub use conv::{convolve, correlate, convolve2d, correlate2d, Mode, Method}convolveThe convolution(a * v)[k] = sum_i a[i] v[k - i], cut tomode.correlateThe cross-correlationc[k] = sum_i a[i + k] v[i], as SciPy'scorrelate:convolve(a, v reversed), so inMode::Fulllag 0 is at indexv.len() - 1.convolve2dThe 2-d convolution ofaand kernelk, cut tomodealong both axes (as SciPy'sconvolve2dwith zero boundaries).correlate2dThe 2-d cross-correlation:convolve2dwithkflipped on both axes.ModeWhich part of the full convolution to return.MethodHow to compute:Autopicks the faster of the two by size.
fft::{fft, ifft, rfft, irfft, fftn, ifftn, Plan, RealPlan, Norm}Source
pub use fft::{fft, ifft, rfft, irfft, fftn, ifftn, Plan, RealPlan, Norm}fftThe DFTX_k = sum_j x_j exp(-2 pi i j k / n)of a real or complex signal, cut or zero-padded tonsamples whennis given.ifftThe inverse DFTx_j = (1/n) sum_k X_k exp(2 pi i j k / n)(the1/nunderNorm::Backward), soifft(fft(x))isxto about1e-15relative.rfftBins0..=n/2of the DFT of a real signal: the rest are their conjugates.irfftThenreal samples whose non-negative bins arex(asrfftgives them);ndefaults to2 * (x.len() - 1).fftnThe N-d DFT ofxoveraxes(every axis by default), in the order given; an axis named twice is transformed twice.ifftnThe inverse offftnover the sameaxes.PlanA complex FFT of one length with its twiddles computed once: build it withPlan::new(n)and run it on many signals.RealPlanA real FFT of one length, reusable:forwardgives then / 2 + 1non-negative frequency bins,inversetakes them back.NormHow a transform is scaled:Backward(the default, as NumPy) leaves the forward transform unscaled and divides the inverse byn;Orthodivides both bysqrt(n), so the pair is unitary;Forwarddivides the forward transform byn.
filter::{Band, butter, cheby1, cheby2, bessel, firwin, lfilter, sosfilt, filtfilt, sosfiltfilt, freqz, sosfreqz}Source
pub use filter::{Band, butter, cheby1, cheby2, bessel, firwin, lfilter, sosfilt, filtfilt, sosfiltfilt, freqz, sosfreqz}BandThe pass band of a filter, edges in the units of the sampling ratefsthe design function takes (with the defaultfs = 2.0an edge of 1 is the Nyquist frequency, as SciPy).butterA Butterworth filter as second-order sections.cheby1A Chebyshev type I filter withrpdB of pass-band ripple, as second-order sections.cheby2A Chebyshev type II filterrsdB down in the stop band, as second-order sections.besselA Bessel filter (phase-normalized, as SciPy's default), as second-order sections: nearly constant group delay in the pass band.firwinA linear-phase FIR filter ofnumtapscoefficients forband(edges in units offs), windowed bywindow(Hamming by default).lfilterxfiltered byb(z) / a(z)from rest:a[0] y[t] = sum b[k] x[t - k] - sum_(k >= 1) a[k] y[t - k](transposed direct form II).sosfiltxfiltered by the cascade of second-order sectionssos(rows[b0, b1, b2, a0, a1, a2]), from rest.filtfiltxfiltered forward and then backward: zero phase, the magnitude response squared, and no delay.sosfiltfiltfiltfiltfor second-order sections: the zero-phase way to run an IIR filter of any order.freqzThe responseH(e^jw) = b(e^jw) / a(e^jw)atnfrequencies from 0 up to (not including) Nyquist, or the whole circle withwhole.sosfreqzfreqzfor second-order sections: the product of the rows' responses.
peaks::{find_peaks, Peaks}Source
pub use peaks::{find_peaks, Peaks}find_peaksThe peaks ofx: samples higher than both neighbours (a flat top counts once, at its middle; the ends never count).PeaksThe peaksfind_peakskept, in increasing position, with the properties it measured.
spectral::{welch, periodogram, stft, istft, spectrogram, hilbert}Source
pub use spectral::{welch, periodogram, stft, istft, spectrogram, hilbert}welchWelch's power spectral density estimate ofxsampled atfs: segments ofnpersegsamples (256 by default, at most the signal length), overlapping bynoverlap(half by default), each detrended (Detrend::Constantby default,nonefor no detrending), windowed (periodic Hann by default) and zero-padded tonfft.periodogramThe periodogram ofx: one segment, the whole signal, no window by default.stftThe STFT ofx(as SciPy'sstft):nperseg / 2zeros added at each end and zeros at the end to fill the last segment, so every sample is covered; segments ofnperseg(256) overlapping bynoverlap(half), windowed by a periodic Hann window, each a real FFT ofnfftpoints scaled by1 / sum(window).istftThe signal back from its STFT (as SciPy'sistft): each column is inverse-transformed, windowed and overlap-added, divided by the summed squared window, and thenperseg / 2boundary samples are dropped.spectrogramThe spectrogram ofx(as SciPy'sspectrogram): power spectral density of each segment, by defaultnperseg256 samples of a Tukey(0.25) window overlapping by an eighth, each detrended.hilbertThe analytic signalx + i H(x)of a realx(Hthe Hilbert transform), zero-padded or cut tonsamples when given.
window::WindowSource
pub use window::WindowWindowA window by name, for the functions that take one (firwin,welch,stft, ...).
Enums
SignalErrorSource
pub enum SignalErrorWhat went wrong in a signal call. Every message names the
argument, the value it got and what to pass instead.
use signal::SignalError;
try signal::window::kaiser(0, 8.6) catch e: SignalError {
println(e); // `kaiser` length: got 0, need at least 1 (pass `m >= 1`)
};
A length, size or count is out of range.
A cutoff or band edge outside
(0, fs/2), or edges out of order.A filter with a pole outside the unit circle: its output grows without bound.
Any other argument with no valid value.
Trait implementations 2
impl Display for SignalErrorimpl Error for SignalError
Traits
SamplesSource
pub trait SamplesA signal the FFT functions take: a real or complex Vector or
List, of f64 or f32. Real input becomes complex with a zero
imaginary part; f32 widens to f64.
The real and imaginary parts as two lists of
f64.
RealSamplesSource
pub trait RealSamplesA real signal: anything that reads as a vector of f64
(linalg::AsVector<f64>): a Vector or List of f64, f32,
i64, i32 or usize, a fixed array, an i64 range.
let xs: List<i64> = [1, 2, 3];
let a = try signal::convolve(xs, [1.0, 1.0]);
let b = try signal::convolve(1..4, [1f32, 1f32]);
assert(a == b && a.to_list() == [1.0, 3.0, 5.0, 3.0]);
The samples as a new list of
f64.
Functions
complexSource
pub fn complex<const N: usize>(x: Array<f64, N>) -> Array<Complex<f64>, N> x as a complex array with zero imaginary parts.
use core::array::Vector;
let x: Vector<f64> = [1, 2];
println(signal::complex(x)); // [Complex { re: 1.0, im: 0.0 }, Complex { re: 2.0, im: 0.0 }]
absSource
pub fn abs<const N: usize>(x: Array<Complex<f64>, N>) -> Array<f64, N> The modulus of each element: |z|.
realSource
pub fn real<const N: usize>(x: Array<Complex<f64>, N>) -> Array<f64, N> The real part of each element.
imagSource
pub fn imag<const N: usize>(x: Array<Complex<f64>, N>) -> Array<f64, N> The imaginary part of each element.
angleSource
pub fn angle<const N: usize>(x: Array<Complex<f64>, N>) -> Array<f64, N> The argument of each element, in (-pi, pi].