DSPRelated.com
Forums

FIR filter with initial conditions

Started by orrsarah March 31, 2015
Hey 

I am trying to write my own FIR filter in MATLAB for a project I am working
on with the aim of eventually porting the project over to c (so I cannot
use MATLAB built-in functions). The filter is taking smaller chunks of a
large chunk of data and filtering them separately so I need to be able to
set the initial conditions. Has anyone any idea how MATLAB uses the initial
conditions, zi, input parameter in its built in filter.m function and how I
might go about setting initial conditions?

Do I take my initial conditions from previously filtered points?


	 

_____________________________		
Posted through www.DSPRelated.com
On Tuesday, March 31, 2015 at 7:58:18 AM UTC-7, orrsarah wrote:
> Hey > > I am trying to write my own FIR filter in MATLAB for a project I am working > on with the aim of eventually porting the project over to c (so I cannot > use MATLAB built-in functions). The filter is taking smaller chunks of a > large chunk of data and filtering them separately so I need to be able to > set the initial conditions. Has anyone any idea how MATLAB uses the initial > conditions, zi, input parameter in its built in filter.m function and how I > might go about setting initial conditions?
In MATLAB the first filter call can use "[]" for zi and each succeeding call can use the zf from the previous call for the subsequent zi. Unless required as a homework problem there is no reason to duplicate MATLAB function call parameters. Your target architecture is unlikely to perform the exact MATLAB organization as efficiently as available libraries or implementation of a generic filter code. You target should have its own data handling characteristics you can use more efficiently.
> > Do I take my initial conditions from previously filtered points? >
In your own implementation you don't need to explicitly pass "initial conditions" just because MATLAB does. You might usefully think of what you have as "internal state" of a block operating process. That state data could be samples from the last block or partial sums calculated from samples in the last block, whichever is more convenient in the target implementation. The initial values would be zeros. Dale B. Dalrymple
Thanks for your response. The reason I am duplicating the built-in filter
code in MATLAB is that I am building my own wavelet transform for
eventually writing in embedded c.The initial conditions will minimize any
calibration time the filter will need before the signal levels off. Running
my code without initial conditions *should* be fine with an allowed number
of samples at the start while the system calibrates, I just wanted to try
and exactly replicate the response of the filter.m code (with initial
conditions set) for comparissons sake.

Thanks again

Sarah Orr
---------------------------------------
Posted through http://www.DSPRelated.com
would this work:

x1 = your signal
x2 = [initial values x1];
y = filter(h,1,x2);

Kaz


---------------------------------------
Posted through http://www.DSPRelated.com
>would this work: > >x1 = your signal >x2 = [initial values x1]; >y = filter(h,1,x2); > >Kaz > > >--------------------------------------- >Posted through http://www.DSPRelated.com
Actually thats funny cos that's exactly what I ended up doing :) I called 'x2' z and used it as the delay line. great minds think alike! --------------------------------------- Posted through http://www.DSPRelated.com
Hi,

A FIR filter includes a delay line, which remembers the state of the
filter. So you need to include it in an implementation, yes. However,
I'd suggest you look at FIR filters in general, not Matlab specifically.
It should be quite obvious what it does.

As an example, take a FIR impulse response [10 20 30 40 50].
I feed in a single sample "1" and get a single output sample "10".
However, feeding future data into the same filter would still output
[20, 30, 40 50] from the first sample. Matlab's "state" output reflects
that.

for example, with 
x = [1]
h = [10 20 30 40 50]
a = 1 (dummy denominator as it's FIR, not IIR)

[y, sf] = filter(h, a, x)
gives
y = 10 
and 
sf = [20 30 40 50].'

I can feed sf into future calls to the filter, where it provides the
missing output samples. 
[y, sf] = filter(h, a, x /*which is now 0 /*, sf)
gives y=20 and updated sf
[y, sf] = filter(h, a, x /*which is now 0 /*, sf)
gives y=30 and updated sf
[y, sf] = filter(h, a, x /*which is now 0 /*, sf)
gives y=40 and updated sf
[y, sf] = filter(h, a, x /*which is now 0 /*, sf)
gives y=50 and updated sf, which finally all zeros.


---------------------------------------
Posted through http://www.DSPRelated.com
typo / browser fail in last post... 
Final lines should read

gives y=20 and updated sf
gives y=30 and updated sf
gives y=40 and updated sf
gives y=50 and updated sf
---------------------------------------
Posted through http://www.DSPRelated.com
typo / browser fail in last post... 

laughing out loud. I give up.

---------------------------------------
Posted through http://www.DSPRelated.com
>typo / browser fail in last post... > >laughing out loud. I give up. > >--------------------------------------- >Posted through http://www.DSPRelated.com
Hi, Stephane here publisher of DSPRelated. There was a character encoding issue that I just solved. Your message looks good now. --------------------------------------- Posted through http://www.DSPRelated.com
Thanks! So it was a display issue, not the post itself.
---------------------------------------
Posted through http://www.DSPRelated.com