DSPRelated.com
Forums

IIR FILTER application - A Total Wreck

Started by murali_hd October 9, 2007
Greetings People,

                    As fate would would have it am working on a project
involving IIR filters and am stuck.I would therefore request you to give
me your opininion on the following issue.
 
                          The project involves coding in C .Herein the
discrete data is read from the sound file(.WAV FORMAT) first and then the
formulae for the IIR filter which happens to be
 
******************************************************************************
 
y[n] = (sigma(i=0 to P) *  bi x[n-i] ) -   (sigma  (i=1 to Q)  * 
                                                             ai y[n-i] )
 
********************************************************************************
 
                  The source for this formulae happens to be
 
http://en.wikipedia.org/wiki/Infinite_impulse_response
 
                         Me and the DSP  team have checked the
coefficients and made sure that the right ones are used.Its basically a
fourth order filter.Now the problem I am facing is that after a certain
count of data read from the .WAV file, 67 to be precise,the filtered o/p 
values obtained from  the formulae becomes greater than the original raw
signal and after a while starts to go awry and bizzare.I have also made
sure that the numbers that has been read using file operations in C
language have been precise(verified it with the matlab results).
 
                         Are these weird results happening because I have
applied the wrong formulae or is it that the feedback added to the output
is playing the devil.I am totally at loss here folks,as the calculations
involved and the previous numbers required to be substituted in the
formulae,ie y[n] are all also flawless.
 
                        Henceforth I would humbly request you to shed some
light on the above problem and put me out of my misery.
 
                            Regards,
 
                                                A Murali Krishnan.
 
                         
      


Maybe it's the minus sign in the denominator, there are two different
conventions in use:

http://www.elisanet.fi/mnentwig/webroot/freqresp_z_sign/index.html
Chances are 50 % that your filter will go instable...

On the same site I've got a bunch of Matlab / Octave examples that load
and write wave files. You could take one of them, and use the filter()
function to check that it works.

-mn
murali_hd wrote:
> Greetings People, > > As fate would would have it am working on a project > involving IIR filters and am stuck.I would therefore request you to give > me your opininion on the following issue. > > The project involves coding in C .Herein the > discrete data is read from the sound file(.WAV FORMAT) first and then the > formulae for the IIR filter which happens to be > > ****************************************************************************** > > y[n] = (sigma(i=0 to P) * bi x[n-i] ) - (sigma (i=1 to Q) * > ai y[n-i] ) > > ******************************************************************************** > > The source for this formulae happens to be > > http://en.wikipedia.org/wiki/Infinite_impulse_response > > Me and the DSP team have checked the > coefficients and made sure that the right ones are used.Its basically a > fourth order filter.Now the problem I am facing is that after a certain > count of data read from the .WAV file, 67 to be precise,the filtered o/p > values obtained from the formulae becomes greater than the original raw > signal and after a while starts to go awry and bizzare.I have also made > sure that the numbers that has been read using file operations in C > language have been precise(verified it with the matlab results). > > Are these weird results happening because I have > applied the wrong formulae or is it that the feedback added to the output > is playing the devil.I am totally at loss here folks,as the calculations > involved and the previous numbers required to be substituted in the > formulae,ie y[n] are all also flawless. > > Henceforth I would humbly request you to shed some > light on the above problem and put me out of my misery. > > Regards, > > A Murali Krishnan. > > > > >
If you are getting correct result in Matlab, but your application code 'blows up', my first guess would be that you have implemented the equation incorrectly. A couple of things I would recommend: 1 - check the (-) sign on the coeffecients in your equation. This is a common mistake. A quick check, swap the minus term on your feed back coeffecients (there will be two of them per pole pair assuming your coeffecients were normalized). 2 - reduce the scale of what you are trying to accomplish in your first attempt. For example, instead or jumping right into a fourth order filter, start with a frist, then get a second order working, etc. Another possibility is that you are running into numerical overflow. You didn't indicate what type of processor you were trying to implement this on, but if you are using a fixed point processor you need to pay particular attention to this. If you are using a fixed point system you also need to pay particular attention to how you are implementing the difference equation; specifically if you are processing the poles first, this has a tendency to make the intermediate products very large and causes problems - choose carefully. Also consider breaking the equation up into either parallel or cascaded second order sections. You may also need to look at how you are scaling the individual sections and how you handle the overall 'gain' term.
murali_hd wrote:
> Greetings People, > > As fate would would have it am working on a project > involving IIR filters and am stuck.I would therefore request you to give > me your opininion on the following issue. > > The project involves coding in C .Herein the > discrete data is read from the sound file(.WAV FORMAT) first and then the > formulae for the IIR filter which happens to be > > ****************************************************************************** > > y[n] = (sigma(i=0 to P) * bi x[n-i] ) - (sigma (i=1 to Q) * > ai y[n-i] ) > > ******************************************************************************** > > The source for this formulae happens to be > > http://en.wikipedia.org/wiki/Infinite_impulse_response > > Me and the DSP team have checked the > coefficients and made sure that the right ones are used.Its basically a > fourth order filter.Now the problem I am facing is that after a certain > count of data read from the .WAV file, 67 to be precise,the filtered o/p > values obtained from the formulae becomes greater than the original raw > signal and after a while starts to go awry and bizzare.I have also made > sure that the numbers that has been read using file operations in C > language have been precise(verified it with the matlab results). > > Are these weird results happening because I have > applied the wrong formulae or is it that the feedback added to the output > is playing the devil.I am totally at loss here folks,as the calculations > involved and the previous numbers required to be substituted in the > formulae,ie y[n] are all also flawless. > > Henceforth I would humbly request you to shed some > light on the above problem and put me out of my misery. >
We're all taking stabs in the dark, since it's not clear exactly what you're doing or what your level of knowledge is. Here's mine... First, take a look at http://www.wescottdesign.com/articles/zTransform/z-transforms.html. It has a recipe and comments on implementing IIR filters, although it's a bit terse. Second, you should never implement a 4th-order filter in one stage. IIR filters are sensitive to rounding in their coefficients and their data, and this sensitivity gets all the worse as their order is increased. Unless you _really_ know what you're doing _and_ you're working on a very odd problem it is wise to break your filter up into sets of 2nd- and 1st-order filters, and cascade them. This will make the numerical problems significantly milder. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" gives you just what it says. See details at http://www.wescottdesign.com/actfes/actfes.html