DSPRelated.com
Forums

What is wrong in this IIR filter code?

Started by Ole December 5, 2006
Hi,

Given are following coefficients to a second order butterworth highpass 
filter with a 3 dB cutoff freq of 4.48 Hz:
double a0 = 1.0;
double a1 = -1.999722319818149;
double a2 = 0.999722358365939;
double b0 = 0.999861169546022;
double b1 = -1.999722339092044;
double b2 = 0.999861169546022;

that is used in the following direct form IIR filter implementation:

double Yn, Yn1, Yn2, Xn, Xn1, Xn2; // initialized to 0 before run

double Filter(double Xn)
{
Yn = (1 / a0) * (b0 * Xn + b1 * Xn1 + b2 * Xn2 - a1 * Yn1 - a2 * Yn2);

Xn2 = Xn1;
Xn1 = Xn;
Yn2 = Yn1;
Yn1 = Yn;

return Yn;
}

But the filter jumps to infinity very fast no matter what it is feed with.

Anyone who can see what I did wrong?????

Thanks
Ole 


Sorry - the coefficients are actually:
double a0 = 1.0;
double a1 = -1,990048023748777;
double a2 = 0,990097300073064;
double b0 = 0,995036330955460;
double b1 = -1,990072661910921;
double b2 = 0,995036330955460;

But the problem is still the same - hope anyone will be able to give me a 
hint.

thanks
Ole 


.... and the sample frequency is 4000 Hz. 


"Ole" <ole@blabla.com> writes:

> Hi, > > Given are following coefficients to a second order butterworth highpass > filter with a 3 dB cutoff freq of 4.48 Hz: > double a0 = 1.0; > double a1 = -1.999722319818149; > double a2 = 0.999722358365939; > double b0 = 0.999861169546022; > double b1 = -1.999722339092044; > double b2 = 0.999861169546022; > > that is used in the following direct form IIR filter implementation: > > double Yn, Yn1, Yn2, Xn, Xn1, Xn2; // initialized to 0 before run > > double Filter(double Xn) > { > Yn = (1 / a0) * (b0 * Xn + b1 * Xn1 + b2 * Xn2 - a1 * Yn1 - a2 * Yn2); > > Xn2 = Xn1; > Xn1 = Xn; > Yn2 = Yn1; > Yn1 = Yn; > > return Yn; > } > > But the filter jumps to infinity very fast no matter what it is feed with. > > Anyone who can see what I did wrong????? > > Thanks > Ole
It looks like you may have some numerical issues - your poles are really, really close to the unit circle. Or you may have some bug in another part of your program, e.g., something may be stomping on your coefficients or data. I'd bet on the former, though. Also, your cutoff appears to be much lower than 4.48 Hz. Try moving your cutoff frequency to something sane, like 10 Hz or so. -- % Randy Yates % "Bird, on the wing, %% Fuquay-Varina, NC % goes floating by %%% 919-577-9882 % but there's a teardrop in his eye..." %%%% <yates@ieee.org> % 'One Summer Dream', *Face The Music*, ELO http://home.earthlink.net/~yatescr
Ole wrote:

> Sorry - the coefficients are actually: > double a0 = 1.0; > double a1 = -1,990048023748777; > double a2 = 0,990097300073064; > double b0 = 0,995036330955460; > double b1 = -1,990072661910921; > double b2 = 0,995036330955460;
Are the commas in place of decimal points in your actual code? If so, the semantics of the comma operator will give you big-time nonsense there. Martin -- Quidquid latine scriptum sit, altum viditur.
Ole skrev:
> Sorry - the coefficients are actually: > double a0 = 1.0; > double a1 = -1,990048023748777; > double a2 = 0,990097300073064; > double b0 = 0,995036330955460; > double b1 = -1,990072661910921; > double b2 = 0,995036330955460; > > But the problem is still the same - hope anyone will be able to give me a > hint.
Hi Ole. Your name indicates you are posting from Scandinavia? If so, be aware that comma is a local decimal separator that the compiler might not recognize. Use dot instead. Rune
Rune Allnor wrote:
> Ole skrev: >> Sorry - the coefficients are actually: >> double a0 = 1.0; >> double a1 = -1,990048023748777; >> double a2 = 0,990097300073064; >> double b0 = 0,995036330955460; >> double b1 = -1,990072661910921; >> double b2 = 0,995036330955460; >> >> But the problem is still the same - hope anyone will be able to give me a >> hint. > > Hi Ole. > > Your name indicates you are posting from Scandinavia? > If so, be aware that comma is a local decimal separator > that the compiler might not recognize. Use dot instead. > > Rune >
One would think the compiler would complain.
John Sampson skrev:
> Rune Allnor wrote: > > Ole skrev: > >> Sorry - the coefficients are actually: > >> double a0 = 1.0; > >> double a1 = -1,990048023748777; > >> double a2 = 0,990097300073064; > >> double b0 = 0,995036330955460; > >> double b1 = -1,990072661910921; > >> double b2 = 0,995036330955460; > >> > >> But the problem is still the same - hope anyone will be able to give me a > >> hint. > > > > Hi Ole. > > > > Your name indicates you are posting from Scandinavia? > > If so, be aware that comma is a local decimal separator > > that the compiler might not recognize. Use dot instead. > > > > Rune > > > > One would think the compiler would complain.
Maybe, but I doubt gcc would. Rune
"Rune Allnor" <allnor@tele.ntnu.no> wrote in message 
news:1165341730.208747.107760@n67g2000cwd.googlegroups.com...
> > John Sampson skrev: >> Rune Allnor wrote: >> > Ole skrev: >> >> Sorry - the coefficients are actually: >> >> double a0 = 1.0; >> >> double a1 = -1,990048023748777; >> >> double a2 = 0,990097300073064; >> >> double b0 = 0,995036330955460; >> >> double b1 = -1,990072661910921; >> >> double b2 = 0,995036330955460; >> >> >> >> But the problem is still the same - hope anyone will be able to give >> >> me a >> >> hint. >> > >> > Hi Ole. >> > >> > Your name indicates you are posting from Scandinavia? >> > If so, be aware that comma is a local decimal separator >> > that the compiler might not recognize. Use dot instead. >> > >> > Rune >> > >> >> One would think the compiler would complain.
Yep - the compiler did complain and I changed it, but the problem stilll existed. I have however located the error now - it was somewhere else in my code :-( Hope taht anyone else will be able to use the example and sorry for wasting your time. Thanks Ole
On Tue, 05 Dec 2006 13:34:01 GMT, Randy Yates <yates@ieee.org> wrote:

  (snipped)
> >It looks like you may have some numerical issues - your poles are >really, really close to the unit circle. Or you may have some bug in >another part of your program, e.g., something may be stomping on your >coefficients or data. I'd bet on the former, though. > >Also, your cutoff appears to be much lower than 4.48 Hz. Try moving >your cutoff frequency to something sane, like 10 Hz or so.
Hi Randy, Ole's tryin' to build a filter whose cutoff freq is roughly Fs/1000 !!! Isn't attempting to build such a "high-Q" filter using a simple 2nd-order filter just askin' for trouble? That filter doesn't seem to be realistic. Maybe Ole should perform some lowpass filtering and decimation. See Ya, [-Rick-]