Signal Processing (GNU Octave (version 6.0.92))

Next: Image Processing, Previous: Geometry, Up: Top [Contents][Index]

## 31 Signal Processing

This chapter describes the signal processing and fast Fourier transform functions available in Octave. Fast Fourier transforms are computed with the FFTW or FFTPACK libraries depending on how Octave is built.

: fft (x)
: fft (x, n)
: fft (x, n, dim)

  Compute the discrete Fourier transform of x using a Fast Fourier Transform (FFT) algorithm.

  The FFT is calculated along the first non-singleton dimension of the array. Thus if x is a matrix, `fft (x)` computes the FFT for each column of x.

  If called with two arguments, n is expected to be an integer specifying the number of elements of x to use, or an empty matrix to specify that its value should be ignored. If n is larger than the dimension along which the FFT is calculated, then x is resized and padded with zeros. Otherwise, if n is smaller than the dimension along which the FFT is calculated, then x is truncated.

  If called with three arguments, dim is an integer specifying the dimension of the matrix along which the FFT is performed.

  See also: ifft, fft2, fftn, fftw.

: ifft (x)
: ifft (x, n)
: ifft (x, n, dim)

  Compute the inverse discrete Fourier transform of x using a Fast Fourier Transform (FFT) algorithm.

  The inverse FFT is calculated along the first non-singleton dimension of the array. Thus if x is a matrix, `fft (x)` computes the inverse FFT for each column of x.

  If called with two arguments, n is expected to be an integer specifying the number of elements of x to use, or an empty matrix to specify that its value should be ignored. If n is larger than the dimension along which the inverse FFT is calculated, then x is resized and padded with zeros. Otherwise, if n is smaller than the dimension along which the inverse FFT is calculated, then x is truncated.

  If called with three arguments, dim is an integer specifying the dimension of the matrix along which the inverse FFT is performed.

  See also: fft, ifft2, ifftn, fftw.

: fft2 (A)
: fft2 (A, m, n)

  Compute the two-dimensional discrete Fourier transform of A using a Fast Fourier Transform (FFT) algorithm.

  The optional arguments m and n may be used specify the number of rows and columns of A to use. If either of these is larger than the size of A, A is resized and padded with zeros.

  If A is a multi-dimensional matrix, each two-dimensional sub-matrix of A is treated separately.

  See also: ifft2, fft, fftn, fftw.

: ifft2 (A)
: ifft2 (A, m, n)

  Compute the inverse two-dimensional discrete Fourier transform of A using a Fast Fourier Transform (FFT) algorithm.

  The optional arguments m and n may be used specify the number of rows and columns of A to use. If either of these is larger than the size of A, A is resized and padded with zeros.

  If A is a multi-dimensional matrix, each two-dimensional sub-matrix of A is treated separately.

  See also: fft2, ifft, ifftn, fftw.

: fftn (A)
: fftn (A, size)

  Compute the N-dimensional discrete Fourier transform of A using a Fast Fourier Transform (FFT) algorithm.

  The optional vector argument size may be used specify the dimensions of the array to be used. If an element of size is smaller than the corresponding dimension of A, then the dimension of A is truncated prior to performing the FFT. Otherwise, if an element of size is larger than the corresponding dimension then A is resized and padded with zeros.

  See also: ifftn, fft, fft2, fftw.

: ifftn (A)
: ifftn (A, size)

  Compute the inverse N-dimensional discrete Fourier transform of A using a Fast Fourier Transform (FFT) algorithm.

  The optional vector argument size may be used specify the dimensions of the array to be used. If an element of size is smaller than the corresponding dimension of A, then the dimension of A is truncated prior to performing the inverse FFT. Otherwise, if an element of size is larger than the corresponding dimension then A is resized and padded with zeros.

  See also: fftn, ifft, ifft2, fftw.

Octave uses the FFTW libraries to perform FFT computations. When Octave starts up and initializes the FFTW libraries, they read a system wide file (on a Unix system, it is typically `/etc/fftw/wisdom`) that contains information useful to speed up FFT computations. This information is called the wisdom. The system-wide file allows wisdom to be shared between all applications using the FFTW libraries.

Use the `fftw` function to generate and save wisdom. Using the utilities provided together with the FFTW libraries (`fftw-wisdom` on Unix systems), you can even add wisdom generated by Octave to the system-wide wisdom file.

: method = fftw ("planner")
: fftw ("planner", method)
: wisdom = fftw ("dwisdom")
: fftw ("dwisdom", wisdom)
: fftw ("threads", nthreads)
: nthreads = fftw ("threads")

  Manage FFTW wisdom data.

  Wisdom data can be used to significantly accelerate the calculation of the FFTs, but implies an initial cost in its calculation. When the FFTW libraries are initialized, they read a system wide wisdom file (typically in `/etc/fftw/wisdom`), allowing wisdom to be shared between applications other than Octave. Alternatively, the `fftw` function can be used to import wisdom. For example,

      wisdom = fftw ("dwisdom")

  will save the existing wisdom used by Octave to the string wisdom. This string can then be saved to a file and restored using the `save` and `load` commands respectively. This existing wisdom can be re-imported as follows

      fftw ("dwisdom", wisdom)

  If wisdom is an empty string, then the wisdom used is cleared.

  During the calculation of Fourier transforms further wisdom is generated. The fashion in which this wisdom is generated is also controlled by the `fftw` function. There are five different manners in which the wisdom can be treated:

  `"estimate"`

    Specifies that no run-time measurement of the optimal means of calculating a particular is performed, and a simple heuristic is used to pick a (probably sub-optimal) plan. The advantage of this method is that there is little or no overhead in the generation of the plan, which is appropriate for a Fourier transform that will be calculated once.

  `"measure"`

    In this case a range of algorithms to perform the transform is considered and the best is selected based on their execution time.

  `"patient"`

    Similar to `"measure"`, but a wider range of algorithms is considered.

  `"exhaustive"`

    Like `"measure"`, but all possible algorithms that may be used to treat the transform are considered.

  `"hybrid"`

    As run-time measurement of the algorithm can be expensive, this is a compromise where `"measure"` is used for transforms up to the size of 8192 and beyond that the `"estimate"` method is used.

  The default method is `"estimate"`. The current method can be queried with

      method = fftw ("planner")

  or set by using

      fftw ("planner", method)

  Note that calculated wisdom will be lost when restarting Octave. However, the wisdom data can be reloaded if it is saved to a file as described above. Saved wisdom files should not be used on different platforms since they will not be efficient and the point of calculating the wisdom is lost.

  The number of threads used for computing the plans and executing the transforms can be set with

      fftw ("threads", NTHREADS)

  Note that octave must be compiled with multi-threaded FFTW support for this feature. The number of processors available to the current process is used per default.

  See also: fft, ifft, fft2, ifft2, fftn, ifftn.

: fftconv (x, y)
: fftconv (x, y, n)

  Convolve two vectors using the FFT for computation.

  `c = fftconv (x, y)` returns a vector of length equal to `length (x) + length (y) - 1`. If x and y are the coefficient vectors of two polynomials, the returned value is the coefficient vector of the product polynomial.

  The computation uses the FFT by calling the function `fftfilt`. If the optional argument n is specified, an N-point FFT is used.

  See also: deconv, conv, conv2.

: fftfilt (b, x)
: fftfilt (b, x, n)

  Filter x with the FIR filter b using the FFT.

  If x is a matrix, filter each column of the matrix.

  Given the optional third argument, n, `fftfilt` uses the overlap-add method to filter x with b using an N-point FFT. The FFT size must be an even power of 2 and must be greater than or equal to the length of b. If the specified n does not meet these criteria, it is automatically adjusted to the nearest value that does.

  See also: filter, filter2.

: y = filter (b, a, x)
: [y, sf] = filter (b, a, x, si)
: [y, sf] = filter (b, a, x, [], dim)
: [y, sf] = filter (b, a, x, si, dim)

  Apply a 1-D digital filter to the data x.

  `filter` returns the solution to the following linear, time-invariant difference equation:

       N                   M
      SUM a(k+1) y(n-k) = SUM b(k+1) x(n-k)    for 1<=n<=length(x)
      k=0                 k=0

  where N=length(a)-1 and M=length(b)-1. The result is calculated over the first non-singleton dimension of x or over dim if supplied.

  An equivalent form of the equation is:

                N                   M
      y(n) = - SUM c(k+1) y(n-k) + SUM d(k+1) x(n-k)  for 1<=n<=length(x)
               k=1                 k=0

  where c = a/a(1) and d = b/a(1).

  If the fourth argument si is provided, it is taken as the initial state of the system and the final state is returned as sf. The state vector is a column vector whose length is equal to the length of the longest coefficient vector minus one. If si is not supplied, the initial state vector is set to all zeros.

  In terms of the Z Transform, y is the result of passing the discrete-time signal x through a system characterized by the following rational system function:

                M
               SUM d(k+1) z^(-k)
               k=0
      H(z) = ---------------------
                  N
             1 + SUM c(k+1) z^(-k)
                 k=1

  See also: filter2, fftfilt, freqz.

: y = filter2 (b, x)
: y = filter2 (b, x, shape)

  Apply the 2-D FIR filter b to x.

  If the argument shape is specified, return an array of the desired shape. Possible values are:

  `"full"`

    pad x with zeros on all sides before filtering.

  `"same"`

    unpadded x (default)

  `"valid"`

    trim x after filtering so edge effects are no included.

  Note this is just a variation on convolution, with the parameters reversed and b rotated 180 degrees.

  See also: conv2.

: [h, w] = freqz (b, a, n, "whole")
: [h, w] = freqz (b)
: [h, w] = freqz (b, a)
: [h, w] = freqz (b, a, n)
: h = freqz (b, a, w)
: [h, w] = freqz (…, Fs)
: freqz (…)

  Return the complex frequency response h of the rational IIR filter whose numerator and denominator coefficients are b and a, respectively.

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

  The output value w is a vector of the frequencies.

  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. For fastest computation, n should factor into a small number of small primes.

  If the fourth argument, `"whole"`, is omitted the response is evaluated at frequencies between 0 and pi.

  `freqz (b, a, w)`

  Evaluate the response at the specific frequencies in the vector w. The values for w are measured in radians.

  `[…] = freqz (…, Fs)`

  Return frequencies in Hz instead of radians assuming a sampling rate Fs. If you are evaluating the response at specific frequencies w, those frequencies should be requested in Hz rather than radians.

  `freqz (…)`

  Plot the magnitude and phase response of h rather than returning them.

  See also: freqz_plot.

: freqz_plot (w, h)
: freqz_plot (w, h, freq_norm)

  Plot the magnitude and phase response of h.

  If the optional freq_norm argument is true, the frequency vector w is in units of normalized radians. If freq_norm is false, or not given, then w is measured in Hertz.

  See also: freqz.

: sinc (x)

  Compute the sinc function.

  Return sin (pi*x) / (pi*x).

: b = unwrap (x)
: b = unwrap (x, tol)
: b = unwrap (x, tol, dim)

  Unwrap radian phases by adding or subtracting multiples of 2*pi as appropriate to remove jumps greater than tol.

  tol defaults to pi.

  Unwrap will work along the dimension dim. If dim is unspecified it defaults to the first non-singleton dimension.

: [a, b] = arch_fit (y, x, p, iter, gamma, a0, b0)

  Fit an ARCH regression model to the time series y using the scoring algorithm in Engle’s original ARCH paper.

  The model is

      y(t) = b(1) * x(t,1) + … + b(k) * x(t,k) + e(t),
      h(t) = a(1) + a(2) * e(t-1)^2 + … + a(p+1) * e(t-p)^2

  in which e(t) is N(0, h(t)), given a time-series vector y up to time t-1 and a matrix of (ordinary) regressors x up to t. The order of the regression of the residual variance is specified by p.

  If invoked as `arch_fit (y, k, p)` with a positive integer k, fit an ARCH(k, p) process, i.e., do the above with the t-th row of x given by

      [1, y(t-1), …, y(t-k)]

  Optionally, one can specify the number of iterations iter, the updating factor gamma, and initial values a0 and b0 for the scoring algorithm.

: arch_rnd (a, b, t)

  Simulate an ARCH sequence of length t with AR coefficients b and CH coefficients a.

  The result y(t) follows the model

      y(t) = b(1) + b(2) * y(t-1) + … + b(lb) * y(t-lb+1) + e(t),

  where e(t), given y up to time t-1, is N(0, h(t)), with

      h(t) = a(1) + a(2) * e(t-1)^2 + … + a(la) * e(t-la+1)^2

: [pval, lm] = arch_test (y, x, p)

  For a linear regression model

      y = x * b + e

  perform a Lagrange Multiplier (LM) test of the null hypothesis of no conditional heteroscedascity against the alternative of CH(p).

  I.e., the model is

      y(t) = b(1) * x(t,1) + … + b(k) * x(t,k) + e(t),

  given y up to t-1 and x up to t, e(t) is N(0, h(t)) with

      h(t) = v + a(1) * e(t-1)^2 + … + a(p) * e(t-p)^2,

  and the null is a(1) == … == a(p) == 0.

  If the second argument is a scalar integer, k, perform the same test in a linear autoregression model of order k, i.e., with

      [1, y(t-1), …, y(t-k)]

  as the t-th row of x.

  Under the null, LM approximately has a chisquare distribution with p degrees of freedom and pval is the p-value (1 minus the CDF of this distribution at LM) of the test.

  If no output argument is given, the p-value is displayed.

: arma_rnd (a, b, v, t, n)

  Return a simulation of the ARMA model.

  The ARMA model is defined by

      x(n) = a(1) * x(n-1) + … + a(k) * x(n-k)
           + e(n) + b(1) * e(n-1) + … + b(l) * e(n-l)

  in which k is the length of vector a, l is the length of vector b and e is Gaussian white noise with variance v. The function returns a vector of length t.

  The optional parameter n gives the number of dummy x(i) used for initialization, i.e., a sequence of length t+n is generated and x(n+1:t+n) is returned. If n is omitted, n = 100 is used.

: autoreg_matrix (y, k)

  Given a time series (vector) y, return a matrix with ones in the first column and the first k lagged values of y in the other columns.

  In other words, for t > k, `[1, y(t-1), …, y(t-k)]` is the t-th row of the result.

  The resulting matrix may be used as a regressor matrix in autoregressions.

: bartlett (m)

  Return the filter coefficients of a Bartlett (triangular) window of length m.

  For a definition of the Bartlett window see, e.g., A.V. Oppenheim & R. W. Schafer, Discrete-Time Signal Processing.

: blackman (m)
: blackman (m, "periodic")
: blackman (m, "symmetric")

  Return the filter coefficients of a Blackman 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.

  For a definition of the Blackman window, see, e.g., A.V. Oppenheim & R. W. Schafer, Discrete-Time Signal Processing.

: detrend (x, p)

  If x is a vector, `detrend (x, p)` removes the best fit of a polynomial of order p from the data x.

  If x is a matrix, `detrend (x, p)` does the same for each column in x.

  The second argument p is optional. If it is not specified, a value of 1 is assumed. This corresponds to removing a linear trend.

  The order of the polynomial can also be given as a string, in which case p must be either `"constant"` (corresponds to `p=0`) or `"linear"` (corresponds to `p=1`).

  See also: polyfit.

: [d, dd] = diffpara (x, a, b)

  Return the estimator d for the differencing parameter of an integrated time series.

  The frequencies from [2*pi*a/t, 2*pi*b/T] are used for the estimation. If b is omitted, the interval [2*pi/T, 2*pi*a/T] is used. If both b and a are omitted then a = 0.5 * sqrt (T) and b = 1.5 * sqrt (T) is used, where T is the sample size. If x is a matrix, the differencing parameter of each column is estimated.

  The estimators for all frequencies in the intervals described above is returned in dd.

  The value of d is simply the mean of dd.

  Reference: P.J. Brockwell & R.A. Davis. Time Series: Theory and Methods. Springer 1987.

: durbinlevinson (c, oldphi, oldv)

  Perform one step of the Durbin-Levinson algorithm.

  The vector c specifies the autocovariances `[gamma_0, …, gamma_t]` from lag 0 to t, oldphi specifies the coefficients based on c(t-1) and oldv specifies the corresponding error.

  If oldphi and oldv are omitted, all steps from 1 to t of the algorithm are performed.

: fftshift (x)
: fftshift (x, dim)

  Perform a shift of the vector x, for use with the `fft` and `ifft` functions, in order to move the frequency 0 to the center of the vector or matrix.

  If x is a vector of N elements corresponding to N time samples spaced by dt, then `fftshift (fft (x))` corresponds to frequencies

      f = [ -(ceil((N-1)/2):-1:1), 0, (1:floor((N-1)/2)) ] * df

  where df = 1 / (N * dt).

  If x is a matrix, the same holds for rows and columns. If x is an array, then the same holds along each dimension.

  The optional dim argument can be used to limit the dimension along which the permutation occurs.

  See also: ifftshift.

: ifftshift (x)
: ifftshift (x, dim)

  Undo the action of the `fftshift` function.

  For even length x, `fftshift` is its own inverse, but odd lengths differ slightly.

  See also: fftshift.

: fractdiff (x, d)

  Compute the fractional differences (1-L)^d x where L denotes the lag-operator and d is greater than -1.

: hamming (m)
: hamming (m, "periodic")
: hamming (m, "symmetric")

  Return the filter coefficients of a Hamming 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.

  For a definition of the Hamming window see, e.g., A.V. Oppenheim & R. W. Schafer, Discrete-Time Signal Processing.

: hanning (m)
: hanning (m, "periodic")
: hanning (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.

  For a definition of the Hanning window see, e.g., A.V. Oppenheim & R. W. Schafer, Discrete-Time Signal Processing.

: hurst (x)

  Estimate the Hurst parameter of sample x via the rescaled range statistic.

  If x is a matrix, the parameter is estimated for every column.

: pp = pchip (x, y)
: yi = pchip (x, y, xi)

  Return the Piecewise Cubic Hermite Interpolating Polynomial (pchip) of points x and y.

  If called with two arguments, return the piecewise polynomial pp that may be used with `ppval` to evaluate the polynomial at specific points.

  When called with a third input argument, `pchip` evaluates the pchip polynomial at the points xi. The third calling form is equivalent to `ppval (pchip (x, y), xi)`.

  The variable x must be a strictly monotonic vector (either increasing or decreasing) of length n.

  y can be either a vector or array. If y is a vector then it must be the same length n as x. If y is an array then the size of y must have the form `[s1, s2, …, sk, n]` The array is reshaped internally to a matrix where the leading dimension is given by `s1 * s2 * … * sk` and each row of this matrix is then treated separately. Note that this is exactly opposite to `interp1` but is done for MATLAB compatibility.

  See also: spline, ppval, mkpp, unmkpp.

: [Pxx, w] = periodogram (x)
: [Pxx, w] = periodogram (x, win)
: [Pxx, w] = periodogram (x, win, nfft)
: [Pxx, f] = periodogram (x, win, nfft, Fs)
: [Pxx, f] = periodogram (…, "range")
: periodogram (…)

  Return the periodogram (Power Spectral Density) of x.

  The possible inputs are:

  x

    data vector. If x is real-valued a one-sided spectrum is estimated. If x is complex-valued, or `"range"` specifies `"twosided"`, the full spectrum is estimated.

  win

    window weight data. If window is empty or unspecified a default rectangular window is used. Otherwise, the window is applied to the signal (`x .* win`) before computing the periodogram. The window data must be a vector of the same length as x.

  nfft

    number of frequency bins. The default is 256 or the next higher power of 2 greater than the length of x (`max (256, 2.^nextpow2 (length (x)))`). If nfft is greater than the length of the input then x will be zero-padded to the length of nfft.

  Fs

    sampling rate. The default is 1.

  range

    range of spectrum. `"onesided"` computes spectrum from [0:nfft/2+1]. `"twosided"` computes spectrum from [0:nfft-1].

  The optional second output w are the normalized angular frequencies. For a one-sided calculation w is in the range [0, pi] if nfft is even and [0, pi) if nfft is odd. Similarly, for a two-sided calculation w is in the range [0, 2*pi] or [0, 2*pi) depending on nfft.

  If a sampling frequency is specified, Fs, then the output frequencies f will be in the range [0, Fs/2] or [0, Fs/2) for one-sided calculations. For two-sided calculations the range will be [0, Fs).

  When called with no outputs the periodogram is immediately plotted in the current figure window.

  See also: fft.

: sinetone (freq, rate, sec, ampl)

  Return a sinetone of frequency freq with a length of sec seconds at sampling rate rate and with amplitude ampl.

  The arguments freq and ampl may be vectors of common size.

  The defaults are rate = 8000, sec = 1, and ampl = 64.

  See also: sinewave.

: sinewave (m, n, d)

  Return an m-element vector with i-th element given by `sin (2 * pi * (i+d-1) / n)`.

  The default value for d is 0 and the default value for n is m.

  See also: sinetone.

: spectral_adf (c)
: spectral_adf (c, win)
: spectral_adf (c, win, b)

  Return the spectral density estimator given a vector of autocovariances c, window name win, and bandwidth, b.

  The window name, e.g., `"triangle"` or `"rectangle"` is used to search for a function called `win_lw`.

  If win is omitted, the triangle window is used.

  If b is omitted, `1 / sqrt (length (x))` is used.

  See also: spectral_xdf.

: spectral_xdf (x)
: spectral_xdf (x, win)
: spectral_xdf (x, win, b)

  Return the spectral density estimator given a data vector x, window name win, and bandwidth, b.

  The window name, e.g., `"triangle"` or `"rectangle"` is used to search for a function called `win_sw`.

  If win is omitted, the triangle window is used.

  If b is omitted, `1 / sqrt (length (x))` is used.

  See also: spectral_adf.

: spencer (x)

  Return Spencer’s 15-point moving average of each column of x.

: y = stft (x)
: y = stft (x, win_size)
: y = stft (x, win_size, inc)
: y = stft (x, win_size, inc, num_coef)
: y = stft (x, win_size, inc, num_coef, win_type)
: [y, c] = stft (…)

  Compute the short-time Fourier transform of the vector x with num_coef coefficients by applying a window of win_size data points and an increment of inc points.

  Before computing the Fourier transform, one of the following windows is applied:

  `"hanning"`

    win_type = 1

  `"hamming"`

    win_type = 2

  `"rectangle"`

    win_type = 3

  The window names can be passed as strings or by the win_type number.

  The following defaults are used for unspecified arguments: win_size = 80, inc = 24, num_coef = 64, and win_type = 1.

  `y = stft (x, …)` returns the absolute values of the Fourier coefficients according to the num_coef positive frequencies.

  `[y, c] = stft (x, …)` returns the entire STFT-matrix y and a 3-element vector c containing the window size, increment, and window type, which is needed by the `synthesis` function.

  See also: synthesis.

: x = synthesis (y, c)

  Compute a signal from its short-time Fourier transform y and a 3-element vector c specifying window size, increment, and window type.

  The values y and c can be derived by

      [y, c] = stft (x , …)

  See also: stft.

: [a, v] = yulewalker (c)

  Fit an AR (p)-model with Yule-Walker estimates given a vector c of autocovariances `[gamma_0, …, gamma_p]`.

  Returns the AR coefficients, a, and the variance of white noise, v.

Next: Image Processing, Previous: Geometry, Up: Top [Contents][Index]