Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

This section describes how to filter discrete signals using
the MATLAB^{®} `filter`

function and other Signal
Processing Toolbox™ functions.
It also discusses how to use the toolbox functions to analyze filter
characteristics, including impulse response, magnitude and phase response,
group delay, and zero-pole locations.

The mathematical foundation of filtering is convolution. The MATLAB `conv`

function performs standard one-dimensional
convolution, convolving one vector with another:

conv([1 1 1],[1 1 1]) ans = 1 2 3 2 1

Convolve rectangular matrices for two-dimensional signal processing
using the `conv2`

function.

A digital filter's output *y*(*k*)
is related to its input *x*(*k*)
by convolution with its impulse response *h*(*k*).

$$y(k)={\displaystyle \sum _{l=-\infty}^{\infty}h}(l)x(k-l)$$

If a digital filter's impulse response *h*(*k*)
is finite in length, and the input *x*(*k*)
is also of finite length, you can implement the filter using `conv`

.
Store *x*(*k*) in a vector `x`

, *h*(*k*)
in a vector `h`

, and convolve the two:

x = randn(5,1); % A random vector of length 5 h = [1 1 1 1]/4; % Length 4 averaging filter y = conv(h,x);

The length of the output is the sum of the finite-length input vectors minus 1.

In general, the *z*-transform *Y*(*z*)
of a discrete-time filter's output *y*(*n*)
is related to the *z*-transform *X*(*z*)
of the input by

$$Y(z)=H(z)X(z)=\frac{b(1)+b(2){z}^{-1}+\mathrm{...}+b(n+1){z}^{-n}}{a(1)+a(2){z}^{-1}+\mathrm{...}+a(m+1){z}^{-m}}X(z)$$

where *H*(*z*) is the filter's *transfer
function*. Here, the constants *b*(*i*)
and *a*(*i*) are the filter coefficients
and the order of the filter is the maximum of *n* and *m*.

The filter coefficients start with subscript 1, rather than 0. This reflects the standard indexing scheme used for MATLAB vectors.

MATLAB filter functions store the coefficients in two vectors, one for the numerator and one for the denominator. By convention, it uses row vectors for filter coefficients.

Many standard names for filters reflect the number of `a`

and `b`

coefficients
present:

When

`n`

= 0 (that is,`b`

is a scalar), the filter is an Infinite Impulse Response (IIR), all-pole, recursive, or autoregressive (AR) filter.When

`m`

= 0 (that is,`a`

is a scalar), the filter is a Finite Impulse Response (FIR), all-zero, nonrecursive, or moving-average (MA) filter.If both

`n`

and`m`

are greater than zero, the filter is an IIR, pole-zero, recursive, or autoregressive moving-average (ARMA) filter.

The acronyms AR, MA, and ARMA are usually applied to filters associated with filtered stochastic processes.

It is simple to work back to a difference equation from the
Z-transform relation shown earlier. Assume that *a*(1) = 1. Move the denominator to the left
side and take the inverse Z-transform.

$$y(k)+a(2)y(k-1)+\dots +a(m+1)y(k-m)=b(1)x(k)+b(2)x(k-1)+\dots +b(n+1)x(k-n)$$

In terms of current and past inputs, and past outputs, *y*(*k*)
is

$$y(k)=b(1)x(k)+b(2)x(k-1)+\dots +b(n+1)x(k-n)-a(2)y(k-1)-\dots -a(m+1)y(k-m)$$

This is the standard time-domain representation of a digital
filter, computed starting with *y*(1) and assuming
a causal system with zero initial conditions. This representation's
progression is

$$\begin{array}{c}y(1)=b(1)x(1)\\ y(2)=b(1)x(2)+b(2)x(1)-a(2)y(1)\\ y(3)=b(1)x(3)+b(2)x(2)+b(3)x(1)-a(2)y(2)-a(3)y(1)\\ \vdots \text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}=\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\text{\hspace{0.05em}}\vdots \end{array}$$

A filter in this form is easy to implement with the `filter`

function. For example, a simple
single-pole filter (lowpass) is

B = 1; % Numerator A = [1 -0.9]; % Denominator

where the vectors `B`

and `A`

represent
the coefficients of a filter in transfer function form. Note that
the `A`

coefficient vectors are written as if the
output and input terms are separated in the difference equation. For
the example, the previous coefficient vectors represent a linear constant-coefficient
difference equation of

$$y(n)-0.9y(n-1)=x(n)$$

`A(2)`

coefficient,
results in the difference equation$$y(n)+0.9y(n-1)=x(n)$$

B = 1; %Numerator A = [1 0.9]; %Denominator

To apply this filter to your data, use

y = filter(B,A,x);

`filter`

gives you as many output samples as
there are input samples, that is, the length of `y`

is
the same as the length of `x`

. If the first element
of `a`

is not 1, `filter`

divides
the coefficients by `a(1)`

before implementing the
difference equation.

Was this topic helpful?