| 7 | 8 | 0 | 4 | <- average
|
10 | +-------------+
8 | | | +--------
| | | |
4 |----+ | |
| | |
+----5----10----15-+==20====25====30====35----40
t ->
Input:
[ (timestamp, new_value), ...]
e.g.: [(0,4), (5,10), (18,0), (35,8), ...]
given:
#define WINDOW_SIZE 10;
Print windowed average:
7, 8, 0, 4, ...
I'm stumped trying to think through the code for this problem. One method I could think of was to break down the (timestamp, value) pairs into their lowest unit, say, 1 time unit, which makes the code easier to write, but is computationally expensive.
Edit: For example, the first value is 7 because I calculate the area under the curve for 5 time steps (4*5 = 20) and the area under the curve for the next 5 time steps (10*5 = 50) and then add these 2 and divide by WINDOW_SIZE (70/10 = 7). However, the problem is that these timestamps are quite arbitrary, i.e, they are not periodic. So how do I know when to stop looking through the array?
Edit: This link seems to be relevant: Calculating moving average in C++