Smoothing the Signal
Stardate 55127.2 ยท UES Magnanimous
Briefing
The pattern Lt. Skree filed for long-term observation is real, and it is buried in jitter. Skree wants a moving average: for each position, the mean of that reading and the ones before it in a window of a given size, rounded to two decimal places.
Skree has anticipated the question about the beginning, where a full window does not exist yet, and answers it thus: use what you have. The first value averages one reading, the second averages two, and so on until the window is full. Skree considers this "the only defensible choice" and has already rejected two others in writing.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขA window of two over four readings
- โขA window of three, with the ramp-up at the start using what exists
- โขA window of one is the readings unchanged
- โขA window larger than the whole list averages everything so far at each step
- โขResults are rounded to two decimal places
Objective
Write moving_average(readings, window): return a list the same length as
readings, where each entry is the mean of that reading and up to
window - 1 readings before it, rounded to two decimal places.
Where fewer than window readings exist yet, average the ones that do.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Write moving_average(readings: &[f64], window: usize) -> Vec<f64>:
a vector the same length as readings, where each entry is the mean of that
reading and up to window - 1 readings before it, rounded to two
decimal places. Where fewer than window readings exist yet,
average the ones that do.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
For position i, average the readings from max(0, i - window + 1) up to and including i. Slicing makes that one line. Round each result to two places, and remember Rust rounds by multiplying, rounding, and dividing.
Debrief
The smoothed signal shows a slow rise over eleven days. Skree has looked at it for a long time, said "hm", and upgraded the file from "pending long-term observation" to "pending". Nobody knows what the difference is, and nobody has dared ask.