forked from jaakkopasanen/AutoEq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
biquad.py
182 lines (139 loc) · 4.78 KB
/
biquad.py
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
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from scipy import signal
def numpyfy(fc, Q, gain, fs):
# Cast lists to Numpy arrays
if type(fc) == list:
fc = np.array(fc)
if type(Q) == list:
Q = np.array(Q)
if type(gain) == list:
gain = np.array(gain)
if type(fs) == list:
fs = np.array(fs)
return fc, Q, gain, fs
def peaking(fc, Q, gain, fs=48000):
"""Peaking filter designer.
Args:
fc: Center frequency
Q: Q factor
gain: Gain
fs: Sampling frequency
Returns:
Biquad filter coefficients a0, a1, a2, b0, b1 and b2 as tuple
"""
# Turn lists into numpy arrays
fc, Q, gain, fs = numpyfy(fc, Q, gain, fs)
A = 10 ** (gain / 40)
w0 = 2 * np.pi * fc / fs
alpha = np.sin(w0) / (2 * Q)
a0 = 1 + alpha / A
a1 = -(-2 * np.cos(w0)) / a0
a2 = -(1 - alpha / A) / a0
b0 = (1 + alpha * A) / a0
b1 = (-2 * np.cos(w0)) / a0
b2 = (1 - alpha * A) / a0
return 1.0, a1, a2, b0, b1, b2
def low_shelf(fc, Q, gain, fs=48000):
"""Low shelf filter designer.
Args:
fc: Center frequency
Q: Q factor
gain: Gain
fs: Sampling frequency
Returns:
Biquad filter coefficients a0, a1, a2, b0, b1 and b2 as tuple
"""
# Turn lists into numpy arrays
fc, Q, gain, fs = numpyfy(fc, Q, gain, fs)
A = 10 ** (gain / 40)
w0 = 2 * np.pi * fc / fs
alpha = np.sin(w0) / (2 * Q)
a0 = (A + 1) + (A - 1) * np.cos(w0) + 2 * np.sqrt(A) * alpha
a1 = -(-2 * ((A - 1) + (A + 1) * np.cos(w0))) / a0
a2 = -((A + 1) + (A - 1) * np.cos(w0) - 2 * np.sqrt(A) * alpha) / a0
b0 = (A * ((A + 1) - (A - 1) * np.cos(w0) + 2 * np.sqrt(A) * alpha)) / a0
b1 = (2 * A * ((A - 1) - (A + 1) * np.cos(w0))) / a0
b2 = (A * ((A + 1) - (A - 1) * np.cos(w0) - 2 * np.sqrt(A) * alpha)) / a0
return 1.0, a1, a2, b0, b1, b2
def high_shelf(fc, Q, gain, fs=48000):
"""High shelf filter designer.
Args:
fc: Center frequency
Q: Q factor
gain: Gain
fs: Sampling frequency
Returns:
Biquad filter coefficients a0, a1, a2, b0, b1 and b2 as tuple
"""
# Turn lists into numpy arrays
fc, Q, gain, fs = numpyfy(fc, Q, gain, fs)
A = 10 ** (gain / 40)
w0 = 2 * np.pi * fc / fs
alpha = np.sin(w0) / (2 * Q)
a0 = (A + 1) - (A - 1) * np.cos(w0) + 2 * np.sqrt(A) * alpha
a1 = -(2 * ((A - 1) - (A + 1) * np.cos(w0))) / a0
a2 = -((A + 1) - (A - 1) * np.cos(w0) - 2 * np.sqrt(A) * alpha) / a0
b0 = (A * ((A + 1) + (A - 1) * np.cos(w0) + 2 * np.sqrt(A) * alpha)) / a0
b1 = (-2 * A * ((A - 1) + (A + 1) * np.cos(w0))) / a0
b2 = (A * ((A + 1) + (A - 1) * np.cos(w0) - 2 * np.sqrt(A) * alpha)) / a0
return 1.0, a1, a2, b0, b1, b2
def digital_coeffs(f, fs, a0, a1, a2, b0, b1, b2):
f = np.array(f)
a0 = np.array(a0)
a1 = np.array(a1)
a2 = np.array(a2)
b0 = np.array(b0)
b1 = np.array(b1)
b2 = np.array(b2)
w = 2 * np.pi * f / fs
phi = 4 * np.sin(w / 2) ** 2
a1 *= -1
a2 *= -1
c = 10 * np.log10(
(b0 + b1 + b2) ** 2 + (b0 * b2 * phi - (b1 * (b0 + b2) + 4 * b0 * b2)) * phi
) - 10 * np.log10(
(a0 + a1 + a2) ** 2 + (a0 * a2 * phi - (a1 * (a0 + a2) + 4 * a0 * a2)) * phi
)
return c
def impulse_response(a0, a1, a2, b0, b1, b2, n=250):
raise NotImplemented('biquad.impulse_response is not correctly implemented!')
ir = signal.unit_impulse(n)
for _a0, _a1, _a2, _b0, _b1, _b2 in zip(a0, a1, a2, b0, b1, b2):
ir = signal.lfilter(np.concatenate([_b0, _b1, _b2]), np.concatenate([_a0, _a1, _a2]), ir)
ir = np.concatenate(([0.0], ir))
return ir
def main():
fc = [20, 220, 450, 1280, 2200, 3000, 5700, 6600, 7600]
Q = [1.1, 0.9, 1.0, 1.5, 4.0, 2.0, 6.0, 7.0, 5.0]
gain = [2.1, -3.8, -2.0, 4.0, -3.5, 4.5, -5.0, 0.4, -2.4]
fs = 48000
a0, a1, a2, b0, b1, b2 = peaking(fc, Q, gain, fs=fs)
f = [20]
while f[-1] < fs:
f.append(f[-1]*2**(1/16))
f = np.repeat(np.expand_dims(f, 1), len(fc), axis=1)
c = digital_coeffs(f, fs, a0, a1, a2, b0, b1, b2)
a0 = [a0] * len(a1)
ir = impulse_response(a0, a1, a2, b0, b1, b2, n=250)
fig, ax = plt.subplots()
#plt.plot(f, np.sum(c, axis=1), linewidth=3)
plt.plot(f, c)
plt.xlabel('Frequency (Hz)')
plt.semilogx()
plt.xlim([20, 20000])
plt.ylabel('Amplitude (dBr)')
plt.grid(True, which='major')
plt.grid(True, which='minor')
ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:.0f}'))
plt.show()
fig, ax = plt.subplots()
plt.plot(np.arange(0, len(ir)) / fs, ir)
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.ylim([-0.01, 0.01])
plt.show()
if __name__ == '__main__':
main()