-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterCoeffs.js
355 lines (310 loc) · 8.49 KB
/
FilterCoeffs.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
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
//================================================================
//
// Calculate 4th order Butterworth low pass filter coefficients
//
//================================================================
inlets = 4; /* This gives the js object two inlets.
The first inlet is used to change the
cut-off frequency.
The second inlet is used to change the
sampling frequency.
The third inlet sets the filter order.
Accepts 2, 3 or 4.
The fourth inlet sets the filter type
(0 for low-pass, 1 for high-pass)
*/
setinletassist(0,"Cut-off frequency");
setinletassist(1,"Sampling frequency");
setinletassist(2,"Filter order");
setinletassist(3,"Filter type");
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)
// Prototype filter transform functions.
var butter2 = new Array(2);
butter2[0] = {
num: [0,0,1],
den: [1,Math.sqrt(2),1]
};
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]
};
var butter4 = new Array(2);
butter4[0] = {
num: [0,0,1],
den: [1,2*Math.cos(Math.PI/8),1]
};
butter4[1] = {
num: [0,0,1],
den: [1,2*Math.cos(3*Math.PI/8),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(butter4,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.
var type = 0; // Filter type: 0,1 for lowpass and highpass
//================================================================
//
// 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;
case 2 :
// Filter order
switch (a) {
case 2 :
proto = butter2
break;
case 3 :
proto = butter3
break;
case 4 :
proto = butter4
break;
}
break;
case 3 :
// Filter type
type = a
break;
}
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(proto,pwc);
hpf_coeffs = hpf(proto,pwc);
switch (type) {
case 0 :
coeffs = blt(lpf_coeffs,T);
break;
case 1 :
coeffs = blt(hpf_coeffs,T);
break;
}
post('coefficients calculated, cutoff ',fc,'\n')
}
//________________________________________________________________
//
// 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 (i in cf) {
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(cf.length + 1);
for (i in cf) {
result[i] = {
num: lpf_qd(cf[i].num,w),
den: lpf_qd(cf[i].den,w)
}
}
return result;
}
// Performs frequency transformation for HPF
hpf.local = 1;
function hpf(cf,w) {
var result = new Array(cf.length + 1);
for (i in cf) {
result[i] = {
num: hpf_qd(cf[i].num,w),
den: hpf_qd2(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;
}
// Ensures that the numerator is s^2.
hpf_qd.local = 1;
function hpf_qd(cf,w) {
var result = new Array(3);
result[0] = 1;
result[1] = 0;
result[2] = 0;
return result;
}
// Similar to lpf_qd, but swaps outer coefficients.
hpf_qd2.local = 1;
function hpf_qd2(cf,w) {
var result = new Array(3);
result[0] = cf[2]*Math.pow(w,0);
result[1] = cf[1]*Math.pow(w,1);
result[2] = cf[0]*Math.pow(w,2);
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 (i in cf) {
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;
}