Plotting the magnitude response of a biquad filter - Signal Processing Stack Exchange most recent 30 from dsp.stackexchange.com 2019-11-19T11:22:38Z https://dsp.stackexchange.com/feeds/question/3091 https://creativecommons.org/licenses/by-sa/4.0/rdf https://dsp.stackexchange.com/q/3091 7 Plotting the magnitude response of a biquad filter MVTC https://dsp.stackexchange.com/users/1255 2012-08-11T23:44:32Z 2016-05-28T07:51:30Z <p>I've put together a multi-band audio EQ using biquad filters. I'm getting the coefficients using the methods from <a href="http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt" rel="nofollow">the RBJ cookbook</a>.</p> <p>Now I want to plot the curve showing the magnitude response. I'm using an equation from <a href="http://rs-met.com/documents/dsp/BasicDigitalFilters.pdf" rel="nofollow">this source</a></p> <p>Here is my function to get the coefficients and get the magnitude response at points of interest.</p> <pre><code>void GetCoefficients (double samplerate = 44100.0) { //from the rbj biquad coefficient cookbook by Robert Bristow-Johnson long double SR = (long double)samplerate; long double A = powl(10.0L, dBGain/40.0L); long double W0 = 2.0L * PI * Center / SR; long double alpha = sinl(W0)*sinhl( LN2/2.0L * WidthInOctaves * W0/sinl(W0)); if (Type == "peaking") { b0 = 1.0L + alpha * A; b1 = -2.0L * cosl(W0); b2 = 1.0L - alpha * A; a0 = 1.0L + alpha / A; a1 = -2.0L * cosl(W0); a2 = 1.0L - alpha / A; } long double w; long double numerator; long double denominator; long double magnitude; for (int i = 0; i &lt; 59; ++ i) { w = 2.0L*PI*FreqPoints[i] / SR; numerator = b0*b0 + b1*b1 + b2*b2 + 2.0L*(b0*b1 + b1*b2)*cosl(w) + 2.0L*b0*b2*cosl(2.0L*w); denominator = 1.0L + a1*a1 + a2*a2 + 2.0L*(a1 + a1*a2)*cosl(w) + 2.0L*a2*cosl(2.0L*w); magnitude = sqrtl(numerator / denominator); FrequencyResponse[i] = magnitude; } } </code></pre> <p>My filters sound correct, but my plot seams way wrong. For example, when I have calculated the coefficients for a peaking filter with a width of \$2\$ octaves, centered at \$4398\textrm{ Hz}\$, with \$3\textrm{ dB}\$ gain; my magnitude response using those coefficients at \$349\textrm{ Hz}\$ is about \$12\$. </p> <p>I must be doing something wrong, but I can't quite figure it out. </p> https://dsp.stackexchange.com/questions/3091/-/3095#3095 4 Answer by MVTC for Plotting the magnitude response of a biquad filter MVTC https://dsp.stackexchange.com/users/1255 2012-08-12T19:13:24Z 2012-08-12T19:13:24Z <p>Someone over at the KRV forum pointed out that I need to normalize my coefficients so that a0 = 1.</p> <pre><code>b0 /= a0; b1 /= a0; b2 /= a0; a1 /= a0; a2 /= a0; a0 = 1; </code></pre> <p>Solved my problem. </p>