@thi.ng/dsp
Version:
Composable signal generators, oscillators, filters, FFT, spectrum, windowing & related DSP utils
30 lines (29 loc) • 960 B
JavaScript
import { cossin } from "@thi.ng/math/angle";
import { TAU } from "@thi.ng/math/api";
import { magDb } from "./convert.js";
import { line } from "./line.js";
const filterResponseRaw = (zeroes, poles, freq, db = true) => {
const w0 = TAU * freq;
const [cp, sp] = __convolve(poles, w0);
const [cz, sz] = __convolve(zeroes, w0);
const mag = Math.sqrt((cz * cz + sz * sz) / (cp * cp + sp * sp));
const phase = Math.atan2(sp, cp) - Math.atan2(sz, cz);
return { freq, phase, mag: db ? magDb(mag) : mag };
};
const filterResponse = (coeffs, freq, db) => filterResponseRaw(coeffs.zeroes, coeffs.poles, freq, db);
const freqRange = (fstart, fend, num) => line(fstart, fend, num - 1).take(num);
const __convolve = (coeffs, w0) => {
let c = 0;
let s = 0;
for (let i = coeffs.length; i-- > 0; ) {
const k = cossin(w0 * i, coeffs[i]);
c += k[0];
s += k[1];
}
return [c, s];
};
export {
filterResponse,
filterResponseRaw,
freqRange
};