-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCppDSP.h
More file actions
340 lines (281 loc) · 7.58 KB
/
CppDSP.h
File metadata and controls
340 lines (281 loc) · 7.58 KB
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
/*------------------------------------------------------------------*\
Interface to a Cutoff, BiQuad Equalizer and Limiter Class. The EQ design
is based on the EQ cookbook by Robert Bristow Johnson, the cutoff design
is performed via matched z-transform / analogue design with bilinear
transformation of poles/zeros. Cutoffs are calculated in second order
sections.
public domain
Version 0.1.0 (debugged and tested, 05.07.2019).
\*------------------------------------------------------------------*/
#ifndef _DSP_H // include guard
#define _DSP_H
#define NUM_COEFFS_PER_BIQUAD 5
#define NUM_STATES_PER_BIQUAD 2
#include <vector>
#include <string>
#include <cmath>
#include <cstdint>
typedef enum {
FLAT_THRU = 0x0,
BUTTERWORTH,
LINKWITZ,
CHEBYSHEV1,
CHEBYSHEV2,
UNKNOWN_FILTERCHAR
} filterChar;
typedef enum {
LOWPASS = 0x10,
HIGHPASS,
UNKNOWN_FILTERTYPE
} filterType;
typedef enum {
LOWPASSEQ = 0x20,
HIGHPASSEQ,
ALLPASS,
BANDPASS,
NOTCH,
PEAKEQ,
LOWSHELV,
HIGHSHELV,
UNKNOWN_EQTYPE
} eqType;
class CppXover {
public:
CppXover(void);
CppXover(double sampleRate, double freq, filterChar charac, filterType type, uint32_t order);
~CppXover(void);
inline int setSampleRate(double fs) {
int error;
this->fs = fs;
error = designFilter();
if (error <0) {
for (uint32_t i=0; i<((uint32_t)coeffs.size()); i++) {
setCoeffsOfSOS(i,1.,0.,0.,0.,0.);
}
return -1;
}
return 0;
}
inline int setFreq(double freq) {
int error;
this->freq = freq;
error = designFilter();
if (error <0) {
for (uint32_t i=0; i<((uint32_t)coeffs.size()); i++) {
setCoeffsOfSOS(i,1.,0.,0.,0.,0.);
}
return -1;
}
return 0;
}
inline int setOrder(uint32_t ord) {
int error;
this->ord = ord;
this->nSOS = (ord+1)/2;
error = designFilter();
if (error <0) {
for (uint32_t i=0; i<((uint32_t)coeffs.size()); i++) {
setCoeffsOfSOS(i,1.,0.,0.,0.,0.);
}
return -1;
}
return 0;
}
inline int setChar(filterChar charac, double ripple = 1.0, double attenuation = 40.0) {
int error = 0;
this->charac = charac;
error = designFilter(ripple, attenuation);
if (error <0) {
for (uint32_t i=0; i<((uint32_t)coeffs.size()); i++) {
setCoeffsOfSOS(i,1.,0.,0.,0.,0.);
}
return -1;
}
return 0;
}
inline int setType(filterType type) {
int error;
this->type = type;
error = designFilter(this->ord);
if (error <0) {
for (uint32_t i=0; i<((uint32_t)coeffs.size()); i++) {
setCoeffsOfSOS(i,1.,0.,0.,0.,0.);
}
return -1;
}
return 0;
}
uint32_t getOrd() const { return this->ord; }
double getFreq() const { return this->freq; }
filterType getType() const { return this->type; }
filterChar getChar() const { return this->charac; }
static std::string getTypeName(filterType type);
static std::string getCharName(filterChar charac);
void process(std::vector<double> &data);
int addTransferFunction(std::vector<double> &tf, uint32_t nfft);
protected:
int designFilter(double ripple = 1.0, double attenuation = 40.0);
inline void reset() {
for (uint32_t i = 0 ; i < states.size(); i++) {
for (uint32_t j = 0 ; j < NUM_STATES_PER_BIQUAD ; j++) {
states[i][j] = 0.0;
}
}
}
inline int setCoeffsOfSOS(int sosID, double b0, double b1, double b2, double a1, double a2) {
if (sosID > coeffs.size()) {
return -1;
} else {
coeffs[sosID][0] = b0;
coeffs[sosID][1] = b1;
coeffs[sosID][2] = b2;
coeffs[sosID][3] = a1;
coeffs[sosID][4] = a2;
}
return 0;
}
private:
std::vector< std::vector<double> > states;
std::vector< std::vector<double> > coeffs;
double freq, fs;
uint32_t ord, nSOS;
filterChar charac;
filterType type ;
};
class CppEQ {
public:
CppEQ(void);
CppEQ(double sampleRate, double gain, double freq, double Q, eqType type);
~CppEQ(void);
inline int setSampleRate(double fs) {
int error;
this->fs = fs;
error = designBiquad();
if (error <0) {
setCoeffs(1.,0.,0.,0.,0.);
return -1;
}
return 0;
}
inline int setGain(double gain) {
int error;
this->gain = gain;
error = designBiquad();
if (error <0) {
setCoeffs(1.,0.,0.,0.,0.);
return -1;
}
return 0;
}
inline int setFreq(double freq) {
int error;
this->freq = freq;
error = designBiquad();
if (error <0) {
setCoeffs(1.,0.,0.,0.,0.);
return -1;
}
return 0;
}
inline int setQFactor(double Q) {
int error;
this->Q = Q;
error = designBiquad();
if (error <0) {
setCoeffs(1.,0.,0.,0.,0.);
return -1;
}
return 0;
}
inline int setType(eqType type) {
int error;
this->type = type;
error = designBiquad();
if (error <0) {
setCoeffs(1.,0.,0.,0.,0.);
return -1;
}
return 0;
}
double getGain() const { return this->gain; }
double getFreq() const { return this->freq; }
double getQFact() const { return this->Q; }
eqType getType() const { return this->type; }
static std::string getTypeName(eqType type);
void process(std::vector<double> &data);
int addTransferFunction(std::vector<double> &tf, uint32_t nfft);
protected:
int designBiquad();
inline void reset() {
for (int32_t i = 0 ; i < NUM_STATES_PER_BIQUAD ; i++) {
states[i] = 0.0;
}
}
inline void setCoeffs(double b0, double b1, double b2, double a1, double a2) {
coeffs[0] = b0;
coeffs[1] = b1;
coeffs[2] = b2;
coeffs[0] = 1.0;
coeffs[1] = a1;
coeffs[2] = a2;
}
private:
std::vector<double> states;
std::vector<double> coeffs;
double gain, freq, fs, Q;
eqType type;
};
class CppLimiter {
public:
CppLimiter(void);
CppLimiter(double sampleRate, double thres, double makeup, double secRel);
~CppLimiter(void);
inline int setSampleRate(double fs) {
double secRel = 0.0;
if (fs<0) {
return -1;
} else {
secRel = 1.0/((1.0-aRel)*this->fs);
this->fs = fs;
aRel = 1.0-1.0/(secRel*fs);
holdSamps = uint32_t(0.01*fs);
lookaheadSamps = uint32_t(0.002*fs);
mem.resize(lookaheadSamps, 0.0);
return 0;
}
}
inline int setThreshold(double thres) {
if (thres<-90.0) {
return -1;
} else {
this->thres = thres;
return 0;
}
}
inline int setMakeupGain(double makeupGainLog) {
if (makeupGainLog < -50.0 || makeupGainLog > 50.0) {
return -1;
} else {
this->makeup = pow(10.0, makeupGainLog*0.05);
return 0;
}
}
inline int setReleaseTime(double secRel) {
if (secRel>10.0 || secRel < 0.01) {
return -1;
} else {
aRel = 1.0-1.0/(secRel*fs);
return 0;
}
}
double getThres() const { return thres; }
double getMakeup() const { return 20.0*log10(makeup); }
double getReleaseTime() const { return 1.0/((1.0-aRel)*fs); }
void process(std::vector<double> &data);
private:
std::vector<double> mem;
double fs, thres, makeup, aRel, logAbsSigRel, logAbsSigSmooth, compGainLog;
uint32_t lookaheadSamps, holdSamps, memCnt;
int32_t holdCnt;
};
#endif // end of include guard