As part of my downsampling process from 48kHz to 16kHz, I need a filter to prevent aliasing. Thankfully, the WebAudio API provides built-in filters that I can utilize:
biquadFilter = context.createBiquadFilter();
biquadFilter.type = "lowpass";
biquadFilter.frequency.value = 12000;
biquadFilter.Q.value = 3;
However, I have also developed a simple algorithm that accomplishes the same task:
//Order 3 - Indirect form 2
var a_coef = [1.000000000000000, -0.965779713179161, 0.582644165984302 , -0.106017056545330];
var b_coef = [0.063855924532476, 0.191567773597429, 0.191567773597429, 0.063855924532476];
var z1 = 0; var z2 = 0; var z3 = 0; var z0 = 0;
for (var i = 0; i < buffer.length; i++) {
z0 = buffer[i] + a_coef[1] * z1-a_coef[2] * z2-a_coef[3] * z3;
buffer[i] = b_coef[0] * z0 + b_coef[1] * z1+b_coef[2] * z2 + b_coef[3] * z3;
z3 = z2; z2 = z1; z1 = z0;
};
I am curious about which method consumes more resources. Were BiquadFilters written in native code for efficiency?