filter

One-dimensional digital filter: applies a difference equation to a signal.

Category: Language Fundamentals > Data Types

Syntax

y = filter(b, a, x)

Filters x with numerator b and denominator a.

[y, zf] = filter(b, a, x)

Also returns the final filter state.

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

Starts from the initial state zi.

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

Filters along dimension dim.

Description

The output follows a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... - a(2)*y(n-1) - .... With a = 1 this is an FIR filter such as a moving average; with other a values the filter is recursive (IIR) and the response to an impulse can last forever.

The filter is causal: y(n) uses only current and past inputs, which introduces a delay. Samples before the first one are taken as zero unless zi is given.

Input arguments

b
Vector of feedforward (numerator) coefficients.
a
Vector of feedback (denominator) coefficients. If a(1) is not 1, all coefficients are divided by it.
x
Input signal: a vector, or a matrix filtered column by column.
zi
Initial conditions (delays), of length max(length(a), length(b)) - 1.

Output arguments

y
Filtered signal, same size as x.
zf
Final delay values, usable as zi to continue filtering the next block of data.

Example

Moving average and recursive filter

filter([1 1 1]/3, 1, [3 6 9 12 15])
filter(1, [1 -0.5], [1 0 0 0])
Output
ans = 1×5

     1     3     6     9    12
ans = 1×4

    1.0000    0.5000    0.2500    0.1250

See also

conv movmean deconv cumsum