what is exactly the content of 'zf' in MATLAB function filter

filtering, matlab, signal-processing

Solution

In case we have a large set of data or we are short in memory the `zf` and `zi` options will come in handy.

For example we can divide our data in two parts, `x` and `newx`, and use the `filter` function like,

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

For a filter with `a` and `b` as in,

we will be referring back to `length(a) -1` samples of `y` and `length(b) -1` samples of `x`.

So for continuing our filter over the second half we will need `max(length(a),length(b)) -1` calls from the first half.

Example 1

y[n] = x[n] + 2 * x[n-1] + 3 * x[n-2];

which is,

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

example input and output are,

x = [1     2     3     4     5     6     7     8     9];
y = [1     4    10    16    22    28    34    40    46];
zf = [42  27]';

Implementing the filter over `newx`, for first two samples we have,

newy[1] = newx[1] + 2*9 + 3*8 = newx[1] + 42 = newx[1] + zf[1];
newy[2] = newx[2] + 2 * newx[1] + 3*9 = newx[2] + 2 * newx[1] + zf[2];

Example 2

x = 1 : 9;
b = [1 1 1];
a = [1 2];
[y,zf] = filter(b,a,x);

This corresponds to `y[n] = x[n] + x[n-1] + x[n-2] - 2*y[n-1]`.

The inputs and outputs are:

 x = [1     2     3     4     5     6     7     8     9];
 y = [1     1     4     1    10    -5    28   -35    94];
 zf = [-171 9]';

Now for the first value of second half:

newy[1] = newx[1] + 9 + 8 - 2 * 94 = newx[1] - 171 = newx[1] + zf(1);
newy[2] = newx[2] + newx[1] + 9 - 2*newy[1] = newx[2] + newx[1] + zf(2) - 2*newy[1];

So I think it's pretty obvious now, how `zf` works.

Problem

I know that in order to filter large amount of data in chuncks, it it possible to use the function 'filter' with the appropriate filter coefficients, and pass the final conditions 'zf' to the next chunk as its initial conditions 'zi'. I am confused. what is exactly the content of 'zf'? does it hold the last relevant input samples? (in pure FIR filter) the last relevant ouput samples? (in IIR) what does it hold when both last inputs and last outputs are relevant? thanks a lot

Original source