packages

signal

On this page

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

  • conv Convolution and correlation, 1-d and 2-d, direct or by FFT.
  • fft Discrete Fourier transforms of any length: complex, real, n-d, the frequency helpers and the cosine and sine transforms built on them.
  • filter Digital filters: FIR and IIR design, filtering (causal and zero-phase), frequency response, resampling and detrending.
  • ops Convolution and correlation as operators: a ⊛ b (U+229B, circled asterisk) is the full linear convolution and a ⋆ b (U+22C6, star operator) the full cross-correlation, both at the level of *.
  • peaks Peak finding as SciPy's find_peaks: local maxima (a flat top counts once, at its middle), then filtered by height, by distance (highest first) and by topographic prominence.
  • spectral Spectral estimation: periodograms averaged over segments (Welch), the short-time Fourier transform and its inverse, spectrograms, and the analytic signal.
  • units Number suffixes for frequencies and times, so a call reads like its spec sheet: butter(4, Band::Lowpass(100hz), fs: 8khz).
  • window Window functions, as SciPy's scipy.signal.windows: symmetric by default (for filter design); sym: false gives the periodic form (for spectral analysis), the first m points of the m + 1 window.

Re-exports

conv::{convolve, correlate, convolve2d, correlate2d, Mode, Method}Source

pub use conv::{convolve, correlate, convolve2d, correlate2d, Mode, Method}
  • convolve The convolution (a * v)[k] = sum_i a[i] v[k - i], cut to mode.
  • correlate The cross-correlation c[k] = sum_i a[i + k] v[i], as SciPy's correlate: convolve(a, v reversed), so in Mode::Full lag 0 is at index v.len() - 1.
  • convolve2d The 2-d convolution of a and kernel k, cut to mode along both axes (as SciPy's convolve2d with zero boundaries).
  • correlate2d The 2-d cross-correlation: convolve2d with k flipped on both axes.
  • Mode Which part of the full convolution to return.
  • Method How to compute: Auto picks 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}
  • fft The DFT X_k = sum_j x_j exp(-2 pi i j k / n) of a real or complex signal, cut or zero-padded to n samples when n is given.
  • ifft The inverse DFT x_j = (1/n) sum_k X_k exp(2 pi i j k / n) (the 1/n under Norm::Backward), so ifft(fft(x)) is x to about 1e-15 relative.
  • rfft Bins 0..=n/2 of the DFT of a real signal: the rest are their conjugates.
  • irfft The n real samples whose non-negative bins are x (as rfft gives them); n defaults to 2 * (x.len() - 1).
  • fftn The N-d DFT of x over axes (every axis by default), in the order given; an axis named twice is transformed twice.
  • ifftn The inverse of fftn over the same axes.
  • Plan A complex FFT of one length with its twiddles computed once: build it with Plan::new(n) and run it on many signals.
  • RealPlan A real FFT of one length, reusable: forward gives the n / 2 + 1 non-negative frequency bins, inverse takes them back.
  • Norm How a transform is scaled: Backward (the default, as NumPy) leaves the forward transform unscaled and divides the inverse by n; Ortho divides both by sqrt(n), so the pair is unitary; Forward divides the forward transform by n.

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}
  • Band The pass band of a filter, edges in the units of the sampling rate fs the design function takes (with the default fs = 2.0 an edge of 1 is the Nyquist frequency, as SciPy).
  • butter A Butterworth filter as second-order sections.
  • cheby1 A Chebyshev type I filter with rp dB of pass-band ripple, as second-order sections.
  • cheby2 A Chebyshev type II filter rs dB down in the stop band, as second-order sections.
  • bessel A Bessel filter (phase-normalized, as SciPy's default), as second-order sections: nearly constant group delay in the pass band.
  • firwin A linear-phase FIR filter of numtaps coefficients for band (edges in units of fs), windowed by window (Hamming by default).
  • lfilter x filtered by b(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).
  • sosfilt x filtered by the cascade of second-order sections sos (rows [b0, b1, b2, a0, a1, a2]), from rest.
  • filtfilt x filtered forward and then backward: zero phase, the magnitude response squared, and no delay.
  • sosfiltfilt filtfilt for second-order sections: the zero-phase way to run an IIR filter of any order.
  • freqz The response H(e^jw) = b(e^jw) / a(e^jw) at n frequencies from 0 up to (not including) Nyquist, or the whole circle with whole.
  • sosfreqz freqz for second-order sections: the product of the rows' responses.

peaks::{find_peaks, Peaks}Source

pub use peaks::{find_peaks, Peaks}
  • find_peaks The peaks of x: samples higher than both neighbours (a flat top counts once, at its middle; the ends never count).
  • Peaks The peaks find_peaks kept, 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}
  • welch 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.
  • periodogram The periodogram of x: one segment, the whole signal, no window by default.
  • stft The STFT of x (as SciPy's stft): nperseg / 2 zeros added at each end and zeros at the end to fill the last segment, so every sample is covered; segments of nperseg (256) overlapping by noverlap (half), windowed by a periodic Hann window, each a real FFT of nfft points scaled by 1 / sum(window).
  • istft The signal back from its STFT (as SciPy's istft): each column is inverse-transformed, windowed and overlap-added, divided by the summed squared window, and the nperseg / 2 boundary samples are dropped.
  • spectrogram The spectrogram of x (as SciPy's spectrogram): power spectral density of each segment, by default nperseg 256 samples of a Tukey(0.25) window overlapping by an eighth, each detrended.
  • hilbert The analytic signal x + i H(x) of a real x (H the Hilbert transform), zero-padded or cut to n samples when given.

window::WindowSource

pub use window::Window
  • Window A window by name, for the functions that take one (firwin, welch, stft, ...).

Enums

SignalErrorSource

pub enum SignalError

What went wrong in a signal call. Every message names the argument, the value it got and what to pass instead.

nabi
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`)
};
  • Length§

    Source

    A length, size or count is out of range.

  • Cutoff§

    Source

    A cutoff or band edge outside (0, fs/2), or edges out of order.

  • Unstable§

    Source

    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 SignalError
  • impl Error for SignalError

Traits

SamplesSource

pub trait Samples

A 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.

  • fn to_split(self) -> (List<f64>, List<f64>)§

    Source

    The real and imaginary parts as two lists of f64.

RealSamplesSource

pub trait RealSamples

A 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.

nabi
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]);
  • fn to_reals(self) -> List<f64>§

    Source

    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.

nabi
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].