1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
# ================================================================
# 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
}
|