Try It: Moving Average Noise Reduction
Try It: Moving Average Noise Reduction
animation
analytics-ml
data-quality
Try Change Window size from 5 to 15.
Observe Watch Moving average output, RMSE vs clean signal, and Latency with window size 15.
Explain Describe how changing window size from 5 affects noise reduction and latency.
Technical boundaries. The clean signal is a slow sine over 50 samples with deterministic noise and spikes of magnitude 10. The filter averages spikes into the result.
NoteTry It: Moving Average Noise Reduction
{
// Deterministic pseudo-random using sine
const prng = (i) => ((Math.sin(i * 127.1 + 311.7) * 43758.5453) % 1 + 1) % 1;
const n = 50;
// Clean signal: slow sine
const clean = Array.from({length: n}, (_, i) => 22 + 3 * Math.sin(i * 0.15));
// Add noise and spikes
const noisy = clean.map((v, i) => {
let val = v + (prng(i) - 0.5) * 2 * mavg_noiseLevel;
if (prng(i + 1000) < mavg_spikeRate) {
val += (prng(i + 2000) > 0.5 ? 1 : -1) * 10;
}
return val;
});
// Moving average filter
const maFiltered = [];
const buf = [];
for (let i = 0; i < n; i++) {
buf.push(noisy[i]);
if (buf.length > mavg_window) buf.shift();
maFiltered.push(buf.reduce((a, b) => a + b, 0) / buf.length);
}
const width = 620;
const height = 250;
const margin = {top: 30, right: 20, bottom: 35, left: 50};
const plotW = width - margin.left - margin.right;
const plotH = height - margin.top - margin.bottom;
const allV = [...noisy, ...maFiltered];
const yMin = Math.min(...allV) - 1;
const yMax = Math.max(...allV) + 1;
const xS = (i) => margin.left + (i / (n - 1)) * plotW;
const yS = (v) => margin.top + plotH - ((v - yMin) / (yMax - yMin)) * plotH;
const noisyPath = noisy.map((v, i) => `${i === 0 ? "M" : "L"}${xS(i).toFixed(1)},${yS(v).toFixed(1)}`).join(" ");
const maPath = maFiltered.map((v, i) => `${i === 0 ? "M" : "L"}${xS(i).toFixed(1)},${yS(v).toFixed(1)}`).join(" ");
const cleanPath = clean.map((v, i) => `${i === 0 ? "M" : "L"}${xS(i).toFixed(1)},${yS(v).toFixed(1)}`).join(" ");
// RMS error
let mse = 0;
for (let i = 0; i < n; i++) mse += (maFiltered[i] - clean[i]) ** 2;
const rmse = Math.sqrt(mse / n).toFixed(3);
return html`<div style="background: var(--bs-light, #f8f9fa); padding: 1rem; border-radius: 8px; color: var(--bs-body-color);">
<svg viewBox="0 0 ${width} ${height}" style="width:100%; max-width:${width}px; font-family: Arial, sans-serif;">
<text x="${width / 2}" y="16" text-anchor="middle" font-size="12" font-weight="bold" fill="var(--bs-body-color)">Moving Average (window=${mavg_window}): noise=${mavg_noiseLevel.toFixed(1)}, spike rate=${(mavg_spikeRate * 100).toFixed(0)}%</text>
<line x1="${margin.left}" y1="${margin.top}" x2="${margin.left}" y2="${margin.top + plotH}" stroke="var(--bs-body-color)" stroke-width="1"/>
<line x1="${margin.left}" y1="${margin.top + plotH}" x2="${margin.left + plotW}" y2="${margin.top + plotH}" stroke="var(--bs-body-color)" stroke-width="1"/>
${[yMin, (yMin + yMax) / 2, yMax].map(v => `<text x="${margin.left - 5}" y="${yS(v) + 4}" text-anchor="end" font-size="9" fill="var(--bs-body-color)">${v.toFixed(1)}</text><line x1="${margin.left}" y1="${yS(v)}" x2="${margin.left + plotW}" y2="${yS(v)}" stroke="#dee2e6" stroke-width="0.5"/>`).join("")}
<path d="${cleanPath}" fill="none" stroke="#16A085" stroke-width="1.5" stroke-dasharray="6,3"/>
<path d="${noisyPath}" fill="none" stroke="#7F8C8D" stroke-width="0.8" opacity="0.6"/>
<path d="${maPath}" fill="none" stroke="#E67E22" stroke-width="2"/>
<text x="${margin.left + plotW / 2}" y="${height - 5}" text-anchor="middle" font-size="10" fill="var(--bs-body-color)">Sample</text>
<rect x="${margin.left + 5}" y="${margin.top + 5}" width="155" height="50" fill="var(--bs-body-bg, white)" stroke="#dee2e6" rx="3"/>
<line x1="${margin.left + 10}" y1="${margin.top + 17}" x2="${margin.left + 28}" y2="${margin.top + 17}" stroke="#16A085" stroke-width="1.5" stroke-dasharray="6,3"/>
<text x="${margin.left + 32}" y="${margin.top + 21}" font-size="9" fill="var(--bs-body-color)">Clean signal</text>
<line x1="${margin.left + 10}" y1="${margin.top + 31}" x2="${margin.left + 28}" y2="${margin.top + 31}" stroke="#7F8C8D" stroke-width="0.8" opacity="0.6"/>
<text x="${margin.left + 32}" y="${margin.top + 35}" font-size="9" fill="var(--bs-body-color)">Noisy input</text>
<line x1="${margin.left + 10}" y1="${margin.top + 45}" x2="${margin.left + 28}" y2="${margin.top + 45}" stroke="#E67E22" stroke-width="2"/>
<text x="${margin.left + 32}" y="${margin.top + 49}" font-size="9" fill="var(--bs-body-color)">Moving average output</text>
</svg>
<p style="margin: 0.5rem 0 0 0; font-size: 0.9em;">
<strong>RMSE vs clean signal:</strong> <span style="color:#E67E22; font-weight:bold;">${rmse}</span> |
Latency: ${((mavg_window - 1) / 2).toFixed(1)} samples.
${mavg_spikeRate > 0.05 ? "Notice how spikes distort the moving average output -- this filter averages spikes into the result rather than removing them." : ""}
Try adding spikes to see the moving average's weakness.
</p>
</div>`;
}