-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiir.c
135 lines (125 loc) · 3.78 KB
/
iir.c
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
#include "iir.h"
inline void biquad_filter(sample_t *s, biquad_t *b)
{
sample_t out = b->b[0] * (*s) + b->d[0];
b->d[0] = b->b[1] * (*s) + b->a[1] * out + b->d[1];
b->d[1] = b->b[2] * (*s) + b->a[2] * out;
*s = out;
}
// https://www.earlevel.com/main/2011/01/02/biquad-formulas/
inline biquad_t generate_biquad(filter_type_t type, float Fc, float Q, float peakGain, float Fs)
{
float a0 = 0.0f, a1 = 0.0f, a2 = 0.0f, b1 = 0.0f, b2 = 0.0f, norm = 0.0f;
float V = powf(10.0f, fabsf(peakGain) / 20.0f);
float K = tanf(M_PI * Fc / Fs);
switch (type)
{
case lowpass:
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 highpass:
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 bandpass:
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 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 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 lowshelf:
if (peakGain >= 0)
{ // boost
norm = 1 / (1 + M_SQRT2 * K + K * K);
a0 = (1 + sqrtf(2 * V) * K + V * K * K) * norm;
a1 = 2 * (V * K * K - 1) * norm;
a2 = (1 - sqrtf(2 * V) * K + V * K * K) * norm;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - M_SQRT2 * K + K * K) * norm;
}
else
{ // cut
norm = 1 / (1 + sqrtf(2 * V) * K + V * K * K);
a0 = (1 + M_SQRT2 * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - M_SQRT2 * K + K * K) * norm;
b1 = 2 * (V * K * K - 1) * norm;
b2 = (1 - sqrtf(2 * V) * K + V * K * K) * norm;
}
break;
case highshelf:
if (peakGain >= 0)
{ // boost
norm = 1 / (1 + M_SQRT2 * K + K * K);
a0 = (V + sqrtf(2 * V) * K + K * K) * norm;
a1 = 2 * (K * K - V) * norm;
a2 = (V - sqrtf(2 * V) * K + K * K) * norm;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - M_SQRT2 * K + K * K) * norm;
}
else
{ // cut
norm = 1 / (V + sqrtf(2 * V) * K + K * K);
a0 = (1 + M_SQRT2 * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - M_SQRT2 * K + K * K) * norm;
b1 = 2 * (K * K - V) * norm;
b2 = (V - sqrtf(2 * V) * K + K * K) * norm;
}
break;
case none:
default:
type = none;
a0 = 0.0f;
a1 = 0.0f;
a2 = 0.0f;
b1 = 0.0f;
b2 = 0.0f;
break;
}
biquad_t flt = {
.type = type,
.b = {a0, a1, a2},
.a = {1.0f, -b1, -b2},
.d = {0.0f, 0.0f}};
return flt;
}