Documentation

filter

1-D digital filter

Syntax

Description

example

y = filter(b,a,x) filters the input data, x, using a rational transfer function defined by the numerator and denominator coefficients b and a, respectively.

If a(1) is not equal to 1, then filter normalizes the filter coefficients by a(1). Therefore, a(1) must be nonzero.

  • If x is a vector, then filter returns the filtered data as a vector of the same size as x.

  • If x is a matrix, then filter acts along the first dimension and returns the filtered data for each column.

  • If x is a multidimensional array, then filter acts along the first array dimension whose size does not equal 1.

example

y = filter(b,a,x,zi) uses initial conditions, zi, for the filter delays. The length of zi must equal max(length(a),length(b))-1.

example

y = filter(b,a,x,zi,dim) acts along dimension dim. For example, if x is a matrix, then filter(b,a,x,zi,2) returns the filtered data for each row.

example

[y,zf] = filter(___) also returns the final conditions, zf, of the filter delays, using any of the previous syntax arguments.

Examples

expand all

Moving-Average Filter of Vector Data

Find the moving-average of a vector without using a for loop.

Create a 1-by-100 row vector of sinusoidal input data corrupted by random noise. Initialize the random number generator to make the output of rand repeatable.

t = linspace(-pi,pi,100);
rng default
x = sin(t) + 0.25*rand(size(t));

A moving-average filter is represented by the following difference equation,

$$ y(n)=\frac{1}{windowSize}\left(x(n)+x(n-1)+...+x(n-(windowSize-1))\right). $$

Define the numerator coefficients of the rational transfer function. Use a window size of 5.

windowSize = 5;

b = (1/windowSize)*ones(1,windowSize)
b =

    0.2000    0.2000    0.2000    0.2000    0.2000

Define the denominator coefficients of the rational transfer function.

a = 1;

Find the moving-average of the data with a window size of 5.

y = filter(b,a,x);

y(1) is equivalent to 0.2*x(1).

y(2) is equivalent to |0.2*(x(1)+x(2)).

...

y(5) is equivalent to 0.2*(sum(x(1:5)) = mean(x(1:5)).

y(6) is equivalent to mean(x(2:6)).

...

y(100) is equivalent to mean(x(96:100)).

Compare the original data and the filtered data using an overlaid plot.

plot(t,x)
hold on
plot(t,y)

grid on
legend('Input Data','Filtered Data','Location','NorthWest')
title('Plot of Input and Filtered Data')

Apply Rational Transfer Function to Matrix

Filter the rows or columns of a matrix with the following rational transfer function,

$$H(z) = \frac{b(1)}{a(1)+a(2)z^{-1}}=\frac{1}{1-0.2z^{-1}}.$$

Create a 15-by-2 matrix of random input data. Initialize the random number generator to make the output of rand repeatable.

rng default
x = rand(15,2)
x =

    0.8147    0.1419
    0.9058    0.4218
    0.1270    0.9157
    0.9134    0.7922
    0.6324    0.9595
    0.0975    0.6557
    0.2785    0.0357
    0.5469    0.8491
    0.9575    0.9340
    0.9649    0.6787
    0.1576    0.7577
    0.9706    0.7431
    0.9572    0.3922
    0.4854    0.6555
    0.8003    0.1712

Define the numerator and denominator coefficients for the rational transfer function.

b = 1;
a = [1 -0.2];

Apply the transfer function along the first dimension (default) and return the 1-D digital filter of each column.

y = filter(b,a,x)
y =

    0.8147    0.1419
    1.0687    0.4501
    0.3407    1.0058
    0.9815    0.9934
    0.8287    1.1582
    0.2633    0.8874
    0.3312    0.2132
    0.6131    0.8918
    1.0801    1.1123
    1.1809    0.9012
    0.3938    0.9380
    1.0494    0.9307
    1.1670    0.5784
    0.7188    0.7712
    0.9440    0.3254

Compare the original data and the filtered data using an overlaid plot for each column.

Plot the first column of input and filtered data.

t = 0:length(x)-1;

plot(t,x(:,1))
hold on
plot(t,y(:,1))

grid on
legend('Input Data','Filtered Data','Location','BestOutside')
title('Plot of Input and Filtered Data from Column 1')

Plot the second column of input and filtered data.

figure

plot(t,x(:,2))
hold on
plot(t,y(:,2))

grid on
legend('Input Data','Filtered Data','Location','BestOutside')
title('Plot of Input and Filtered Data from Column 2')

Alternatively, you can filter the rows of a matrix by specifying dim = 2.

Consider the 2-by-15 matrix, x', whose rows contain the data to filter.

Filter the matrix along the second dimension. Use the default initial conditions for filter delays.

dim = 2;
y2 = filter(b,a,x',[],dim)
y2 =

  Columns 1 through 7

    0.8147    1.0687    0.3407    0.9815    0.8287    0.2633    0.3312
    0.1419    0.4501    1.0058    0.9934    1.1582    0.8874    0.2132

  Columns 8 through 14

    0.6131    1.0801    1.1809    0.3938    1.0494    1.1670    0.7188
    0.8918    1.1123    0.9012    0.9380    0.9307    0.5784    0.7712

  Column 15

    0.9440
    0.3254

y2 is the transpose of y from above.

Filter Data in Sections

Use initial and final conditions for filter delays to filter data in sections, especially if memory limitations are a consideration.

Generate a large random data sequence and split it into two segments, x1 and x2.

x = randn(10000,1);

x1 = x(1:5000);
x2 = x(5001:end);

The whole sequence, x, is the vertical concatenation of x1 and x2.

Define the numerator and denominator coefficients for the rational transfer function,

$$ H(z) = \frac{b(1)+b(2)z^{-1}}{a(1)+a(2)z^{-1}} =
\frac{2+3z^{-1}}{1+0.2z^{-1}}. $$

b = [2,3];
a = [1,0.2];

Filter the subsequences x1 and x2 one at a time. Output the final conditions from filtering x1 to store the internal status of the filter at the end of the first segment.

[y1,zf] = filter(b,a,x1);

Use the final conditions from filtering x1 as initial conditions to filter the second segment, x2.

y2 = filter(b,a,x2,zf);

y1 is the filtered data from x1, and y2 s the filtered data from x2. The entire filtered sequence is the vertical concatenation of y1 and y2.

Filter the entire sequence simultaneously for comparison.

y = filter(b,a,x);

isequal(y,[y1;y2])
ans =

     1

Input Arguments

expand all

b — Numerator coefficients of rational transfer functionvector

Numerator coefficients of the rational transfer function, specified as a vector.

Data Types: double | single
Complex Number Support: Yes

a — Denominator coefficients of rational transfer functionvector

Denominator coefficients of the rational transfer function, specified as a vector.

Data Types: double | single
Complex Number Support: Yes

x — Input datavector | matrix | multidimensional array

Input data, specified as a vector, matrix, or multidimensional array.

Data Types: double | single
Complex Number Support: Yes

zi — Initial conditions for filter delays[] (default) | vector | matrix | multidimensional array

Initial conditions for filter delays, specified as a vector, matrix, or multidimensional array.

  • If zi is a vector, then its length must be max(length(a),length(b))-1.

  • If zi is a matrix or multidimensional array, then the size of the leading dimension must be max(length(a),length(b))-1. The size of each remaining dimension must match the size of the corresponding dimension of x.

The default value, specified by [], initializes all filter delays to zero.

Data Types: double | single
Complex Number Support: Yes

dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, the default is the first array dimension whose size does not equal 1.

Consider a two-dimensional input array, x.

  • If dim = 1, then filter(b,a,x,zi,1) works along the rows of x and returns the filter applied to each column.

  • If dim = 2, then filter(b,a,zi,2) works along the columns of x and returns the filter applied to each row.

If dim is greater than ndims(x), then filter returns x.

Data Types: double | single

Output Arguments

expand all

y — Filtered datavector | matrix | multidimensional array

Filtered data, returned as a vector, matrix, or multidimensional array of the same size as the input data, x.

zf — Final conditions for filter delaysvector | matrix | multidimensional array

Final conditions for filter delays, returned as a vector, matrix, or multidimensional array.

  • If x is a vector, then zf is a column vector of length max(length(a),length(b))-1.

  • If x is a matrix or multidimensional array, then zf is an array of column vectors of length max(length(a),length(b))-1, such that the number of columns in zf is equivalent to the number of columns in x.

More About

expand all

Rational Transfer Function

The input-output description of the filter operation on a vector in the Z-transform domain is a rational transfer function. A rational transfer function is of the form,

Y(z)=b(1)+b(2)z1+...+b(nb+1)znb1+a(2)z1+...+a(na+1)znaX(z),

which handles both FIR and IIR filters [1]. na is the feedback filter order, and nb is the feedforward filter order.

You also can express the rational transfer function as the following difference equation,

a(1)y(n)=b(1)x(n)+b(2)x(n1)+...+b(nb+1)x(nnb)a(2)y(n1)...a(na+1)y(nna).

Furthermore, you can represent the rational transfer function using its direct form II transposed implementation, as in the following diagram. Due to normalization, assume a(1) = 1.

The operation of filter at sample m is given by the time domain difference equations

y(m)=b(1)x(m)+z1(m1)z1(m)=b(2)x(m)+z2(m1)a(2)y(m)       =                 zn2(m)=b(n1)x(m)+zn1(m1)a(n1)y(m)zn1(m)=b(n)x(m)a(n)y(m).

Tips

  • If you have the Signal Processing Toolbox™, you can design a filter, d, using designfilt. Then, you can use Y = filter(d,X) to filter your data.

References

[1] Oppenheim, Alan V., Ronald W. Schafer, and John R. Buck. Discrete-Time Signal Processing. Upper Saddle River, NJ: Prentice-Hall, 1999.

Was this topic helpful?