From 7096c6c2608b2f5af07f0f06fdbfbc365224091d Mon Sep 17 00:00:00 2001 From: David Buezas Date: Sat, 7 Jan 2023 16:25:37 +0100 Subject: [PATCH] implement delta filter --- readme.md | 1 + src/filters/filters.ts | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 016ed59..ceee5b1 100644 --- a/readme.md +++ b/readme.md @@ -417,6 +417,7 @@ entities: - 0.0 -> 0.0 - 40.0 -> 45.0 - 100.0 -> 102.5 + - delata # computes the delta between each two consecutive numeric y values. - derivate: h # computes rate of change per unit of time: h # ms (milisecond), s (second), m (minute), h (hour), d (day), w (week), M (month), y (year) - integrate: h # computes area under the curve per unit of time using Right hand riemann integration. Same units as the derivative - map_y_numbers: Math.sqrt(y + 10*100) # map the y coordinate of each datapoint. diff --git a/src/filters/filters.ts b/src/filters/filters.ts index c506b00..448650e 100644 --- a/src/filters/filters.ts +++ b/src/filters/filters.ts @@ -75,6 +75,24 @@ const filters = { meta: { ...meta, regression }, }; }, + delta: + () => + ({ ys, meta }) => { + const last = { + y: NaN, + }; + return { + meta: { + ...meta, + unit_of_measurement: `Δ${meta.unit_of_measurement}`, + }, + ys: mapNumbers(ys, (y) => { + const yDelta = y - last.y; + last.y = y; + return yDelta; + }), + }; + }, derivate: (unit: keyof typeof timeUnits = "h") => ({ xs, ys, meta }) => { @@ -91,10 +109,10 @@ const filters = { ys: mapNumbers(ys, (y, i) => { const x = +xs[i]; const dateDelta = (x - last.x) / timeUnits[unit]; - const yDelta = (y - last.y) / dateDelta; + const yDeriv = (y - last.y) / dateDelta; last.y = y; last.x = x; - return yDelta; + return yDeriv; }), }; },