# Initialize filter so that filtered output begins with initial value of the input

127 views (last 30 days)

Show older comments

##### 1 Comment

Christoph F.
on 25 Apr 2017

y(n) = y(n-1)*(1-a) + a*x(n)

And you want x(1) = y(1) so:

x(1) = y(0)*(1-a) + a*x(1)

(1-a)*x(1) = (1-a)*y(0)

y(0) = x(1)

You need to set the initial condition y(0) of your filter to x(1). Note that the exact values of the initial conditions depend on the filter structure, so if you're using something other than direct form 1, the calculation of the initial values for the delay line is somewhat different.

Example, step response to x=1, with transition

a = 0.25; y=0; x=1; for ii=1:40; y = y*(1-a) + a*x; yout(ii)=y; end;plot( yout);

Example with y(0) set to x(1):

a = 0.25; y=1; x=1; for ii=1:40; y = y*(1-a) + a*x; yout(ii)=y; end;plot( yout);

I don't think using filtfilt is the answer, since it effectively doubles the filter order and changes the filters amplitude response significantly. Also, it is not possible to use forward-backward-filtering if real-time output is required.

### Answers (3)

Star Strider
on 12 Mar 2017

I always use the filtfilt (link) function for filtering because it avoids this problem and also has a maximally flat phase response, so that all filters, regardless of design, have the phase response that a hardware Bessel filter would have.

If you want to use the filter (link) function, see the section of the documentation on Filter Data in Sections (link) for details on setting the initial conditions.

##### 0 Comments

Christoph F.
on 25 Apr 2017

Oops. That was supposed to be an answer, not a comment. So I'm posting it again as an answer.

y(n) = y(n-1)*(1-a) + a*x(n)

And you want x(1) = y(1) so:

x(1) = y(0)*(1-a) + a*x(1)

(1-a)*x(1) = (1-a)*y(0)

y(0) = x(1)

You need to set the initial condition y(0) of your filter to x(1). Note that the exact values of the initial conditions depend on the filter structure, so if you're using something other than direct form 1, the calculation of the initial values for the delay line is somewhat different.

Example, step response to x=1, with transition

a = 0.25; y=0; x=1; for ii=1:40; y = y*(1-a) + a*x; yout(ii)=y; end;plot( yout);

Example with y(0) set to x(1):

a = 0.25; y=1; x=1; for ii=1:40; y = y*(1-a) + a*x; yout(ii)=y; end;plot( yout);

I don't think using filtfilt is the answer, since it effectively doubles the filter order and changes the filters amplitude response significantly. Also, it is not possible to use forward-backward-filtering if real-time output is required.

##### 0 Comments

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!