Mercurial > hg > beaglert
comparison examples/10-Instruments/d-box/Biquad.cpp @ 464:8fcfbfb32aa0 prerelease
Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author | Robert Jack <robert.h.jack@gmail.com> |
---|---|
date | Mon, 20 Jun 2016 16:20:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
463:c47709e8b5c9 | 464:8fcfbfb32aa0 |
---|---|
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 #include <iostream> | |
22 | |
23 Biquad::Biquad() { | |
24 type = bq_type_lowpass; | |
25 a0 = 1.0; | |
26 a1 = a2 = b1 = b2 = 0.0; | |
27 Fc = 0.50; | |
28 Q = 0.707; | |
29 peakGain = 0.0; | |
30 z1 = z2 = 0.0; | |
31 } | |
32 | |
33 Biquad::Biquad(int type, double Fc, double Q, double peakGainDB) { | |
34 setBiquad(type, Fc, Q, peakGainDB); | |
35 z1 = z2 = 0.0; | |
36 } | |
37 | |
38 Biquad::~Biquad() { | |
39 } | |
40 | |
41 void Biquad::setType(int type) { | |
42 this->type = type; | |
43 calcBiquad(); | |
44 } | |
45 | |
46 void Biquad::setQ(double Q) { | |
47 this->Q = Q; | |
48 calcBiquad(); | |
49 } | |
50 | |
51 void Biquad::setFc(double Fc) { | |
52 this->Fc = Fc; | |
53 calcBiquad(); | |
54 } | |
55 | |
56 void Biquad::setPeakGain(double peakGainDB) { | |
57 this->peakGain = peakGainDB; | |
58 calcBiquad(); | |
59 } | |
60 | |
61 void Biquad::setBiquad(int type, double Fc, double Q, double peakGainDB) { | |
62 this->type = type; | |
63 this->Q = Q; | |
64 this->Fc = Fc; | |
65 startFc = Fc; | |
66 startQ = Q; | |
67 startPeakGain = peakGainDB; | |
68 setPeakGain(peakGainDB); | |
69 } | |
70 | |
71 void Biquad::calcBiquad(void) { | |
72 double norm; | |
73 double V = pow(10, fabs(peakGain) / 20.0); | |
74 double K = tan(M_PI * Fc); | |
75 switch (this->type) { | |
76 case bq_type_lowpass: | |
77 norm = 1 / (1 + K / Q + K * K); | |
78 a0 = K * K * norm; | |
79 a1 = 2 * a0; | |
80 a2 = a0; | |
81 b1 = 2 * (K * K - 1) * norm; | |
82 b2 = (1 - K / Q + K * K) * norm; | |
83 break; | |
84 | |
85 case bq_type_highpass: | |
86 norm = 1 / (1 + K / Q + K * K); | |
87 a0 = 1 * norm; | |
88 a1 = -2 * a0; | |
89 a2 = a0; | |
90 b1 = 2 * (K * K - 1) * norm; | |
91 b2 = (1 - K / Q + K * K) * norm; | |
92 break; | |
93 | |
94 case bq_type_bandpass: | |
95 norm = 1 / (1 + K / Q + K * K); | |
96 a0 = K / Q * norm; | |
97 a1 = 0; | |
98 a2 = -a0; | |
99 b1 = 2 * (K * K - 1) * norm; | |
100 b2 = (1 - K / Q + K * K) * norm; | |
101 break; | |
102 | |
103 case bq_type_notch: | |
104 norm = 1 / (1 + K / Q + K * K); | |
105 a0 = (1 + K * K) * norm; | |
106 a1 = 2 * (K * K - 1) * norm; | |
107 a2 = a0; | |
108 b1 = a1; | |
109 b2 = (1 - K / Q + K * K) * norm; | |
110 break; | |
111 | |
112 case bq_type_peak: | |
113 if (peakGain >= 0) { // boost | |
114 norm = 1 / (1 + 1/Q * K + K * K); | |
115 a0 = (1 + V/Q * K + K * K) * norm; | |
116 a1 = 2 * (K * K - 1) * norm; | |
117 a2 = (1 - V/Q * K + K * K) * norm; | |
118 b1 = a1; | |
119 b2 = (1 - 1/Q * K + K * K) * norm; | |
120 } | |
121 else { // cut | |
122 norm = 1 / (1 + V/Q * K + K * K); | |
123 a0 = (1 + 1/Q * K + K * K) * norm; | |
124 a1 = 2 * (K * K - 1) * norm; | |
125 a2 = (1 - 1/Q * K + K * K) * norm; | |
126 b1 = a1; | |
127 b2 = (1 - V/Q * K + K * K) * norm; | |
128 } | |
129 break; | |
130 case bq_type_lowshelf: | |
131 if (peakGain >= 0) { // boost | |
132 norm = 1 / (1 + sqrt(2) * K + K * K); | |
133 a0 = (1 + sqrt(2*V) * K + V * K * K) * norm; | |
134 a1 = 2 * (V * K * K - 1) * norm; | |
135 a2 = (1 - sqrt(2*V) * K + V * K * K) * norm; | |
136 b1 = 2 * (K * K - 1) * norm; | |
137 b2 = (1 - sqrt(2) * K + K * K) * norm; | |
138 } | |
139 else { // cut | |
140 norm = 1 / (1 + sqrt(2*V) * K + V * K * K); | |
141 a0 = (1 + sqrt(2) * K + K * K) * norm; | |
142 a1 = 2 * (K * K - 1) * norm; | |
143 a2 = (1 - sqrt(2) * K + K * K) * norm; | |
144 b1 = 2 * (V * K * K - 1) * norm; | |
145 b2 = (1 - sqrt(2*V) * K + V * K * K) * norm; | |
146 } | |
147 break; | |
148 case bq_type_highshelf: | |
149 if (peakGain >= 0) { // boost | |
150 norm = 1 / (1 + sqrt(2) * K + K * K); | |
151 a0 = (V + sqrt(2*V) * K + K * K) * norm; | |
152 a1 = 2 * (K * K - V) * norm; | |
153 a2 = (V - sqrt(2*V) * K + K * K) * norm; | |
154 b1 = 2 * (K * K - 1) * norm; | |
155 b2 = (1 - sqrt(2) * K + K * K) * norm; | |
156 } | |
157 else { // cut | |
158 norm = 1 / (V + sqrt(2*V) * K + K * K); | |
159 a0 = (1 + sqrt(2) * K + K * K) * norm; | |
160 a1 = 2 * (K * K - 1) * norm; | |
161 a2 = (1 - sqrt(2) * K + K * K) * norm; | |
162 b1 = 2 * (K * K - V) * norm; | |
163 b2 = (V - sqrt(2*V) * K + K * K) * norm; | |
164 } | |
165 break; | |
166 } | |
167 | |
168 return; | |
169 } |