miller/test/input/window2.mlr
John Kerl 77811a4788
Sliding window averages (#894)
* todo

* Neaten existing DSL sketch

* rebase on #893, and sketch

* code-complete

* build artifacts for previous commit

* replace - with _ in shift and slwin
2022-01-23 23:03:46 -05:00

52 lines
1.6 KiB
Text

# ================================================================
# Sliding average with window over m previous rows, current row, and
# n subsequent rows.
# ================================================================
begin {
# Input parameters
# They can do 'mlr put -s input_field_names=x,y ...'
@window_size_backward ??= 3;
@window_size_forward ??= 3;
@input_field_names ??= "x,y";
@input_field_names = splitnv(@input_field_names, ",");
# Initialization
@window_size = @window_size_backward + 1 + @window_size_forward;
@center_record_index = @window_size_backward; # index 0-up
@output_field_names = apply(@input_field_names, func(k,v) { return {v: v . "_avg"}});
@window_records = {};
# dump;
}
# Slide the windows, then update them with new data
for (i = 1; i < @window_size; i+=1) {
@window_records[i-1] = @window_records[i]
}
@window_records[@window_size-1] = $*;
# Compute the averages
denominator = @window_size;
if (NR < @window_size) {
denominator = NR
}
sums = {};
for (_, name in @input_field_names) {
sums[name] = 0.0;
for (i = 0; i < denominator; i+=1) {
# Windows are filled in from the end, not the beginning, so index backward
int j = @window_size - 1 - i;
sums[name] += float(@window_records[j][name]);
}
}
# Emit the record from the window center, with averages attached to it
if (NR > @center_record_index) {
output_record = @window_records[@center_record_index];
for (_, name in @input_field_names) {
output_record[@output_field_names[name]] = sums[name] / denominator;
}
emit output_record
}