{"id":88,"date":"2011-01-02T14:39:10","date_gmt":"2011-01-02T22:39:10","guid":{"rendered":"http:\/\/www.earlevel.com\/main\/?p=88"},"modified":"2012-11-26T00:22:20","modified_gmt":"2012-11-26T08:22:20","slug":"biquad-formulas","status":"publish","type":"post","link":"https:\/\/www.earlevel.com\/main\/2011\/01\/02\/biquad-formulas\/","title":{"rendered":"Biquad formulas"},"content":{"rendered":"<p>For fixed filters, we can plug biquad coefficients into our programs. But often, we need to calculate them on the fly, to user settings or changes in sample rate. As a companion to the biquad <a href=\"2010\/12\/20\/biquad-calculator\">calculator<\/a>, here are the formulas used, in JavaScript; refer to our article on <a href=\"2003\/02\/28\/biquads\">biquads<\/a> for implementation diagrams:<\/p>\n<pre>\r\nfunction calcBiquad(type, Fc, Fs, Q, peakGain) {\r\n    var a0,a1,a2,b1,b2,norm;\r\n    \r\n    var V = Math.pow(10, Math.abs(peakGain) \/ 20);\r\n    var K = Math.tan(Math.PI * Fc \/ Fs);\r\n    switch (type) {\r\n        case \"lowpass\":\r\n            norm = 1 \/ (1 + K \/ Q + K * K);\r\n            a0 = K * K * norm;\r\n            a1 = 2 * a0;\r\n            a2 = a0;\r\n            b1 = 2 * (K * K - 1) * norm;\r\n            b2 = (1 - K \/ Q + K * K) * norm;\r\n            break;\r\n        \r\n        case \"highpass\":\r\n            norm = 1 \/ (1 + K \/ Q + K * K);\r\n            a0 = 1 * norm;\r\n            a1 = -2 * a0;\r\n            a2 = a0;\r\n            b1 = 2 * (K * K - 1) * norm;\r\n            b2 = (1 - K \/ Q + K * K) * norm;\r\n            break;\r\n        \r\n        case \"bandpass\":\r\n            norm = 1 \/ (1 + K \/ Q + K * K);\r\n            a0 = K \/ Q * norm;\r\n            a1 = 0;\r\n            a2 = -a0;\r\n            b1 = 2 * (K * K - 1) * norm;\r\n            b2 = (1 - K \/ Q + K * K) * norm;\r\n            break;\r\n        \r\n        case \"notch\":\r\n            norm = 1 \/ (1 + K \/ Q + K * K);\r\n            a0 = (1 + K * K) * norm;\r\n            a1 = 2 * (K * K - 1) * norm;\r\n            a2 = a0;\r\n            b1 = a1;\r\n            b2 = (1 - K \/ Q + K * K) * norm;\r\n            break;\r\n        \r\n        case \"peak\":\r\n            if (peakGain >= 0) {    \/\/ boost\r\n                norm = 1 \/ (1 + 1\/Q * K + K * K);\r\n                a0 = (1 + V\/Q * K + K * K) * norm;\r\n                a1 = 2 * (K * K - 1) * norm;\r\n                a2 = (1 - V\/Q * K + K * K) * norm;\r\n                b1 = a1;\r\n                b2 = (1 - 1\/Q * K + K * K) * norm;\r\n            }\r\n            else {    \/\/ cut\r\n                norm = 1 \/ (1 + V\/Q * K + K * K);\r\n                a0 = (1 + 1\/Q * K + K * K) * norm;\r\n                a1 = 2 * (K * K - 1) * norm;\r\n                a2 = (1 - 1\/Q * K + K * K) * norm;\r\n                b1 = a1;\r\n                b2 = (1 - V\/Q * K + K * K) * norm;\r\n            }\r\n            break;\r\n        case \"lowShelf\":\r\n            if (peakGain >= 0) {    \/\/ boost\r\n                norm = 1 \/ (1 + Math.SQRT2 * K + K * K);\r\n                a0 = (1 + Math.sqrt(2*V) * K + V * K * K) * norm;\r\n                a1 = 2 * (V * K * K - 1) * norm;\r\n                a2 = (1 - Math.sqrt(2*V) * K + V * K * K) * norm;\r\n                b1 = 2 * (K * K - 1) * norm;\r\n                b2 = (1 - Math.SQRT2 * K + K * K) * norm;\r\n            }\r\n            else {    \/\/ cut\r\n                norm = 1 \/ (1 + Math.sqrt(2*V) * K + V * K * K);\r\n                a0 = (1 + Math.SQRT2 * K + K * K) * norm;\r\n                a1 = 2 * (K * K - 1) * norm;\r\n                a2 = (1 - Math.SQRT2 * K + K * K) * norm;\r\n                b1 = 2 * (V * K * K - 1) * norm;\r\n                b2 = (1 - Math.sqrt(2*V) * K + V * K * K) * norm;\r\n            }\r\n            break;\r\n        case \"highShelf\":\r\n            if (peakGain >= 0) {    \/\/ boost\r\n                norm = 1 \/ (1 + Math.SQRT2 * K + K * K);\r\n                a0 = (V + Math.sqrt(2*V) * K + K * K) * norm;\r\n                a1 = 2 * (K * K - V) * norm;\r\n                a2 = (V - Math.sqrt(2*V) * K + K * K) * norm;\r\n                b1 = 2 * (K * K - 1) * norm;\r\n                b2 = (1 - Math.SQRT2 * K + K * K) * norm;\r\n            }\r\n            else {    \/\/ cut\r\n                norm = 1 \/ (V + Math.sqrt(2*V) * K + K * K);\r\n                a0 = (1 + Math.SQRT2 * K + K * K) * norm;\r\n                a1 = 2 * (K * K - 1) * norm;\r\n                a2 = (1 - Math.SQRT2 * K + K * K) * norm;\r\n                b1 = 2 * (K * K - V) * norm;\r\n                b2 = (V - Math.sqrt(2*V) * K + K * K) * norm;\r\n            }\r\n            break;\r\n    }\r\n\r\n    return [ a0, a1, a2, b1, b2 ];\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For fixed filters, we can plug biquad coefficients into our programs. But often, we need to calculate them on the fly, to user settings or changes in sample rate. As a companion to the biquad calculator, here are the formulas &hellip; <a href=\"https:\/\/www.earlevel.com\/main\/2011\/01\/02\/biquad-formulas\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,4,8,9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/88"}],"collection":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":1,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/88\/revisions\/98"}],"wp:attachment":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}