|
| 1 | +// |
| 2 | +// Biquad.cpp |
| 3 | +// |
| 4 | +// Created by Nigel Redmon on 11/24/12 |
| 5 | +// EarLevel Engineering: earlevel.com |
| 6 | +// Copyright 2012 Nigel Redmon |
| 7 | +// |
| 8 | +// For a complete explanation of the Biquad code: |
| 9 | +// http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/ |
| 10 | +// |
| 11 | +// License: |
| 12 | +// |
| 13 | +// This source code is provided as is, without warranty. |
| 14 | +// You may copy and distribute verbatim copies of this document. |
| 15 | +// You may modify and use this source code to create binary code |
| 16 | +// for your own purposes, free or commercial. |
| 17 | +// |
| 18 | + |
| 19 | +#include <math.h> |
| 20 | +#include "Biquad.h" |
| 21 | + |
| 22 | +Biquad::Biquad() { |
| 23 | + type = bq_type_lowpass; |
| 24 | + a0 = 1.0; |
| 25 | + a1 = a2 = b1 = b2 = 0.0; |
| 26 | + Fc = 0.50; |
| 27 | + Q = 0.707; |
| 28 | + peakGain = 0.0; |
| 29 | + z1 = z2 = 0.0; |
| 30 | +} |
| 31 | + |
| 32 | +Biquad::Biquad(int type, double Fc, double Q, double peakGainDB) { |
| 33 | + setBiquad(type, Fc, Q, peakGainDB); |
| 34 | + z1 = z2 = 0.0; |
| 35 | +} |
| 36 | + |
| 37 | +Biquad::~Biquad() { |
| 38 | +} |
| 39 | + |
| 40 | +void Biquad::setType(int type) { |
| 41 | + this->type = type; |
| 42 | + calcBiquad(); |
| 43 | +} |
| 44 | + |
| 45 | +void Biquad::setQ(double Q) { |
| 46 | + this->Q = Q; |
| 47 | + calcBiquad(); |
| 48 | +} |
| 49 | + |
| 50 | +void Biquad::setFc(double Fc) { |
| 51 | + this->Fc = Fc; |
| 52 | + calcBiquad(); |
| 53 | +} |
| 54 | + |
| 55 | +void Biquad::setPeakGain(double peakGainDB) { |
| 56 | + this->peakGain = peakGainDB; |
| 57 | + calcBiquad(); |
| 58 | +} |
| 59 | + |
| 60 | +void Biquad::setBiquad(int type, double Fc, double Q, double peakGainDB) { |
| 61 | + this->type = type; |
| 62 | + this->Q = Q; |
| 63 | + this->Fc = Fc; |
| 64 | + setPeakGain(peakGainDB); |
| 65 | +} |
| 66 | + |
| 67 | +void Biquad::calcBiquad(void) { |
| 68 | + double norm; |
| 69 | + double V = pow(10, fabs(peakGain) / 20.0); |
| 70 | + double K = tan(M_PI * Fc); |
| 71 | + switch (this->type) { |
| 72 | + case bq_type_lowpass: |
| 73 | + norm = 1 / (1 + K / Q + K * K); |
| 74 | + a0 = K * K * norm; |
| 75 | + a1 = 2 * a0; |
| 76 | + a2 = a0; |
| 77 | + b1 = 2 * (K * K - 1) * norm; |
| 78 | + b2 = (1 - K / Q + K * K) * norm; |
| 79 | + break; |
| 80 | + |
| 81 | + case bq_type_highpass: |
| 82 | + norm = 1 / (1 + K / Q + K * K); |
| 83 | + a0 = 1 * norm; |
| 84 | + a1 = -2 * a0; |
| 85 | + a2 = a0; |
| 86 | + b1 = 2 * (K * K - 1) * norm; |
| 87 | + b2 = (1 - K / Q + K * K) * norm; |
| 88 | + break; |
| 89 | + |
| 90 | + case bq_type_bandpass: |
| 91 | + norm = 1 / (1 + K / Q + K * K); |
| 92 | + a0 = K / Q * norm; |
| 93 | + a1 = 0; |
| 94 | + a2 = -a0; |
| 95 | + b1 = 2 * (K * K - 1) * norm; |
| 96 | + b2 = (1 - K / Q + K * K) * norm; |
| 97 | + break; |
| 98 | + |
| 99 | + case bq_type_notch: |
| 100 | + norm = 1 / (1 + K / Q + K * K); |
| 101 | + a0 = (1 + K * K) * norm; |
| 102 | + a1 = 2 * (K * K - 1) * norm; |
| 103 | + a2 = a0; |
| 104 | + b1 = a1; |
| 105 | + b2 = (1 - K / Q + K * K) * norm; |
| 106 | + break; |
| 107 | + |
| 108 | + case bq_type_peak: |
| 109 | + if (peakGain >= 0) { // boost |
| 110 | + norm = 1 / (1 + 1/Q * K + K * K); |
| 111 | + a0 = (1 + V/Q * K + K * K) * norm; |
| 112 | + a1 = 2 * (K * K - 1) * norm; |
| 113 | + a2 = (1 - V/Q * K + K * K) * norm; |
| 114 | + b1 = a1; |
| 115 | + b2 = (1 - 1/Q * K + K * K) * norm; |
| 116 | + } |
| 117 | + else { // cut |
| 118 | + norm = 1 / (1 + V/Q * K + K * K); |
| 119 | + a0 = (1 + 1/Q * K + K * K) * norm; |
| 120 | + a1 = 2 * (K * K - 1) * norm; |
| 121 | + a2 = (1 - 1/Q * K + K * K) * norm; |
| 122 | + b1 = a1; |
| 123 | + b2 = (1 - V/Q * K + K * K) * norm; |
| 124 | + } |
| 125 | + break; |
| 126 | + case bq_type_lowshelf: |
| 127 | + if (peakGain >= 0) { // boost |
| 128 | + norm = 1 / (1 + sqrt(2) * K + K * K); |
| 129 | + a0 = (1 + sqrt(2*V) * K + V * K * K) * norm; |
| 130 | + a1 = 2 * (V * K * K - 1) * norm; |
| 131 | + a2 = (1 - sqrt(2*V) * K + V * K * K) * norm; |
| 132 | + b1 = 2 * (K * K - 1) * norm; |
| 133 | + b2 = (1 - sqrt(2) * K + K * K) * norm; |
| 134 | + } |
| 135 | + else { // cut |
| 136 | + norm = 1 / (1 + sqrt(2*V) * K + V * K * K); |
| 137 | + a0 = (1 + sqrt(2) * K + K * K) * norm; |
| 138 | + a1 = 2 * (K * K - 1) * norm; |
| 139 | + a2 = (1 - sqrt(2) * K + K * K) * norm; |
| 140 | + b1 = 2 * (V * K * K - 1) * norm; |
| 141 | + b2 = (1 - sqrt(2*V) * K + V * K * K) * norm; |
| 142 | + } |
| 143 | + break; |
| 144 | + case bq_type_highshelf: |
| 145 | + if (peakGain >= 0) { // boost |
| 146 | + norm = 1 / (1 + sqrt(2) * K + K * K); |
| 147 | + a0 = (V + sqrt(2*V) * K + K * K) * norm; |
| 148 | + a1 = 2 * (K * K - V) * norm; |
| 149 | + a2 = (V - sqrt(2*V) * K + K * K) * norm; |
| 150 | + b1 = 2 * (K * K - 1) * norm; |
| 151 | + b2 = (1 - sqrt(2) * K + K * K) * norm; |
| 152 | + } |
| 153 | + else { // cut |
| 154 | + norm = 1 / (V + sqrt(2*V) * K + K * K); |
| 155 | + a0 = (1 + sqrt(2) * K + K * K) * norm; |
| 156 | + a1 = 2 * (K * K - 1) * norm; |
| 157 | + a2 = (1 - sqrt(2) * K + K * K) * norm; |
| 158 | + b1 = 2 * (K * K - V) * norm; |
| 159 | + b2 = (V - sqrt(2*V) * K + K * K) * norm; |
| 160 | + } |
| 161 | + break; |
| 162 | + } |
| 163 | + |
| 164 | + return; |
| 165 | +} |
0 commit comments