-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButterworth_3rdOrder_LPF.js
277 lines (242 loc) · 7.11 KB
/
Butterworth_3rdOrder_LPF.js
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
//================================================================
//
// Calculate 3rd order Butterworth low pass filter coefficients
//
//================================================================
inlets = 2; /* This gives the js object two inlets.
The left hand inlet is used to change the
cut-off frequency.
The right hand inlet is used to change the
sampling frequency.
*/
outlets = 1; /* This gives the js object a single outlet.
This is used to output the list to set the
coefficients of a Max/MSP biquad~ object.
*/
//________________________________________________________________
//
// Global variables that can be changed using the inlets to the js
// object
//
var fs = 44100; // The sampling frequency (Hz)
var fc = 440; // The cut-off frequency (Hz)
//________________________________________________________________
//
// This is the prototype 3rd order Butterworth LPF (i.e. the
// cut-off angular frequency is 1).
//
// The num field is an array containing the coefficients of the
// numerator of the transfer function.
//
// The den field is an array containing the coefficients of the
// denominator of the transfer function.
//
// This is constant.
//
var butter3 = new Array(2);
butter3[0] = {
num: [0,0,1],
den: [1,1,1]
};
butter3[1] = {
num: [0,0,1],
den: [0,1,1]
};
//________________________________________________________________
//
// These are the remaining global variables
//
var T = 1/fs; // The sampling period
var wc = 2*Math.PI*fc; // The cut-off angular frequency (rad/s)
var pwc = prewarp(wc); // The prewarped cut-off angular freq.
var lpf_coeffs = lpf(butter3,pwc); // The coefficients of the
// analogue filter that is to be
// transformed using the bilinear
// transformation.
var coeffs = blt(lpf_coeffs,T); // The coefficients of the digital
// filter.
//================================================================
//
// Public functions
//
//================================================================
//________________________________________________________________
//
// This responds to integers in the inlets
//
function msg_int(a) {
switch (inlet) {
case 0 :
// Change the cut-off frequency
fc = a;
break;
case 1 :
// Change the sampling frequency
fs = a;
}
calculate_coeffs();
output_coeffs();
}
//________________________________________________________________
//
// This responds to floats in the inlets
//
function msg_float(a) {
switch (inlet) {
case 0 :
// Change the cut-off frequency
fc = a;
}
calculate_coeffs();
output_coeffs();
}
//________________________________________________________________
//
// Output the biquad coefficients when a bang is received.
//
function bang() {
output_coeffs();
}
//________________________________________________________________
//
// Output the biquad coefficients on loading.
//
function loadbang() {
output_coeffs();
}
//================================================================
//
// Private functions
//
//================================================================
//________________________________________________________________
//
// Output the biquad coefficients.
//
output_coeffs.local = 1; // Make the function local (private)
function output_coeffs() {
var bq_cf = convert_to_cascade_list(coeffs);
outlet(0,bq_cf);
// Post the coefficients in the Max window
post('cascade coefficients',bq_cf,'\n');
}
//________________________________________________________________
//
// Calculate the coefficients when either the cut-off frequency
// or the sampling frequency has changed.
//
calculate_coeffs.local = 1;
function calculate_coeffs() {
T = 1/fs;
wc = 2*Math.PI*fc;
pwc = prewarp(wc);
lpf_coeffs = lpf(butter3,pwc);
coeffs = blt(lpf_coeffs,T);
}
//________________________________________________________________
//
// Prewarp the cut-off (angular) frequency when designing the
// analogue filter before applying the bilinear transform.
//
prewarp.local = 1;
function prewarp(w) {
return (2/T)*Math.atan(w*T/2);
}
//________________________________________________________________
//
// Concatanate the lists of biquad coefficients.
//
function convert_to_cascade_list(cf) {
var a = [];
for (var i=0; i<2; i++) {
a.push.apply(a,convert_to_biquad_list(cf[i]));
}
return a;
}
//________________________________________________________________
//
// Convert the coefficients from the num/den form used in
// butter2, etc. to a list that can be used by the Max/MSP
// biquad~ object.
//
// The leading coefficient in the denominator must be 1,
// so the coefficients are scaled accordingly.
//
// I wondered about using a loop here but decided it wasn't
// worth it.
//
convert_to_biquad_list.local = 1;
function convert_to_biquad_list(cf) {
var result = new Array(5);
result[0] = cf.num[0]/cf.den[0];
result[1] = cf.num[1]/cf.den[0];
result[2] = cf.num[2]/cf.den[0];
result[3] = cf.den[1]/cf.den[0];
result[4] = cf.den[2]/cf.den[0];
return result;
}
//________________________________________________________________
//
// This function takes the coefficients for the prototype analogue
// low pass filter and transforms them into the coefficients for
// an analogue low pass filter with the specified cut-off frequency.
//
lpf.local = 1;
function lpf(cf,w) {
var result = new Array(2);
for (var i=0; i<2; i++) {
result[i] = {
num: lpf_qd(cf[i].num,w),
den: lpf_qd(cf[i].den,w)
}
}
return result;
}
//________________________________________________________________
//
// Given a 3 element array [a,b,c] and a scalar w, this function
// returns the array [a,b*w,c*w^2].
//
// This is only used in the lpf function.
//
lpf_qd.local = 1;
function lpf_qd(cf,w) {
var result = new Array(3);
for (var i=0; i<3; i++) {
result[i] = cf[i]*Math.pow(w,i);
}
return result;
}
//________________________________________________________________
//
// This function takes the coefficients for an analogue filter
// and uses the bilinear transform to generate an equivalent
// digital filter.
//
blt.local = 1;
function blt(cf,T) {
var result = new Array(2);
for (var i=0; i<2; i++) {
result[i] = {
num: blt_qd(cf[i].num,T),
den: blt_qd(cf[i].den,T)
}
}
return result;
}
blt_qd.local = 1;
function blt_qd(cf,T) {
var result = new Array(3);
result[0] = blt_qd_cf(cf,[4,2,1],T);
result[1] = blt_qd_cf(cf,[-8,0,2],T);
result[2] = blt_qd_cf(cf,[4,-2,1],T);
return result;
}
blt_qd_cf.local = 1;
function blt_qd_cf(cf,cf2,T) {
var result = 0;
for (var i=0; i<3; i++) {
result += cf[i]*cf2[i]*Math.pow(T,i);
}
return result;
}