DSPRelated.com
Forums

My BiQuad is Unstable

Started by Robert A. May 8, 2006
Hi guys,

I am trying to implement the BiQuad from Robert's paper:

http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt

I did it like this (I only did the low pass version):

struct BiQuad
{
 BiQuad();

 double process(double in);
 void set(double frequency,double q);

 double a1,a2;
 double b0,b1,b2;

 double x1,x2;
 double y1,y2;
};

BiQuad::BiQuad()
{
  // initialize delay

 x1=0.0;
 x2=0.0;
 y1=0.0;
 y2=0.0;
}

void BiQuad::set(double frequency,double q)
{
 double w=TwoPi*frequency/(double)sample_rate;
 double alpha=sin(w)/(2.0*q);

 // lowpass version

 double a0=1.0+alpha;

 b0=(1.0-cos(w))/2.0;
 b1=1.0-cos(w);
 b2=(1.0-cos(w))/2.0;

 a1=-2.0*cos(w);
 a2=1.0-alpha;

 // to avoid some division in process()

 b0/=a0;
 b1/=a0;
 b2/=a0;

 a1/=a0;
 a2/=a0;
}

double BiQuad::process(double in)
{
 double out=b0*in+b1*x1+b2*x2-a1*y1-a2*y2;

 x2=x1;
 x1=in;

 y2=y1;
 y1=out;

 return out;
}

...does anyone see anything wrong with it ? I tried q=0.5, q=5.0, q=10.0 but 
it always breaks when I put in high frequencies (like white noise).

Will it always output in the range [-1.0,1.0] if the input is in the same 
range ?

I'm still learning about this stuff and I wanted to implement something 
solid to keep me occupied when I'm not studying it.

Thank you.


it looks okay to me.  i wonder where sample_rate gets defined.

(cos(w) doesn't need to be computed 4 times.  once is enough.)

r b-j

Hi Robert,

Thanks for looking it over. I am sure sample_rate is defined properly 
beforehand ( #define sample_rate 44100 ), and I just left all the cos(w) 
calls for the post here.

Do you think it could be caused by under-normalisation ? I've been meaning 
to read up on that.

I even clamped the input to [-1.0,1.0] just to be sure and it still gave me 
random pops and hisses. I'm writing the output directly to a .wav file, I'm 
going to test the BiQuad in a VST softsynth now and see what happens. I 
wonder what could be wrong.

Thanks again,
Robert A.


"robert bristow-johnson" <rbj@audioimagination.com> wrote in message 
news:1147104858.350392.273950@i40g2000cwc.googlegroups.com...
> it looks okay to me. i wonder where sample_rate gets defined. > > (cos(w) doesn't need to be computed 4 times. once is enough.) > > r b-j >
Hi,

I did some tests and here is what I've found:

input: white noise in the range [-1.0,1.0]
output: works fine and is in the range [-1.0,1.0]

input: chaotic series in the range [-1.0,1.0]
output: a disaster in the range (about) [-6.5,6.5]

I really don't understand why that happens because the chaotic series is 
actually smoother than pure white noise. The chaotic series breaks another 
BiQuad as well.

I am positive the chaotic series is in the range [-1.0,1.0].

What the hell is going on ?

Thanks,
Robert A.


Robert A. schrieb:

> Hi, > > I did some tests and here is what I've found: > > input: white noise in the range [-1.0,1.0] > output: works fine and is in the range [-1.0,1.0] > > input: chaotic series in the range [-1.0,1.0] > output: a disaster in the range (about) [-6.5,6.5]
Sounds interesting. Can you post the input and the output as text files somewhere?
Andor wrote:
> Robert A. schrieb: > > I did some tests and here is what I've found: > > > > input: white noise in the range [-1.0,1.0] > > output: works fine and is in the range [-1.0,1.0] > > > > input: chaotic series in the range [-1.0,1.0] > > output: a disaster in the range (about) [-6.5,6.5] > > Sounds interesting. Can you post the input and the output as text files > somewhere?
also, will you post an example corner frequency, q, and the resulting alpha and the 5 coefs? we can look at those to make sure they are stable r b-j
Robert A. wrote:
> Hi, > > I did some tests and here is what I've found: > > input: white noise in the range [-1.0,1.0] > output: works fine and is in the range [-1.0,1.0] > > input: chaotic series in the range [-1.0,1.0] > output: a disaster in the range (about) [-6.5,6.5] > > I really don't understand why that happens because the chaotic series is > actually smoother than pure white noise. The chaotic series breaks another > BiQuad as well.
this'll teach me to read the whole thing next time. it sounds to me that your chaotic series is possibly some judiciously computed sequence so that, when filtered, it comes out with all frequency components in phase. do you have code to generate it? can we see it? r b-j
robert bristow-johnson wrote:
> Robert A. wrote: >> Hi, >> >> I did some tests and here is what I've found: >> >> input: white noise in the range [-1.0,1.0] >> output: works fine and is in the range [-1.0,1.0] >> >> input: chaotic series in the range [-1.0,1.0] >> output: a disaster in the range (about) [-6.5,6.5] >> >> I really don't understand why that happens because the chaotic series is >> actually smoother than pure white noise. The chaotic series breaks another >> BiQuad as well. > > this'll teach me to read the whole thing next time. it sounds to me > that your chaotic series is possibly some judiciously computed sequence > so that, when filtered, it comes out with all frequency components in > phase. > > do you have code to generate it? can we see it? > > r b-j >
From dictionary.com, chaos means: 1. A condition or place of great disorder or confusion. 2. A disorderly mass; a jumble: The desk was a chaos of papers and unopened letters. 3. often Chaos The disordered state of unformed matter and infinite space supposed in some cosmogonic views to have existed before the ordered universe. 4. Mathematics. A dynamical system that has a sensitive dependence on its initial conditions. 5. Obsolete. An abyss; a chasm. Isn't that wonderful. Definition 3 means white noise, while definition 4 means highly structured to cause grief. Almost completely opposite meanings. :-) Steve
Robert A. wrote:

> I even clamped the input to [-1.0,1.0] just to be sure and it still gave > me random pops and hisses. I'm writing the output directly to a .wav file, > I'm going to test the BiQuad in a VST softsynth now and see what happens. > I wonder what could be wrong. >
Hi Robert, this sounds like calculation errors. Besides the coefficients, which might be wrong, and which have been mentioned already, you should take care of the calculation precision. Double suggests to be highly precise, though it often isn't precise enough. If you have a 32bit double, this might mean that your values are stored with 23bit mantissa, 7bit exponent and 2bit sign. To get a feeling for this, calculate your filter manually with values of three decimals (instead of 0.3535... use 0.353 or 0.354 etc.). Since your biquad stores intermediate results and reuses it in every consequent calculation, these resolution insufficience will considerably limit the frequency range where your biquad will be usable. As an example: your biquad will probably fail if you run it with 44.1kS/s and try to implement a filter at 0.1Hz. To find out if your design is sensible to such errors, just start at a secure value like 10kHz and then decrease the value until your hisses and pops occur. If this happens, you have probably detected the source of your problem. Another issue is this: if you switch the filter's cutoff frequency during processing of input samples, the stored values don't fit to the new filter setting. The filter will create distortions (like the transient part of a differential equation they fade away while the filter settles). Therefore it's quite natural if the pops and hisses occur after switching the filter frequency. However, they should disappear after a while if your filter is stable. If you are able to store the output values over some time (e.g. a couple of seconds), you might want to have a look to the output where the distortions occur. I would expect that you see a sort of limitation effect. Then store the intermediate values x1,x2,y1,y2 and compare them. You would certainly find locations where rounding/clipping happens. These are probably causing your distortions. If you are able to locate these, manual calculation would tell you what to do to overcome the problem. Bernhard
"Steve Underwood" <steveu@dis.org> wrote in message 
news:e3othm$1f8$1@home.itg.ti.com...
> From dictionary.com, chaos means: > > 1. A condition or place of great disorder or confusion. > 2. A disorderly mass; a jumble: The desk was a chaos of papers and > unopened letters. > 3. often Chaos The disordered state of unformed matter and infinite > space supposed in some cosmogonic views to have existed before the ordered > universe. > 4. Mathematics. A dynamical system that has a sensitive dependence on > its initial conditions. > 5. Obsolete. An abyss; a chasm. > > Isn't that wonderful. Definition 3 means white noise, while definition 4 > means highly structured to cause grief. Almost completely opposite > meanings. :-) > > Steve
Hello Steve, You forgot Kaos who was the arch enemy of Control on the old TV series "Get Smart." Clay