Instead of implementing a buffer and smooth it, there is a much easier and efficient way to do it.
Here is a simple JavaScript implementation.
First you define your accumulator function:
function lowPass(alpha, value, acc) {
return (value * alpha) + (acc * (1 - alpha));
}
Then you need to define your
APLHA
coefficient:
const ALPHA = 0.015;
Then you can invoke the accumulator with the aplha coefficient on the data to smooth:
let filteredGustArray = [];
let acc = 0;
data.data.forEach(dp => {
acc = lowPass(ALPHA, dp.gust, acc);
filteredGustArray.push(acc);
});
This produces an array containing the smoothed data.
Here is a representation of what it looks like, along with the data to smooth (raw data in red, smoothed data in blue):
The demo data are available
here.
Just run the script with
nodejs
like
$ node max.gust.js both > data.csv
This will produce a
csv
file you can then import into any spreadsheet program, to see the figure above.