{"version":3,"file":"ml-savitzky-golay-generalized.min.js","sources":["../node_modules/is-any-array/lib-esm/index.js","../lib-esm/index.js"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/unbound-method\nconst toString = Object.prototype.toString;\n/**\n * Checks if an object is an instance of an Array (array or typed array, except those that contain bigint values).\n *\n * @param value - Object to check.\n * @returns True if the object is an array or a typed array.\n */\nexport function isAnyArray(value) {\n const tag = toString.call(value);\n return tag.endsWith('Array]') && !tag.includes('Big');\n}\n//# sourceMappingURL=index.js.map","import { isAnyArray } from 'is-any-array';\n/**\n * Apply Savitzky Golay algorithm\n * @param [ys] Array of y values\n * @param [xs] Array of X or deltaX\n * @return Array containing the new ys (same length)\n */\nexport function sgg(ys, xs, options = {}) {\n const { windowSize = 9, derivative = 0, polynomial = 3 } = options;\n if (windowSize % 2 === 0 || windowSize < 5 || !Number.isInteger(windowSize)) {\n throw new RangeError('Invalid window size (should be odd and at least 5 integer number)');\n }\n if (!isAnyArray(ys)) {\n throw new TypeError('Y values must be an array');\n }\n if (typeof xs === 'undefined') {\n throw new TypeError('X must be defined');\n }\n if (windowSize > ys.length) {\n throw new RangeError(`Window size is higher than the data length ${windowSize}>${ys.length}`);\n }\n if (derivative < 0 || !Number.isInteger(derivative)) {\n throw new RangeError('Derivative should be a positive integer');\n }\n if (polynomial < 1 || !Number.isInteger(polynomial)) {\n throw new RangeError('Polynomial should be a positive integer');\n }\n if (polynomial >= 6) {\n // eslint-disable-next-line no-console\n console.warn('You should not use polynomial grade higher than 5 if you are' +\n ' not sure that your data arises from such a model. Possible polynomial oscillation problems');\n }\n const half = Math.floor(windowSize / 2);\n const np = ys.length;\n const ans = new Float64Array(np);\n const weights = fullWeights(windowSize, polynomial, derivative);\n let hs = 0;\n let constantH = true;\n if (isAnyArray(xs)) {\n constantH = false;\n }\n else {\n hs = xs ** derivative;\n }\n //For the borders\n for (let i = 0; i < half; i++) {\n const wg1 = weights[half - i - 1];\n const wg2 = weights[half + i + 1];\n let d1 = 0;\n let d2 = 0;\n for (let l = 0; l < windowSize; l++) {\n d1 += wg1[l] * ys[l];\n d2 += wg2[l] * ys[np - windowSize + l];\n }\n if (constantH) {\n ans[half - i - 1] = d1 / hs;\n ans[np - half + i] = d2 / hs;\n }\n else {\n hs = getHs(xs, half - i - 1, half, derivative);\n ans[half - i - 1] = d1 / hs;\n hs = getHs(xs, np - half + i, half, derivative);\n ans[np - half + i] = d2 / hs;\n }\n }\n //For the internal points\n const wg = weights[half];\n for (let i = windowSize; i <= np; i++) {\n let d = 0;\n for (let l = 0; l < windowSize; l++)\n d += wg[l] * ys[l + i - windowSize];\n if (!constantH) {\n hs = getHs(xs, i - half - 1, half, derivative);\n }\n ans[i - half - 1] = d / hs;\n }\n return ans;\n}\nfunction getHs(h, center, half, derivative) {\n let hs = 0;\n let count = 0;\n for (let i = center - half; i < center + half; i++) {\n if (i >= 0 && i < h.length - 1) {\n hs += h[i + 1] - h[i];\n count++;\n }\n }\n return (hs / count) ** derivative;\n}\nfunction gramPoly(i, m, k, s) {\n let Grampoly = 0;\n if (k > 0) {\n Grampoly =\n ((4 * k - 2) / (k * (2 * m - k + 1))) *\n (i * gramPoly(i, m, k - 1, s) + s * gramPoly(i, m, k - 1, s - 1)) -\n (((k - 1) * (2 * m + k)) / (k * (2 * m - k + 1))) *\n gramPoly(i, m, k - 2, s);\n }\n else if (k === 0 && s === 0) {\n Grampoly = 1;\n }\n else {\n Grampoly = 0;\n }\n return Grampoly;\n}\nfunction genFact(a, b) {\n let gf = 1;\n if (a >= b) {\n for (let j = a - b + 1; j <= a; j++) {\n gf *= j;\n }\n }\n return gf;\n}\nfunction weight(i, t, m, n, s) {\n let sum = 0;\n for (let k = 0; k <= n; k++) {\n sum +=\n (2 * k + 1) *\n (genFact(2 * m, k) / genFact(2 * m + k + 1, k + 1)) *\n gramPoly(i, m, k, 0) *\n gramPoly(t, m, k, s);\n }\n return sum;\n}\n/**\n * @private\n * @param m Number of points\n * @param n Polynomial grade\n * @param s Derivative\n */\nfunction fullWeights(m, n, s) {\n const weights = new Array(m);\n const np = Math.floor(m / 2);\n for (let t = -np; t <= np; t++) {\n weights[t + np] = new Float64Array(m);\n for (let j = -np; j <= np; j++) {\n weights[t + np][j + np] = weight(j, t, np, n, s);\n }\n }\n return weights;\n}\n//# sourceMappingURL=index.js.map"],"names":["toString","Object","prototype","isAnyArray","value","tag","call","endsWith","includes","getHs","h","center","half","derivative","hs","count","i","length","gramPoly","m","k","s","Grampoly","genFact","a","b","gf","j","weight","t","n","sum","ys","xs","options","windowSize","polynomial","Number","isInteger","RangeError","TypeError","console","warn","Math","floor","np","ans","Float64Array","weights","Array","fullWeights","constantH","wg1","wg2","d1","d2","l","wg","d"],"mappings":"2OACA,MAAMA,EAAWC,OAAOC,UAAUF,SAoB5B,SAAUG,EAAWC,GACzB,MAAMC,EAAML,EAASM,KAAKF,GAC1B,OAAOC,EAAIE,SAAS,YAAcF,EAAIG,SAAS,MACjD,CCoFA,SAASC,EACPC,EACAC,EACAC,EACAC,GAEA,IAAIC,EAAK,EACLC,EAAQ,EACZ,IAAK,IAAIC,EAAIL,EAASC,EAAMI,EAAIL,EAASC,EAAMI,IACzCA,GAAK,GAAKA,EAAIN,EAAEO,OAAS,IAC3BH,GAAMJ,EAAEM,EAAI,GAAKN,EAAEM,GACnBD,KAGJ,OAAQD,EAAKC,IAAUF,CACzB,CAEA,SAASK,EAASF,EAAWG,EAAWC,EAAWC,GACjD,IAAIC,EAAW,EAYf,OAVEA,EADEF,EAAI,GAEF,EAAIA,EAAI,IAAMA,GAAK,EAAID,EAAIC,EAAI,KAC9BJ,EAAIE,EAASF,EAAGG,EAAGC,EAAI,EAAGC,GAAKA,EAAIH,EAASF,EAAGG,EAAGC,EAAI,EAAGC,EAAI,KAC7DD,EAAI,IAAM,EAAID,EAAIC,IAAOA,GAAK,EAAID,EAAIC,EAAI,IAC3CF,EAASF,EAAGG,EAAGC,EAAI,EAAGC,GACX,IAAND,GAAiB,IAANC,EACT,EAEA,EAEN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