-
Notifications
You must be signed in to change notification settings - Fork 0
/
Biquad.h
192 lines (178 loc) · 5.95 KB
/
Biquad.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef COLIN_BIQUAD_H
#define COLIN_BIQUAD_H
#include <math.h>
/*
==============================================================================
Biquad.h
Created: 20 Sep 2022 2:44:09pm
Updated: 30 Nov 2022
added sampleRate variable to save effort
Author: Colin Raab
==============================================================================
*/
namespace Colin
{
enum class Biquad_Type {
LPF, HPF, BPF, Notch, Peak, LowShelf, HighShelf
};
class Biquad_Filter {
protected:
Biquad_Type type;
double a0, a1, a2, b1, b2;
float Fc, Q, peakGain;
double z1, z2;
int sampleRate;
public:
Biquad_Filter()
{
type = Biquad_Type::Peak;
a0 = 1.0;
a1 = a2 = b1 = b2 = 0.0;
Fc = 0.50;
Q = 0.707;
peakGain = 0.0;
z1 = z2 = 0.0;
sampleRate = 44100;
}
~Biquad_Filter() = default;
Biquad_Filter(Biquad_Type type, float Fc, float Q, float peakGainDB)
{
setBiquad(type, Fc, Q, peakGainDB);
z1 = z2 = 0.0;
}
void setSampleRate(int fs) {
if(this->sampleRate != fs)
this->sampleRate = fs;
}
void setType(Biquad_Type type)
{
this->type = type;
calcBiquad();
}
void setQ(float Q)
{
this->Q = Q;
calcBiquad();
}
void setFc(float Fc)
{
this->Fc = Fc / (float)sampleRate;
calcBiquad();
}
void setPeakGain(float peakGainDB)
{
this->peakGain = peakGainDB;
calcBiquad();
}
void setBiquad(Biquad_Type type, float Fc, float Q, float peakGainDB)
{
this->type = type;
this->Q = Q;
this->Fc = Fc;
this->peakGain = peakGainDB;
setFc(Fc);
}
float processAudioSample(float sample) {
float out = sample * a0 + z1;
z1 = sample * a1 + z2 - b1 * out;
z2 = sample * a2 - b2 * out;
return out;
}
void calcBiquad()
{
double norm;
double V = pow(10, fabs(peakGain) / 20.0);
double K = tan(M_PI * Fc);
switch (this->type) {
case Biquad_Type::LPF:
norm = 1 / (1 + K / Q + K * K);
a0 = K * K * norm;
a1 = 2 * a0;
a2 = a0;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - K / Q + K * K) * norm;
break;
case Biquad_Type::HPF:
norm = 1 / (1 + K / Q + K * K);
a0 = 1 * norm;
a1 = -2 * a0;
a2 = a0;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - K / Q + K * K) * norm;
break;
case Biquad_Type::BPF:
norm = 1 / (1 + K / Q + K * K);
a0 = K / Q * norm;
a1 = 0;
a2 = -a0;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - K / Q + K * K) * norm;
break;
case Biquad_Type::Notch:
norm = 1 / (1 + K / Q + K * K);
a0 = (1 + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = a0;
b1 = a1;
b2 = (1 - K / Q + K * K) * norm;
break;
case Biquad_Type::Peak:
if (peakGain >= 0) { // boost
norm = 1 / (1 + 1/Q * K + K * K);
a0 = (1 + V/Q * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - V/Q * K + K * K) * norm;
b1 = a1;
b2 = (1 - 1/Q * K + K * K) * norm;
}
else { // cut
norm = 1 / (1 + V/Q * K + K * K);
a0 = (1 + 1/Q * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - 1/Q * K + K * K) * norm;
b1 = a1;
b2 = (1 - V/Q * K + K * K) * norm;
}
break;
case Biquad_Type::LowShelf:
if (peakGain >= 0) { // boost
norm = 1 / (1 + sqrt(2) * K + K * K);
a0 = (1 + sqrt(2*V) * K + V * K * K) * norm;
a1 = 2 * (V * K * K - 1) * norm;
a2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - sqrt(2) * K + K * K) * norm;
}
else { // cut
norm = 1 / (1 + sqrt(2*V) * K + V * K * K);
a0 = (1 + sqrt(2) * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - sqrt(2) * K + K * K) * norm;
b1 = 2 * (V * K * K - 1) * norm;
b2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
}
break;
case Biquad_Type::HighShelf:
if (peakGain >= 0) { // boost
norm = 1 / (1 + sqrt(2) * K + K * K);
a0 = (V + sqrt(2*V) * K + K * K) * norm;
a1 = 2 * (K * K - V) * norm;
a2 = (V - sqrt(2*V) * K + K * K) * norm;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - sqrt(2) * K + K * K) * norm;
}
else { // cut
norm = 1 / (V + sqrt(2*V) * K + K * K);
a0 = (1 + sqrt(2) * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - sqrt(2) * K + K * K) * norm;
b1 = 2 * (K * K - V) * norm;
b2 = (V - sqrt(2*V) * K + K * K) * norm;
}
break;
}
return;
}
};
}
#endif