UNPKG

dsp-collection

Version:

A collection of JavaScript modules for digital signal processing (written in TypeScript)

49 lines 1.49 kB
import Complex from "../math/Complex.js"; import MutableComplex from "../math/MutableComplex.js"; import ComplexArray from "../math/ComplexArray.js"; export function goertzelSingle(x, relativeFrequency) { const n = x.length; if (n == 0) { throw new Error("Input array must not be empty."); } const w = 2 * Math.PI / n * relativeFrequency; const c = Complex.expj(w); const cr2 = c.re * 2; let s1 = 0; let s2 = 0; for (let p = 0; p < n; p++) { const s0 = x[p] + cr2 * s1 - s2; s2 = s1; s1 = s0; } return new MutableComplex(c.re * s1 - s2, c.im * s1); } export function goertzel(x) { const n = x.length; const a = new ComplexArray(n); for (let frequency = 0; frequency < n; frequency++) { const c = goertzelSingle(x, frequency); a.set(frequency, c); } return a; } export function goertzelSpectrum(x, inclNyquist = false) { const n = x.length; if (n == 0) { throw new Error("Input array must not be empty."); } const m = (n % 2 == 0 && inclNyquist) ? n / 2 + 1 : Math.ceil(n / 2); const a = new ComplexArray(m); for (let frequency = 0; frequency < m; frequency++) { const c = goertzelSingle(x, frequency); if (frequency == 0 || frequency == n / 2) { c.divByReal(n); } else { c.mulByReal(2 / n); } a.set(frequency, c); } return a; } //# sourceMappingURL=Goertzel.js.map