17.01.2013 Views

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Notch filter (click this to go back to the index)<br />

Type : 2 poles 2 zeros IIR<br />

References : Posted by Olli Niemitalo<br />

Notes :<br />

Creates a muted spot in the spectrum with adjustable steepness. A complex conjugate pair of zeros on the z- plane unit circle and neutralizing poles<br />

approaching at the same angles from inside the unit circle.<br />

Code :<br />

Parameters:<br />

0 =< freq =< samplerate/2<br />

0 =< q < 1 (The higher, the narrower)<br />

AlgoAlgo=double pi = 3.141592654;<br />

double sqrt2 = sqrt(2.0);<br />

double freq = 2050; // Change! (zero & pole angle)<br />

double q = 0.4; // Change! (pole magnitude)<br />

double z1x = cos(2*pi*freq/samplerate);<br />

double a0a2 = (1-q)*(1-q)/(2*(fabs(z1x)+1)) + q;<br />

double a1 = -2*z1x*a0a2;<br />

double b1 = -2*z1x*q;<br />

double b2 = q*q;<br />

double reg0, reg1, reg2;<br />

unsigned int streamofs;<br />

reg1 = 0;<br />

reg2 = 0;<br />

/* Main loop */<br />

for (streamofs = 0; streamofs < streamsize; streamofs++)<br />

{<br />

reg0 = a0a2 * ((double)fromstream[streamofs]<br />

+ fromstream[streamofs+2])<br />

+ a1 * fromstream[streamofs+1]<br />

- b1 * reg1<br />

- b2 * reg2;<br />

}<br />

reg2 = reg1;<br />

reg1 = reg0;<br />

int temp = reg0;<br />

/* Check clipping */<br />

if (temp > 32767) {<br />

temp = 32767;<br />

} else if (temp < -32768) temp = -32768;<br />

/* Store new value */<br />

tostream[streamofs] = temp;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!