Signal Processing
Advanced tools for signal analysis, transforms, and filtering. These functions are available via SciMathJS and are automatically optimized using WASM when a provider is configured.
Usage
import { SciMathJS } from '@velo-sci/sci-math-wasm';
const data = new Float64Array([1, 0, -1, 0, 1, 0, -1, 0]);
const spectrum = SciMathJS.fft(data);API Reference
fft
Fast Fourier Transform.
Algorithm: Uses the Cooley-Tukey algorithm for power-of-two lengths and falling back to Bluestein's algorithm for arbitrary lengths. Implemented in Rust using the rustfft crate or custom SIMD-optimized kernels for common sizes.
Formula:
Signature:
function fft(input: Float64Array | number[]): Float64Arrayifft
Inverse Fast Fourier Transform.
Algorithm: Applies the FFT algorithm on the conjugate of the input and scales the result by .
Signature:
function ifft(re: Float64Array, im: Float64Array): Float64Arrayrfft
Real-valued Fast Fourier Transform. Returns only the non-redundant positive frequencies.
Signature:
function rfft(input: Float64Array | number[]): Float64Arraymagnitude
Computes the magnitude of a complex FFT result.
Formula:
Signature:
function magnitude(complexData: Float64Array | number[]): Float64ArraymovingAverage
Smoothes a signal using a sliding window.
Formula:
Signature:
function movingAverage(data: Float64Array | number[], window: number): Float64ArrayfindPeaks
Detects peaks (local maxima) in a signal.
Algorithm: Uses a dual-stage algorithm:
- Identification: Identifying local maxima above a specified
threshold. - Refinement: Filtering peaks based on Prominence. Prominence Measures how much a peak stands out from the surrounding baseline. A peak's prominence is the least drop in height necessary to reach a higher terrain.
Signature:
function findPeaks(data: Float64Array | number[], threshold: number, prominence: number): Uint32ArraysmoothSG
Smooths a signal using a Generalized Savitzky-Golay filter.
Background: The Savitzky-Golay filter works by fitting a low-degree polynomial to a window of adjacent data points using the method of linear least squares. This is superior to a moving average because it preserves features like peak height and width while removing high-frequency noise.
Signature:
function smoothSG(data: Float64Array | number[], window: number, degree: number): Float64Arraywindow: The size of the smoothing window (must be an odd integer).degree: The degree of the polynomial to fit (usually 2 or 4).
removeBaseline / removeBaselineIterative
Removes the baseline (background) from a signal.
Algorithm: Uses Modified Poly-fit. In the iterative version, the algorithm performs a polynomial fit, then "clips" the data points that are above the fit (peaks) and re-fits. After several iterations, the fit converges to the "true" background of the signal, ignoring the analytical peaks.
Signature:
function removeBaseline(data: Float64Array, x: Float64Array, order: number): Float64Array;
function removeBaselineIterative(data: Float64Array, x: Float64Array, order: number, iters: number): Float64Array;butterworthFilter / butterworthLowpass
Applies a digital Butterworth lowpass filter.
Background: The Butterworth filter is designed to have a frequency response as flat as possible in the passband. It's often called a "maximally flat magnitude" filter.
Signature:
function butterworthFilter(data: Float64Array | number[], cutoff: number, fs: number): Float64ArrayestimateSNR
Estimates the Signal-to-Noise Ratio (SNR) of a signal.
Algorithm: Uses the Median Absolute Deviation (MAD) of the first-order differences of the signal to estimate the noise level .
Signature:
function estimateSNR(data: Float64Array | number[]): numberdeconvolveRL
Performs Richardson-Lucy deconvolution.
Algorithm: An iterative method for restoring a signal that has been blurred by a known point spread function (PSF) or kernel.
Where is the observed data and is the kernel.
Signature:
function deconvolveRL(data: Float64Array | number[], kernel: Float64Array | number[], iterations: number): Float64Arraydecimate
Downsamples the signal by keeping every n-th sample.
Signature:
function decimate(data: Float64Array | number[], factor: number): Float64Arrayresample_linear
Resamples the signal to a new length using linear interpolation.
Signature:
function resample_linear(data: Float64Array | number[], new_len: number): Float64Array