Try It: Filter Window Size Calculator
Try Change Window size (N) from 5 to 50.
Observe Compare Output noise (sigma / sqrt(N)), Noise reduction, and Latency ((N-1)/2 samples) at N = 50.
Explain Explain why increasing N from 5 reduces output noise and increases latency.
Technical boundaries. Output noise uses sigma / sqrt(N), and latency uses (N-1)/2 samples divided by sample rate. Assessment uses latency seconds.
Try It: Filter Window Size Calculator
viewof filterWindowSize = Inputs.range([2, 50], {
value: 5, step: 1,
label: "Window size (N)"
})
viewof filterNoiseStdDev = Inputs.range([0.1, 10.0], {
value: 2.0, step: 0.1,
label: "Input noise std dev (units)"
})
viewof filterSampleRate = Inputs.range([0.1, 100], {
value: 1.0, step: 0.1,
label: "Sample rate (Hz)"
})
{
const N = filterWindowSize;
const sigma = filterNoiseStdDev;
const fs = filterSampleRate;
const filteredNoise = sigma / Math.sqrt(N);
const reductionPct = ((1 - 1 / Math.sqrt(N)) * 100).toFixed(1);
const latencySamples = (N - 1) / 2;
const latencySeconds = latencySamples / fs;
return html`<div style="background: var(--bs-light, #f8f9fa); padding: 1rem; border-radius: 8px; margin-top: 0.5rem; color: var(--bs-body-color);">
<h4 style="margin-top:0; color: var(--bs-body-color);">Moving Average Filter Performance</h4>
<table style="width:100%; border-collapse: collapse; color: var(--bs-body-color);">
<tr style="border-bottom: 2px solid var(--bs-body-color);">
<th style="text-align:left; padding: 4px;">Metric</th>
<th style="text-align:right; padding: 4px;">Value</th>
</tr>
<tr style="border-bottom: 1px solid #dee2e6;">
<td style="padding: 4px;">Input noise</td>
<td style="text-align:right; padding: 4px;">${sigma.toFixed(1)} units</td>
</tr>
<tr style="border-bottom: 1px solid #dee2e6;">
<td style="padding: 4px;">Output noise (sigma / sqrt(N))</td>
<td style="text-align:right; padding: 4px;"><strong>${filteredNoise.toFixed(2)} units</strong></td>
</tr>
<tr style="border-bottom: 1px solid #dee2e6;">
<td style="padding: 4px;">Noise reduction</td>
<td style="text-align:right; padding: 4px;">${reductionPct}%</td>
</tr>
<tr style="border-bottom: 1px solid #dee2e6;">
<td style="padding: 4px;">Latency ((N-1)/2 samples)</td>
<td style="text-align:right; padding: 4px;">${latencySamples.toFixed(1)} samples = ${latencySeconds.toFixed(2)}s</td>
</tr>
</table>
<p style="margin-top: 0.5rem; margin-bottom:0;"><strong>Assessment:</strong> ${latencySeconds < 1
? "Excellent - sub-second latency suitable for real-time control."
: latencySeconds < 10
? "Good - acceptable for monitoring applications."
: "High latency - consider a smaller window for time-sensitive applications."}</p>
</div>`;
}
Back to the chapter