Signal Functions (20)

Signal processing — FFT, filters, transforms, wavelets

bandpassFilter
Apply a 2nd order Butterworth bandpass filter to a signal by cascading a highpass at lowCutoff and a lowpass at highCutoff.
Syntax
bandpassFilter(signal | lowCutoff | highCutoff
Type Signatures
Array, number, number, number, Matrix, number, number, number
Parameters
NameTypeDescription
signalArray | MatrixInput signal array
lowCutoffnumberLower cutoff frequency in Hz
highCutoffnumberUpper cutoff frequency in Hz
sampleRatenumberSample rate in Hz
Returns
Array — Filtered signal
Examples
bandpassFilter([1
0
-1
0
1
convolve
Compute the discrete linear convolution of two arrays. The output length is len(a) + len(b) - 1.
Syntax
convolve(a | b)
Type Signatures
Array, Array, Matrix, Matrix, Array, Matrix, Matrix, Array
Parameters
NameTypeDescription
aArray | MatrixFirst input array or matrix
bArray | MatrixSecond input array or matrix
Returns
Array — The convolution of a and b
Examples
convolve([1
2
3
See Also
correlate fft ifft
correlate
Compute the cross-correlation of two arrays. When called with one argument, computes the autocorrelation. Equivalent to convolve(a, reverse(b)).
Syntax
correlate(a) | correlate(a | b)
Type Signatures
Array, Array, Matrix, Matrix, Array, Matrix, Matrix, Array
Parameters
NameTypeDescription
aArray | MatrixFirst input array or matrix
bArray | MatrixSecond input array or matrix (defaults to a for autocorrelation)
Returns
Array — The cross-correlation of a and b
Examples
correlate([1
2
3
See Also
convolve fft ifft
dct
Compute the Discrete Cosine Transform (DCT-II) of a real-valued signal. DCT-II is defined as X[k] = sum_{n=0}^{N-1} x[n] * cos(pi*(2n+1)*k/(2N)). Computed directly in O(N^2). The DC component X[0] equals the sum of all input samples.
Syntax
dct(signal)
Parameters
NameTypeDescription
signalArrayReal-valued input array
Returns
Array — DCT-II coefficients
Examples
dct([1
1
1
1
dst
Compute the Discrete Sine Transform (DST-I) of a real-valued signal. X[k] = sum_{n=0}^{N-1} x[n] * sin(pi * (n+1) * (k+1) / (N+1)). Computed directly in O(N^2). Useful for PDEs with Dirichlet boundary conditions. The DST-I is its own inverse up to the scaling factor 2/(N+1), so idst(dst(x)) == x.
Syntax
dst(signal)
Parameters
NameTypeDescription
signalArrayReal-valued input array
Returns
Array — DST-I coefficients
Examples
dst([1
0
-1
See Also
idst dct fourier fft
dwt
Compute one level of the Discrete Wavelet Transform (DWT). Decomposes the signal into approximation (lowpass) and detail (highpass) coefficients. Supported wavelets: "haar" (Haar wavelet) and "db2" (Daubechies-2). For "haar": approx[i] = (x[2i] + x[2i+1]) / sqrt(2), detail[i] = (x[2i] - x[2i+1]) / s
Syntax
dwt(signal | wavelet)
Type Signatures
Array, string
Parameters
NameTypeDescription
signalArrayReal-valued input signal (length must be even for haar)
waveletstringWavelet type: 'haar' or 'db2'
Returns
Object — { approximation: number[], detail: number[] }
Examples
dwt([1
1
1
1
See Also
fourier dct fft
fft2d
Compute the 2D Discrete Fourier Transform of a real-valued matrix. The 2D DFT is applied by computing a 1D DFT along each row, then along each column of the result. Returns a 2D array of complex numbers with properties re and im. The DC component (zero frequency) is at position [0][0].
Syntax
fft2d(matrix)
Parameters
NameTypeDescription
matrixArray2D array of real numbers (array of arrays)
Returns
Array — 2D array of complex numbers { re, im }
Examples
fft2d([[1
1
See Also
fft ifft fourier
fourier
Compute the Fourier transform of a real-valued signal with user-friendly output. Returns an object with frequencies, amplitudes, and phases arrays. Amplitudes are scaled by 1/N. Optionally accepts options.sampleRate (default: 1) for physical frequency bins, and options.output = "complex" to return t
Syntax
fourier(signal) | fourier(signal | options)
Type Signatures
Array, Object
Parameters
NameTypeDescription
signalArrayReal-valued input signal array
optionsObjectOptions:
Returns
Object|Array —
Examples
fourier([1
0
1
0
freqz
Calculates the frequency response of a filter given its numerator and denominator coefficients.
Syntax
freqz(b | a) | freqz(b
Type Signatures
Array, Array, Array, Array, Array, Array, Array, number, Matrix, Matrix, Matrix, Matrix, Matrix, Matrix, Matrix, number
Parameters
NameTypeDescription
bArray.<number>The numerator coefficients of the filter.
aArray.<number>The denominator coefficients of the filter.
wArray.<number>A vector of frequencies (in radians/sample) at which the frequency response is to be computed or the number of points to compute (if a number is not provided, the default is 512 points)
Returns
Object — An object with two properties: h, a vector containing the complex frequency response, and w, a vector containing the normalized frequencies (in radians/sample) at which the response was computed.
Examples
freqz([1
2
See Also
highpassFilter
Apply a 2nd order Butterworth highpass IIR filter to a signal. The cutoff and sampleRate must use the same time unit (e.g., both in Hz).
Syntax
highpassFilter(signal | cutoff | sampleRate)
Type Signatures
Array, number, number, Matrix, number, number
Parameters
NameTypeDescription
signalArray | MatrixInput signal array
cutoffnumberCutoff frequency in Hz
sampleRatenumberSample rate in Hz
Returns
Array — Filtered signal
Examples
highpassFilter([1
-1
1
-1
1
hilbertTransform
Compute the Hilbert transform of a real-valued signal. The Hilbert transform produces a 90-degree phase shift of the input. It is computed via DFT: multiply positive-frequency components by -i, zero out the DC and Nyquist, zero out negative frequencies, then take the imaginary part of the IDFT resul
Syntax
hilbertTransform(signal)
Parameters
NameTypeDescription
signalArrayReal-valued input array (length should be a power of 2 for best accuracy)
Returns
Array — Hilbert transform (imaginary part of the analytic signal)
Examples
hilbertTransform([1
0
-1
0
See Also
fft ifft dst fourier
idst
Compute the Inverse Discrete Sine Transform (IDST-I). x[n] = (2/(N+1)) * sum_{k=0}^{N-1} X[k] * sin(pi * (n+1) * (k+1) / (N+1)). The IDST-I is self-inverse: applying it to the output of dst with scaling 2/(N+1) recovers the original signal. idst(dst(x)) == x within floating-point tolerance.
Syntax
idst(spectrum)
Parameters
NameTypeDescription
spectrumArrayDST-I coefficients (output of dst)
Returns
Array — Reconstructed time-domain signal
Examples
idst(dst([1
2
3
4
See Also
dst dct fourier fft
invFourier
Reconstruct a real-valued signal from its Fourier spectrum. Accepts either a complex coefficient array (as returned by fourier with output:"complex") or a spectrum object { frequencies, amplitudes, phases } (as returned by the default fourier() call). Applies the inverse FFT with proper N-scaling.
Syntax
invFourier(spectrum)
Parameters
NameTypeDescription
spectrumArray|Object
Returns
Array — Reconstructed signal (real parts of ifft output, scaled by N)
Examples
invFourier(fourier([1
0
1
0
lowpassFilter
Apply a 2nd order Butterworth lowpass IIR filter to a signal. The cutoff and sampleRate must use the same time unit (e.g., both in Hz).
Syntax
lowpassFilter(signal | cutoff | sampleRate)
Type Signatures
Array, number, number, Matrix, number, number
Parameters
NameTypeDescription
signalArray | MatrixInput signal array
cutoffnumberCutoff frequency in Hz
sampleRatenumberSample rate in Hz
Returns
Array — Filtered signal
Examples
lowpassFilter([1
1
1
1
1
medfilt
Apply a median filter to a 1D signal. For each position, computes the median of the windowSize-element sliding window centered on that position. Edge samples use mirror (reflect) padding. windowSize must be a positive odd integer. The median filter is effective for removing impulse noise (spikes) wh
Syntax
medfilt(signal | windowSize)
Type Signatures
Array, number
Parameters
NameTypeDescription
signalArrayInput signal
windowSizenumberMust be a positive odd integer
Returns
Array — Filtered signal, same length as input
Examples
medfilt([1
2
100
2
1
periodogram
Estimate the power spectral density (PSD) of a signal using the periodogram method. Computes |FFT(x)|^2 scaled by window normalization. Returns the one-sided spectrum (DC to Nyquist) as an object with frequencies and power arrays. Options: sampleRate (default: 1), window type (default: "rectangular"
Syntax
periodogram(signal) | periodogram(signal | options)
Type Signatures
Array, Object
Parameters
NameTypeDescription
signalArrayReal-valued input signal
optionsObjectOptions:
Returns
Object — { frequencies: number[], power: number[] }
Examples
periodogram([1
0
-1
0
1
resample
Resample a signal by a given factor using linear interpolation. A factor > 1 upsamples (increases length), a factor < 1 downsamples (decreases length). The output has round((N-1)*factor)+1 samples, mapping evenly between the first and last input samples. Non-integer factors are fully supported.
Syntax
resample(signal | factor)
Type Signatures
Array, number
Parameters
NameTypeDescription
signalArrayInput signal
factornumberResampling factor (> 0). Factor > 1 upsamples, < 1 downsamples.
Returns
Array — Resampled signal
Examples
resample([1
2
3
spectrogram
Compute the spectrogram (Short-Time Fourier Transform magnitude) of a signal. Slides a window across the signal, applies a window function, and computes the FFT magnitude at each position. Returns an object with times, frequencies, and magnitude (2D array indexed by [timeIndex][freqBin]). Options: h
Syntax
spectrogram(signal | windowSize) | spectrogram(signal
Type Signatures
Array, number, Array, number, Object
Parameters
NameTypeDescription
signalArrayInput signal
windowSizenumberLength of each analysis window
optionsObjectOptions:
Returns
Object — { times: number[], frequencies: number[], magnitude: number[][] }
Examples
spectrogram([1
0
-1
0
1
windowFunction
Compute a window function of length n. Supported types: rectangular, hamming, hanning, blackman, kaiser. Default type is hamming.
Syntax
windowFunction(n) | windowFunction(n | type)
Type Signatures
number, string, number, string, number
Parameters
NameTypeDescription
nnumberLength of the window
typestringWindow type: 'rectangular', 'hamming', 'hanning', 'blackman', 'kaiser' (default: 'hamming')
alphanumberShape parameter for kaiser window (default: 2)
Returns
Array — Array of length n with window values
Examples
windowFunction(5)
windowFunction(5
hamming")
windowFunction(5
hanning")
See Also
fft ifft convolve
zpk2tf
Compute the transfer function of a zero-pole-gain model.
Syntax
zpk2tf(z | p | k)
Type Signatures
Array,Array,number, Array,Array, Matrix,Matrix,number, Matrix,Matrix
Parameters
NameTypeDescription
zArrayArray of zeros values
pArrayArray of poles values
knumberGain value
Returns
Array — Two dimensional array containing the numerator (first row) and denominator (second row) polynomials
Examples
zpk2tf([1
2
See Also