Octave Signal - Signal Toolkit for GNU octave

# Octave Signal Toolkit

Copyright © The Octave Project Developers

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.

## 1 Overview

The Signal Toolkit contains signal processing tools, including filtering, windowing and display functions.

## 2 Installing and loading

The Signal Toolkit must be installed and then loaded to be used.

It can be installed in GNU Octave directly from octave-forge,

### 2.1 Windows install

If running in Windows, the package may already be installed, to check run:

    pkg list signal

### 2.2 Installing

With an internet connection available, the Signal package can be installed from octave-forge using the following command within GNU Octave:

    pkg install -forge signal

The latest released version of the toolkit will be downloaded and installed.

Otherwise, if the package file has already been downloaded it can be installed using the follwoing command in GNU Octave:

    pkg install signal-1.4.7.tar.gz

### 2.3 Loading

Regardless of the method of installing the toolkit, in order to use its functions, the toolkit must be loaded using the pkg load command:

    pkg load signal

The toolkit must be loaded on each GNU Octave session.

## 3 Function Reference

### 3.1 Signals

#### 3.1.1 buffer

Function File: `y =` buffer `(x, n, p, opt)`
Function File: `[y, z, opt] =` buffer `(…)`

  Buffer a signal into a data frame. The arguments to `buffer` are

  x

    The data to be buffered.

  n

    The number of rows in the produced data buffer. This is an positive integer value and must be supplied.

  p

    An integer less than n that specifies the under- or overlap between column in the data frame. The default value of p is 0.

  opt

    In the case of an overlap, opt can be either a vector of length p or the string ’nodelay’. If opt is a vector, then the first p entries in y will be filled with these values. If opt is the string ’nodelay’, then the first value of y corresponds to the first value of x.

    In the can of an underlap, opt must be an integer between 0 and `-p`. The represents the initial underlap of the first column of y.

    The default value for opt the vector `zeros (1, p)` in the case of an overlap, or 0 otherwise.

  In the case of a single output argument, y will be padded with zeros to fill the missing values in the data frame. With two output arguments z is the remaining data that has not been used in the current data frame.

  Likewise, the output opt is the overlap, or underlap that might be used for a future call to `code` to allow continuous buffering.

#### 3.1.2 chirp

Function File: chirp `(t)`
Function File: chirp `(t, f0)`
Function File: chirp `(t, f0, t1)`
Function File: chirp `(t, f0, t1, f1)`
Function File: chirp `(t, f0, t1, f1, shape)`
Function File: chirp `(t, f0, t1, f1, shape, phase)`

  Evaluate a chirp signal at time t. A chirp signal is a frequency swept cosine wave.

  t

    vector of times to evaluate the chirp signal

  f0

    frequency at time t=0 [ 0 Hz ]

  t1

    time t1 [ 1 sec ]

  f1

    frequency at time t=t1 [ 100 Hz ]

  shape

    shape of frequency sweep ’linear’ f(t) = (f1-f0)*(t/t1) + f0 ’quadratic’ f(t) = (f1-f0)*(t/t1)^2 + f0 ’logarithmic’ f(t) = (f1/f0)^(t/t1) * f0

  phase

    phase shift at t=0

  For example:

       specgram (chirp ([0:0.001:5]));  # default linear chirp of 0-100Hz in 1 sec
       specgram (chirp ([-2:0.001:15], 400, 10, 100, "quadratic"));
       soundsc (chirp ([0:1/8000:5], 200, 2, 500, "logarithmic"), 8000);

  If you want a different sweep shape f(t), use the following:

       y = cos (2 * pi * integral (f(t)) + phase);

#### 3.1.3 cmorwavf

Function File: `[psi, x] =` cmorwavf `(lb, ub, n, fb, fc)`

  Compute the Complex Morlet wavelet.

#### 3.1.4 diric

Function File: `y =` diric `(x,n)`

  Compute the dirichlet function.

  See also: sinc, gauspuls, sawtooth.

#### 3.1.5 gauspuls

Function File: `y =` gauspuls `(t)`
Function File: `y =` gauspuls `(t, fc)`
Function File: `y =` gauspuls `(t, fc, bw)`

  Generate a Gaussian modulated sinusoidal pulse sampled at times t.

  See also: pulstran, rectpuls, tripuls.

#### 3.1.6 gmonopuls

Function File: `y =` gmonopuls `(t,fc)`

  Return the gaussian monopulse.

#### 3.1.7 mexihat

Function File: `[psi, x] =` mexihat `(lb, ub, n)`

  Compute the Mexican hat wavelet.

#### 3.1.8 meyeraux

Function File: `y =` meyeraux `(x)`

  Compute the Meyer wavelet auxiliary function.

#### 3.1.9 morlet

Function File: `[psi, x] =` morlet `(lb, ub, n)`

  Compute the Morlet wavelet.

#### 3.1.10 pulstran

Function File: `y =` pulstran `(t, d, func, …)`
Function File: `y =` pulstran `(t, d, p)`
Function File: `y =` pulstran `(t, d, p, Fs)`
Function File: `y =` pulstran `(t, d, p, Fs, method)`

  Generate the signal y=sum(func(t+d,...)) for each d. If d is a matrix of two columns, the first column is the delay d and the second column is the amplitude a, and y=sum(a*func(t+d)) for each d,a. Clearly, func must be a function which accepts a vector of times. Any extra arguments needed for the function must be tagged on the end.

  Example:

       fs = 11025;  # arbitrary sample rate
       f0 = 100;    # pulse train sample rate
       w = 0.001;   # pulse width of 1 millisecond
       auplot (pulstran (0:1/fs:0.1, 0:1/f0:0.1, "rectpuls", w), fs);

  If instead of a function name you supply a pulse shape sampled at frequency Fs (default 1 Hz), an interpolated version of the pulse is added at each delay d. The interpolation stays within the the time range of the delayed pulse. The interpolation method defaults to linear, but it can be any interpolation method accepted by the function interp1.

  Example:

       fs = 11025;      # arbitrary sample rate
       f0 = 100;        # pulse train sample rate
       w = boxcar(10);  # pulse width of 1 millisecond at 10 kHz
       auplot (pulstran (0:1/fs:0.1, 0:1/f0:0.1, w, 10000), fs);

#### 3.1.11 rectpuls

Function File: `y =` rectpuls `(t)`
Function File: `y =` rectpuls `(t, w)`

  Generate a rectangular pulse over the interval [-w/2,w/2), sampled at times t. This is useful with the function `pulstran` for generating a series of pulses.

  Example:

       fs = 11025;  # arbitrary sample rate
       f0 = 100;    # pulse train sample rate
       w = 0.3/f0;  # pulse width 3/10th the distance between pulses
       plot (pulstran (0:1/fs:4/f0, 0:1/f0:4/f0, "rectpuls", w));

  See also: gauspuls, pulstran, tripuls.

#### 3.1.12 sawtooth

Function File: `y =` sawtooth `(t)`
Function File: `y =` sawtooth `(t, width)`

  Generates a sawtooth wave of period `2 * pi` with limits `+1/-1` for the elements of t.

  width is a real number between `0` and `1` which specifies the point between `0` and `2 * pi` where the maximum is. The function increases linearly from `-1` to `1` in `[0, 2 * pi * width]` interval, and decreases linearly from `1` to `-1` in the interval `[2 * pi * width, 2 * pi]`.

  If width is 0.5, the function generates a standard triangular wave.

  If width is not specified, it takes a value of 1, which is a standard sawtooth function.

#### 3.1.13 shanwavf

Function File: `[psi, x] =` shanwavf `(lb, ub, n, fb, fc)`

  Compute the Complex Shannon wavelet.

#### 3.1.14 shiftdata

Function File: `[out perm shifts] =` shiftdata `(in)`
Function File: `[out perm shifts] =` shiftdata `(in, dim)`

  Shift data in to permute the dimension dim to the first column.

  See also: unshiftdata.

#### 3.1.15 sigmoid_train

Function File: `[y s] =` sigmoid_train `(t, ranges, rc)`

  Evaluate a train of sigmoid functions at t.

  The number and duration of each sigmoid is determined from ranges. Each row of ranges represents a real interval, e.g. if sigmoid `i` starts at `t=0.1` and ends at `t=0.5`, then `ranges(i,:) = [0.1 0.5]`. The input rc is an array that defines the rising and falling time constants of each sigmoid. Its size must equal the size of ranges.

  The individual sigmoids are returned in s. The combined sigmoid train is returned in the vector y of length equal to t, and such that `Y = max (S)`.

  Run `demo sigmoid_train` to some examples of the use of this function.

#### 3.1.16 specgram

Function File: specgram `(x)`
Function File: specgram `(x, n)`
Function File: specgram `(x, n, Fs)`
Function File: specgram `(x, n, Fs, window)`
Function File: specgram `(x, n, Fs, window, overlap)`
Function File: `[S, f, t] =` specgram `(…)`

  Generate a spectrogram for the signal x. The signal is chopped into overlapping segments of length n, and each segment is windowed and transformed into the frequency domain using the FFT. The default segment size is 256. If fs is given, it specifies the sampling rate of the input signal. The argument window specifies an alternate window to apply rather than the default of `hanning (n)`. The argument overlap specifies the number of samples overlap between successive segments of the input signal. The default overlap is `length (window)/2`.

  If no output arguments are given, the spectrogram is displayed. Otherwise, S is the complex output of the FFT, one row per slice, f is the frequency indices corresponding to the rows of S, and t is the time indices corresponding to the columns of S.

  Example:

          x = chirp([0:0.001:2],0,2,500);  # freq. sweep from 0-500 over 2 sec.
          Fs=1000;                  # sampled every 0.001 sec so rate is 1 kHz
          step=ceil(20*Fs/1000);    # one spectral slice every 20 ms
          window=ceil(100*Fs/1000); # 100 ms data window
          specgram(x, 2^nextpow2(window), Fs, window, window-step);

          ## Speech spectrogram
          [x, Fs] = auload(file_in_loadpath("sample.wav")); # audio file
          step = fix(5*Fs/1000);     # one spectral slice every 5 ms
          window = fix(40*Fs/1000);  # 40 ms data window
          fftn = 2^nextpow2(window); # next highest power of 2
          [S, f, t] = specgram(x, fftn, Fs, window, window-step);
          S = abs(S(2:fftn*4000/Fs,:)); # magnitude in range 0<f<=4000 Hz.
          S = S/max(S(:));           # normalize magnitude so that max is 0 dB.
          S = max(S, 10^(-40/10));   # clip below -40 dB.
          S = min(S, 10^(-3/10));    # clip above -3 dB.
          imagesc (t, f, log(S));    # display in log scale
          set (gca, "ydir", "normal"); # put the 'y' direction in the correct direction

  The choice of window defines the time-frequency resolution. In speech for example, a wide window shows more harmonic detail while a narrow window averages over the harmonic detail and shows more formant structure. The shape of the window is not so critical so long as it goes gradually to zero on the ends.

  Step size (which is window length minus overlap) controls the horizontal scale of the spectrogram. Decrease it to stretch, or increase it to compress. Increasing step size will reduce time resolution, but decreasing it will not improve it much beyond the limits imposed by the window size (you do gain a little bit, depending on the shape of your window, as the peak of the window slides over peaks in the signal energy). The range 1-5 msec is good for speech.

  FFT length controls the vertical scale. Selecting an FFT length greater than the window length does not add any information to the spectrum, but it is a good way to interpolate between frequency points which can make for prettier spectrograms.

  After you have generated the spectral slices, there are a number of decisions for displaying them. First the phase information is discarded and the energy normalized:

  S = abs(S); S = S/max(S(:));

  Then the dynamic range of the signal is chosen. Since information in speech is well above the noise floor, it makes sense to eliminate any dynamic range at the bottom end. This is done by taking the max of the magnitude and some minimum energy such as minE=-40dB. Similarly, there is not much information in the very top of the range, so clipping to a maximum energy such as maxE=-3dB makes sense:

  S = max(S, 10^(minE/10)); S = min(S, 10^(maxE/10));

  The frequency range of the FFT is from 0 to the Nyquist frequency of one half the sampling rate. If the signal of interest is band limited, you do not need to display the entire frequency range. In speech for example, most of the signal is below 4 kHz, so there is no reason to display up to the Nyquist frequency of 10 kHz for a 20 kHz sampling rate. In this case you will want to keep only the first 40% of the rows of the returned S and f. More generally, to display the frequency range [minF, maxF], you could use the following row index:

  idx = (f >= minF & f <= maxF);

  Then there is the choice of colormap. A brightness varying colormap such as copper or bone gives good shape to the ridges and valleys. A hue varying colormap such as jet or hsv gives an indication of the steepness of the slopes. The final spectrogram is displayed in log energy scale and by convention has low frequencies on the bottom of the image:

  imagesc(t, f, flipud(log(S(idx,:))));

#### 3.1.17 square

Function File: `s =` square `(t, duty)`
Function File: `s =` square `(t)`

  Generate a square wave of period 2 pi with limits +1/-1.

  If duty is specified, it is the percentage of time the square wave is "on". The square wave is +1 for that portion of the time.

                         on time * 100
          duty cycle = ------------------
                       on time + off time

  See also: cos, sawtooth, sin, tripuls.

#### 3.1.18 tripuls

Function File: `y =` tripuls `(t)`
Function File: `y =` tripuls `(t, w)`
Function File: `y =` tripuls `(t, w, skew)`

  Generate a triangular pulse over the interval [-w/2,w/2), sampled at times t. This is useful with the function `pulstran` for generating a series of pulses.

  skew is a value between -1 and 1, indicating the relative placement of the peak within the width. -1 indicates that the peak should be at -w/2, and 1 indicates that the peak should be at w/2. The default value is 0.

  Example:

       fs = 11025;  # arbitrary sample rate
       f0 = 100;    # pulse train sample rate
       w = 0.3/f0;  # pulse width 3/10th the distance between pulses
       plot (pulstran (0:1/fs:4/f0, 0:1/f0:4/f0, "tripuls", w));

  See also: gauspuls, pulstran, rectpuls.

#### 3.1.19 udecode

Function File: `out =` udecode `(in, n)`
Function File: `out =` udecode `(in, n, v)`
Function File: `out =` udecode `(in, n, v, overflows)`

  Invert the operation of uencode.

  See also: uencode.

#### 3.1.20 uencode

Function File: `out =` uencode `(in, n)`
Function File: `out =` uencode `(in, n, v)`
Function File: `out =` uencode `(in, n, v, signed)`

  Quantize the entries of the array in using 2^n quantization levels.

  See also: udecode.

#### 3.1.21 unshiftdata

Function File: `[out] =` unshiftdata `(in, perm, shifts)`

  Reverse what is done by shiftdata.

  See also: shiftdata.

#### 3.1.22 vco

: `y =` vco `(x, fc, fs)`
: `y =` vco `(x, [fmin, fmax], fs)`

  Creates a signal that oscillates at a frequency determined by input x with a sampling frequency fs.

  Inputs:

  - x - input data with a range of -1 to 1. A value of -1 means no output, 0 cuoreesponds to fc, and 1 corresponds to 2*fc.
  - fc - Carrier frequency
  - fs - Sampling rate
  - fmin, fmax - Frequency modulation range limits.

  Outputs:

  - y - output signal

### 3.2 Signal Measurement

#### 3.2.1 findpeaks

Function File: `[pks, loc, extra] =` findpeaks `(data)`
Function File: `… =` findpeaks `(…, property, value)`
Function File: `… =` findpeaks `(…, "DoubleSided")`

  Finds peaks on data.

  Peaks of a positive array of data are defined as local maxima. For double-sided data, they are maxima of the positive part and minima of the negative part. data is expected to be a single column vector.

  The function returns the value of data at the peaks in pks. The index indicating their position is returned in loc.

  The third output argument is a structure with additional information:

  "parabol"

    A structure containing the parabola fitted to each returned peak. The structure has two fields, "x" and "pp". The field "pp" contains the coefficients of the 2nd degree polynomial and "x" the extrema of the interval where it was fitted.

  "height"

    The estimated height of the returned peaks (in units of data).

  "baseline"

    The height at which the roots of the returned peaks were calculated (in units of data).

  "roots"

    The abscissa values (in index units) at which the parabola fitted to each of the returned peaks realizes its width as defined below.

  This function accepts property-value pair given in the list below:

  "MinPeakHeight"

    Minimum peak height (non-negative scalar). Only peaks that exceed this value will be returned. For data taking positive and negative values use the option "DoubleSided". Default value `eps`.

  "MinPeakDistance"

    Minimum separation between (positive integer). Peaks separated by less than this distance are considered a single peak. This distance is also used to fit a second order polynomial to the peaks to estimate their width, therefore it acts as a smoothing parameter. The neighborhood size is equal to the value of "MinPeakDistance". Default value 1.

  "MinPeakWidth"

    Minimum width of peaks (non-negative scalar). The width of the peaks is estimated using a parabola fitted to the neighborhood of each peak. The width is calculated with the formula

         a * width^2 = 1

    where a is the the concavity of the parabola. Default value `eps`.

  "MaxPeakWidth"

    Maximum width of peaks (positive integer). Default value `Inf`.

  "DoubleSided"

    Tells the function that data takes positive and negative values. The base-line for the peaks is taken as the mean value of the function. This is equivalent as passing the absolute value of the data after removing the mean.

  Run `demo findpeaks` to see some examples.

#### 3.2.2 peak2peak

Function File: `y =` peak2peak `(x)`
Function File: `y =` peak2peak `(x, dim)`

  Compute the difference between the maximum and minimum values in the vector x.

  If x is a matrix, compute the difference for each column and return them in a row vector.

  If the optional argument dim is given, operate along this dimension.

  See also: max, min, peak2rms, rms, rssq.

#### 3.2.3 peak2rms

Function File: `y =` peak2rms `(x)`
Function File: `y =` peak2rms `(x, dim)`

  Compute the ratio of the largest absolute value to the root-mean-square (RMS) value of the vector x.

  If x is a matrix, compute the peak-magnitude-to-RMS ratio for each column and return them in a row vector.

  If the optional argument dim is given, operate along this dimension.

  See also: max, min, peak2peak, rms, rssq.

#### 3.2.4 rms

rms has moved to the octave core from v11.0.0 onwards

#### 3.2.5 rssq

Function File: `y =` rssq `(x)`
Function File: `y =` rssq `(x, dim)`

  Compute the root-sum-of-squares (RSS) of the vector x.

  The root-sum-of-squares is defined as

       rssq (x) = SQRT (SUM_i x(i)^2)

  If x is a matrix, compute the root-sum-of-squares for each column and return them in a row vector.

  If the optional argument dim is given, operate along this dimension.

  See also: mean, meansq, sumsq, rms.

### 3.3 Correlation and Convolution

#### 3.3.1 cconv

Function File: `c =` cconv `(a, b, n)`
Function File: `c =` cconv `(a, b)`

  Compute the modulo-N circular convolution.

  a and b are input vectors and c is the modolo-n convolution of a and b. If n is not provided, its assumed default value is `length(a) + length(b) - 1`, which provides the same result as a linear convolution.

  Examples:

       cconv (1:2, 1:4)
          ⇒  1   4   7   10   8

       cconv (1:2, 1:4, 2)
          ⇒  16   14

       cconv (1:2, 1:4, 4)
          ⇒  9   4   7   10

  See also: conv, circshift.

#### 3.3.2 convmtx

Function File: convmtx `(a, n)`

  If a is a column vector and x is a column vector of length n, then

  `convmtx(a, n) * x`

  gives the convolution of of a and x and is the same as `conv(a, x)`. The difference is if many vectors are to be convolved with the same vector, then this technique is possibly faster.

  Similarly, if a is a row vector and x is a row vector of length n, then

  `x * convmtx(a, n)`

  is the same as `conv(x, a)`.

See also: conv.

#### 3.3.3 wconv

Function File: `y =` wconv `(type, x, f)`
Function File: `y =` wconv `(type, x, f, shape)`

  1-D or 2-D convolution.

  Inputs

  type

    Type of convolution.

  x

    Signal vector or matrix.

  f

    Coefficients of FIR filter.

  shape

    Shape.

  Outputs

  y

    Convoluted signal.

#### 3.3.4 xcorr

Function File: `[R, lag] =` xcorr `( X )`
Function File: `… =` xcorr `( X, Y )`
Function File: `… =` xcorr `( …, maxlag)`
Function File: `… =` xcorr `( …, scale)`

  Estimates the cross-correlation.

  Estimate the cross correlation R_xy(k) of vector arguments X and Y or, if Y is omitted, estimate autocorrelation R_xx(k) of vector X, for a range of lags k specified by argument "maxlag". If X is a matrix, each column of X is correlated with itself and every other column.

  The cross-correlation estimate between vectors "x" and "y" (of length N) for lag "k" is given by

                  N
       R_xy(k) = sum x_{i+k} conj(y_i),
                 i=1

  where data not provided (for example x(-1), y(N+1)) is zero. Note the definition of cross-correlation given above. To compute a cross-correlation consistent with the field of statistics, see `xcov`.

  ARGUMENTS

  X

    [non-empty; real or complex; vector or matrix] data

  Y

    [real or complex vector] data

    If X is a matrix (not a vector), Y must be omitted. Y may be omitted if X is a vector; in this case xcorr estimates the autocorrelation of X.

  maxlag

    [integer scalar] maximum correlation lag If omitted, the default value is N-1, where N is the greater of the lengths of X and Y or, if X is a matrix, the number of rows in X.

  scale

    [character string] specifies the type of scaling applied to the correlation vector (or matrix). is one of:

    ‘`none`’

      return the unscaled correlation, R,

    ‘`biased`’

      return the biased average, R/N,

    ‘`unbiased`’

      return the unbiased average, R(k)/(N-|k|),

    ‘`coeff or normalized`’

      return the correlation coefficient, R/(rms(x).rms(y)), where "k" is the lag, and "N" is the length of X. If omitted, the default value is "none". If Y is supplied but does not have the same length as X, scale must be "none".

  RETURNED VARIABLES

  R

    array of correlation estimates

  lag

    row vector of correlation lags [-maxlag:maxlag]

  The array of correlation estimates has one of the following forms: (1) Cross-correlation estimate if X and Y are vectors.

  (2) Autocorrelation estimate if is a vector and Y is omitted.

  (3) If X is a matrix, R is an matrix containing the cross-correlation estimate of each column with every other column. Lag varies with the first index so that R has 2*maxlag+1 rows and P^2 columns where P is the number of columns in X.

  If Rij(k) is the correlation between columns i and j of X

  `R(k+maxlag+1,P*(i-1)+j) == Rij(k)`

  for lag k in [-maxlag:maxlag], or

  `R(:,P*(i-1)+j) == xcorr(X(:,i),X(:,j))`.

  `reshape(R(k,:),P,P)` is the cross-correlation matrix for `X(k,:)`.

  See also: xcov.

#### 3.3.5 xcorr2

Function File: xcorr2 `(a)`
Function File: xcorr2 `(a, b)`
Function File: xcorr2 `(…, scale)`

  Compute the 2D cross-correlation of matrices a and b.

  If b is not specified, computes autocorrelation of a, i.e., same as `xcorr (a, a)`.

  The optional argument scale, defines the type of scaling applied to the cross-correlation matrix. Possible values are:

  "none" (default)

    No scaling.

  "biased"

    Scales the raw cross-correlation by the maximum number of elements of a and b involved in the generation of any element of c.

  "unbiased"

    Scales the raw correlation by dividing each element in the cross-correlation matrix by the number of products a and b used to generate that element.

  "coeff"

    Scales the normalized cross-correlation on the range of [0 1] so that a value of 1 corresponds to a correlation coefficient of 1.

  See also: conv2, corr2, xcorr.

#### 3.3.6 xcov

Function File: `[R, lag] =` xcov `( X )`
Function File: `… =` xcov `( X, Y )`
Function File: `… =` xcov `( …, maxlag)`
Function File: `… =` xcov `( …, scale)`

  Compute covariance at various lags [=correlation(x-mean(x),y-mean(y))].

  X

    input vector

  Y

    if specified, compute cross-covariance between X and Y, otherwise compute autocovariance of X.

  maxlag

    is specified, use lag range [-maxlag:maxlag], otherwise use range [-n+1:n-1].

  scale:
    ‘`biased`’

      for covariance=raw/N,

    ‘`unbiased`’

      for covariance=raw/(N-|lag|),

    ‘`coeff`’

      for covariance=raw/(covariance at lag 0),

    ‘`none`’

      for covariance=raw

    ‘`none`’

      is the default.

  Returns the covariance for each lag in the range, plus an optional vector of lags.

  See also: xcorr.

### 3.4 Filtering

#### 3.4.1 filtfilt

Function File: `y =` filtfilt `(b, a, x)`

  Forward and reverse filter the signal. This corrects for phase distortion introduced by a one-pass filter, though it does square the magnitude response in the process. That’s the theory at least. In practice the phase correction is not perfect, and magnitude response is distorted, particularly in the stop band.

  Example

       [b, a]=butter(3, 0.1);                  # 5 Hz low-pass filter
       t = 0:0.01:1.0;                         # 1 second sample
       x=sin(2*pi*t*2.3)+0.25*randn(size(t));  # 2.3 Hz sinusoid+noise
       y = filtfilt(b,a,x); z = filter(b,a,x); # apply filter
       plot(t,x,';data;',t,y,';filtfilt;',t,z,';filter;')

#### 3.4.2 filtic

Function File: `zf =` filtic `(b, a, y)`
Function File: `zf =` filtic `(b, a, y, x)`

  Set initial condition vector for filter function The vector zf has the same values that would be obtained from function filter given past inputs x and outputs y

  The vectors x and y contain the most recent inputs and outputs respectively, with the newest values first:

  x = [x(-1) x(-2) ... x(-nb)], nb = length(b)-1 y = [y(-1) y(-2) ... y(-na)], na = length(a)-a

  If length(x)<nb then it is zero padded If length(y)<na then it is zero padded

  zf = filtic(b, a, y) Initial conditions for filter with coefficients a and b and output vector y, assuming input vector x is zero

  zf = filtic(b, a, y, x) Initial conditions for filter with coefficients a and b input vector x and output vector y

#### 3.4.3 medfilt1

: `y =` medfilt1 `(x, n)`
: `y =` medfilt1 `(x, n, [], dim)`
: `y =` medfilt1 `(..., NaN_flag, padding)`

  Apply a one dimensional median filter with a window size of n to the data x, which must be real, double and full. For n = 2m+1, y(i) is the median of x(i-m:i+m). For n = 2m, y(i) is the median of x(i-m:i+m-1).

  The calculation is performed over the first non-singleton dimension, or over dimension dim if that is specified as the fourth argument. (The third argument is ignored; Matlab used to use it to tune its algorithm.)

  NaN_flag may be `omitnan` or `includenan` (the default). If it is `omitnan` then any NaN values are removed from the window before the median is taken. Otherwise, any window containing an NaN returns a median of NaN.

  padding determines how the partial windows at the start and end of x are treated. It may be `truncate` or `zeropad` (the default). If it is `truncate` then the window for y(i) is the intersection of the window stated above with 1:length(x). If it is `zeropad`, then partial windows have additional zeros to bring them up to size n.

  See also: filter, medfilt2.

#### 3.4.4 movingrms

Function File: `[rmsx,w] =` movingrms `(x,w,rc,Fs=1)`

  Calculate moving RMS value of the signal in x.

  The signal is convoluted against a sigmoid window of width w and risetime rc. The units of these parameters are relative to the value of the sampling frequency given in Fs (Default value = 1).

  Run `demo movingrms` to see an example.

  See also: sigmoid_train.

#### 3.4.5 sgolayfilt

Function File: `y =` sgolayfilt `(x)`
Function File: `y =` sgolayfilt `(x, p)`
Function File: `y =` sgolayfilt `(x, p, n)`
Function File: `y =` sgolayfilt `(x, p, n, m)`
Function File: `y =` sgolayfilt `(x, p, n, m, ts)`
Function File: `y =` sgolayfilt `(x, p, n, m, ts)`
Function File: `y =` sgolayfilt `(x, f)`

  Smooth the data in x with a Savitsky-Golay smoothing filter of polynomial order p and length n, n odd, n > p. By default, p=3 and n=p+2 or n=p+3 if p is even.

  If f is given as a matrix, it is expected to be a filter as computed by `sgolay`.

  These filters are particularly good at preserving lineshape while removing high frequency squiggles. Particularly, compare a 5 sample averager, an order 5 butterworth lowpass filter (cutoff 1/3) and sgolayfilt(x, 3, 5), the best cubic estimated from 5 points:

       [b, a] = butter (5, 1/3);
       x = [zeros(1,15), 10*ones(1,10), zeros(1,15)];
       plot (sgolayfilt (x), "r;sgolayfilt;", ...
             filtfilt (ones (1,5)/5, 1, x), "g;5 sample average;", ...
             filtfilt (b, a, x), "c;order 5 butterworth;", ...
             x, "+b;original data;");

  See also: sgolay.

#### 3.4.6 sosfilt

Loadable Function: `y =` sosfilt `(sos, x)`

  Second order section IIR filtering of x. The second order section filter is described by the matrix sos with:

  | [ B1 A1 ]
  sos = | [ … ],
  | [ BN AN ]

  where `B1 = [b0 b1 b2]` and `A1 = [1 a1 a2]` for section 1, etc. The b0 entry must be nonzero for each section.

### 3.5 Filter Analysis

#### 3.5.1 filternorm

Function File: `L =` filternorm `(b, a)`
Function File: `L =` filternorm `(b, a, pnorm)`
Function File: `L =` filternorm `(b, a, 2, tol)`

  Compute the 2-norm of a digital filter defined by the numerator coefficients, b, and the denominator coefficients, a. It is also possible to compute the infinity-norm by passing inf in the pnorm parameter. pnorm only accepts 2 or inf.

  Example:

       [b, a] = butter (8, 0.5);
       filternorm (b, a)

#### 3.5.2 filtord

Function File: `n =` filtord `(b, a)`
Function File: `n =` filtord `(sos)`

  Returns the filter order n for a filter defined by the numerator coefficients, b, and the denominator coefficients, a. It also accepts a filter defined by a matrix of second-order sections, sos.

  Example:

       [b, a] = butter (8, 0.5);
       filtord (b, a)

#### 3.5.3 freqs

Function File: `h =` freqs `(b, a, w)`
Function File: freqs `(b, a, w)`

  Compute the s-plane frequency response of the IIR filter B(s)/A(s) as H = polyval(B,j*W)./polyval(A,j*W). If called with no output argument, a plot of magnitude and phase are displayed.

  Example:

       b = [1 2]; a = [1 1];
       w = linspace (0, 4, 128);
       freqs (b, a, w);

#### 3.5.4 freqs_plot

Function File: freqs_plot `(w, h)`

  Plot the amplitude and phase of the vector h.

#### 3.5.5 fwhm

Function File: `f =` fwhm `(y)`
Function File: `f =` fwhm `(x, y)`
Function File: `f =` fwhm `(…, "zero")`
Function File: `f =` fwhm `(…, "min")`
Function File: `f =` fwhm `(…, "alevel", level)`
Function File: `f =` fwhm `(…, "rlevel", level)`

  Compute peak full-width at half maximum (FWHM) or at another level of peak maximum for vector or matrix data y, optionally sampled as y(x). If y is a matrix, return FWHM for each column as a row vector.

  The default option "zero" computes fwhm at half maximum, i.e. 0.5*max(y). The option "min" computes fwhm at the middle curve, i.e. 0.5*(min(y)+max(y)).

  The option "rlevel" computes full-width at the given relative level of peak profile, i.e. at rlevel*max(y) or rlevel*(min(y)+max(y)), respectively. For example, `fwhm (…, "rlevel", 0.1)` computes full width at 10 % of peak maximum with respect to zero or minimum; FWHM is equivalent to `fwhm(…, "rlevel", 0.5)`.

  The option "alevel" computes full-width at the given absolute level of y.

  Return 0 if FWHM does not exist (e.g. monotonous function or the function does not cut horizontal line at rlevel*max(y) or rlevel*(max(y)+min(y)) or alevel, respectively).

#### 3.5.6 grpdelay

Function File: `[g, w] =` grpdelay `(b)`
Function File: `[g, w] =` grpdelay `(b, a)`
Function File: `[g, w] =` grpdelay `(…, n)`
Function File: `[g, w] =` grpdelay `(…, n, "whole")`
Function File: `[g, f] =` grpdelay `(…, n, Fs)`
Function File: `[g, f] =` grpdelay `(…, n, "whole", Fs)`
Function File: `[g, w] =` grpdelay `(…, w)`
Function File: `[g, f] =` grpdelay `(…, f, Fs)`
Function File: grpdelay `(…)`

  Compute the group delay of a filter.

  [g, w] = grpdelay(b) returns the group delay g of the FIR filter with coefficients b. The response is evaluated at 512 angular frequencies between 0 and pi. w is a vector containing the 512 frequencies. The group delay is in units of samples. It can be converted to seconds by multiplying by the sampling period (or dividing by the sampling rate fs).

  [g, w] = grpdelay(b,a) returns the group delay of the rational IIR filter whose numerator has coefficients b and denominator coefficients a.

  [g, w] = grpdelay(b,a,n) returns the group delay evaluated at n angular frequencies. For fastest computation n should factor into a small number of small primes.

  [g, w] = grpdelay(b,a,n,’whole’) evaluates the group delay at n frequencies between 0 and 2*pi.

  [g, f] = grpdelay(b,a,n,Fs) evaluates the group delay at n frequencies between 0 and Fs/2.

  [g, f] = grpdelay(b,a,n,’whole’,Fs) evaluates the group delay at n frequencies between 0 and Fs.

  [g, w] = grpdelay(b,a,w) evaluates the group delay at frequencies w (radians per sample).

  [g, f] = grpdelay(b,a,f,Fs) evaluates the group delay at frequencies f (in Hz).

  grpdelay(...) plots the group delay vs. frequency.

  If the denominator of the computation becomes too small, the group delay is set to zero. (The group delay approaches infinity when there are poles or zeros very close to the unit circle in the z plane.)

  Theory: group delay, g(w) = -d/dw [arg{H(e^jw)}], is the rate of change of phase with respect to frequency. It can be computed as:

                     d/dw H(e^-jw)
              g(w) = -------------
                       H(e^-jw)

  where

               H(z) = B(z)/A(z) = sum(b_k z^k)/sum(a_k z^k).

  By the quotient rule,

                          A(z) d/dw B(z) - B(z) d/dw A(z)
              d/dw H(z) = -------------------------------
                                     A(z) A(z)

  Substituting into the expression above yields:

                      A dB - B dA
              g(w) =  ----------- = dB/B - dA/A
                          A B

  Note that,

              d/dw B(e^-jw) = sum(k b_k e^-jwk)
              d/dw A(e^-jw) = sum(k a_k e^-jwk)

  which is just the FFT of the coefficients multiplied by a ramp.

  As a further optimization when nfft>>length(a), the IIR filter (b,a) is converted to the FIR filter conv(b,fliplr(conj(a))). For further details, see http://ccrma.stanford.edu/~jos/filters/Numerical_Computation_Group_Delay.html

#### 3.5.7 impz

Function File: `[x, t] =` impz `(b)`
Function File: `[x, t] =` impz `(b, a)`
Function File: `[x, t] =` impz `(b, a, n)`
Function File: `[x, t] =` impz `(b, a, n, fs)`
Function File: impz `(…)`

  Generate impulse-response characteristics of the filter. The filter coefficients correspond to the the z-plane rational function with numerator b and denominator a. If a is not specified, it defaults to 1. If n is not specified, or specified as [], it will be chosen such that the signal has a chance to die down to -120dB, or to not explode beyond 120dB, or to show five periods if there is no significant damping. If no return arguments are requested, plot the results.

  See also: freqz, zplane.

#### 3.5.8 isallpass

Function File: `L =` isallpass `(b, a)`
Function File: `L =` isallpass `(sos)`

  Determine whether a digital filter is allpass. The filter might be defined by the numerator coefficients, b, and the denominator coefficients, a, or, alternatively, by a matrix of second-order sections, sos.

  Example:

       a = [1 2 3];
       b = [3 2 1];
       isallpass (b, a)

  Ref [1] Shyu, Jong-Jy, & Pei, Soo-Chang, A new approach to the design of complex all-pass IIR digital filters, Signal Processing, 40(2–3), 207–215, 1994. https://doi.org/10.1016/0165-1684(94)90068-x

  Ref [2] Vaidyanathan, P. P. Multirate Systems and Filter Banks. 1st edition, Pearson College Div, 1992.

#### 3.5.9 ismaxphase

Function File: `L =` ismaxphase `(b, a)`
Function File: `L =` ismaxphase `(sos)`
Function File: `L =` ismaxphase `(…, tol)`

  Determine whether a digital filter is maximum phase (maximum energy-delay). The filter might be defined by the numerator coefficients, b, and the denominator coefficients, a, or, alternatively, by a matrix of second-order sections, sos. A tolerance tol might be given to define when two numbers are close enough to be considered equal.

  Example:

       b = [1 2 4 4 2 1];
       zplane (b);
       ismaxphase (b)

  Ref [1] Oppenheim, Alan, and Ronald Schafer. Discrete-Time Signal Processing. 3rd edition, Pearson, 2009.

#### 3.5.10 isminphase

Function File: `L =` isminphase `(b, a)`
Function File: `L =` isminphase `(sos)`
Function File: `L =` isminphase `(…, tol)`

  Determine whether a digital filter is minimum phase. The filter might be defined by the numerator coefficients, b, and the denominator coefficients, a, or, alternatively, by a matrix of second-order sections, sos. A toleranve tol might be given to define when two numbers are close enough to be considered equal.

  Example:

       a = [1 0.5]; b = [3 1];
       isminphase (b, a)

  Ref [1] Oppenheim, Alan, and Ronald Schafer. Discrete-Time Signal Processing. 3rd edition, Pearson, 2009.

#### 3.5.11 isstable

Function File: `FLAG =` isstable `(B, A)`

  Returns a logical output equal to TRUE, if the filter is stable. This can be done with coeffients of the filer B and A. Alternatively by using a second order sections matrix (SOS).

  Inputs:

  - B: Numerator coefficients of the filter
  - A: Denominator coeffients of the filter. Can be an empty vector.

  Output:

  - FLAG: Returns a logical output, equal to TRUE if the filter is stable.

  Examples:

         b = [1 2 3 4 5 5 1 2];
         a = [4 5 6 7 9 10 4 6];
         flag = isstable (b, a)
         flag = 0

  Using SOS

         [z, p, k] = butter (6, 0.7, 'high');
         sos = zp2sos (z, p, k);
         flag = isstable (sos)
         flag = 1

#### 3.5.12 phasez

Function File: `[phi, w] =` phasez `(b, a, n)`
Function File: `[phi, w] =` phasez `(b, a)`
Function File: `[phi, w] =` phasez `(sos, n)`
Function File: `[phi, w] =` phasez `(sos)`
Function File: `[phi, w] =` phasez `(…, n, "whole")`
Function File: `[phi, w] =` phasez `(…, n, Fs)`
Function File: phasez `(…)`

  Compute the phase response of digital filter defined either by its coefficients (b and a are the numerator and denominator coefficients respectively) or by its second-order sections representation, given by the matrix sos. The output phi is the phase response computed in a vector the vector of frequencies w.

  The phase response is evaluated at n angular frequencies between 0 and pi.

  If a is omitted, the denominator is assumed to be 1 (this corresponds to a simple FIR filter).

  If n is omitted, a value of 512 is assumed.

  If the third/forth argument, `"whole"`, is given, the response is evaluated at n angular frequencies between 0 and 2*pi. It is possible also to pass the value `"half"`, which will lead to the default behaviour.

  Example:

       [b, a] = butter (2, [.15,.3]);
       phasez (b, a);

  Ref [1] Oppenheim, Alan, and Ronald Schafer. Discrete-Time Signal Processing. 3rd edition, Pearson, 2009.

  See also: freqz, phasedelay.

#### 3.5.13 zplane

Function File: zplane `(z, p)`
Function File: zplane `(b, a)`

  Plot the poles and zeros on a complex plane. If the arguments are column vectors z and p, the complex zeros z and poles p are displayed. If the arguments are row vectors b and a, the zeros and poles of the transfer function represented by these filter coefficients are displayed.

  If z and p are matrices, the columns are distinct sets of zeros and poles and are displayed together in distinct colors.

  Note that due to the nature of the `roots` function, poles and zeros may be displayed as occurring around a circle rather than at a single point.

  The transfer function is

              B(z)   b0 + b1 z^(-1) + b2 z^(-2) + ... + bM z^(-M)
       H(z) = ---- = --------------------------------------------
              A(z)   a0 + a1 z^(-1) + a2 z^(-2) + ... + aN z^(-N)

                     b0          (z - z1) (z - z2) ... (z - zM)
                   = -- z^(-M+N) ------------------------------
                     a0          (z - p1) (z - p2) ... (z - pN)

  If called with only one argument, the poles p defaults to an empty vector, and the denominator coefficient vector a defaults to 1.

### 3.6 Filter Conversion

#### 3.6.1 residued

Function File: `[r, p, f, m] =` residued `(b, a)`

  Compute the partial fraction expansion (PFE) of filter H(z) = B(z)/A(z). In the usual PFE function `residuez`, the IIR part (poles p and residues r) is driven in parallel with the FIR part (f). In this variant, the IIR part is driven by the output of the FIR part. This structure can be more accurate in signal modeling applications.

  INPUTS: b and a are vectors specifying the digital filter H(z) = B(z)/A(z). See `help filter` for documentation of the b and a filter coefficients.

  RETURNED:

  - r = column vector containing the filter-pole residues
  - p = column vector containing the filter poles
  - f = row vector containing the FIR part, if any
  - m = column vector of pole multiplicities

  EXAMPLES:

       See test residued verbose to see a number of examples.

  For the theory of operation, see ‘`http://ccrma.stanford.edu/~jos/filters/residued.html`’

  See also: residue, residued.

#### 3.6.2 residuez

Function File: `[r, p, f, m] =` residuez `(b, a)`

  Compute the partial fraction expansion of filter H(z) = B(z)/A(z).

  INPUTS: b and a are vectors specifying the digital filter H(z) = B(z)/A(z). See `help filter` for documentation of the b and a filter coefficients.

  RETURNED:

  - r = column vector containing the filter-pole residues
  - p = column vector containing the filter poles
  - f = row vector containing the FIR part, if any
  - m = column vector of pole multiplicities

  EXAMPLES:

       See test residuez verbose to see a number of examples.

  For the theory of operation, see ‘`http://ccrma.stanford.edu/~jos/filters/residuez.html`’

  See also: residue, residued.

#### 3.6.3 sos2cell

Function File: `C =` sos2cell `(S)`
Function File: `C =` sos2cell `(S, G)`

  Convert a second-order-section matrix to a cell array.

  S is an L-by-6 matrix, where each row represents a second-order section in the form:

       S = [B1 A1;
            B2 A2;
            ...
            BL AL]

  where Bi and Ai are the numerator and denominator coefficients of a linear or quadratic polynomial. The function converts this matrix into a cell array C with the following format:

       C = { {B1, A1}, {B2, A2}, ..., {BL, AL} }

  Each element of the cell array is a cell containing a pair of vectors: Bi and Ai.

  If an additional gain term G is provided, the function returns:

       C = { {G, 1}, {B1, A1}, {B2, A2}, ..., {BL, AL} }

  where {G, 1} represents the constant gain term applied to the filter.

       S = [ [1, 2, 3, 4, 5, 6];
             [7, 8, 9, 10, 11, 12] ];
       C = sos2cell(S);

#### 3.6.4 sos2ss

Function File: `[a, b, c, d] =` sos2ss `(sos)`

  Convert series second-order sections to state-space.

  See also: sos2ss, ss2tf.

#### 3.6.5 sos2tf

Function File: `[b, a] =` sos2tf `(sos)`
Function File: `[b, a] =` sos2tf `(sos, g)`

  Convert series second-order sections to transfer function.

  INPUTS:

  - sos = matrix of series second-order sections, one per row:

        sos = [B1.' A1.'; ...; BN.' AN.']

    where `B1.' = [b0 b1 b2] and A1.' = [a0 a1 a2]` for section 1, etc.

    a0 is usually equal to 1 because all 2nd order transfer functions can be scaled so that a0 = 1. However, this is not mandatory for this implementation, which supports all kinds of transfer functions, including first order transfer functions. See `filter` for documentation of the second-order direct-form filter coefficients Bi and Ai.

  - g is an overall gain factor that effectively scales the output b vector (or any one of the input Bi vectors). If not given the gain is assumed to be 1.

  RETURNED: b and a are vectors specifying the analog or digital filter H(s) = B(s)/A(s) or H(z) = B(z)/A(z). See `filter` for further details.

  See also: tf2sos, zp2sos, sos2pz, zp2tf, tf2zp.

#### 3.6.6 sos2zp

Function File: `[z, p, k] =` sos2zp `(sos)`
Function File: `[z, p, k] =` sos2zp `(sos, g)`

  Convert series second-order sections to zeros, poles, and gains (pole residues).

  INPUTS:

  - sos = matrix of series second-order sections, one per row:

        sos = [B1.' A1.'; ...; BN.' AN.']

    where `B1.' = [b0 b1 b2] and A1.' = [a0 a1 a2]` for section 1, etc.

    a0 is usually equal to 1 because all 2nd order transfer functions can be scaled so that a0 = 1. However, this is not mandatory for this implementation, which supports all kinds of transfer functions, including first order transfer functions. See `filter` for documentation of the second-order direct-form filter coefficients Bi and Ai.

  - g is an overall gain factor that effectively scales any one of the input Bi vectors. If not given the gain is assumed to be 1.

  RETURNED:

  - z = column-vector containing all zeros (roots of B(z))
  - p = column-vector containing all poles (roots of A(z))
  - k = overall gain = B(Inf)

  EXAMPLE:

       [z, p, k] = sos2zp ([1 0 1, 1 0 -0.81; 1 0 0, 1 0 0.49])
         ⇒ z =
            0 + 1i
            0 - 1i
            0 + 0i
            0 + 0i
         ⇒ p =
           -0.9000 + 0i
            0.9000 + 0i
            0 + 0.7000i
            0 - 0.7000i
         ⇒ k =  1

  See also: zp2sos, sos2tf, tf2sos, zp2tf, tf2zp.

#### 3.6.7 ss2tf

Function File: `[num, den] =` ss2tf `(a, b, c, d)`

  Conversion from state-space to transfer function representation. The state space system:

             .
             x = Ax + Bu
             y = Cx + Du

  is converted to a transfer function:

                       num(s)
                 G(s)=-------
                       den(s)

#### 3.6.8 ss2zp

Function File: `[z, p, k] =` ss2zp `(a, b, c, d)`

  Converts a state space representation to a set of poles and zeros; k is a gain associated with the zeros.

#### 3.6.9 tf2sos

Function File: `[sos, g] =` tf2sos `(b, a)`
Function File: `sos =` tf2sos `(b, a)`

  Convert direct-form filter coefficients to series second-order sections.

  INPUTS:

  b and a are vectors specifying the digital filter H(z) = B(z)/A(z). See `filter` for documentation of the b and a filter coefficients.

  RETURNED:

  - sos = matrix of series second-order sections, one per row:

        sos = [b1.' a1.'; ...; bn.' an.']

    where `B1.' = [b0 b1 b2] and A1.' = [1 a1 a2]` for section 1, etc. The b0 entry must be nonzero for each section (zeros at infinity not supported).

  - g is an overall gain factor that effectively scales any one of the Bi vectors.

  If called with only one output argument, the overall filter gain is applied to the first second-order section in the matrix sos.

  EXAMPLE:

       B = [1 0 0 0 0 1];
       A = [1 0 0 0 0 .9];
       [sos, g] = tf2sos (B, A)

       sos =

          1.00000   0.61803   1.00000   1.00000   0.60515   0.95873
          1.00000  -1.61803   1.00000   1.00000  -1.58430   0.95873
          1.00000   1.00000  -0.00000   1.00000   0.97915  -0.00000

       g = 1

  See also: sos2tf, zp2sos, sos2pz, zp2tf, tf2zp.

#### 3.6.10 tf2ss

Function File: `[a, b, c, d] =` tf2ss `(num, den)`

  Conversion from transfer function to state-space. The state space system:

             .
             x = Ax + Bu
             y = Cx + Du

  is obtained from a transfer function:

                       num(s)
                 G(s)=-------
                       den(s)

  The state space system matrices obtained from this function will be in observable companion form as Wolovich’s Observable Structure Theorem is used.

#### 3.6.11 tf2zp

Function File: `[z, p, k] =` tf2zp `(num, den)`

  Convert transfer functions to poles-and-zero representations.

  Returns the zeros and poles of the system defined by num/den. k is a gain associated with the system zeros.

#### 3.6.12 zp2sos

Function File: `[sos, g] =` zp2sos `(z)`
Function File: `[sos, g] =` zp2sos `(z, p)`
Function File: `[sos, g] =` zp2sos `(z, p, k)`
Function File: `sos =` zp2sos `(…)`

  Convert filter poles and zeros to second-order sections.

  INPUTS:

  - z = column-vector containing the filter zeros
  - p = column-vector containing the filter poles
  - k = overall filter gain factor. If not given the gain is assumed to be 1.

  RETURNED:

  - sos = matrix of series second-order sections, one per row:

        sos = [B1.' A1.'; ...; BN.' AN.']

    where `B1.' = [b0 b1 b2] and A1.' = [a0 a1 a2]` for section 1, etc. See `filter` for documentation of the second-order direct-form filter coefficients Bi and %Ai, i=1:N.

  - g is the overall gain factor that effectively scales any one of the Bi vectors.

  If called with only one output argument, the overall filter gain is applied to the first second-order section in the matrix sos.

  EXAMPLE:

         [z, p, k] = tf2zp ([1 0 0 0 0 1], [1 0 0 0 0 .9]);
         [sos, g] = zp2sos (z, p, k)

       sos =
          1.0000    0.6180    1.0000    1.0000    0.6051    0.9587
          1.0000   -1.6180    1.0000    1.0000   -1.5843    0.9587
          1.0000    1.0000         0    1.0000    0.9791         0

       g =
           1

  See also: sos2zp, sos2tf, tf2sos, zp2tf, tf2zp.

#### 3.6.13 zp2ss

Function File: `[a, b, c, d] =` zp2ss `(z, p, k)`

  Conversion from zero / pole to state space.

  Inputs

  z
  p

    Vectors of (possibly) complex poles and zeros of a transfer function. Complex values must come in conjugate pairs (i.e., x+jy in z means that x-jy is also in z).

  k

    Real scalar (leading coefficient).

  Outputs

  a
  b
  c
  d

    The state space system, in the form:

              .
              x = Ax + Bu
              y = Cx + Du

#### 3.6.14 zp2tf

Function File: `[num, den] =` zp2tf `(z, p, k)`

  Converts zeros / poles to a transfer function.

  Inputs

  z
  p

    Vectors of (possibly complex) poles and zeros of a transfer function. Complex values must appear in conjugate pairs.

  k

    Real scalar (leading coefficient).

### 3.7 IIR Filter Design

#### 3.7.1 besselap

Function File: `[zero, pole, gain] =` besselap `(n)`

  Return bessel analog filter prototype.

  References:

  http://en.wikipedia.org/wiki/Bessel_polynomials

#### 3.7.2 besself

Function File: `[b, a] =` besself `(n, w)`
Function File: `[b, a] =` besself `(n, w, "high")`
Function File: `[z, p, g] =` besself `(…)`
Function File: `[a, b, c, d] =` besself `(…)`
Function File: `[…] =` besself `(…, "z")`

  Generate a Bessel filter. Default is a Laplace space (s) filter.

  [b,a] = besself(n, Wc) low pass filter with cutoff pi*Wc radians

  [b,a] = besself(n, Wc, ’high’) high pass filter with cutoff pi*Wc radians

  [z,p,g] = besself(...) return filter as zero-pole-gain rather than coefficients of the numerator and denominator polynomials.

  [...] = besself(...,’z’) return a discrete space (Z) filter, W must be less than 1.

  [a,b,c,d] = besself(...) return state-space matrices

  References:

  Proakis & Manolakis (1992). Digital Signal Processing. New York: Macmillan Publishing Company.

#### 3.7.3 bilinear

Function File: `[Zb, Za] =` bilinear `(Sb, Sa, T)`
Function File: `[Zb, Za] =` bilinear `(Sz, Sp, Sg, T)`
Function File: `[Zz, Zp, Zg] =` bilinear `(…)`

  Transform a s-plane filter specification into a z-plane specification. Filters can be specified in either zero-pole-gain or transfer function form. The input form does not have to match the output form. 1/T is the sampling frequency represented in the z plane.

  Note: this differs from the bilinear function in the signal processing toolbox, which uses 1/T rather than T.

  Theory: Given a piecewise flat filter design, you can transform it from the s-plane to the z-plane while maintaining the band edges by means of the bilinear transform. This maps the left hand side of the s-plane into the interior of the unit circle. The mapping is highly non-linear, so you must design your filter with band edges in the s-plane positioned at 2/T tan(w*T/2) so that they will be positioned at w after the bilinear transform is complete.

  The following table summarizes the transformation:

       +---------------+-----------------------+----------------------+
       | Transform     | Zero at x             | Pole at x            |
       |    H(S)       |   H(S) = S-x          |    H(S)=1/(S-x)      |
       +---------------+-----------------------+----------------------+
       |       2 z-1   | zero: (2+xT)/(2-xT)   | zero: -1             |
       |  S -> - ---   | pole: -1              | pole: (2+xT)/(2-xT)  |
       |       T z+1   | gain: (2-xT)/T        | gain: (2-xT)/T       |
       +---------------+-----------------------+----------------------+

  With tedious algebra, you can derive the above formulae yourself by substituting the transform for S into H(S)=S-x for a zero at x or H(S)=1/(S-x) for a pole at x, and converting the result into the form:

          H(Z)=g prod(Z-Xi)/prod(Z-Xj)

  Please note that a pole and a zero at the same place exactly cancel. This is significant since the bilinear transform creates numerous extra poles and zeros, most of which cancel. Those which do not cancel have a "fill-in" effect, extending the shorter of the sets to have the same number of as the longer of the sets of poles and zeros (or at least split the difference in the case of the band pass filter). There may be other opportunistic cancellations but I will not check for them.

  Also note that any pole on the unit circle or beyond will result in an unstable filter. Because of cancellation, this will only happen if the number of poles is smaller than the number of zeros. The analytic design methods all yield more poles than zeros, so this will not be a problem.

  References:

  Proakis & Manolakis (1992). Digital Signal Processing. New York: Macmillan Publishing Company.

#### 3.7.4 buttap

Function File: `[z, p, g] =` buttap `(n)`

  Design lowpass analog Butterworth filter.

  This function exists for MATLAB compatibility only, and is equivalent to `butter (n, 1, "s")`.

  See also: butter.

#### 3.7.5 butter

Function File: `[b, a] =` butter `(n, wc)`
Function File: `[b, a] =` butter `(n, wc, filter_type)`
Function File: `[z, p, g] =` butter `(…)`
Function File: `[a, b, c, d] =` butter `(…)`
Function File: `[…] =` butter `(…, "s")`

  Generate a Butterworth filter. Default is a discrete space (Z) filter.

  The cutoff frequency, wc should be specified in radians for analog filters. For digital filters, it must be a value between zero and one. For bandpass filters, wc is a two-element vector with `w(1) < w(2)`.

  The filter type must be one of `"low"`, `"high"`, `"bandpass"`, or `"stop"`. The default is `"low"` if wc is a scalar and `"bandpass"` if wc is a two-element vector.

  If the final input argument is `"s"` design an analog Laplace space filter.

  Low pass filter with cutoff `pi*Wc` radians:

       [b, a] = butter (n, Wc)

  High pass filter with cutoff `pi*Wc` radians:

       [b, a] = butter (n, Wc, "high")

  Band pass filter with edges `pi*Wl` and `pi*Wh` radians:

       [b, a] = butter (n, [Wl, Wh])

  Band reject filter with edges `pi*Wl` and `pi*Wh` radians:

       [b, a] = butter (n, [Wl, Wh], "stop")

  Return filter as zero-pole-gain rather than coefficients of the numerator and denominator polynomials:

       [z, p, g] = butter (...)

  Return a Laplace space filter, Wc can be larger than 1:

       [...] = butter (..., "s")

  Return state-space matrices:

       [a, b, c, d] = butter (...)

  References:

  Proakis & Manolakis (1992). Digital Signal Processing. New York: Macmillan Publishing Company.

#### 3.7.6 buttord

Function File: `n =` buttord `(wp, ws, rp, rs)`
Function File: `n =` buttord `([wp1, wp2], [ws1, ws2], rp, rs)`
Function File: `n =` buttord `([wp1, wp2], [ws1, ws2], rp, rs, "s")`
Function File: `[n, wc_p] =` buttord `(…)`
Function File: `[n, wc_p, wc_s] =` buttord `(…)`

  Compute the minimum filter order of a Butterworth filter with the desired response characteristics. The filter frequency band edges are specified by the passband frequency wp and stopband frequency ws. Frequencies are normalized to the Nyquist frequency in the range [0,1]. rp is the allowable passband ripple measured in decibels, and rs is the minimum attenuation in the stop band, also in decibels.

  The output arguments n and wc_p (or n and wc_n) can be given as inputs to `butter`. Using wc_p makes the filter characteristic touch at least one pass band corner and using wc_s makes the characteristic touch at least one stop band corner.

  If wp and ws are scalars, then wp is the passband cutoff frequency and ws is the stopband edge frequency. If ws is greater than wp, the filter is a low-pass filter. If wp is greater than ws, the filter is a high-pass filter.

  If wp and ws are vectors of length 2, then wp defines the passband interval and ws defines the stopband interval. If wp is contained within ws (ws1 < wp1 < wp2 < ws2), the filter is a band-pass filter. If ws is contained within wp (wp1 < ws1 < ws2 < wp2), the filter is a band-stop or band-reject filter.

  If the optional argument `"s"` is given, the minimum order for an analog elliptic filter is computed. All frequencies wp and ws are specified in radians per second.

  Theory: For Low pass filters, |H(W)|^2 = 1/[1+(W/Wc)^(2N)] = 10^(-R/10). With some algebra, you can solve simultaneously for Wc and N given Ws,Rs and Wp,Rp. Rounding N to the next greater integer, one can recalculate the allowable range for Wc (filter caracteristic touching the pass band edge or the stop band edge).

  For other types of filter, before making the above calculation, the requirements must be transformed to LP requirements. After calculation, Wc must be transformed back to original filter type.

  See also: butter, cheb1ord, cheb2ord, ellipord.

#### 3.7.7 cheb

Function File: cheb `(n, x)`

  Returns the value of the nth-order Chebyshev polynomial calculated at the point x. The Chebyshev polynomials are defined by the equations:

                 / cos(n acos(x),    |x| <= 1
         Tn(x) = |
                 \ cosh(n acosh(x),  |x| > 1

  If x is a vector, the output is a vector of the same size, where each element is calculated as y(i) = Tn(x(i)).

#### 3.7.8 cheb1ap

Function File: `[z, p, g] =` cheb1ap `(n, Rp)`

  Design lowpass analog Chebyshev type I filter.

  This function exists for MATLAB compatibility only, and is equivalent to `cheby1 (n, Rp, 1, "s")`.

  Input:

  - N Order of the filter must be a positive integer
  - RP Ripple in the passband in dB

  Output:

  - z The zero vector
  - p The pole vectorAngle
  - g The gain factor

  Example

       [z, p, g] = cheb1ap (2, 1)
       z = [](0x1)
       p =

        -0.54887 - 0.89513i
        -0.54887 + 0.89513i

       g =  0.98261

  See also: buttap, cheby1, cheb2ap, ellipap.

#### 3.7.9 cheb1ord

Function File: `n =` cheb1ord `(wp, ws, rp, rs)`
Function File: `n =` cheb1ord `([wp1, wp2], [ws1, ws2], rp, rs)`
Function File: `n =` cheb1ord `([wp1, wp2], [ws1, ws2], rp, rs, "s")`
Function File: `[n, wc] =` cheb1ord `(…)`
Function File: `[n, wc_p, wc_s] =` cheb1ord `(…)`

  Compute the minimum filter order of a Chebyshev type I filter with the desired response characteristics. The filter frequency band edges are specified by the passband frequency wp and stopband frequency ws. Frequencies are normalized to the Nyquist frequency in the range [0,1]. rp is the allowable passband ripple measured in decibels, and rs is the minimum attenuation in the stop band, also in decibels.

  The output arguments n and wc_p (or n and wc_s) can be given as inputs to `cheby1`. Using wc_p makes the filter characteristic touch at least one pass band corner and using wc_s makes the characteristic touch at least one stop band corner.

  If wp and ws are scalars, then wp is the passband cutoff frequency and ws is the stopband edge frequency. If ws is greater than wp, the filter is a low-pass filter. If wp is greater than ws, the filter is a high-pass filter.

  If wp and ws are vectors of length 2, then wp defines the passband interval and ws defines the stopband interval. If wp is contained within ws (ws1 < wp1 < wp2 < ws2), the filter is a band-pass filter. If ws is contained within wp (wp1 < ws1 < ws2 < wp2), the filter is a band-stop or band-reject filter.

  If the optional argument `"s"` is given, the minimum order for an analog elliptic filter is computed. All frequencies wp and ws are specified in radians per second.

  See also: buttord, cheby1, cheb2ord, ellipord.

#### 3.7.10 cheb2ap

Function File: `[z, p, g] =` cheb2ap `(n, Rs)`

  Design lowpass analog Chebyshev type II filter.

  This function exists for MATLAB compatibility only, and is equivalent to `cheby2 (n, Rs, 1, "s")`.

  Demo

       demo cheb2ap

  See also: cheby2.

#### 3.7.11 cheb2ord

Function File: `n =` cheb2ord `(wp, ws, rp, rs)`
Function File: `n =` cheb2ord `([wp1, wp2], [ws1, ws2], rp, rs)`
Function File: `n =` cheb2ord `([wp1, wp2], [ws1, ws2], rp, rs, "s")`
Function File: `[n, wc_s] =` cheb2ord `(…)`
Function File: `[n, wc_s, wc_p] =` cheb2ord `(…)`

  Compute the minimum filter order of a Chebyshev type II filter with the desired response characteristics. The filter frequency band edges are specified by the passband frequency wp and stopband frequency ws. Frequencies are normalized to the Nyquist frequency in the range [0,1]. rp is the allowable passband ripple measured in decibels, and rs is the minimum attenuation in the stop band, also in decibels.

  The output arguments n and wc_p (or n and wc_s) can be given as inputs to `cheby2`. Using wc_p makes the filter characteristic touch at least one pass band corner and using wc_s makes the characteristic touch at least one stop band corner.

  If wp and ws are scalars, then wp is the passband cutoff frequency and ws is the stopband edge frequency. If ws is greater than wp, the filter is a low-pass filter. If wp is greater than ws, the filter is a high-pass filter.

  If wp and ws are vectors of length 2, then wp defines the passband interval and ws defines the stopband interval. If wp is contained within ws (ws1 < wp1 < wp2 < ws2), the filter is a band-pass filter. If ws is contained within wp (wp1 < ws1 < ws2 < wp2), the filter is a band-stop or band-reject filter.

  If the optional argument `"s"` is given, the minimum order for an analog elliptic filter is computed. All frequencies wp and ws are specified in radians per second.

  See also: buttord, cheb1ord, cheby2, ellipord.

#### 3.7.12 cheby1

Function File: `[b, a] =` cheby1 `(n, rp, w)`
Function File: `[b, a] =` cheby1 `(n, rp, w, "high")`
Function File: `[b, a] =` cheby1 `(n, rp, [wl, wh])`
Function File: `[b, a] =` cheby1 `(n, rp, [wl, wh], "stop")`
Function File: `[z, p, g] =` cheby1 `(…)`
Function File: `[a, b, c, d] =` cheby1 `(…)`
Function File: `[…] =` cheby1 `(…, "s")`

  Generate a Chebyshev type I filter with rp dB of passband ripple.

  [b, a] = cheby1(n, Rp, Wc) low pass filter with cutoff pi*Wc radians

  [b, a] = cheby1(n, Rp, Wc, ’high’) high pass filter with cutoff pi*Wc radians

  [b, a] = cheby1(n, Rp, [Wl, Wh]) band pass filter with edges pi*Wl and pi*Wh radians

  [b, a] = cheby1(n, Rp, [Wl, Wh], ’stop’) band reject filter with edges pi*Wl and pi*Wh radians

  [z, p, g] = cheby1(...) return filter as zero-pole-gain rather than coefficients of the numerator and denominator polynomials.

  [...] = cheby1(...,’s’) return a Laplace space filter, W can be larger than 1.

  [a,b,c,d] = cheby1(...) return state-space matrices

  References:

  Parks & Burrus (1987). Digital Filter Design. New York: John Wiley & Sons, Inc.

#### 3.7.13 cheby2

Function File: `[b, a] =` cheby2 `(n, rs, wc)`
Function File: `[b, a] =` cheby2 `(n, rs, wc, "high")`
Function File: `[b, a] =` cheby2 `(n, rs, [wl, wh])`
Function File: `[b, a] =` cheby2 `(n, rs, [wl, wh], "stop")`
Function File: `[z, p, g] =` cheby2 `(…)`
Function File: `[a, b, c, d] =` cheby2 `(…)`
Function File: `[…] =` cheby2 `(…, "s")`

  Generate a Chebyshev type II filter with rs dB of stopband attenuation.

  [b, a] = cheby2(n, Rs, Wc) low pass filter with cutoff pi*Wc radians

  [b, a] = cheby2(n, Rs, Wc, ’high’) high pass filter with cutoff pi*Wc radians

  [b, a] = cheby2(n, Rs, [Wl, Wh]) band pass filter with edges pi*Wl and pi*Wh radians

  [b, a] = cheby2(n, Rs, [Wl, Wh], ’stop’) band reject filter with edges pi*Wl and pi*Wh radians

  [z, p, g] = cheby2(...) return filter as zero-pole-gain rather than coefficients of the numerator and denominator polynomials.

  [...] = cheby2(...,’s’) return a Laplace space filter, W can be larger than 1.

  [a,b,c,d] = cheby2(...) return state-space matrices

  References:

  Parks & Burrus (1987). Digital Filter Design. New York: John Wiley & Sons, Inc.

#### 3.7.14 ellip

Function File: `[b, a] =` ellip `(n, rp, rs, wp)`
Function File: `[b, a] =` ellip `(n, rp, rs, wp, "high")`
Function File: `[b, a] =` ellip `(n, rp, rs, [wl, wh])`
Function File: `[b, a] =` ellip `(n, rp, rs, [wl, wh], "stop")`
Function File: `[z, p, g] =` ellip `(…)`
Function File: `[a, b, c, d] =` ellip `(…)`
Function File: `[…] =` ellip `(…, "s")`

  Generate an elliptic or Cauer filter with rp dB of passband ripple and rs dB of stopband attenuation.

  [b,a] = ellip(n, Rp, Rs, Wp) low pass filter with order n, cutoff pi*Wp radians, Rp decibels of ripple in the passband and a stopband Rs decibels down.

  [b,a] = ellip(n, Rp, Rs, Wp, ’high’) high pass filter with cutoff pi*Wp...

  [b,a] = ellip(n, Rp, Rs, [Wl, Wh]) band pass filter with band pass edges pi*Wl and pi*Wh ...

  [b,a] = ellip(n, Rp, Rs, [Wl, Wh], ’stop’) band reject filter with edges pi*Wl and pi*Wh, ...

  [z,p,g] = ellip(...) return filter as zero-pole-gain.

  [...] = ellip(...,’s’) return a Laplace space filter, W can be larger than 1.

  [a,b,c,d] = ellip(...) return state-space matrices

  References:

  - Oppenheim, Alan V., Discrete Time Signal Processing, Hardcover, 1999. - Parente Ribeiro, E., Notas de aula da disciplina TE498 - Processamento Digital de Sinais, UFPR, 2001/2002.

#### 3.7.15 ellipap

Function File: `[z, p, g] =` ellipap `(n, Rp, Rs)`

  Design lowpass analog elliptic filter.

  This function exists for MATLAB compatibility only, and is equivalent to `ellip (n, Rp, Rs, 1, "s")`.

  See also: ellip.

#### 3.7.16 ellipord

Function File: `n =` ellipord `(wp, ws, rp, rs)`
Function File: `n =` ellipord `([wp1, wp2], [ws1, ws2], rp, rs)`
Function File: `n =` ellipord `([wp1, wp2], [ws1, ws2], rp, rs, "s")`
Function File: `[n, wc] =` ellipord `(…)`

  Compute the minimum filter order of an elliptic filter with the desired response characteristics. The filter frequency band edges are specified by the passband frequency wp and stopband frequency ws. Frequencies are normalized to the Nyquist frequency in the range [0,1]. rp is the allowable passband ripple measured in decibels, and rs is the minimum attenuation in the stop band, also in decibels. The output arguments n and wc can be given as inputs to `ellip`.

  If wp and ws are scalars, then wp is the passband cutoff frequency and ws is the stopband edge frequency. If ws is greater than wp, the filter is a low-pass filter. If wp is greater than ws, the filter is a high-pass filter.

  If wp and ws are vectors of length 2, then wp defines the passband interval and ws defines the stopband interval. If wp is contained within ws (ws1 < wp1 < wp2 < ws2), the filter is a band-pass filter. If ws is contained within wp (wp1 < ws1 < ws2 < wp2), the filter is a band-stop or band-reject filter.

  If the optional argument `"s"` is given, the minimum order for an analog elliptic filter is computed. All frequencies wp and ws are specified in radians per second.

  Reference: Lamar, Marcus Vinicius, Notas de aula da disciplina TE 456 - Circuitos Analogicos II, UFPR, 2001/2002.

  See also: buttord, cheb1ord, cheb2ord, ellip.

#### 3.7.17 firpm

Loadable Function: `b =` firpm `(n, f, a)`
Loadable Function: `b =` firpm `(n, f, @respFn)`
Loadable Function: `b =` firpm `(n, f, {@respFn, …})`
Loadable Function: `b =` firpm `(…, w)`
Loadable Function: `b =` firpm `(…, class)`
Loadable Function: `b =` firpm `(…, {accuracy, …})`
Loadable Function: `[b, minimax] =` firpm `(…)`
Loadable Function: `[b, minimax, res] =` firpm `(…)`

  Designs a linear-phase FIR filter according to given specifications and the ‘minimax’ criterion. The method (per McClellan et al.1) uses successive approximation to minimize the maximum weighted error between the desired and actual frequency response of the filter. Such filters are variably described as being ‘minimax’, ‘equiripple’, or ‘optimal (in the Chebyshev sense)’.

  ### Arguments

  …

    Where shown as the first argument to `firpm`, indicates that any previously-indicated list of arguments may substitute for the ellipsis.

  n

    A positive integer giving the filter order.

  f

    A vector of real-numbers, increasing in the range [0,1], giving the frequencies of the left and right edges of each band for which a specific amplitude response is desired: [l1 r1 l2 r2 …]. 1 represents the Nyquist-frequency. Transition-bands are defined implicitly as the regions between or outside the given bands.

  a

    A vector of real-numbers giving the desired amplitude response. An amplitude value is given either for each band edge: [a(l1) a(r1) a(l2) a(r2) …], or for each band: [a1 a2 …]. In the former case, in-band amplitude is determined by linear interpolation between the given band-edge values. 1 represents unity-gain, 0 represents infinite attenuation, and −1 represents a phase change of pi radians.

    Note that amplitude response is necessarily zero at f=0 for type III and IV filters, and at f=1 for type II and III filters.

  @respFn

    A handle to a ‘response function’ that supplies the desired amplitude response and error-weighting. This, unlike a above, allows the response to be arbitrary (subject to the note above). `firpm` invokes the response function according to the following syntax:

        ag = respFn (n,f,g,w, ...)
        [ag wg] = respFn (n,f,g,w, ...)
        symmetry = respFn ("defaults", {n,f,g,w, ...})

    where:

    - n and f are as given to `firpm`.
    - w is as given to `firpm`, or ones if not given.
    - ag and wg are the desired amplitude and weighting functions evaluated at each frequency in vector g (which are frequencies within the non-transition bands of f). Returning ag alone gives uniform weighting.
    - symmetry is either `"even"` or `"odd"`; this provides an alternative to using the class values `"symmetric"` and `"antisymmetric"`.
    - Per the ellipses shown here and above, when @respFn is given contained in a cell-array, any additionally contained values are appended to the respFn invocation argument-list.
  w

    When used in conjunction with a, w is a vector of positive real-numbers giving error-weightings to be applied at each given band-edge [w(l1) w(r1) w(l2) w(r2) …], or for each band [w1 w2 …]. In the former case, in-band weighting is determined by linear interpolation between the given band-edge values. A higher relative error weighting yields a lower relative error.

    When used in conjunction with @respFn, w is a vector (constrained as above) that is passed through to respFn.

  class

    A string, which may be abbreviated, giving the filter-class:

    - `"symmetric"` (the default) for type I or II filters,
    - `"antisymmetric"` (or `"hilbert"`) for standard type III or IV filters,
    - `"differentiator"` for type III or IV filters with inverted phase and with error-weighting (further to w) of 2/f applied in the pass-band(s).
  accuracy, …

    Up to three properties contained within a cell-array: accuracy, persistence, robustness, that respectively control how close the computed filter will be to the ideal minimax solution, the number of computation iterations over which the required accuracy will be sought, and the precision of certain internal processing. Each can each be set to a small positive number (typically ≤3), to increase the relevant item; this may increase computation time, but the need to do so should be rare. A value of 0 can be used to leave an item unchanged.

    Alternatively, setting accuracy ≥16 emulates MATLAB’s lgrid argument.

  ### Results

  If a problem occurs during the computation, a diagnostic message will normally be displayed. If this happens, adjusting accuracy, persistence, or robustness may provide the solution. Some filters however, may not be realizable due to machine-precision limitations. If a filter can be computed, returned values are as follows:

  b

    A length N+1 row-vector containing the computed filter coefficients.

  minimax

    The absolute value of the minimized, maximum weighted error, or this number negated if the required accuracy could not be achieved.

  res

    A structure of data relating to the filter computation and a partial response-analysis of the resultant filter; fields are vectors:

    > `fgrid` | Analysis frequencies per f.
    > `des` | Desired amplitude response.
    > `wt` | Error weighting.
    > `H` | Complex frequency response.
    > `error` | Desired minus actual amplitude response.
    > `iextr` | Indices of local peaks in `error`.
    > `fextr` | Frequencies of local peaks in `error`.

  Using res is not recommended because it can be slow to compute and, since the analysis excludes transition-bands, any ‘anomalies’2 therein are not easy to discern. In general, `freqz` suffices to check that the response of the computed filter is satisfactory.

  ### Examples

      # Low-pass with frequencies in Hz:
      Fs = 96000; Fn = Fs/2; # Sampling & Nyquist frequencies.
      b = firpm (50, [0 20000 28000 48000] / Fn, [1 0]);

      # Type IV high-pass:
      b = firpm (31, [0 0.5 0.7 1], [0 1], "antisym");

      # Inverse-sinc (arbitrary response):
      b = firpm (20, [0 0.5 0.9 1], @(n,f,g) ...
      deal ((g<=f(2))./sinc (g), (g>=f(3))*9+1));

      # Band-pass with filter-response check:
      freqz (firpm (40, [0 3 4 6 8 10]/10, [0 1 0]))

  Further examples can be found in the `firpm` and `firpmord` demonstration scripts.

  ### Compatibility

  Given invalid filter specifications, Octave emits an error and does not produce a filter; MATLAB in such circumstances may still produce filter coefficients.

  Unlike with MATLAB, with Octave minimax can be negative; for compatibility, take the absolute value.

  See also: firpmord.

#### 3.7.18 firpmord

Function File: `[n, Fout, a, w] =` firpmord `(f, a, d)`
Function File: `[n, Fout, a, w] =` firpmord `(f, a, d, fs)`
Function File: `c =` firpmord `(f, a, d, "cell")`
Function File: `c =` firpmord `(f, a, d, fs, "cell")`

  Estimate the filter-order needed for `firpm` to design a type-I or type-II linear-phase FIR filter according to the given specifications.

  ### Arguments

  f

    A vector of real-numbers, increasing in the range (0, fs/2), giving the frequencies of the left and right edges of each band for which a specific amplitude response is desired (omitting 0 and fs/2, which are implied): [r1 l2 r2 …]. Transition-bands are defined implicitly as the regions between the given bands.

  a

    A vector of real-numbers giving the ideal amplitude response. An amplitude value is given for each band specified by f: [a1 a2 …]. 1 represents unity-gain, 0 represents infinite attenuation, and −1 represents a phase change of pi radians.

  d

    A vector of positive real-numbers giving the maximum allowable linear deviation from the amplitudes given in a, thus constraining the actual amplitude response (where defined by f) to be within a +/− d. Note that, though related, d does not equate to `firpm`’s w argument.

  fs

    The sampling-frequency, which defaults to 2.

  ### Usage

  The function returns the estimated filter-order, together with the other design specification values, in one of two forms suitable for use with `firpm`. By default, multiple return values are used; alternatively, by giving `"cell"` (or `"c"`) as the last argument to `firpmord`, the returned values are contained within a cell-array that can, if desired, be passed directly to `firpm`.

  The following examples illustrate the use of both mechanisms, as well as aspects of `firpmord` usage in general:

       # Low-pass; frequencies in kHz:
       [n f a w] = firpmord ([2.5 3], [1 0], [0.01 db2mag(-60)], 8);
       b = firpm (n, f, a, w);

       # Band-pass:
       c = firpmord ([3 4 8 9], [0 1 0], [1e-3 1e-2 1e-3], 20, "cell");
       b = firpm (c{:});

       # High-pass:
       b = firpm (firpmord ([6.4 8]/16, [0 1], [1e-4 0.01], "c"){:});

  In cases where elements of d follow a repeating pattern (e.g. all the elements are equal, or elements corresponding to pass-bands are equal and elements corresponding to stop-bands are equal), only as many elements as are needed to establish the pattern need be given.

  For example, the following `firpmord` invocation pairs are equivalent:

       # Low-pass:
       firpmord ([0.4 0.5], [0 1], [db2mag(-72) db2mag(-72)]);
       firpmord ([0.4 0.5], [0 1], [db2mag(-72)]);

       # Multi-band-pass:
       ds = db2mag(-80); dp = 0.01;
       firpmord ([1 2 3 4 5 6 7 8]/10, [0 1 0 1 0], [ds dp ds dp ds]);
       firpmord ([1 2 3 4 5 6 7 8]/10, [0 1 0 1 0], [ds dp]);

  ### Notes

  The estimation algorithm used is per Ichige et al.3 Accuracy tends to decrease as the number of bands increases. Even with two bands (i.e. high-pass or low-pass), the algorithm may under- or over-estimate. See the `firpmord` demonstrations for some examples.

  In order to precisely determine the minimum order needed for a particular design, `firpmord` could be used to seed an algorithm iterating invocations of `firpm` (as exemplified in demonstration number five).

  ### Related documentation

  See also: firpm, kaiserord.

#### 3.7.19 impinvar

Function File: `[b_out, a_out] =` impinvar `(b, a, fs, tol)`
Function File: `[b_out, a_out] =` impinvar `(b, a, fs)`
Function File: `[b_out, a_out] =` impinvar `(b, a)`

  Converts analog filter with coefficients b and a to digital, conserving impulse response.

  If fs is not specified, or is an empty vector, it defaults to 1Hz.

  If tol is not specified, it defaults to 0.0001 (0.1%) This function does the inverse of impinvar so that the following example should restore the original values of a and b.

  `invimpinvar` implements the reverse of this function.

       [b, a] = impinvar (b, a);
       [b, a] = invimpinvar (b, a);

  Reference: Thomas J. Cavicchi (1996) “Impulse invariance and multiple-order poles”. IEEE transactions on signal processing, Vol 44 (9): 2344–2347

  See also: bilinear, invimpinvar.

#### 3.7.20 invimpinvar

Function File: `[b_out, a_out] =` invimpinvar `(b, a, fs, tol)`
Function File: `[b_out, a_out] =` invimpinvar `(b, a, fs)`
Function File: `[b_out, a_out] =` invimpinvar `(b, a)`

  Converts digital filter with coefficients b and a to analog, conserving impulse response.

  This function does the inverse of impinvar so that the following example should restore the original values of a and b.

       [b, a] = impinvar (b, a);
       [b, a] = invimpinvar (b, a);

  If fs is not specified, or is an empty vector, it defaults to 1Hz.

  If tol is not specified, it defaults to 0.0001 (0.1%)

  Reference: Thomas J. Cavicchi (1996) “Impulse invariance and multiple-order poles”. IEEE transactions on signal processing, Vol 40 (9): 2344–2347

  See also: bilinear, impinvar.

#### 3.7.21 ncauer

Function File: `[z, p, g] =` cauer(Rp, `Rs, n)`

  Analog prototype for Cauer filter.

  Rp

    Passband ripple

  Rs

    Stopband ripple

  n

    Desired order

  z

    complex vector of zeros for the model.

  p

    complex vector of poles for the model.

  g

    gain value.

  References:

  - Serra, Celso Penteado, Teoria e Projeto de Filtros, Campinas: CARTGRAF, 1983.

  - Lamar, Marcus Vinicius, Notas de aula da disciplina TE 456 - Circuitos Analogicos II, UFPR, 2001/2002.

#### 3.7.22 pei_tseng_notch

Function File: `[b, a] =` pei_tseng_notch `(frequencies, bandwidths)`

  Return coefficients for an IIR notch-filter with one or more filter frequencies and according (very narrow) bandwidths to be used with `filter` or `filtfilt`. The filter construction is based on an allpass which performs a reversal of phase at the filter frequencies. Thus, the mean of the phase-distorted and the original signal has the respective frequencies removed. See the demo for an illustration.

  Original source: Pei, Soo-Chang, and Chien-Cheng Tseng "IIR Multiple Notch Filter Design Based on Allpass Filter" 1996 IEEE Tencon doi: 10.1109/TENCON.1996.608814)

#### 3.7.23 sftrans

Function File: `[Sz, Sp, Sg] =` sftrans `(Sz, Sp, Sg, W, stop)`

  Transform band edges of a generic lowpass filter (cutoff at W=1) represented in splane zero-pole-gain form. W is the edge of the target filter (or edges if band pass or band stop). Stop is true for high pass and band stop filters or false for low pass and band pass filters. Filter edges are specified in radians, from 0 to pi (the nyquist frequency).

  Theory: Given a low pass filter represented by poles and zeros in the splane, you can convert it to a low pass, high pass, band pass or band stop by transforming each of the poles and zeros individually. The following table summarizes the transformation:

       Transform         Zero at x                  Pole at x
       ----------------  -------------------------  ------------------------
       Low Pass          zero: Fc x/C               pole: Fc x/C
       S -> C S/Fc       gain: C/Fc                 gain: Fc/C
       ----------------  -------------------------  ------------------------
       High Pass         zero: Fc C/x               pole: Fc C/x
       S -> C Fc/S       pole: 0                    zero: 0
                         gain: -x                   gain: -1/x
       ----------------  -------------------------  ------------------------
       Band Pass         zero: b +- sqrt(b^2-FhFl)  pole: b +- sqrt(b^2-FhFl)
              S^2+FhFl   pole: 0                    zero: 0
       S -> C --------   gain: C/(Fh-Fl)            gain: (Fh-Fl)/C
              S(Fh-Fl)   b=x/C (Fh-Fl)/2            b=x/C (Fh-Fl)/2
       ----------------  -------------------------  ------------------------
       Band Stop         zero: b +- sqrt(b^2-FhFl)  pole: b +- sqrt(b^2-FhFl)
              S(Fh-Fl)   pole: +-sqrt(-FhFl)        zero: +-sqrt(-FhFl)
       S -> C --------   gain: -x                   gain: -1/x
              S^2+FhFl   b=C/x (Fh-Fl)/2            b=C/x (Fh-Fl)/2
       ----------------  -------------------------  ------------------------
       Bilinear          zero: (2+xT)/(2-xT)        pole: (2+xT)/(2-xT)
            2 z-1        pole: -1                   zero: -1
       S -> - ---        gain: (2-xT)/T             gain: (2-xT)/T
            T z+1
       ----------------  -------------------------  ------------------------

  where C is the cutoff frequency of the initial lowpass filter, Fc is the edge of the target low/high pass filter and [Fl,Fh] are the edges of the target band pass/stop filter. With abundant tedious algebra, you can derive the above formulae yourself by substituting the transform for S into H(S)=S-x for a zero at x or H(S)=1/(S-x) for a pole at x, and converting the result into the form:

          H(S)=g prod(S-Xi)/prod(S-Xj)

  The transforms are from the references. The actual pole-zero-gain changes I derived myself.

  Please note that a pole and a zero at the same place exactly cancel. This is significant for High Pass, Band Pass and Band Stop filters which create numerous extra poles and zeros, most of which cancel. Those which do not cancel have a "fill-in" effect, extending the shorter of the sets to have the same number of as the longer of the sets of poles and zeros (or at least split the difference in the case of the band pass filter). There may be other opportunistic cancellations but I will not check for them.

  Also note that any pole on the unit circle or beyond will result in an unstable filter. Because of cancellation, this will only happen if the number of poles is smaller than the number of zeros and the filter is high pass or band pass. The analytic design methods all yield more poles than zeros, so this will not be a problem.

  References:

  Proakis & Manolakis (1992). Digital Signal Processing. New York: Macmillan Publishing Company.

### 3.8 FIR Filter Design

#### 3.8.1 cl2bp

Loadable Function: `h =` cl2bp `(m, w1, w2, up, lo)`
Loadable Function: `h =` cl2bp `(m, w1, w2, up, lo, gridsize)`

  Constrained L2 bandpass FIR filter design. This is a fast implementation of the algorithm cited below. Compared to remez, it offers implicit specification of transition bands, a higher likelihood of convergence, and an error criterion combining features of both L2 and Chebyshev approaches.

  Inputs:

  m

    degree of cosine polynomial, i.e. the number of output coefficients will be m*2+1

  w1
  w2

    bandpass filter cutoffs in the range 0 <= w1 < w2 <= pi, where pi is the Nyquist frequency

  up

    vector of 3 upper bounds for [stopband1, passband, stopband2]

  lo

    vector of 3 lower bounds for [stopband1, passband, stopband2]

  gridsize

    search grid size; larger values may improve accuracy, but greatly increase calculation time. Default value is 2048, max value is 1e6.

  Output:

  A vector of m*2+1 FIR coefficients, or an empty value if the solver failed to converge.

  Example:

      h = cl2bp(30, 0.3*pi, 0.6*pi, [0.02, 1.02, 0.02], [-0.02, 0.98, -0.02], 2^11);

  Original Paper: I. W. Selesnick, M. Lang, and C. S. Burrus. A modified algorithm for constrained least square design of multiband FIR filters without specified transition bands. IEEE Trans. on Signal Processing, 46(2):497-501, February 1998.

See also: remez.

#### 3.8.2 fir1

Function File: `b =` fir1 `(n, w)`
Function File: `b =` fir1 `(n, w, type)`
Function File: `b =` fir1 `(n, w, type, window)`
Function File: `b =` fir1 `(n, w, type, window, noscale)`

  Produce an order n FIR filter with the given frequency cutoff w, returning the n+1 filter coefficients in b. If w is a scalar, it specifies the frequency cutoff for a lowpass or highpass filter. If w is a two-element vector, the two values specify the edges of a bandpass or bandstop filter. If w is an N-element vector, each value specifies a band edge of a multiband pass/stop filter.

  The filter type can be specified with one of the following strings: "low", "high", "stop", "pass", "bandpass", "DC-0", or "DC-1". The default is "low" is w is a scalar, "pass" if w is a pair, or "DC-0" if w is a vector with more than 2 elements.

  An optional shaping window can be given as a vector with length n+1. If not specified, a Hamming window of length n+1 is used.

  With the option "noscale", the filter coefficients are not normalized. The default is to normalize the filter such that the magnitude response of the center of the first passband is 1.

  To apply the filter, use the return vector b with the `filter` function, for example `y = filter (b, 1, x)`.

  Examples:

       freqz (fir1 (40, 0.3));
       freqz (fir1 (15, [0.2, 0.5], "stop"));  # note the zero-crossing at 0.1
       freqz (fir1 (15, [0.2, 0.5], "stop", "noscale"));

  See also: filter, fir2.

#### 3.8.3 fir2

Function File: `b =` fir2 `(n, f, m)`
Function File: `b =` fir2 `(n, f, m, grid_n)`
Function File: `b =` fir2 `(n, f, m, grid_n, ramp_n)`
Function File: `b =` fir2 `(n, f, m, grid_n, ramp_n, window)`

  Produce an order n FIR filter with arbitrary frequency response m over frequency bands f, returning the n+1 filter coefficients in b. The vector f specifies the frequency band edges of the filter response and m specifies the magnitude response at each frequency.

  The vector f must be nondecreasing over the range [0,1], and the first and last elements must be 0 and 1, respectively. A discontinuous jump in the frequency response can be specified by duplicating a band edge in f with different values in m.

  The resolution over which the frequency response is evaluated can be controlled with the grid_n argument. The default is 512 or the next larger power of 2 greater than the filter length.

  The band transition width for discontinuities can be controlled with the ramp_n argument. The default is grid_n/25. Larger values will result in wider band transitions but better stopband rejection.

  An optional shaping window can be given as a vector with length n+1. If not specified, a Hamming window of length n+1 is used.

  To apply the filter, use the return vector b with the `filter` function, for example `y = filter (b, 1, x)`.

  Example:

       f = [0, 0.3, 0.3, 0.6, 0.6, 1]; m = [0, 0, 1, 1/2, 0, 0];
       [h, w] = freqz (fir2 (100, f, m));
       plot (f, m, ";target response;", w/pi, abs (h), ";filter response;");

  See also: filter, fir1.

#### 3.8.4 firls

Function File: `b =` firls `(n, f, a)`
Function File: `b =` firls `(n, f, a, w)`

  FIR filter design using least squares method. Returns a length n+1 linear phase filter such that the integral of the weighted mean squared error in the specified bands is minimized.

  The vector f specifies the frequencies of the band edges, normalized so that half the sample frequency is equal to 1. Each band is specified by two frequencies, to the vector must have an even length.

  The vector a specifies the amplitude of the desired response at each band edge.

  The optional argument w is a weighting function that contains one value for each band that weights the mean squared error in that band.

  a must be the same length as f, and w must be half the length of f. n must be even. If given an odd value, `firls` increments it by 1.

  The least squares optimization algorithm for computing FIR filter coefficients is derived in detail in:

  I. Selesnick, "Linear-Phase FIR Filter Design by Least Squares," http://cnx.org/content/m10577

#### 3.8.5 kaiserord

Function File: `[n, Wn, beta, ftype] =` kaiserord `(f, m, dev)`
Function File: `[…] =` kaiserord `(f, m, dev, fs)`

  Return the parameters needed to produce a filter of the desired specification from a Kaiser window. The vector f contains pairs of frequency band edges in the range [0,1]. The vector m specifies the magnitude response for each band. The values of m must be zero for all stop bands and must have the same magnitude for all pass bands. The deviation of the filter dev can be specified as a scalar or a vector of the same length as m. The optional sampling rate fs can be used to indicate that f is in Hz in the range [0,fs/2].

  The returned value n is the required order of the filter (the length of the filter minus 1). The vector Wn contains the band edges of the filter suitable for passing to `fir1`. The value beta is the parameter of the Kaiser window of length n+1 to shape the filter. The string ftype contains the type of filter to specify to `fir1`.

  The Kaiser window parameters n and beta are computed from the relation between ripple (A=-20*log10(dev)) and transition width (dw in radians) discovered empirically by Kaiser:

                 / 0.1102(A-8.7)                        A > 50
          beta = | 0.5842(A-21)^0.4 + 0.07886(A-21)     21 <= A <= 50
                 \ 0.0                                  A < 21

          n = (A-8)/(2.285 dw)

  Example:

       [n, w, beta, ftype] = kaiserord ([1000, 1200], [1, 0], [0.05, 0.05], 11025);
       b = fir1 (n, w, kaiser (n+1, beta), ftype, "noscale");
       freqz (b, 1, [], 11025);

  See also: fir1, kaiser.

#### 3.8.6 qp_kaiser

Function File: qp_kaiser `(nb, at)`
Function File: qp_kaiser `(nb, at, linear)`

  Computes a finite impulse response (FIR) filter for use with a quasi-perfect reconstruction polyphase-network filter bank. This version utilizes a Kaiser window to shape the frequency response of the designed filter. Tha number nb of bands and the desired attenuation at in the stop-band are given as parameters.

  The Kaiser window is multiplied by the ideal impulse response h(n)=a.sinc(a.n) and converted to its minimum-phase version by means of a Hilbert transform.

  By using a third non-null argument, the minimum-phase calculation is omitted at all.

#### 3.8.7 remez

Loadable Function: `b =` remez `(n, f, a)`
Loadable Function: `b =` remez `(n, f, a, w)`
Loadable Function: `b =` remez `(n, f, a, w, ftype)`
Loadable Function: `b =` remez `(n, f, a, w, ftype, griddensity)`

  Parks-McClellan optimal FIR filter design.

  n

    gives the filter order, where the generated filter length taps is n+1

  f

    gives frequency at the band edges [b1 e1 b2 e2 b3 e3 …]

  a

    gives amplitude at the band edges [a(b1) a(e1) a(b2) a(e2) …]

  w

    gives weighting applied to each band

  ftype

    is "bandpass", "hilbert" or "differentiator"

  griddensity

    determines how accurately the filter will be constructed. The minimum value is 16, but higher numbers are slower to compute.

  Frequency is in the range (0, 1), with 1 being the Nyquist frequency.

#### 3.8.8 sgolay

Function File: `f =` sgolay `(p, n)`
Function File: `f =` sgolay `(p, n, m)`
Function File: `f =` sgolay `(p, n, m, ts)`

  Computes the filter coefficients for all Savitzsky-Golay smoothing filters of order p for length n (odd). m can be used in order to get directly the mth derivative. In this case, ts is a scaling factor.

  The early rows of F smooth based on future values and later rows smooth based on past values, with the middle row using half future and half past. In particular, you can use row i to estimate x(k) based on the i-1 preceding values and the n-i following values of x values as y(k) = F(i,:) * x(k-i+1:k+n-i).

  Normally, you would apply the first (n-1)/2 rows to the first k points of the vector, the last k rows to the last k points of the vector and middle row to the remainder, but for example if you were running on a realtime system where you wanted to smooth based on the all the data collected up to the current time, with a lag of five samples, you could apply just the filter on row n-5 to your window of length n each time you added a new sample.

  Reference: Numerical recipes in C. p 650

  See also: sgolayfilt.

### 3.9 Transforms

#### 3.9.1 bitrevorder

Function File: `y =` bitrevorder `(x)`
Function File: `[y i] =` bitrevorder `(x)`

  Reorder the elements of the vector x in bit-reversed order. Equivalent to calling `digitrevorder (x, 2)`.

  See also: digitrevorder, fft, ifft.

#### 3.9.2 cceps

Function File: cceps `(x)`
Function File: cceps `(x, correct)`

  Return the complex cepstrum of the vector x. If the optional argument correct has the value 1, a correction method is applied. The default is not to do this.

#### 3.9.3 cplxreal

Function File: `[zc, zr] =` cplxreal `(z)`
Function File: `[zc, zr] =` cplxreal `(z, tol)`
Function File: `[zc, zr] =` cplxreal `(z, tol, dim)`

  Sort the numbers z into complex-conjugate-valued and real-valued elements. The positive imaginary complex numbers of each complex conjugate pair are returned in zc and the real numbers are returned in zr.

  tol is a weighting factor in the range [0, 1) which determines the tolerance of the matching. The default value is `100 * eps` and the resulting tolerance for a given complex pair is `tol * abs (z(i)))`.

  By default the complex pairs are sorted along the first non-singleton dimension of z. If dim is specified, then the complex pairs are sorted along this dimension.

  Signal an error if some complex numbers could not be paired. Signal an error if all complex numbers are not exact conjugates (to within tol). Note that there is no defined order for pairs with identical real parts but differing imaginary parts.

  See also: cplxpair.

#### 3.9.4 czt

Function File: czt `(x)`
Function File: czt `(x, m)`
Function File: czt `(x, m, w)`
Function File: czt `(x, m, w, a)`

  Chirp z-transform. Compute the frequency response starting at a and stepping by w for m steps. a is a point in the complex plane, and w is the ratio between points in each step (i.e., radius increases exponentially, and angle increases linearly).

  To evaluate the frequency response for the range f1 to f2 in a signal with sampling frequency Fs, use the following:

       m = 32;                               ## number of points desired
       w = exp(-j*2*pi*(f2-f1)/((m-1)*Fs));  ## freq. step of f2-f1/m
       a = exp(j*2*pi*f1/Fs);                ## starting at frequency f1
       y = czt(x, m, w, a);

  If you don’t specify them, then the parameters default to a Fourier transform: m=length(x), w=exp(-j*2*pi/m), a=1

  If x is a matrix, the transform will be performed column-by-column.

#### 3.9.5 dct

Function File: dct `(x)`
Function File: dct `(x, n)`

  Compute the discrete cosine transform of x. If n is given, then x is padded or trimmed to length n before computing the transform. If x is a matrix, compute the transform along the columns of the the matrix. The transform is faster if x is real-valued and has even length.

  The discrete cosine transform x can be defined as follows:

                     N-1
         X[k] = w(k) sum x[n] cos (pi (2n+1) k / 2N ),  k = 0, ..., N-1
                     n=0

  with w(0) = sqrt(1/N) and w(k) = sqrt(2/N), k = 1, ..., N-1. There are other definitions with different scaling of X[k], but this form is common in image processing.

  See also: idct, dct2, idct2, dctmtx.

#### 3.9.6 dct2

Function File: dct2 `(x)`
Function File: dct2 `(x, m, n)`
Function File: dct2 `(x, [m, n])`

  Compute the 2-D discrete cosine transform of matrix x. If m and n are specified, the input is padded or trimmed to the desired size.

  See also: dct, idct, idct2.

#### 3.9.7 dctmtx

Function File: dctmtx `(n)`

  Return the DCT transformation matrix of size n-by-n.

  If A is an n-by-n matrix, then the following are true:

           T*A    == dct(A),  T'*A   == idct(A)
           T*A*T' == dct2(A), T'*A*T == idct2(A)

  A DCT transformation matrix is useful for doing things like jpeg image compression, in which an 8x8 DCT matrix is applied to non-overlapping blocks throughout an image and only a subblock on the top left of each block is kept. During restoration, the remainder of the block is filled with zeros and the inverse transform is applied to the block.

  See also: dct, idct, dct2, idct2.

#### 3.9.8 dftmtx

Function File: `d =` dftmtx `(n)`

  Compute the n-by-n Fourier transformation matrix. This is the matrix d such that the Fourier transform of a column vector of length n is given by `dftmtx(n) * x` and the inverse Fourier transform is given by `inv(dftmtx(n)) * x`.

  In general this is less efficient than calling the `fft` and `ifft` functions directly.

  See also: fft, ifft.

#### 3.9.9 digitrevorder

Function File: `y =` digitrevorder `(x, r)`
Function File: `[y, i] =` digitrevorder `(x, r)`

  Reorder the elements of the vector x in digit-reversed order. The elements of x are converted to radix r and reversed. The reordered indices of the elements of x are returned in i.

  See also: bitrevorder, fft, ifft.

#### 3.9.10 dst

Function File: `y =` dst `(x)`
Function File: `y =` dst `(x, n)`

  Computes the type I discrete sine transform of x. If n is given, then x is padded or trimmed to length n before computing the transform. If x is a matrix, compute the transform along the columns of the the matrix.

  The discrete sine transform X of x can be defined as follows:

              N
       X[k] = sum x[n] sin (pi n k / (N+1) ),  k = 1, ..., N
              n=1

  See also: idst.

#### 3.9.11 dwt

Function File: `[u, v] =` dwt `(x, wname)`
Function File: `[u, v] =` dwt `(x, Hp, Gp)`
Function File: `[u, v] =` dwt `(x, Hp, Gp, …)`

  Discrete wavelet transform (1D).

  Inputs

  x

    Signal vector.

  wname

    Wavelet name.

  Hp

    Coefficients of low-pass decomposition FIR filter.

  Gp

    Coefficients of high-pass decomposition FIR filter.

  Outputs

  u

    Signal vector of average, approximation.

  v

    Signal vector of difference, detail.

#### 3.9.12 fht

Function File: `m =` fht `(d)`
Function File: `m =` fht `(d, n)`
Function File: `m =` fht `(d, n, dim)`

  Calculate the Fast Hartley Transform of real input d. If d is a matrix, the Hartley transform is calculated along the columns by default. The options n and dim are similar to the options of FFT function.

  The forward and inverse Hartley transforms are the same (except for a scale factor of 1/N for the inverse Hartley transform), but implemented using different functions.

  The definition of the forward hartley transform for vector d, m[K] = \sum_{i=0}^{N-1} d[i]*(cos[K*2*pi*i/N] + sin[K*2*pi*i/N]), for 0 <= K < N. m[K] = \sum_{i=0}^{N-1} d[i]*CAS[K*i], for 0 <= K < N.

       fht(1:4)

  See also: ifht, fft.

#### 3.9.13 fwht

Function File: fwht `(x)`
Function File: fwht `(x, n)`
Function File: fwht `(x, n, order)`

  Compute the Walsh-Hadamard transform of x using the Fast Walsh-Hadamard Transform (FWHT) algorithm. If the input is a matrix, the FWHT is calculated along the columns of x.

  The number of elements of x must be a power of 2; if not, the input will be extended and filled with zeros. If a second argument is given, the input is truncated or extended to have length n.

  The third argument specifies the order in which the returned Walsh-Hadamard transform coefficients should be arranged. The order may be any of the following strings:

  "sequency"

    The coefficients are returned in sequency order. This is the default if order is not given.

  "hadamard"

    The coefficients are returned in Hadamard order.

  "dyadic"

    The coefficients are returned in Gray code order.

  See also: ifwht.

#### 3.9.14 hilbert

Function File: `h =` hilbert `(f, N, dim)`

  Analytic extension of real valued signal.

  `h = hilbert (f)` computes the extension of the real valued signal f to an analytic signal. If f is a matrix, the transformation is applied to each column. For N-D arrays, the transformation is applied to the first non-singleton dimension.

  `real (h)` contains the original signal f. `imag (h)` contains the Hilbert transform of f.

  `hilbert (f, N)` does the same using a length N Hilbert transform. The result will also have length N.

  `hilbert (f, [], dim)` or `hilbert (f, N, dim)` does the same along dimension dim.

#### 3.9.15 idct

Function File: `y =` idct `(x)`
Function File: `y =` idct `(x, n)`

  Compute the inverse discrete cosine transform of x. If n is given, then x is padded or trimmed to length n before computing the transform. If x is a matrix, compute the transform along the columns of the the matrix. The transform is faster if x is real-valued and even length.

  The inverse discrete cosine transform x can be defined as follows:

                N-1
         x[n] = sum w(k) X[k] cos (pi (2n+1) k / 2N ),  n = 0, ..., N-1
                k=0

  with w(0) = sqrt(1/N) and w(k) = sqrt(2/N), k = 1, ..., N-1

  See also: dct, dct2, idct2, dctmtx.

#### 3.9.16 idct2

Function File: `y =` idct2 `(x)`
Function File: `y =` idct2 `(x, m, n)`
Function File: `y =` idct2 `(x, [m, n])`

  Compute the inverse 2-D discrete cosine transform of matrix x. If m and n are specified, the input is either padded or truncated to have m rows and n columns.

#### 3.9.17 idst

Function File: `y =` idst `(x)`
Function File: `y =` idst `(x, n)`

  Computes the inverse type I discrete sine transform of y. If n is given, then y is padded or trimmed to length n before computing the transform. If y is a matrix, compute the transform along the columns of the the matrix.

  See also: dst.

#### 3.9.18 ifht

Function File: `m =` ifht `(d, n, dim)`

  Calculate the inverse Fast Hartley Transform of real input d. If d is a matrix, the inverse Hartley transform is calculated along the columns by default. The options n and dim are similar to the options of FFT function.

  The forward and inverse Hartley transforms are the same (except for a scale factor of 1/N for the inverse hartley transform), but implemented using different functions.

  The definition of the forward hartley transform for vector d, m[K] = 1/N \sum_{i=0}^{N-1} d[i]*(cos[K*2*pi*i/N] + sin[K*2*pi*i/N]), for 0 <= K < N. m[K] = 1/N \sum_{i=0}^{N-1} d[i]*CAS[K*i], for 0 <= K < N.

       ifht(1:4)

  See also: fht, fft.

#### 3.9.19 ifwht

Function File: ifwht `(x)`
Function File: ifwht `(x, n)`
Function File: ifwht `(x, n, order)`

  Compute the inverse Walsh-Hadamard transform of x using the Fast Walsh-Hadamard Transform (FWHT) algorithm. If the input is a matrix, the inverse FWHT is calculated along the columns of x.

  The number of elements of x must be a power of 2; if not, the input will be extended and filled with zeros. If a second argument is given, the input is truncated or extended to have length n.

  The third argument specifies the order in which the returned inverse Walsh-Hadamard transform coefficients should be arranged. The order may be any of the following strings:

  "sequency"

    The coefficients are returned in sequency order. This is the default if order is not given.

  "hadamard"

    The coefficients are returned in Hadamard order.

  "dyadic"

    The coefficients are returned in Gray code order.

  See also: fwht.

#### 3.9.20 rceps

Function File: `[y, ym] =` rceps `(x)`

  Return the cepstrum of the signal x.

  If x is a matrix, return the cepstrum of each column.

  If called with two output arguments, the minimum phase reconstruction of the signal x is returned in ym.

  For example:

       f0 = 70; Fs = 10000;            # 100 Hz fundamental, 10kHz sampling rate
       a = poly (0.985 * exp (1i * pi * [0.1, -0.1, 0.3, -0.3])); # two formants
       s = 0.005 * randn (1024, 1);    # Noise excitation signal
       s(1:Fs/f0:length(s)) = 1;       # Impulse glottal wave
       x = filter (1, a, s);           # Speech signal
       [y, ym] = rceps (x .* hanning (1024));

  Reference: Programs for Digital Signal Processing, IEEE Press, John Wiley & Sons, New York, 1979.

### 3.10 Power Spectrum Analysis

#### 3.10.1 __power

Function File: `[P, w] =` __power `(b, a)`
Function File: `[…] =` __power `(b, a, nfft)`
Function File: `[…] =` __power `(b, a, nfft, Fs)`
Function File: `[…] =` __power `(b, a, nfft, Fs, range)`
Function File: `[…] =` __power `(b, a, nfft, Fs, range, units)`
Function File: __power `(…)`

  Plot the power spectrum of the given ARMA model.

  b, a: filter coefficients (b=numerator, a=denominator) nfft is number of points at which to sample the power spectrum Fs is the sampling frequency of x range is ’half’ (default) or ’whole’ units is ’squared’ or ’db’ (default) range and units may be specified any time after the filter, in either order

  Returns P, the magnitude vector, and w, the frequencies at which it is sampled. If there are no return values requested, then plot the power spectrum and don’t return anything.

#### 3.10.2 ar_psd

Function File: ar_psd `(a, v)`
Function File: ar_psd `(a, v, freq)`
Function File: ar_psd `(a, v, freq, Fs)`
Function File: ar_psd `(…, range)`
Function File: ar_psd `(…, method)`
Function File: ar_psd `(…, plottype)`
Function File: `[psd, f_out] =` ar_psd `(…)`

  Calculate the power spectrum of the autoregressive model

                              M
       x(n) = sqrt(v).e(n) + SUM a(k).x(n-k)
                             k=1

  where x(n) is the output of the model and e(n) is white noise. This function is intended for use with `[a, v, k] = arburg (x, poles, criterion)` which use the Burg (1968) method to calculate a "maximum entropy" autoregressive model of x.

  If the freq argument is a vector (of frequencies) the spectrum is calculated using the polynomial method and the method argument is ignored. For scalar freq, an integer power of 2, or method = "FFT", causes the spectrum to be calculated by FFT. Otherwise, the spectrum is calculated as a polynomial. It may be computationally more efficient to use the FFT method if length of the model is not much smaller than the number of frequency values. The spectrum is scaled so that spectral energy (area under spectrum) is the same as the time-domain energy (mean square of the signal).

  ARGUMENTS: All but the first two arguments are optional and may be empty.

  - a list of M=(order+1) autoregressive model coefficients. The first element of "ar_coeffs" is the zero-lag coefficient, which always has a value of 1.
  - v square of the moving-average coefficient of the AR model.
  - freq frequencies at which power spectral density is calculated, or a scalar indicating the number of uniformly distributed frequency values at which spectral density is calculated. (default = 256)
  - Fs sampling frequency (Hertz) (default=1)

  CONTROL-STRING ARGUMENTS – each of these arguments is a character string. Control-string arguments can be in any order after the other arguments.

  Range:

  ’half’, ’onesided’ : frequency range of the spectrum is from zero up to but not including sample_f/2. Power from negative frequencies is added to the positive side of the spectrum. ’whole’, ’twosided’ : frequency range of the spectrum is -sample_f/2 to sample_f/2, with negative frequencies stored in "wrap around" order after the positive frequencies; e.g. frequencies for a 10-point ’twosided’ spectrum are 0 0.1 0.2 0.3 0.4 0.5 -0.4 -0.3 -0.2 -0.1 ’shift’, ’centerdc’ : same as ’whole’ but with the first half of the spectrum swapped with second half to put the zero-frequency value in the middle. (See "help fftshift". If "freq" is vector, ’shift’ is ignored. If model coefficients "ar_coeffs" are real, the default range is ’half’, otherwise default range is ’whole’.

  Method:

  ’fft’: use FFT to calculate power spectrum. ’poly’: calculate power spectrum as a polynomial of 1/z N.B. this argument is ignored if the "freq" argument is a vector. The default is ’poly’ unless the "freq" argument is an integer power of 2.

  Plot type:

  ’plot’, ’semilogx’, ’semilogy’, ’loglog’, ’squared’ or ’db’: specifies the type of plot. The default is ’plot’, which means linear-linear axes. ’squared’ is the same as ’plot’. ’dB’ plots "10*log10(psd)". This argument is ignored and a spectrum is not plotted if the caller requires a returned value.

  RETURNED VALUES: If returned values are not required by the caller, the spectrum is plotted and nothing is returned.

  - psd estimate of power-spectral density
  - f_out frequency values

  REFERENCE [1] Equation 2.28 from Steven M. Kay and Stanley Lawrence Marple Jr.: "Spectrum analysis – a modern perspective", Proceedings of the IEEE, Vol 69, pp 1380-1419, Nov., 1981

#### 3.10.3 cohere

Function File: `[Pxx, freq]` = `cohere(x,y,Nfft,Fs,window,overlap,range,plot_type,detrend)`

  Estimate (mean square) coherence of signals "x" and "y".

  Use the Welch (1967) periodogram/FFT method.

  Compatible with Matlab R11 cohere and earlier.

  See "help pwelch" for description of arguments, hints and references — especially hint (7) for Matlab R11 defaults.

#### 3.10.4 cpsd

Function File: `[Pxx, freq] =` cpsd `(x, y)`
Function File: `[…] =` cpsd `(x, y, window)`
Function File: `[…] =` cpsd `(x, y, window, overlap)`
Function File: `[…] =` cpsd `(x, y, window, overlap, Nfft)`
Function File: `[…] =` cpsd `(x, y, window, overlap, Nfft, Fs)`
Function File: `[…] =` cpsd `(x, y, window, overlap, Nfft, Fs, range)`
Function File: cpsd `(…)`

  Estimate cross power spectrum of data x and y by the Welch (1967) periodogram/FFT method.

  See also: pwelch.

#### 3.10.5 csd

Function File: `[Pxx,freq]` = `csd(x, y, Nfft, Fs, window, overlap, range, plot_type, detrend)`

  Estimate cross power spectrum of data "x" and "y" by the Welch (1967) periodogram/FFT method.

  Compatible with Matlab R11 csd and earlier.

  See "help pwelch" for description of arguments, hints and references — especially hint (7) for Matlab R11 defaults.

#### 3.10.6 db2pow

Function File: db2pow `(x)`

  Convert decibels (dB) to power.

  The power of x is defined as p = `10 ^ (x/10)`.

  If x is a vector, matrix, or N-dimensional array, the power is computed over the elements of x.

  Example:

       db2pow ([-10, 0, 10])
      ⇒ 0.1000 1.0000 10.0000

  See also: pow2db.

#### 3.10.7 mscohere

Function File: `[Pxx, freq] =` mscohere `(x, y)`
Function File: `[…] =` mscohere `(x, y, window)`
Function File: `[…] =` mscohere `(x, y, window, overlap)`
Function File: `[…] =` mscohere `(x, y, window, overlap, Nfft)`
Function File: `[…] =` mscohere `(x, y, window, overlap, Nfft, Fs)`
Function File: `[…] =` mscohere `(x, y, window, overlap, Nfft, Fs, range)`
Function File: mscohere `(…)`

  Estimate (mean square) coherence of signals x and y. Use the Welch (1967) periodogram/FFT method.

  See also: pwelch.

#### 3.10.8 pburg

Function File: `[psd,f_out] =` pburg(x, `poles, freq, Fs, range, method, plot_type, criterion)`

  Calculate Burg maximum-entropy power spectral density.

  The functions "arburg" and "ar_psd" do all the work. See "help arburg" and "help ar_psd" for further details.

  ARGUMENTS:

  All but the first two arguments are optional and may be empty.

  x

    [vector] sampled data

  poles

    [integer scalar] required number of poles of the AR model

  freq

    [real vector] frequencies at which power spectral density is calculated.

    [integer scalar] number of uniformly distributed frequency values at which spectral density is calculated. [default=256]

  Fs

    [real scalar] sampling frequency (Hertz) [default=1]

  CONTROL-STRING ARGUMENTS – each of these arguments is a character string.

  Control-string arguments can be in any order after the other arguments.

  range

    ’half’, ’onesided’ : frequency range of the spectrum is from zero up to but not including sample_f/2. Power from negative frequencies is added to the positive side of the spectrum.

    ’whole’, ’twosided’ : frequency range of the spectrum is -sample_f/2 to sample_f/2, with negative frequencies stored in "wrap around" order after the positive frequencies; e.g. frequencies for a 10-point ’twosided’ spectrum are 0 0.1 0.2 0.3 0.4 0.5 -0.4 -0.3 -0.2 -0.1

    ’shift’, ’centerdc’ : same as ’whole’ but with the first half of the spectrum swapped with second half to put the zero-frequency value in the middle. (See "help fftshift". If "freq" is vector, ’shift’ is ignored. If model coefficients "ar_coeffs" are real, the default range is ’half’, otherwise default range is ’whole’.

  method

    ’fft’: use FFT to calculate power spectral density.

    ’poly’: calculate spectral density as a polynomial of 1/z N.B. this argument is ignored if the "freq" argument is a vector. The default is ’poly’ unless the "freq" argument is an integer power of 2.

  plot_type

    ’plot’, ’semilogx’, ’semilogy’, ’loglog’, ’squared’ or ’db’: specifies the type of plot. The default is ’plot’, which means linear-linear axes. ’squared’ is the same as ’plot’. ’dB’ plots "10*log10(psd)". This argument is ignored and a spectrum is not plotted if the caller requires a returned value.

  criterion

    [optional string arg] model-selection criterion. Limits the number of poles so that spurious poles are not added when the whitened data has no more information in it (see Kay & Marple, 1981). Recognized values are

    ’AKICc’ – approximate corrected Kullback information criterion (recommended),

    ’KIC’ – Kullback information criterion

    ’AICc’ – corrected Akaike information criterion

    ’AIC’ – Akaike information criterion

    ’FPE’ – final prediction error" criterion

    The default is to NOT use a model-selection criterion

  RETURNED VALUES:

  If return values are not required by the caller, the spectrum is plotted and nothing is returned.

  psd

    [real vector] power-spectral density estimate

  f_out

    [real vector] frequency values

  HINTS

  This function is a wrapper for arburg and ar_psd.

  See "help arburg", "help ar_psd".

#### 3.10.9 pow2db

Function File: pow2db `(x)`

  Convert power to decibels (dB).

  The decibel value of x is defined as d = `10 * log10 (x)`.

  If x is a vector, matrix, or N-dimensional array, the decibel value is computed over the elements of x.

  Examples:

       pow2db ([0, 10, 100])
      ⇒ -Inf 10 20

  See also: db2pow.

#### 3.10.10 pwelch

Function File: `[spectra,freq] =` pwelch(x, `window, overlap, Nfft, Fs, range, plot_type, detrend, sloppy)`

  Estimate power spectral density of data "x" by the Welch (1967) periodogram/FFT method.

  All arguments except "x" are optional.

  The data is divided into segments. If "window" is a vector, each segment has the same length as "window" and is multiplied by "window" before (optional) zero-padding and calculation of its periodogram. If "window" is a scalar, each segment has a length of "window" and a Hamming window is used.

  The spectral density is the mean of the periodograms, scaled so that area under the spectrum is the same as the mean square of the data. This equivalence is supposed to be exact, but in practice there is a mismatch of up to 0.5% when comparing area under a periodogram with the mean square of the data.

  [spectra,freq] = pwelch(x,y,window,overlap,Nfft,Fs, range,plot_type,detrend,sloppy,results)

  Two-channel spectrum analyser. Estimate power spectral density, cross- spectral density, transfer function and/or coherence functions of time- series input data "x" and output data "y" by the Welch (1967) periodogram/FFT method.

  pwelch treats the second argument as "y" if there is a control-string argument "cross", "trans", "coher" or "ypower"; "power" does not force the 2nd argument to be treated as "y". All other arguments are optional. All spectra are returned in matrix "spectra".

  [spectra,Pxx_ci,freq] = pwelch(x,window,overlap,Nfft,Fs,conf, range,plot_type,detrend,sloppy)

  [spectra,Pxx_ci,freq] = pwelch(x,y,window,overlap,Nfft,Fs,conf, range,plot_type,detrend,sloppy,results)

  Estimates confidence intervals for the spectral density.

  See Hint (7) below for compatibility options.

  Confidence level "conf" is the 6th or 7th numeric argument. If "results" control-string arguments are used, one of them must be "power" when the "conf" argument is present; pwelch can estimate confidence intervals only for the power spectrum of the "x" data. It does not know how to estimate confidence intervals of the cross-power spectrum, transfer function or coherence; if you can suggest a good method, please send a bug report.

  ARGUMENTS

  All but the first argument are optional and may be empty, except that the "results" argument may require the second argument to be "y".

  x

    [non-empty vector] system-input time-series data

  y

    [non-empty vector] system-output time-series data

  window

    [real vector] of window-function values; the data segment has the same length as the window. Default window shape is Hamming.

    [integer scalar] length of each data segment. The default value is window=sqrt(length(x)) rounded up to the nearest integer power of 2; see ’sloppy’ argument.

  overlap

    [real scalar] segment overlap expressed as a multiple of window or segment length. 0 <= overlap < 1, The default is overlap=0.5 .

  Nfft

    [integer scalar] Length of FFT. The default is the length of the "window" vector or has the same value as the scalar "window" argument. If Nfft is larger than the segment length, "seg_len", the data segment is padded with "Nfft-seg_len" zeros. The default is no padding. Nfft values smaller than the length of the data segment (or window) are ignored silently.

  Fs

    [real scalar] sampling frequency (Hertz); default=1.0

  conf

    [real scalar] confidence level between 0 and 1. Confidence intervals of the spectral density are estimated from scatter in the periodograms and are returned as Pxx_ci. Pxx_ci(:,1) is the lower bound of the confidence interval and Pxx_ci(:,2) is the upper bound. If there are three return values, or conf is an empty matrix, confidence intervals are calculated for conf=0.95 . If conf is zero or is not given, confidence intervals are not calculated. Confidence intervals can be obtained only for the power spectral density of x; nothing else.

  CONTROL-STRING ARGUMENTS – each of these arguments is a character string. Control-string arguments must be after the other arguments but can be in any order.

  range

    ’half’, ’onesided’ : frequency range of the spectrum is zero up to but not including Fs/2. Power from negative frequencies is added to the positive side of the spectrum, but not at zero or Nyquist (Fs/2) frequencies. This keeps power equal in time and spectral domains. See reference [2].

    ’whole’, ’twosided’ : frequency range of the spectrum is -Fs/2 to Fs/2, with negative frequencies stored in "wrap around" order after the positive frequencies; e.g. frequencies for a 10-point ’twosided’ spectrum are 0 0.1 0.2 0.3 0.4 0.5 -0.4 -0.3 -0.2 -0.1

    ’shift’, ’centered’, ’centerdc’ (deprecated) : same as ’whole’ but with the first half of the spectrum swapped with second half to put the zero-frequency value in the middle. See "help fftshift".

    If data (x and y) are real, the default range is ’half’, otherwise default range is ’whole’.

    ’centerdc’ : (deprecated) Use ’centered’ instead.

  plot_type

    ’plot’, ’semilogx’, ’semilogy’, ’loglog’, ’squared’ or ’db’: specifies the type of plot. The default is ’plot’, which means linear-linear axes. ’squared’ is the same as ’plot’. ’dB’ plots "10*log10(psd)". This argument is ignored and a spectrum is not plotted if the caller requires a returned value.

  detrend

    ’no-strip’, ’none’ – do NOT remove mean value from the data

    ’short’, ’mean’ – remove the mean value of each segment from each segment of the data.

    ’linear’, – remove linear trend from each segment of the data.

    ’long-mean’ – remove the mean value from the data before splitting it into segments. This is the default.

  sloppy

    ’sloppy’: FFT length is rounded up to the nearest integer power of 2 by zero padding. FFT length is adjusted after addition of padding by explicit Nfft argument. The default is to use exactly the FFT and window/ segment lengths specified in argument list.

  results

    specifies what results to return (in the order specified and as many as desired).

    ’power’ calculate power spectral density of "x"

    ’cross’ calculate cross spectral density of "x" and "y"

    ’trans’ calculate transfer function of a system with input "x" and output "y"

    ’coher’ calculate coherence function of "x" and "y"

    ’ypower’ calculate power spectral density of "y"

    The default is ’power’, with argument "y" omitted.

  RETURNED VALUES:

  If return values are not required by the caller, the results are plotted and nothing is returned.

  spectra

    [real-or-complex matrix] columns of the matrix contain results in the same order as specified by "results" arguments. Each column contains one of the result vectors.

  Pxx_ci

    [real matrix] estimate of confidence interval for power spectral density of x. First column is the lower bound. Second column is the upper bound.

  freq

    [real column vector] frequency values

  HINTS

  1. EMPTY ARGS: if you don’t want to use an optional argument you can leave it empty by writing its value as [].
  2. FOR BEGINNERS: The profusion of arguments may make pwelch difficult to use, and an unskilled user can easily produce a meaningless result or can easily mis-interpret the result. With real data "x" and sampling frequency "Fs", the easiest and best way for a beginner to use pwelch is probably "pwelch(x,[],[],[],Fs)". Use the "window" argument to control the length of the spectrum vector. For real data and integer scalar M, "pwelch(x,2*M,[],[],Fs)" gives an M+1 point spectrum. Run "demo pwelch" (octave only).
  3. WINDOWING FUNCTIONS: Without a window function, sharp spectral peaks can have strong sidelobes because the FFT of a data in a segment is in effect convolved with a rectangular window. A window function which tapers off (gradually) at the ends produces much weaker sidelobes in the FFT. Hann (hanning), hamming, bartlett, blackman, flattopwin etc are available as separate Matlab/sigproc or Octave functions. The sidelobes of the Hann window have a roll-off rate of 60dB/decade of frequency. The first sidelobe of the Hamming window is suppressed and is about 12dB lower than the first Hann sidelobe, but the roll-off rate is only 20dB/decade. You can inspect the FFT of a Hann window by plotting "abs(fft(postpad(hanning(256),4096,0)))". The default window is Hamming.
  4. ZERO PADDING: Zero-padding reduces the frequency step in the spectrum, and produces an artificially smoothed spectrum. For example, "Nfft=2*length(window)" gives twice as many frequency values, but adjacent PSD (power spectral density) values are not independent; adjacent PSD values are independent if "Nfft=length(window)", which is the default value of Nfft.
  5. REMOVING MEAN FROM SIGNAL: If the mean is not removed from the signal there is a large spectral peak at zero frequency and the sidelobes of this peak are likely to swamp the rest of the spectrum. For this reason, the default behavior is to remove the mean. However, the matlab pwelch does not do this.
  6. WARNING ON CONFIDENCE INTERVALS Confidence intervals are obtained by measuring the sample variance of the periodograms and assuming that the periodograms have a Gaussian probability distribution. This assumption is not accurate. If, for example, the data (x) is Gaussian, the periodogram has a Rayleigh distribution. However, the confidence intervals may still be useful.
  7. COMPATIBILITY WITH Matlab R11, R12, etc When used without the second data (y) argument, arguments are compatible with the pwelch of Matlab R12, R13, R14, 2006a and 2006b except that

     1) overlap is expressed as a multiple of window length — effect of overlap scales with window length

     2) default values of length(window), Nfft and Fs are more sensible, and

     3) Goertzel algorithm is not available so Nfft cannot be an array of frequencies as in Matlab 2006b.

     Pwelch has four persistent Matlab-compatibility levels. Calling pwelch with an empty first argument sets the order of arguments and defaults specified above in the USAGE and ARGUMENTS section of this documentation.

                   prev_compat=pwelch([]);
                   [Pxx,f]=pwelch(x,window,overlap,Nfft,Fs,conf,...);

     Calling pwelch with a single string argument (as described below) gives compatibility with Matlab R11 or R12, or the R14 spectrum.welch defaults. The returned value is the PREVIOUS compatibility string.

     Matlab R11: For compatibility with the Matlab R11 pwelch:

                   prev_compat=pwelch('R11-');
                   [Pxx,f]=pwelch(x,Nfft,Fs,window,overlap,conf,range,units);
                   %% units of overlap are "number of samples"
                   %% defaults: Nfft=min(length(x),256), Fs=2*pi, length(window)=Nfft,
                   %%           window=Hanning, do not detrend,
                   %% N.B.  "Sloppy" is not available.

     Matlab R12: For compatibility with Matlab R12 to 2006a pwelch:

                   prev_compat=pwelch('R12+');
                   [Pxx,f]=pwelch(x,window,overlap,nfft,Fs,...);
                   %% units of overlap are "number of samples"
                   %% defaults: length(window)==length(x)/8, window=Hamming,
                   %%           Nfft=max(256,NextPow2), Fs=2*pi, do not detrend
                   %% NextPow2 is the next power of 2 greater than or equal to the
                   %% window length. "Sloppy", "conf" are not available.  Default
                   %% window length gives very poor amplitude resolution.

     To adopt defaults of the Matlab R14 "spectrum.welch" spectrum object associated "psd" method.

                   prev_compat=pwelch('psd');
                   [Pxx,f] = pwelch(x,window,overlap,Nfft,Fs,conf,...);
                   %% overlap is expressed as a percentage of window length,
                   %% defaults: length(window)==64, Nfft=max(256,NextPow2), Fs=2*pi
                   %%           do not detrend
                   %% NextPow2 is the next power of 2 greater than or equal to the
                   %% window length. "Sloppy" is not available.
                   %% Default window length gives coarse frequency resolution.

  REFERENCES

  [1] Peter D. Welch (June 1967): "The use of fast Fourier transform for the estimation of power spectra: a method based on time averaging over short, modified periodograms." IEEE Transactions on Audio Electroacoustics, Vol AU-15(6), pp 70-73

  [2] William H. Press and Saul A. Teukolsky and William T. Vetterling and Brian P. Flannery, "Numerical recipes in C, The art of scientific computing", 2nd edition, Cambridge University Press, 2002 — Section 13.7.

#### 3.10.11 pyulear

Function File: `[psd,f_out] =` pyulear(x,poles,freq,Fs,range,method,plot_type)

  Calculates a Yule-Walker autoregressive (all-pole) model of the data "x" and computes the power spectrum of the model.

  This is a wrapper for functions "aryule" and "ar_psd" which perform the argument checking.

  See "help aryule" and "help ar_psd" for further details.

  ARGUMENTS:

  All but the first two arguments are optional and may be empty.

  x

    [vector] sampled data

  poles

    [integer scalar] required number of poles of the AR model

  freq

    [real vector] frequencies at which power spectral density is calculated

    [integer scalar] number of uniformly distributed frequency values at which spectral density is calculated. [default=256]

  Fs

    [real scalar] sampling frequency (Hertz) [default=1]

  CONTROL-STRING ARGUMENTS – each of these arguments is a character string.

  Control-string arguments can be in any order after the other arguments.

  range

    ’half’, ’onesided’ : frequency range of the spectrum is from zero up to but not including sample_f/2. Power from negative frequencies is added to the positive side of the spectrum.

    ’whole’, ’twosided’ : frequency range of the spectrum is -sample_f/2 to sample_f/2, with negative frequencies stored in "wrap around" order after the positive frequencies; e.g. frequencies for a 10-point ’twosided’ spectrum are 0 0.1 0.2 0.3 0.4 0.5 -0.4 -0.3 -0.2 -0.1

    ’shift’, ’centerdc’ : same as ’whole’ but with the first half of the spectrum swapped with second half to put the zero-frequency value in the middle. (See "help fftshift". If "freq" is vector, ’shift’ is ignored.

    If model coefficients "ar_coeffs" are real, the default range is ’half’, otherwise default range is ’whole’.

  method

    ’fft’: use FFT to calculate power spectrum.

    ’poly’: calculate power spectrum as a polynomial of 1/z N.B. this argument is ignored if the "freq" argument is a vector. The default is ’poly’ unless the "freq" argument is an integer power of 2.

  plot_type

    ’plot’, ’semilogx’, ’semilogy’, ’loglog’, ’squared’ or ’db’: specifies the type of plot. The default is ’plot’, which means linear-linear axes. ’squared’ is the same as ’plot’. ’dB’ plots "10*log10(psd)". This argument is ignored and a spectrum is not plotted if the caller requires a returned value.

  RETURNED VALUES:

  If return values are not required by the caller, the spectrum is plotted and nothing is returned.

  psd

    [real vector] power-spectrum estimate

  f_out

    [real vector] frequency values

  HINTS

  This function is a wrapper for aryule and ar_psd.

  See "help aryule", "help ar_psd".

#### 3.10.12 tfe

Function File: `[Pxx,freq] =` tfe(x,y,Nfft,Fs,window,overlap,range,plot_type,detrend)

  Estimate transfer function of system with input "x" and output "y".

  Use the Welch (1967) periodogram/FFT method.

  Compatible with Matlab R11 tfe and earlier.

  See "help pwelch" for description of arguments, hints and references — especially hint (7) for Matlab R11 defaults.

#### 3.10.13 tfestimate

Function File: tfestimate `(x, y)`
Function File: tfestimate `(x, y, window)`
Function File: tfestimate `(x, y, window, overlap)`
Function File: tfestimate `(x, y, window, overlap, Nfft)`
Function File: tfestimate `(x, y, window, overlap, Nfft, Fs)`
Function File: tfestimate `(x, y, window, overlap, Nfft, Fs, range)`
Function File: `[Pxx, freq] =` tfestimate `(…)`

  Estimate transfer function of system with input x and output y. Use the Welch (1967) periodogram/FFT method.

  See also: pwelch.

### 3.11 Window Functions

#### 3.11.1 barthannwin

Function File: barthannwin `(m)`

  Return the filter coefficients of a modified Bartlett-Hann window of length m.

  See also: rectwin, bartlett.

#### 3.11.2 blackmanharris

Function File: blackmanharris `(m)`
Function File: blackmanharris `(m, "periodic")`
Function File: blackmanharris `(m, "symmetric")`

  Return the filter coefficients of a Blackman-Harris window of length m.

  If the optional argument `"periodic"` is given, the periodic form of the window is returned. This is equivalent to the window of length m+1 with the last coefficient removed. The optional argument `"symmetric"` is equivalent to not specifying a second argument.

  See also: rectwin, bartlett.

#### 3.11.3 blackmannuttall

Function File: blackmannuttall `(m)`
Function File: blackmannuttall `(m, "periodic")`
Function File: blackmannuttall `(m, "symmetric")`

  Return the filter coefficients of a Blackman-Nuttall window of length m.

  If the optional argument `"periodic"` is given, the periodic form of the window is returned. This is equivalent to the window of length m+1 with the last coefficient removed. The optional argument `"symmetric"` is equivalent to not specifying a second argument.

  See also: nuttallwin, kaiser.

#### 3.11.4 bohmanwin

Function File: bohmanwin `(m)`

  Return the filter coefficients of a Bohman window of length m.

  See also: rectwin, bartlett.

#### 3.11.5 boxcar

Function File: boxcar `(m)`

  Return the filter coefficients of a rectangular window of length m.

#### 3.11.6 chebwin

Function File: chebwin `(m)`
Function File: chebwin `(m, at)`

  Return the filter coefficients of a Dolph-Chebyshev window of length m. The Fourier transform of the window has a stop-band attenuation of at dB. The default attenuation value is 100 dB.

  For the definition of the Chebyshev window, see

  * Peter Lynch, "The Dolph-Chebyshev Window: A Simple Optimal Filter", Monthly Weather Review, Vol. 125, pp. 655-660, April 1997. (http://www.maths.tcd.ie/~plynch/Publications/Dolph.pdf)

  * C. Dolph, "A current distribution for broadside arrays which optimizes the relationship between beam width and side-lobe level", Proc. IEEE, 34, pp. 335-348.

  The window is described in frequency domain by the expression:

                Cheb(m-1, beta * cos(pi * k/m))
         W(k) = -------------------------------
                       Cheb(m-1, beta)

  with

         beta = cosh(1/(m-1) * acosh(10^(at/20))

  and Cheb(m,x) denoting the m-th order Chebyshev polynomial calculated at the point x.

  Note that the denominator in W(k) above is not computed, and after the inverse Fourier transform the window is scaled by making its maximum value unitary.

  See also: kaiser.

#### 3.11.7 expwin

Function File: `expwin` (m, `alpha)`
Function File: `expwin` (m, `sll)`
Function File: `expwin` (…, `"canonical")`

  Return the coefficients of an exponential window4 of length m.

  The window’s adjustable parameter can be set directly as alpha (≥0) or indirectly with sll (<0), the latter being the desired side-lobe level in dBc.

  By default, the returned window is both symmetric and periodic. With the optional argument `"canonical"`, the canonical form of the window is returned, which is symmetric but not periodic.

  See also: kaiser, poisswin.

#### 3.11.8 flattopwin

Function File: flattopwin `(m)`
Function File: flattopwin `(m, "periodic")`
Function File: flattopwin `(m, "symmetric")`

  Return the filter coefficients of a Flat Top window of length m. The Flat Top window is defined by the function f(w):

         f(w) = 1 - 1.93 cos(2 pi w) + 1.29 cos(4 pi w)
                  - 0.388 cos(6 pi w) + 0.0322cos(8 pi w)

  where w = i/(m-1) for i=0:m-1 for a symmetric window, or w = i/m for i=0:m-1 for a periodic window. The default is symmetric. The returned window is normalized to a peak of 1 at w = 0.5.

  This window has low pass-band ripple, but high bandwidth.

  According to [1]:

  The main use for the Flat Top window is for calibration, due to its negligible amplitude errors.

  [1] Gade, S; Herlufsen, H; (1987) "Use of weighting functions in DFT/FFT analysis (Part I)", Bruel & Kjaer Technical Review No.3.

#### 3.11.9 gaussian

Function File: gaussian `(m)`
Function File: gaussian `(m, a)`

  Return a Gaussian convolution window of length m. The width of the window is inversely proportional to the parameter a. Use larger a for a narrower window. Use larger m for longer tails.

  w = exp ( -(a*x)^2/2 )

  for x = linspace ( -(m-1)/2, (m-1)/2, m ).

  Width a is measured in frequency units (sample rate/num samples). It should be f when multiplying in the time domain, but 1/f when multiplying in the frequency domain (for use in convolutions).

#### 3.11.10 gausswin

Function File: gausswin `(m)`
Function File: gausswin `(m, a)`

  Return the filter coefficients of a Gaussian window of length m. The width of the window is inversely proportional to the parameter a. Use larger a for a narrow window. Use larger m for a smoother curve.

  w = exp ( -(a*x)^2/2 )

  for x = linspace(-(m-1)/m, (m-1)/m, m)

#### 3.11.11 hann

Function File: hann `(m)`
Function File: hann `(m, "periodic")`
Function File: hann `(m, "symmetric")`

  Return the filter coefficients of a Hanning window of length m.

  If the optional argument `"periodic"` is given, the periodic form of the window is returned. This is equivalent to the window of length m+1 with the last coefficient removed. The optional argument `"symmetric"` is equivalent to not specifying a second argument.

  This function exists for MATLAB compatibility only, and is equivalent to `hanning (m)`.

  See also: hanning.

#### 3.11.12 kaiser

Function File: kaiser `(m)`
Function File: kaiser `(m, beta)`

  Return the filter coefficients of a Kaiser window of length m. The Fourier transform of the window has a stop-band attenuation that is derived from the parameter beta.

  For the definition of the Kaiser window, see A. V. Oppenheim & R. W. Schafer, "Discrete-Time Signal Processing".

  The continuous version of width m centered about x=0 is:

               besseli(0, beta * sqrt(1-(2*x/m).^2))
       k(x) =  -------------------------------------,  m/2 <= x <= m/2
                      besseli(0, beta)

  See also: kaiserord.

#### 3.11.13 nuttallwin

Function File: nuttallwin `(m)`
Function File: nuttallwin `(m, "periodic")`
Function File: nuttallwin `(m, "symmetric")`

  Return the filter coefficients of a Blackman-Harris window defined by Nuttall of length m.

  If the optional argument `"periodic"` is given, the periodic form of the window is returned. This is equivalent to the window of length m+1 with the last coefficient removed. The optional argument `"symmetric"` is equivalent to not specifying a second argument.

  See also: blackman, blackmanharris.

#### 3.11.14 parzenwin

Function File: parzenwin `(m)`

  Return the filter coefficients of a Parzen window of length m.

  See also: rectwin, bartlett.

#### 3.11.15 poisswin

Function File: `poisswin` (m, `alpha)`

  Return the coefficients of a Poisson (a.k.a. exponential) window5 of length m and adjustable parameter alpha.

#### 3.11.16 rectwin

Function File: rectwin `(m)`

  Return the filter coefficients of a rectangular window of length m.

  See also: boxcar, hamming, hanning.

#### 3.11.17 taylorwin

Function File: `taylorwin` (m)
Function File: `taylorwin` (m, `nbar)`
Function File: `taylorwin` (m, `nbar, sll)`
Function File: `taylorwin` (m, `nbar, sll, plots)`

  Return the coefficients of a Taylor window of length m, whose Fourier transform has nbar (default 4) quasi-equiripple side-lobes adjacent to the main-lobe, at a nominal level of sll (default −30) dBc.

  If plots is non-zero then time and frequency domains plots of the resultant window are shown.

  Reference: Doerry, ‘Catalog of Window Taper Functions for Sidelobe Control’, 2017.

  See also: chebwin.

#### 3.11.18 triang

Function File: triang `(m)`

  Return the filter coefficients of a triangular window of length m. Unlike the Bartlett window, `triang` does not go to zero at the edges of the window. For odd m, `triang (m)` is equal to `bartlett (m + 2)` except for the zeros at the edges of the window.

  See also: bartlett.

#### 3.11.19 tukeywin

Function File: tukeywin `(m)`
Function File: tukeywin `(m, r)`

  Return the filter coefficients of a Tukey window (also known as the cosine-tapered window) of length m. r defines the ratio between the constant section and and the cosine section. It has to be between 0 and 1. The function returns a Hanning window for r equal to 1 and a rectangular window for r equal to 0. The default value of r is 1/2.

  For a definition of the Tukey window, see e.g. Fredric J. Harris, "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform, Proceedings of the IEEE", Vol. 66, No. 1, January 1978, Page 67, Equation 38.

  See also: hanning.

#### 3.11.20 ultrwin

Function File: `[w, xmu] =` ultrwin `(m, mu, beta)`
Function File: `[w, xmu] =` ultrwin `(m, mu, att, "att")`
Function File: `[w, xmu] =` ultrwin `(m, mu, latt, "latt")`
Function File: `w =` ultrwin `(m, mu, xmu, "xmu")`

  Return the coefficients of an Ultraspherical window of length m. The parameter mu controls the window’s Fourier transform’s side-lobe to side-lobe ratio, and the third given parameter controls the transform’s main-lobe width/side-lobe-ratio; normalize w such that the central coefficient(s) value is unitary.

  By default, the third parameter is beta, which sets the main lobe width to beta times that of a rectangular window. Alternatively, giving att or latt sets the ripple ratio at the first or last side-lobe respectively, or giving xmu sets the (un-normalized) window’s Fourier transform according to its canonical definition:

                    (MU)
            W(k) = C   [ XMU cos(pi k/M) ],  k = 0, 1, ..., M-1,
                    M-1

  where C is the Ultraspherical (a.k.a. Gegenbauer) polynomial, which can be defined using the recurrence relationship:

             (l)    1                  (l)                    (l)
            C (x) = - [ 2x(m + l - 1) C   (x) - (m + 2l - 2) C   (x) ]
             m      m                  m-1                    m-2

                                       (l)        (l)
            for m an integer > 1, and C (x) = 1, C (x) = 2lx.
                                       0          1

  For given beta, att, or latt, the corresponding (determined) value of xmu is also returned.

  The Dolph-Chebyshev and Saramaki windows are special cases of the Ultraspherical window, with mu set to 0 and 1 respectively. Note that when not giving xmu, stability issues may occur with mu <= -1.5. For further information about the window, see

  - Kabal, P., 2009: Time Windows for Linear Prediction of Speech. Technical Report, Dept. Elec. & Comp. Eng., McGill University.
  - Bergen, S., Antoniou, A., 2004: Design of Ultraspherical Window Functions with Prescribed Spectral Characteristics. Proc. JASP, 13/13, pp. 2053-2065.
  - Streit, R., 1984: A two-parameter family of weights for nonrecursive digital filters and antennas. Trans. ASSP, 32, pp. 108-118.

  See also: chebwin, kaiser.

#### 3.11.21 welchwin

Function File: welchwin `(m)`
Function File: welchwin `(m, "periodic")`
Function File: welchwin `(m, "symmetric")`

  Return the filter coefficients of a Welch window of length m. The Welch window is given by w(n)=1-(n/N-1)^2, n=[0,1, ... m-1]. The optional argument specifies a "symmetric" window (the default) or a "periodic" window.

  A symmetric window has zero at each end and maximum in the middle, and the length must be an integer greater than 2. The variable N in the formula above is `(m-1)/2`.

  A periodic window wraps around the cyclic interval [0,1, ... m-1], and is intended for use with the DFT. The length must be an integer greater than 1. The variable N in the formula above is `m/2`.

  See also: blackman, kaiser.

#### 3.11.22 window

Function File: `w =` window `(f, m)`
Function File: `w =` window `(f, m, opts)`

  Create an m-point window from the function f. The function f can be for example `@blackman`. Any additional arguments opt are passed to the windowing function.

#### 3.11.23 wvtool

Function File: `wvtool` (w, `…)`
Function File: `wvtool` (…, `options)`
Function File: `wvtool` (title, `…)`
Function File: `h` = `wvtool (…)`

  Plot time and frequency domain graphs of each vector of window function coefficients given in w and any following arguments. Given vectors must have the same length.

  The graphs are: (1) Time domain, (2) Frequency domain (initial bins), and (3) Frequency domain (all bins). A subset of these can be selected by including their numbers as above as characters within the string argument options; for example, ‘13’ selects only graphs (1) and (3).

  By default, the x-axis of graph (3) is logarithmic, to facilitate assessment of side-lobe roll-off (SLR) rate. Including an ‘l’ character in options changes to a linear x-axis.

  The layout of the graphs is set to horizontal if options contains an ‘h’ character.

  Any other characters in options are passed to the plot command for graph (1) as its fmt argument (for an example, see demo number 3).

  A figure title can be supplied as the first given argument; alternatively the returned handle h provides a more general means to modify the figure.

  Measurements made on the window(s) are reported in the graph legends (subject to graph selection per above) and in the console output.

  To convert reported bin positions to bandwidth (in bins), multiply by 2. To convert bandwidth in bins to normalized bandwidth, divide by the window-length.

### 3.12 System Identification

#### 3.12.1 arburg

Function File: `[a, v, k] =` arburg `(x, poles)`
Function File: `[a, v, k] =` arburg `(x, poles, criterion)`

  Calculate coefficients of an autoregressive (AR) model of complex data x using the whitening lattice-filter method of Burg (1968). The inverse of the model is a moving-average filter which reduces x to white noise. The power spectrum of the AR model is an estimate of the maximum entropy power spectrum of the data. The function `ar_psd` calculates the power spectrum of the AR model.

  ARGUMENTS:

  - x sampled data
  - poles number of poles in the AR model or limit to the number of poles if a valid criterion is provided.
  - criterion model-selection criterion. Limits the number of poles so that spurious poles are not added when the whitened data has no more information in it (see Kay & Marple, 1981). Recognized values are ’AKICc’ – approximate corrected Kullback information criterion (recommended), ’KIC’ – Kullback information criterion ’AICc’ – corrected Akaike information criterion ’AIC’ – Akaike information criterion ’FPE’ – final prediction error" criterion The default is to NOT use a model-selection criterion

  RETURNED VALUES:

  - a list of (P+1) autoregression coefficients; for data input x(n) and white noise e(n), the model is

                               P+1
         x(n) = sqrt(v).e(n) + SUM a(k).x(n-k)
                               k=1

    v mean square of residual noise from the whitening operation of the Burg lattice filter.

  - k reflection coefficients defining the lattice-filter embodiment of the model

  HINTS:

  (1) arburg does not remove the mean from the data. You should remove the mean from the data if you want a power spectrum. A non-zero mean can produce large errors in a power-spectrum estimate. See "help detrend". (2) If you don’t know what the value of "poles" should be, choose the largest (reasonable) value you could want and use the recommended value, criterion=’AKICc’, so that arburg can find it. E.g. arburg(x,64,’AKICc’) The AKICc has the least bias and best resolution of the available model-selection criteria. (3) Autoregressive and moving-average filters are stored as polynomials which, in matlab, are row vectors.

  NOTE ON SELECTION CRITERION:

  AIC, AICc, KIC and AKICc are based on information theory. They attempt to balance the complexity (or length) of the model against how well the model fits the data. AIC and KIC are biased estimates of the asymmetric and the symmetric Kullback-Leibler divergence respectively. AICc and AKICc attempt to correct the bias. See reference [4].

  REFERENCES:

  [1] John Parker Burg (1968) "A new analysis technique for time series data", NATO advanced study Institute on Signal Processing with Emphasis on Underwater Acoustics, Enschede, Netherlands, Aug. 12-23, 1968.

  [2] Steven M. Kay and Stanley Lawrence Marple Jr.: "Spectrum analysis – a modern perspective", Proceedings of the IEEE, Vol 69, pp 1380-1419, Nov., 1981

  [3] William H. Press and Saul A. Teukolsky and William T. Vetterling and Brian P. Flannery "Numerical recipes in C, The art of scientific computing", 2nd edition, Cambridge University Press, 2002 — Section 13.7.

  [4] Abd-Krim Seghouane and Maiza Bekara "A small sample model selection criterion based on Kullback’s symmetric divergence", IEEE Transactions on Signal Processing, Vol. 52(12), pp 3314-3323, Dec. 2004

  See also: ar_psd.

#### 3.12.2 aryule

Function File: `a =` aryule `(x, p)`
Function File: `[a, v, k] =` aryule `(x, p)`

  Fit an AR (p)-model with Yule-Walker estimates.

  x

    data vector to estimate

  a

    AR coefficients

  v

    variance of white noise

  k

    reflection coefficients for use in lattice filter

  The power spectrum of the resulting filter can be plotted with pyulear(x, p), or you can plot it directly with ar_psd(a,v,...).

  See also: pyulear, power, freqz, impz – for observing characteristics of the model arburg – for alternative spectral estimators

  Example: Use example from arburg, but substitute aryule for arburg.

  Note: Orphanidis ’85 claims lattice filters are more tolerant of truncation errors, which is why you might want to use them. However, lacking a lattice filter processor, I haven’t tested that the lattice filter coefficients are reasonable.

#### 3.12.3 invfreq

Function File: `[B,A] =` invfreq(H,F,nB,nA)
: `[B,A] =` invfreq(H,F,nB,nA,W)
: `[B,A] =` invfreq(H,F,nB,nA,W,[],[],plane)
: `[B,A] =` invfreq(H,F,nB,nA,W,iter,tol,plane)

  Fit filter B(z)/A(z) or B(s)/A(s) to complex frequency response at frequency points F.

  A and B are real polynomial coefficients of order nA and nB respectively. Optionally, the fit-errors can be weighted vs frequency according to the weights W. Also, the transform plane can be specified as either ’s’ for continuous time or ’z’ for discrete time. ’z’ is chosen by default. Eventually, Steiglitz-McBride iterations will be specified by iter and tol.

  H: desired complex frequency response It is assumed that A and B are real polynomials, hence H is one-sided.

  F: vector of frequency samples in radians

  nA: order of denominator polynomial A

  nB: order of numerator polynomial B

  plane=’z’: F on unit circle (discrete-time spectra, z-plane design)

  plane=’s’: F on jw axis (continuous-time spectra, s-plane design)

  H(k) = spectral samples of filter frequency response at points zk, where zk=exp(sqrt(-1)*F(k)) when plane=’z’ (F(k) in [0,.5]) and zk=(sqrt(-1)*F(k)) when plane=’s’ (F(k) nonnegative)

  Example:

           [B,A] = butter(12,1/4);
           [H,w] = freqz(B,A,128);
           [Bh,Ah] = invfreq(H,F,4,4);
           Hh = freqz(Bh,Ah);
           disp(sprintf('||frequency response error|| = %f',norm(H-Hh)));

  References:

  J. O. Smith, "Techniques for Digital Filter Design and System Identification with Application to the Violin, Ph.D. Dissertation, Elec. Eng. Dept., Stanford University, June 1983, page 50; or,

  http://ccrma.stanford.edu/~jos/filters/FFT_Based_Equation_Error_Method.html

#### 3.12.4 invfreqs

Function File: `[B,A] =` invfreqs(H,F,nB,nA)
: `[B,A] =` invfreqs(H,F,nB,nA,W)
: `[B,A] =` invfreqs(H,F,nB,nA,W,iter,tol,'trace')

  Fit filter B(s)/A(s)to the complex frequency response H at frequency points F.

  A and B are real polynomial coefficients of order nA and nB.

  Optionally, the fit-errors can be weighted vs frequency according to the weights W.

  Note: all the guts are in invfreq.m

  H: desired complex frequency response

  F: frequency (must be same length as H)

  nA: order of the denominator polynomial A

  nB: order of the numerator polynomial B

  W: vector of weights (must be same length as F)

  Example:

             B = [1/2 1];
             A = [1 1];
             w = linspace(0,4,128);
             H = freqs(B,A,w);
             [Bh,Ah] = invfreqs(H,w,1,1);
             Hh = freqs(Bh,Ah,w);
             plot(w,[abs(H);abs(Hh)])
             legend('Original','Measured');
             err = norm(H-Hh);
             disp(sprintf('L2 norm of frequency response error = %f',err));

#### 3.12.5 invfreqz

Function File: `[B,A] =` invfreqz(H,F,nB,nA)
: `[B,A] =` invfreqz(H,F,nB,nA,W)
: `[B,A] =` invfreqz(H,F,nB,nA,W,iter,tol,'trace')

  Fit filter B(z)/A(z)to the complex frequency response H at frequency points F.

  A and B are real polynomial coefficients of order nA and nB. Optionally, the fit-errors can be weighted vs frequency according to the weights W.

  Note: all the guts are in invfreq.m

  H: desired complex frequency response

  F: normalized frequency (0 to pi) (must be same length as H)

  nA: order of the denominator polynomial A

  nB: order of the numerator polynomial B

  W: vector of weights (must be same length as F)

  Example:

           [B,A] = butter(4,1/4);
           [H,F] = freqz(B,A);
           [Bh,Ah] = invfreq(H,F,4,4);
           Hh = freqz(Bh,Ah);
           disp(sprintf('||frequency response error|| = %f',norm(H-Hh)));

#### 3.12.6 levinson

Function File: `[a, v, ref] =` levinson `(acf)`
Function File: `[…] =` levinson `(acf, p)`

  Use the Durbin-Levinson algorithm to solve: toeplitz(acf(1:p)) * x = -acf(2:p+1). The solution [1, x’] is the denominator of an all pole filter approximation to the signal x which generated the autocorrelation function acf.

  acf is the autocorrelation function for lags 0 to p. p defaults to length(acf)-1. Returns a=[1, x’] the denominator filter coefficients. v= variance of the white noise = square of the numerator constant ref = reflection coefficients = coefficients of the lattice implementation of the filter Use freqz(sqrt(v),a) to plot the power spectrum.

  REFERENCE [1] Steven M. Kay and Stanley Lawrence Marple Jr.: "Spectrum analysis – a modern perspective", Proceedings of the IEEE, Vol 69, pp 1380-1419, Nov., 1981

#### 3.12.7 lpc

Function File: `a =` lpc `(x)`
Function File: `a =` lpc `(x, p)`
Function File: `[a, g] =` lpc `(…)`
Function File: `[a, g] =` lpc `(x, p)`

  Determines the forward linear predictor by minimizing the prediction error in the least squares sense. Use the Durbin-Levinson algorithm to solve the Yule-Walker equations obtained by the autocorrelation of the input signal.

  x is a data vector used to estimate the lpc model of p-th order, given by the prediction polynomial `a = [1 a(2) … a(p+1)]`. If p is not provided, `length(p) - 1` is used as default.

  x might also be a matrix, in which case each column is regarded as a separate signal. `lpc` will return a model estimate for each column of x.

  g is the variance (power) of the prediction error for each signal in x.

  See also: aryule,levinson.

### 3.13 Sample Rate Change

#### 3.13.1 data2fun

Function File: `[fhandle, fullname] =` data2fun `(ti, yi)`
Function File: `[…] =` data2fun `(…, property, value)`

  Create a vectorized function based on data samples using interpolation.

  The values given in yi (N-by-k matrix) correspond to evaluations of the function y(t) at the points ti (N-by-1 matrix). The data is interpolated and the function handle to the generated interpolant is returned.

  The function accepts property-value pairs described below.

  ‘`file`’

    Code is generated and .m file is created. The value contains the name of the function. The returned function handle is a handle to that file. If value is empty, then a name is automatically generated using `tempname` and the file is created in the current directory. value must not have an extension, since .m will be appended. Numerical values used in the function are stored in a .mat file with the same name as the function.

  ‘`interp`’

    Type of interpolation. See `interp1`.

  See also: interp1.

#### 3.13.2 decimate

Function File: `y =` decimate `(x, q)`
Function File: `y =` decimate `(x, q, n)`
Function File: `y =` decimate `(…, "fir")`

  Downsample the signal x by a reduction factor of q. A lowpass antialiasing filter is applied to the signal prior to reducing the input sequence. By default, an order n Chebyshev type I filter is used. If n is not specified, the default is 8.

  If the optional argument `"fir"` is given, an order n FIR filter is used, with a default order of 30 if n is not given.

  Note that q must be an integer for this rate change method.

  Example:

       ## Generate a signal that starts away from zero, is slowly varying
       ## at the start and quickly varying at the end, decimate and plot.
       ## Since it starts away from zero, you will see the boundary
       ## effects of the antialiasing filter clearly.  Next you will see
       ## how it follows the curve nicely in the slowly varying early
       ## part of the signal, but averages the curve in the quickly
       ## varying late part of the signal.
       t = 0:0.01:2;
       x = chirp (t, 2, .5, 10, "quadratic") + sin (2*pi*t*0.4);
       y = decimate (x, 4);
       stem (t(1:121) * 1000, x(1:121), "-g;Original;"); hold on; # original
       stem (t(1:4:121) * 1000, y(1:31), "-r;Decimated;"); hold off; # decimated

#### 3.13.3 downsample

Function File: `y =` downsample `(x, n)`
Function File: `y =` downsample `(x, n, offset)`

  Downsample the signal, selecting every nth element. If x is a matrix, downsample every column.

  For most signals you will want to use `decimate` instead since it prefilters the high frequency components of the signal and avoids aliasing effects.

  If offset is defined, select every nth element starting at sample offset.

  See also: decimate, interp, resample, upfirdn, upsample.

#### 3.13.4 interp

Function File: `y =` interp `(x, q)`
Function File: `y =` interp `(x, q, n)`
Function File: `y =` interp `(x, q, n, Wc)`

  Upsample the signal x by a factor of q, using an order 2*q*n+1 FIR filter. Note that q must be an integer for this rate change method. n defaults to 4 and Wc defaults to 0.5.

  Example

       # Generate a signal.
       t=0:0.01:2; x=chirp(t,2,.5,10,'quadratic')+sin(2*pi*t*0.4);
       y = interp(x(1:4:length(x)),4,4,1);   # interpolate a sub-sample
       stem(t(1:121)*1000,x(1:121),"-g;Original;"); hold on;
       stem(t(1:121)*1000,y(1:121),"-r;Interpolated;");
       stem(t(1:4:121)*1000,x(1:4:121),"-b;Subsampled;"); hold off;

  See also: decimate, resample.

#### 3.13.5 resample

Function File: `[y, h] =` resample `(x, p, q)`
Function File: `y =` resample `(x, p, q, h)`

  Change the sample rate of x by a factor of p/q. This is performed using a polyphase algorithm. The impulse response h of the antialiasing filter is either specified or either designed with a Kaiser-windowed sinecard.

  Ref [1] J. G. Proakis and D. G. Manolakis, Digital Signal Processing: Principles, Algorithms, and Applications, 4th ed., Prentice Hall, 2007. Chap. 6

  Ref [2] A. V. Oppenheim, R. W. Schafer and J. R. Buck, Discrete-time signal processing, Signal processing series, Prentice-Hall, 1999

#### 3.13.6 upfirdn

Loadable Function: `y =` upfirdn `(x, h, p, q)`

  Upsample, FIR filtering, and downsample.

#### 3.13.7 upsample

Function File: `y =` upsample `(x, n)`
Function File: `y =` upsample `(x, n, offset)`

  Upsample the signal, inserting n-1 zeros between every element.

  If x is a matrix, upsample every column.

  If offset is specified, control the position of the inserted sample in the block of n zeros.

  See also: decimate, downsample, interp, resample, upfirdn.

### 3.14 Pulse Metrics

#### 3.14.1 statelevels

Function File: `levels =` statelevels `(A)`
Function File: `levels =` statelevels `(A, nbins)`
Function File: `levels =` statelevels `(A, nbins, method)`
Function File: `levels =` statelevels `(A, nbins, method, bounds)`
Function File: `[levels, histograms] =` statelevels `(…)`
Function File: `[levels, histograms, binlevels] =` statelevels `(…)`
Function File: statelevels `(…)`

  Estimate state-level for bilevel waveform A using histogram method

  INPUTS:

  A

    Bylevel waveform

  nbins

    Number of histogram bins (100 default)

  method

    State-level estimation method ’mode’ (default) or ’mean’.

  bounds

    2 element vector for histogram lower and upper bounds. Values outside of this will be ignored.

  OUTPUTS:

  levels

    Levels of high and low states

  histograms

    Histogram counts

  binlevels

    Histogram bincenters

  If no outputs are provided, the signal and histogram will be plotted, and display the levels.

### 3.15 Utility

#### 3.15.1 buffer

Function File: `y =` buffer `(x, n, p, opt)`
Function File: `[y, z, opt] =` buffer `(…)`

  Buffer a signal into a data frame. The arguments to `buffer` are

  x

    The data to be buffered.

  n

    The number of rows in the produced data buffer. This is an positive integer value and must be supplied.

  p

    An integer less than n that specifies the under- or overlap between column in the data frame. The default value of p is 0.

  opt

    In the case of an overlap, opt can be either a vector of length p or the string ’nodelay’. If opt is a vector, then the first p entries in y will be filled with these values. If opt is the string ’nodelay’, then the first value of y corresponds to the first value of x.

    In the can of an underlap, opt must be an integer between 0 and `-p`. The represents the initial underlap of the first column of y.

    The default value for opt the vector `zeros (1, p)` in the case of an overlap, or 0 otherwise.

  In the case of a single output argument, y will be padded with zeros to fill the missing values in the data frame. With two output arguments z is the remaining data that has not been used in the current data frame.

  Likewise, the output opt is the overlap, or underlap that might be used for a future call to `code` to allow continuous buffering.

#### 3.15.2 clustersegment

Function File: `clusteridx =` clustersegment `(unos)`

  Calculate boundary indexes of clusters of 1’s.

  The function calculates the initial index and end index of the sequences of 1’s in the rows of unos. The clusters are sought in the rows of the array unos.

  The result is returned in a cell array of size 1-by-Np, where Np is the number of rows in unos. Each element of the cell has two rows. The first row is the initial index of a sequence of 1’s and the second row is the end index of that sequence.

  If Np == 1 the output is a matrix with two rows.

  The function works by finding the indexes of jumps between consecutive values in the rows of unos.

#### 3.15.3 fracshift

Function File: `[y, h] =` fracshift `(x, d)`
Function File: `y =` fracshift `(x, d, h)`

  Shift the series x by a (possibly fractional) number of samples d. The interpolator h is either specified or either designed with a Kaiser-windowed sinecard.

  See also: circshift.

#### 3.15.4 marcumq

Function File: `q =` marcumq `(a, b)`
Function File: `q =` marcumq `(a, b, m)`
Function File: `q =` marcumq `(a, b, m, tol)`

  Compute the generalized Marcum Q function of order m with noncentrality parameter a and argument b. If the order m is omitted it defaults to 1. An optional relative tolerance tol may be included, the default is `eps`.

  If the input arguments are commensurate vectors, this function will produce a table of values.

  This function computes Marcum’s Q function using the infinite Bessel series, truncated when the relative error is less than the specified tolerance. The accuracy is limited by that of the Bessel functions, so reducing the tolerance is probably not useful.

  Reference: Marcum, "Tables of Q Functions", Rand Corporation.

  Reference: R.T. Short, "Computation of Noncentral Chi-squared and Rice Random Variables", www.phaselockedsystems.com/publications

#### 3.15.5 primitive

Function File: `F =` primitive `(f, t, F0)`

  Calculate the primitive of a function.

  The function approximates the primitive (indefinite integral) of the univariate function handle f with constant of integration F0. The output is the primitive evaluated at the points t. The vector t must be ordered and ascending.

  This function is a fancy way of calculating the cumulative sum,

  `F = cumsum (f(t).*diff (t)([1 1:end]))`.

  Example:

       f = @(t) sin (2 * pi * 3 * t);
       t = [0; sort(rand (100, 1))];
       F = primitive (f, t, 0);
       t_true = linspace (0, 1, 1e3).';
       F_true = (1 - cos (2 * pi * 3 * t_true)) / (2 * pi * 3);
       plot (t, F, 'o', t_true, F_true);

  See also: quadgk, cumsum.

#### 3.15.6 sampled2continuous

Function File: `xt =` sampled2continuous `(xn, T, t)`

  Calculate the x(t) reconstructed from samples x[n] sampled at a rate 1/T samples per unit time.

  t is all the instants of time when you need x(t) from x[n]; this time is relative to x[0] and not an absolute time.

  This function can be used to calculate sampling rate effects on aliasing, actual signal reconstruction from discrete samples.

#### 3.15.7 schtrig

v =: `schtrig` (x,lvl,rst=1)
[v,rng] =: `schtrig` (…)

  Implements a multisignal Schmitt trigger with levels lvl.

  The triger works along the first dimension of the 2-dimensional array x. It compares each column in x to the levels in lvl, when the value is higher `max (lvl)` the output v is high (i.e. 1); when the value is below `min (lvl)` the output is low (i.e. 0); and when the value is between the two levels the output retains its value.

  The threshold levels are passed in the array lvl. If this is a scalar, the thresholds are symmetric around 0, i.e. `[-lvl lvl]`.

  The second output argument stores the ranges in which the output is high, so the indexes rng(1,i):rng(2,i) point to the i-th segment of 1s in v. See `clustersegment` for a detailed explanation.

  The function conserves the state of the trigger across calls (persistent variable). If the reset flag is active, i.e. `rst== true`, then the state of the trigger for all signals is set to the low state (i.e. 0).

  Example:

       x = [0 0.5 1 1.5 2 1.5 1.5 1.2 1 0 0].';
       y = schtrig (x, [1.3 1.6]);
       disp ([x y]);
         0.0   0
         0.5   0
         1.0   0
         1.5   0
         2.0   1
         1.5   1
         1.5   1
         1.2   0
         1.0   0
         0.0   0
         0.0   0

  Run `demo schtrig` to see further examples.

  See also: clustersegment.

#### 3.15.8 upsamplefill

Function File: `y =` upsamplefill `(x, v)`
Function File: `y =` upsamplefill `(…, copy)`

  Upsamples a vector interleaving given values or copies of the vector elements.

  The values in the vector v are placed between the elements of x.

  If the optional argument copy is true then v should be a scalar and each value in x are repeat v times.

  Example:

       upsamplefill (eye (2), 2, true)
      ⇒  1   0
           1   0
           1   0
           0   1
           0   1
           0   1
       upsamplefill (eye (2), [-1 -1 -1])
      ⇒  1   0
          -1  -1
          -1  -1
          -1  -1
           0   1
          -1  -1
          -1  -1
          -1  -1

  See also: upsample.

#### 3.15.9 wkeep

Function File: `y =` wkeep `(x, l)`
Function File: `y =` wkeep `(x, l, opt)`

  Extract the elements of x of size l from the center, the right or the left.

#### 3.15.10 wrev

Function File: `y =` wrev `(x)`

  Reverse the order of the element of the vector x.

  See also: flipud, fliplr.

#### 3.15.11 zerocrossing

Function File: `x0 =` zerocrossing `(x, y)`

  Estimates the points at which a given waveform y=y(x) crosses the x-axis using linear interpolation.

  See also: fzero, roots.

#### Footnotes

##### (1)

J. H. McClellan, T. W. Parks and L. R. Rabiner, ‘A Computer Program for Designing Optimum FIR Linear Phase Digital Filters’, IEEE Trans. Audio Electroacoust., vol. AU-21, 1973, pp. 506–525.

##### (2)

Tapio Saramäki, ‘Finite impulse response filter design’, Chapter 4 in ‘Handbook for Digital Signal Processing’, edited by S. K. Mitra and J. F. Kaiser, John Wiley and Sons, New York, 1993, pp. 155–277. (https://homepages.tuni.fi/tapio.saramaki/Mitra_Kaiser.pdf)

##### (3)

K. Ichige, M. Iwaki, algorithm and R. Ishii, ‘Accurate Estimation of Minimum Filter Length for Optimum FIR Digital Filters’, IEEE Transactions on Circuits and Systems, Vol. 47, No. 10, 2000, pp. 1008–1017

##### (4)

K. Avci & A. Nacaroglu, ‘Exponential Window Family’, SIPIJ Vol. 4 No. 4, August 2013.

##### (5)

S. Gade & H. Herlufsen, ‘Windows to FFT analysis (Part I)’, Technical Review 3, Bruel & Kjaer, 1987