Skip to content

Commit fed95f8

Browse files
authored
implement delta filter (#176)
1 parent 4ed0380 commit fed95f8

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ entities:
454454
- 0.0 -> 0.0
455455
- 40.0 -> 45.0
456456
- 100.0 -> 102.5
457+
- delata # computes the delta between each two consecutive numeric y values.
457458
- 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)
458459
- integrate: h # computes area under the curve per unit of time using Right hand riemann integration. Same units as the derivative
459460
- map_y_numbers: Math.sqrt(y + 10*100) # map the y coordinate of each datapoint.

src/filters/filters.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ const filters = {
7575
meta: { ...meta, regression },
7676
};
7777
},
78+
delta:
79+
() =>
80+
({ ys, meta }) => {
81+
const last = {
82+
y: NaN,
83+
};
84+
return {
85+
meta: {
86+
...meta,
87+
unit_of_measurement: ${meta.unit_of_measurement}`,
88+
},
89+
ys: mapNumbers(ys, (y) => {
90+
const yDelta = y - last.y;
91+
last.y = y;
92+
return yDelta;
93+
}),
94+
};
95+
},
7896
derivate:
7997
(unit: keyof typeof timeUnits = "h") =>
8098
({ xs, ys, meta }) => {
@@ -91,10 +109,10 @@ const filters = {
91109
ys: mapNumbers(ys, (y, i) => {
92110
const x = +xs[i];
93111
const dateDelta = (x - last.x) / timeUnits[unit];
94-
const yDelta = (y - last.y) / dateDelta;
112+
const yDeriv = (y - last.y) / dateDelta;
95113
last.y = y;
96114
last.x = x;
97-
return yDelta;
115+
return yDeriv;
98116
}),
99117
};
100118
},

0 commit comments

Comments
 (0)