MathMath

Help › Functions

Signal processing

convolve correlate hannWindow hammingWindow blackmanWindow kaiserWindow firLowpass firHighpass firBandpass firBandstop applyFilter zeroPhaseFilter medsmooth unwrapPhase freqResponse impulseResponse stepResponse

convolve(a, b) MathMath’s own

The full convolution of two vectors, which has one value fewer than their lengths added.

Element k is the sum of a_i·b_(k−i). For vectors of lengths m and n the result is a column of m + n − 1 values. Convolving a signal with a filter's coefficients is how an FIR filter acts; applyFilter does that and keeps the signal's length. Complex values are accepted. Long vectors use the FFT inside.

Units

Units multiply, so a voltage convolved with a plain filter stays in volts.

Refused

  • Anything that is not a vector, an empty vector, or nested arrays.
convolve⎝⎛​⎝⎛​123​⎠⎞​,(11​)⎠⎞​=⎝⎜⎜⎛​1353​⎠⎟⎟⎞​

correlate(a, b) MathMath’s own

The cross-correlation of two vectors at every lag.

The result is a column of m + n − 1 values for vectors of lengths m and n, covering lags from −(n − 1) to m − 1. Element k is the sum of a_(i+k)·conj(b_i), the second vector being conjugated if it is complex. Correlating a vector with itself puts lag 0 in the middle, and that is its largest value.

Units

Units multiply.

Refused

  • Anything that is not a vector, an empty vector, or nested arrays.
correlate⎝⎛​⎝⎛​123​⎠⎞​,⎝⎛​123​⎠⎞​⎠⎞​=⎝⎜⎜⎜⎜⎛​381483​⎠⎟⎟⎟⎟⎞​

hannWindow(n) MathMath’s own

A Hann window of n points: a raised cosine that falls to 0 at both ends.

The window is symmetric, so its first and last points match, as a filter design wants. Multiply a signal by it, element by element, to reduce leakage in a spectrum. n is a whole number, 1 or more; a window of 1 point is 1.

Refused

  • An n that is not a whole number of 1 or more.
hannWindow(5)=⎝⎜⎜⎜⎜⎛​00.510.50​⎠⎟⎟⎟⎟⎞​

hammingWindow(n) MathMath’s own

A Hamming window of n points: a raised cosine that falls to 0.08 at both ends.

The window is symmetric, so its first and last points match. It is the default window of the FIR design functions such as firLowpass. n is a whole number, 1 or more; a window of 1 point is 1.

Refused

  • An n that is not a whole number of 1 or more.
hammingWindow(5)=⎝⎜⎜⎜⎜⎛​0.080.5410.540.08​⎠⎟⎟⎟⎟⎞​

blackmanWindow(n) MathMath’s own

A Blackman window of n points: a three-term cosine window with low side lobes.

The window is symmetric, so its first and last points match, and both are about 0. It gives lower leakage than hannWindow or hammingWindow but a wider main lobe. n is a whole number, 1 or more; a window of 1 point is 1.

Refused

  • An n that is not a whole number of 1 or more.
blackmanWindow(5)=⎝⎜⎜⎜⎜⎛​00.3410.340​⎠⎟⎟⎟⎟⎞​

kaiserWindow(n, β) MathMath’s own

A Kaiser window of n points, whose shape β sets from a rectangle to a narrow bell.

The window is symmetric, with a value of 1 in the middle. β = 0 gives a rectangle, all 1; larger β lowers the side lobes and widens the main lobe. Around 5 is like a hammingWindow, and 8.6 like a blackmanWindow. n is a whole number, 1 or more.

Refused

  • An n that is not a whole number of 1 or more.
  • A negative β.
kaiserWindow(5,0)=⎝⎜⎜⎜⎜⎛​11111​⎠⎟⎟⎟⎟⎞​
kaiserWindow(5,5)=⎝⎜⎜⎜⎜⎛​0.0370.55310.5530.037​⎠⎟⎟⎟⎟⎞​

firLowpass(n, fc, [window]) MathMath’s own

The n coefficients of a windowed-sinc low-pass FIR filter with cutoff fc.

A frequency is a fraction of the sample rate, from 0 to 0.5, where 0.5 is the Nyquist frequency. Typing fc/fs with both in Hz gives that fraction.

The result is a column of n coefficients, scaled so the gain at frequency 0 is exactly 1. The window is a vector of n points, as hannWindow gives; without one, a Hamming window is used. More taps give a sharper cutoff. The filter has a linear phase, and a delay of (n − 1)/2 samples. Use it with applyFilter or zeroPhaseFilter, or look at it with freqResponse.

Units

Cutoffs are plain fractions of the sample rate: divide by the sample rate, as fc/fs. A frequency with units is refused.

Refused

  • A number of taps that is not a whole number of 1 or more.
  • A cutoff at or beyond 0 or 0.5.
  • A window whose length is not n.
h≔firLowpass(5,0.25)
freqResponse(h,0)=1

firHighpass(n, fc, [window]) MathMath’s own

The n coefficients of a windowed-sinc high-pass FIR filter with cutoff fc.

A frequency is a fraction of the sample rate, from 0 to 0.5, where 0.5 is the Nyquist frequency. Typing fc/fs with both in Hz gives that fraction.

The result is a column of n coefficients, scaled so the gain at the Nyquist frequency is exactly 1. n must be odd, because a filter with an even number of taps cannot pass the Nyquist frequency. The window is a vector of n points; without one, a Hamming window is used.

Units

Cutoffs are plain fractions of the sample rate: divide by the sample rate, as fc/fs. A frequency with units is refused.

Refused

  • An even number of taps.
  • A cutoff at or beyond 0 or 0.5.
  • A window whose length is not n.
h≔firHighpass(5,0.25)
∣freqResponse(h,0.5)∣=1

firBandpass(n, f1, f2, [window]) MathMath’s own

The n coefficients of a windowed-sinc band-pass FIR filter passing f1 to f2.

A frequency is a fraction of the sample rate, from 0 to 0.5, where 0.5 is the Nyquist frequency. Typing fc/fs with both in Hz gives that fraction.

The result is a column of n coefficients, scaled so the gain at the middle of the band is exactly 1. f2 must be above f1. The window is a vector of n points; without one, a Hamming window is used.

Units

Cutoffs are plain fractions of the sample rate: divide by the sample rate, as fc/fs. A frequency with units is refused.

Refused

  • A number of taps that is not a whole number of 1 or more.
  • An edge at or beyond 0 or 0.5, or f2 not above f1.
  • A window whose length is not n.
h≔firBandpass(7,0.1,0.3)
∣freqResponse(h,0.2)∣=1

firBandstop(n, f1, f2, [window]) MathMath’s own

The n coefficients of a windowed-sinc band-stop FIR filter rejecting f1 to f2.

A frequency is a fraction of the sample rate, from 0 to 0.5, where 0.5 is the Nyquist frequency. Typing fc/fs with both in Hz gives that fraction.

The result is a column of n coefficients, scaled so the gain at frequency 0 is exactly 1. n must be odd, because the filter passes the Nyquist frequency as well. f2 must be above f1. The window is a vector of n points; without one, a Hamming window is used.

Units

Cutoffs are plain fractions of the sample rate: divide by the sample rate, as fc/fs. A frequency with units is refused.

Refused

  • An even number of taps.
  • An edge at or beyond 0 or 0.5, or f2 not above f1.
  • A window whose length is not n.
h≔firBandstop(7,0.1,0.3)
freqResponse(h,0)=1

applyFilter(filter, x) MathMath’s own

Runs the signal x through a filter, once, forward in time, starting from rest.

The filter is a vector of FIR coefficients b, or a vector of two vectors (b; a) for an IIR filter, as the design functions such as butterLowpass give. The result has the length of x and the same delay the filter has. If x is a matrix, each column is a channel and is filtered separately; a row vector counts as one channel. For no delay, use zeroPhaseFilter.

Units

The result has the units of x. The coefficients must be plain numbers.

Refused

  • A filter that is not a coefficient vector or a pair (b; a), or whose first a is 0.
  • A signal with complex values or text.
x≔⎝⎜⎜⎛​1111​⎠⎟⎟⎞​
applyFilter((0.50.5​),x)=⎝⎜⎜⎛​0.5111​⎠⎟⎟⎞​

zeroPhaseFilter(filter, x) MathMath’s own

Filters x forward and then backward, so the result has no delay and the filter's gain is squared.

The filter is as for applyFilter. Running it both ways cancels the phase shift, so features of the signal stay where they are, but the gain in dB doubles: a -3 dB cutoff becomes -6 dB. The ends are padded by reflection to keep the starting transients small. If x is a matrix, each column is filtered separately.

Units

The result has the units of x. The coefficients must be plain numbers.

Refused

  • A signal with 3 times the filter's length of values or fewer.
  • A filter that is not a coefficient vector or a pair (b; a).
x≔⎝⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎛​22222222​⎠⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎞​
zeroPhaseFilter((0.50.5​),x)=⎝⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎛​22222222​⎠⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎞​

medsmooth(y, n) As in Mathcad

A running median of y over a window of n points, which removes spikes and keeps edges.

n is odd. Each value is replaced by the median of the n values centred on it. At the ends the window is cut short rather than padded, so the first and last values are medians of fewer points.

Units

The result has the units of y.

Refused

  • An even window, or one less than 1.
y≔⎝⎜⎜⎜⎜⎛​22922​⎠⎟⎟⎟⎟⎞​
medsmooth(y,3)=⎝⎜⎜⎜⎜⎛​22222​⎠⎟⎟⎟⎟⎞​

unwrapPhase(φ) MathMath’s own

Removes the jumps of 2π from a vector of phase angles.

Where two neighbours differ by π or more, a multiple of 2π is added to this value and all after it, so the phase is continuous. Angles are in radians. To unwrap a response, take its arg first: unwrapPhase(arg(H)).

Refused

  • Complex values.
  • Anything that is not a vector.
unwrapPhase⎝⎜⎜⎛​⎝⎜⎜⎛​03−30​⎠⎟⎟⎞​⎠⎟⎟⎞​=⎝⎜⎜⎛​033.2836.283​⎠⎟⎟⎞​

freqResponse(system, f) MathMath’s own

The complex gain H of a filter or system at frequency f, or at each frequency of a vector.

The system is a vector of coefficients b, or a pair (b; a), with the constant term first, as butterLowpass and the other design functions give. How f is written decides the kind. A plain number is a digital filter and f is a fraction of the sample rate: H = Σ b_k·e^(−2πi·f·k) / Σ a_k·e^(−2πi·f·k). A frequency in Hz is an analog system, H(s) at s = 2πi·f, with coefficients of s.

The result is complex: its magnitude is the gain (see ampToDb for decibels) and arg is the phase. A vector of frequencies gives a vector of values.

Units

Frequencies are plain fractions for a digital filter, or in Hz for an analog one. The result has no units.

Refused

  • A frequency with units other than Hz for a digital filter, or plain numbers mixed with units for an analog one.
  • A system that is not a coefficient vector or a pair (b; a).
h≔(0.50.5​)
freqResponse(h,0)=1
∣freqResponse(h,0.25)∣=0.707

impulseResponse(system, t) MathMath’s own

The output of a system, started at rest, after an impulse at time 0, at time t or at each time of a vector.

The system is a vector b, or a pair (b; a), as for freqResponse. Times with units, such as 2·s, treat it as an analog H(s) with coefficients of s; plain whole numbers are sample numbers 0, 1, 2… of a digital filter, whose impulse is a single 1 at sample 0.

For an analog system, the impulse is the response to a unit area spike. A direct feed-through term in H(s) is left out of the impulse response. stepResponse gives the response to a constant 1.

Units

Times in seconds for an analog system; plain sample numbers for a digital one. The result has no units.

Refused

  • An analog system whose numerator is of higher degree than its denominator.
  • Plain numbers that are not whole and 0 or more, for a digital filter.
  • A digital filter given with units in its coefficients.
h≔butterLowpass(1,0.25)
k≔⎝⎜⎜⎛​0123​⎠⎟⎟⎞​
impulseResponse(h,k)=⎝⎜⎜⎛​0.50.500​⎠⎟⎟⎞​

stepResponse(system, t) MathMath’s own

The output of a system, started at rest, after a constant input of 1 from time 0, at time t or at each time of a vector.

The system is a vector b, or a pair (b; a), as for freqResponse. Times with units, such as 2·s, treat it as an analog H(s) with coefficients of s; plain whole numbers are sample numbers 0, 1, 2… of a digital filter. The response settles at the system's gain at frequency 0. impulseResponse gives the response to a single spike.

Units

Times in seconds for an analog system; plain sample numbers for a digital one. The result has no units.

Refused

  • An analog system whose numerator is of higher degree than its denominator.
  • Plain numbers that are not whole and 0 or more, for a digital filter.
  • A digital filter given with units in its coefficients.
h≔butterLowpass(1,0.25)
k≔⎝⎜⎜⎛​0123​⎠⎟⎟⎞​
stepResponse(h,k)=⎝⎜⎜⎛​0.5111​⎠⎟⎟⎞​