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.

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

Type : LPF 24dB/Oct<br />

References : Posted by Christian[at]savioursofsoul[dot]de<br />

Code :<br />

First calculate the prewarped digital frequency:<br />

K = tan(Pi * Frequency / Samplerate);<br />

Now calc some intermediate variables: (see 'Factors of Polynoms' at<br />

http://en.wikipedia.<strong>org</strong>/wiki/Butterworth_filter, especially if you want a higher order like 48dB/Oct)<br />

a = 0.76536686473 * Q * K;<br />

b = 1.84775906502 * Q * K;<br />

K = K*K; (to optimize it a little bit)<br />

Calculate the first biquad:<br />

A0 = (K+a+1);<br />

A1 = 2*(1-K);<br />

A2 =(a-K-1);<br />

B0 = K;<br />

B1 = 2*B0;<br />

B2 = B0;<br />

Calculate the second biquad:<br />

A3 = (K+b+1);<br />

A4 = 2*(1-K);<br />

A5 = (b-K-1);<br />

B3 = K;<br />

B4 = 2*B3;<br />

B5 = B3;<br />

Then calculate the output as follows:<br />

Stage1 = B0*Input + State0;<br />

State0 = B1*Input + A1/A0*Stage1 + State1;<br />

State1 = B2*Input + A2/A0*Stage1;<br />

Output = B3*Stage1 + State2;<br />

State2 = B4*Stage1 + A4/A3*Output + State2;<br />

State3 = B5*Stage1 + A5/A3*Output;<br />

Comments<br />

from : Christian@savioursofsoul.de<br />

comment : Once you figured it out, it's even possible to do higher order butterworth shelving filters. Here's an example of an 8th order lowshelf.<br />

First we start as usual prewarping the cutoff frequency:<br />

K = tan(fW0*0.5);<br />

Then we settle up the Coefficient V:<br />

V = Power(GainFactor,-1/4)-1;<br />

Finally here's the loop to calculate the filter coefficients:<br />

for i = 0 to 3<br />

{<br />

cm = cos(PI*(i*2+1) / (2*8) );<br />

B[3*i+0] = 1/ ( 1 + 2*K*cm + K*K + 2*V*K*K + 2*V*K*cm + V*V*K*K);<br />

B[3*i+1] = 2 * ( 1 - K*K - 2*V*K*K - V*V*K*K);<br />

B[3*i+2] = (-1 + 2*K*cm - K*K - 2*V*K*K + 2*V*K*cm - V*V*K*K);<br />

A[3*i+0] = ( 1-2*K*cm+K*K);<br />

A[3*i+1] = 2*(-1 +K*K);<br />

A[3*i+2] = ( 1+2*K*cm+K*K);<br />

}<br />

from : scoofy@inf.elte.hu<br />

comment : Hmm... interesting. I guess the phase response/group delay gets quite funky, which is generally unwanted for an equalizer.<br />

I think the 1/ is not necessary for the first B coefficient! (of course you divide all the other coeffs with the inverse of that coeff at the end...)<br />

I guess the next will be Chebyshev shelving filters ;)<br />

BTW did you check whether my 4 pole highpass Butterworth <strong>code</strong> is correct?<br />

Peter<br />

from : Christian@savioursofsoul.de<br />

comment : The 1/ is of course an error here. It's left of my own implementation, where I divide directly. Also I think A and B is exchanged.

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

Saved successfully!

Ooh no, something went wrong!