UNPKG

dsp-collection

Version:

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

68 lines 1.81 kB
export function argMax_scanArith(f, argLo, argHi, argIncr) { let maxArg = NaN; let maxVal = -Infinity; let lastArg = NaN; for (let x = argLo; x < argHi; x += argIncr) { const v = f(x); if (v > maxVal) { maxArg = x; maxVal = v; } lastArg = x; } if (lastArg != argHi && argHi > argLo && f(argHi) > maxVal) { maxArg = argHi; } return maxArg; } export function argMax_scanGeom(f, argLo, argHi, argFactor) { let maxArg = NaN; let maxVal = -Infinity; let lastArg = NaN; for (let x = argLo; x < argHi; x *= argFactor) { const v = f(x); if (v > maxVal) { maxArg = x; maxVal = v; } lastArg = x; } if (lastArg != argHi && argHi > argLo && f(argHi) > maxVal) { maxArg = argHi; } return maxArg; } export function argMax_goldenSectionSearch(f, argLo, argHi, tolerance) { const invPhi = (Math.sqrt(5) - 1) / 2; const invPhi2 = 1 - invPhi; let lo = argLo; let hi = argHi; let width = hi - lo; let x1 = lo + width * invPhi2; let x2 = lo + width * invPhi; let y1 = f(x1); let y2 = f(x2); if (width <= tolerance) { return (lo + hi) / 2; } const n = Math.ceil(Math.log(tolerance / width) / Math.log(invPhi)); for (let i = 0; i < n; i++) { width *= invPhi; if (y1 >= y2) { hi = x2; x2 = x1; y2 = y1; x1 = lo + width * invPhi2; y1 = f(x1); } else { lo = x1; x1 = x2; y1 = y2; x2 = lo + width * invPhi; y2 = f(x2); } } return (y1 >= y2) ? (lo + x2) / 2 : (x1 + hi) / 2; } //# sourceMappingURL=NumApprox.js.map