UNPKG

dsp-collection

Version:

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

55 lines 1.85 kB
import * as WindowFunctions from "../signal/WindowFunctions.js"; import { goertzelSingle } from "../signal/Goertzel.js"; import * as ArrayUtils from "../utils/ArrayUtils.js"; export function createWindowKernel(f, width, symetric = true) { const nudge = symetric ? 1 : 0; const a1 = Float64Array.from({ length: width }, (_x, i) => f(i / (width - nudge))); const sum = ArrayUtils.sum(a1); const a2 = a1.map(x => x / sum); return a2; } export function createLpFilterKernel(windowFunctionId, normFirstMinFreq) { const descr = WindowFunctions.getFunctionDescrById(windowFunctionId); const width = Math.round(descr.firstMinPos / normFirstMinFreq); if (width < 3) { throw new Error("Filter parameters out of range."); } return createWindowKernel(descr.f, width); } export function calcFreqRespAt(kernel, normFreq) { const relFreq = normFreq * kernel.length; return goertzelSingle(kernel, relFreq); } function reflectIndex(i, n) { if (i >= 0 && i < n) { return i; } if (n <= 1) { return 0; } const period = 2 * (n - 1); const t = (i % period + period) % period; return (t < n) ? t : period - t; } export function applyFirKernelAt(signal, pos, kernel) { if (pos < 0 || pos >= signal.length) { return NaN; } const width = kernel.length; const m = Math.floor(kernel.length / 2); const p0 = pos - m; let acc = 0; for (let i = 0; i < width; i++) { const p = reflectIndex(p0 + i, signal.length); acc += signal[p] * kernel[i]; } return acc; } export function applyFirKernel(signal, kernel) { const out = new Float64Array(signal.length); for (let i = 0; i < signal.length; i++) { out[i] = applyFirKernelAt(signal, i, kernel); } return out; } //# sourceMappingURL=FirFilterWin.js.map