-
Notifications
You must be signed in to change notification settings - Fork 0
/
iirfilter.cpp
364 lines (300 loc) · 11.4 KB
/
iirfilter.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
This file is part of WaverIIR
Copyright (C) 2021 Peter Papp
Please visit https://launchpad.net/waveriir for details
*/
#include "iirfilter.h"
// calculate coefficients for biquad filters (gainDecibell is ignored for non-gaining filter types)
CoefficientList IIRFilter::calculateBiquadCoefficients(FilterTypes filterType, double Q, double K, double gainDecibel)
{
CoefficientList returnValue;
double temp;
double gainFactor = pow(10, std::abs(gainDecibel) / 20);
switch (filterType) {
case LowPass:
temp = 1 / (1 + K / Q + K * K);
returnValue.appendA(K * K * temp);
returnValue.appendA(2 * returnValue.aValue(0));
returnValue.appendA(returnValue.aValue(0));
returnValue.appendB(2 * (K * K - 1) * temp);
returnValue.appendB((1 - K / Q + K * K) * temp);
break;
case HighPass:
temp = 1 / (1 + K / Q + K * K);
returnValue.appendA(temp);
returnValue.appendA(-2 * returnValue.aValue(0));
returnValue.appendA(returnValue.aValue(0));
returnValue.appendB(2 * (K * K - 1) * temp);
returnValue.appendB((1 - K / Q + K * K) * temp);
break;
case BandPass:
temp = 1 / (1 + K / Q + K * K);
returnValue.appendA(K / Q * temp);
returnValue.appendA(0);
returnValue.appendA(-1 * returnValue.aValue(0));
returnValue.appendB(2 * (K * K - 1) * temp);
returnValue.appendB((1 - K / Q + K * K) * temp);
break;
case BandStop:
temp = 1 / (1 + K / Q + K * K);
returnValue.appendA((1 + K * K) * temp);
returnValue.appendA(2 * (K * K - 1) * temp);
returnValue.appendA(returnValue.aValue(0));
returnValue.appendB(returnValue.aValue(1));
returnValue.appendB((1 - K / Q + K * K) * temp);
break;
case LowShelf:
if (gainDecibel > 0) {
temp = 1 / (1 + sqrt(2) * K + K * K);
returnValue.appendA((1 + sqrt(2 * gainFactor) * K + gainFactor * K * K) * temp);
returnValue.appendA(2 * (gainFactor * K * K - 1) * temp);
returnValue.appendA((1 - sqrt(2 * gainFactor) * K + gainFactor * K * K) * temp);
returnValue.appendB(2 * (K * K - 1) * temp);
returnValue.appendB((1 - sqrt(2) * K + K * K) * temp);
}
else if (gainDecibel == 0) {
returnValue.appendA(1);
returnValue.appendA(0);
returnValue.appendA(0);
returnValue.appendB(0);
returnValue.appendB(0);
}
else {
temp = 1 / (1 + sqrt(2 * gainFactor) * K + gainFactor * K * K);
returnValue.appendA((1 + sqrt(2) * K + K * K) * temp);
returnValue.appendA(2 * (K * K - 1) * temp);
returnValue.appendA((1 - sqrt(2) * K + K * K) * temp);
returnValue.appendB(2 * (gainFactor * K * K - 1) * temp);
returnValue.appendB((1 - sqrt(2 * gainFactor) * K + gainFactor * K * K) * temp);
}
break;
case HighShelf:
if (gainDecibel > 0) {
temp = 1 / (1 + sqrt(2) * K + K * K);
returnValue.appendA((gainFactor + sqrt(2 * gainFactor) * K + K * K) * temp);
returnValue.appendA(2 * (K * K - gainFactor) * temp);
returnValue.appendA((gainFactor - sqrt(2 * gainFactor) * K + K * K) * temp);
returnValue.appendB(2 * (K * K - 1) * temp);
returnValue.appendB((1 - sqrt(2) * K + K * K) * temp);
}
else if (gainDecibel == 0) {
returnValue.appendA(1);
returnValue.appendA(0);
returnValue.appendA(0);
returnValue.appendB(0);
returnValue.appendB(0);
}
else {
temp = 1 / (gainFactor + sqrt(2 * gainFactor) * K + K * K);
returnValue.appendA((1 + sqrt(2) * K + K * K) * temp);
returnValue.appendA(2 * (K * K - 1) * temp);
returnValue.appendA((1 - sqrt(2) * K + K * K) * temp);
returnValue.appendB(2 * (K * K - gainFactor) * temp);
returnValue.appendB((gainFactor - sqrt(2 * gainFactor) * K + K * K) * temp);
}
break;
case BandShelf:
if (gainDecibel > 0) {
temp = 1 / (1 + 1 / Q * K + K * K);
returnValue.appendA((1 + gainFactor / Q * K + K * K) * temp);
returnValue.appendA(2 * (K * K - 1) * temp);
returnValue.appendA((1 - gainFactor / Q * K + K * K) * temp);
returnValue.appendB(returnValue.aValue(1));
returnValue.appendB((1 - 1 / Q * K + K * K) * temp);
}
else if (gainDecibel == 0) {
returnValue.appendA(1);
returnValue.appendA(0);
returnValue.appendA(0);
returnValue.appendB(0);
returnValue.appendB(0);
}
else {
temp = 1 / (1 + gainFactor / Q * K + K * K);
returnValue.appendA((1 + 1 / Q * K + K * K) * temp);
returnValue.appendA(2 * (K * K - 1) * temp);
returnValue.appendA((1 - 1 / Q * K + K * K) * temp);
returnValue.appendB(returnValue.aValue(1));
returnValue.appendB((1 - gainFactor / Q * K + K * K) * temp);
}
}
return returnValue;
}
// calculate coefficients for biquad filters overload: easier to use
CoefficientList IIRFilter::calculateBiquadCoefficients(FilterTypes filterType, double centerFrequency, double bandwidth, int sampleRate, double gainDecibel)
{
double Q = centerFrequency / bandwidth;
double K = tan(M_PI * (centerFrequency / sampleRate));
return calculateBiquadCoefficients(filterType, Q, K, gainDecibel);
}
// calculate coefficients for biquad filters overload: no gain
CoefficientList IIRFilter::calculateBiquadCoefficients(FilterTypes filterType, double centerFrequency, double bandwidth, int sampleRate)
{
return calculateBiquadCoefficients(filterType, centerFrequency, bandwidth, sampleRate, 0);
}
// return our sample type from Qt's audio format
IIRFilter::SampleTypes IIRFilter::getSampleTypeFromAudioFormat(QAudioFormat audioFormat)
{
SampleTypes sampleType = Unknown;
if (audioFormat.sampleType() == QAudioFormat::Float) {
sampleType = floatSample;
}
else if (audioFormat.sampleType() == QAudioFormat::SignedInt) {
switch (audioFormat.sampleSize()) {
case 8:
sampleType = int8Sample;
break;
case 16:
sampleType = int16Sample;
break;
case 32:
sampleType = int32Sample;
}
}
else if (audioFormat.sampleType() == QAudioFormat::UnSignedInt) {
switch (audioFormat.sampleSize()) {
case 8:
sampleType = uint8Sample;
break;
case 16:
sampleType = uint16Sample;
break;
case 32:
sampleType = uint32Sample;
}
}
return sampleType;
}
// constructor
IIRFilter::IIRFilter()
{
updateData = true;
a = nullptr;
b = nullptr;
aLength = 0;
reset();
callbackRawMember = nullptr;
callbackRawObject = nullptr;
callbackFilteredObject = nullptr;
callbackFilteredMember = nullptr;
}
// constructor overload
IIRFilter::IIRFilter(CoefficientList coefficientList)
{
updateData = true;
a = nullptr;
b = nullptr;
aLength = 0;
reset();
callbackRawMember = nullptr;
callbackRawObject = nullptr;
callbackFilteredObject = nullptr;
callbackFilteredMember = nullptr;
applyCoefficients(coefficientList);
}
// destructor
IIRFilter::~IIRFilter()
{
// free memory
if (a != nullptr) {
delete[] a;
}
if (b != nullptr) {
delete[] b;
}
}
// change coefficients
void IIRFilter::setCoefficients(CoefficientList coefficientList)
{
applyCoefficients(coefficientList);
}
// treat PCM data readonly, this can be useful when using callbacks in a paralell filters situation
void IIRFilter::disableUpdateData()
{
updateData = false;
}
// change coefficients (helper method called from multiple public methods)
void IIRFilter::applyCoefficients(CoefficientList coefficientList)
{
// reset current coefficients if any
if (a != nullptr) {
delete[] a;
a = nullptr;
}
if (b != nullptr) {
delete[] b;
b = nullptr;
}
aLength = 0;
// parameter checking (either b is one element shorter than a, or the two are the same size in which case b[0] is ignored)
if ((coefficientList.aSize() < 2) || !((coefficientList.aSize() == coefficientList.bSize()) ||
(coefficientList.aSize() == (coefficientList.bSize() + 1)))) {
return;
}
// cap number of coefficients
aLength = qMin(coefficientList.aSize(), MAX_ORDER + 1);
// create the arrays
a = new double[aLength];
b = new double[aLength];
// copy the data
b[0] = 0;
for (int i = 0; i < aLength; i++) {
a[i] = coefficientList.aValue(i);
if (i < coefficientList.bSize()) {
b[coefficientList.aSize() > coefficientList.bSize() ? i + 1 : i] = coefficientList.bValue(i);
}
}
}
// set callback pointer for raw data
void IIRFilter::setCallbackRaw(IIRFilterCallback *callbackRawObject, IIRFilterCallback::FilterCallbackPointer callbackRawMember)
{
this->callbackRawObject = callbackRawObject;
this->callbackRawMember = callbackRawMember;
}
// set callback pointer for filtered data
void IIRFilter::setCallbackFiltered(IIRFilterCallback *callbackFilteredObject,
IIRFilterCallback::FilterCallbackPointer callbackFilteredMember)
{
this->callbackFilteredObject = callbackFilteredObject;
this->callbackFilteredMember = callbackFilteredMember;
}
// apply the filter to PCM data
void IIRFilter::processPCMData(void *data, int byteCount, SampleTypes sampleType, int channelCount)
{
// make sure coefficiants were set
if ((a == nullptr) || (b == nullptr) || (aLength < 2)) {
return;
}
// call the template method (see in header) with appropriate data type for each supported sample type
switch (sampleType) {
case Unknown:
break;
case int8Sample:
process<qint8>(data, byteCount, channelCount);
break;
case uint8Sample:
process<quint8>(data, byteCount, channelCount);
break;
case int16Sample:
process<qint16>(data, byteCount, channelCount);
break;
case uint16Sample:
process<quint16>(data, byteCount, channelCount);
break;
case int32Sample:
process<qint32>(data, byteCount, channelCount);
break;
case uint32Sample:
process<quint32>(data, byteCount, channelCount);
break;
case floatSample:
process<float>(data, byteCount, channelCount);
}
}
// reset buffers
void IIRFilter::reset()
{
memset(&inputBuffer, 0, MAX_CHANNELS * MAX_ORDER * sizeof(double));
memset(&outputBuffer, 0, MAX_CHANNELS * MAX_ORDER * sizeof(double));
currentChannel = 0;
}