viewof comb_medWindow = Inputs.range([3, 11], {
value: 5, step: 2,
label: "Stage 1: Median window (odd)"
})
viewof comb_expAlpha = Inputs.range([0.05, 0.95], {
value: 0.3, step: 0.05,
label: "Stage 2: Exp smoothing alpha"
})
viewof comb_spikeRate = Inputs.range([0, 0.2], {
value: 0.1, step: 0.01,
label: "Spike probability"
})
viewof comb_noiseAmp = Inputs.range([0, 3], {
value: 1.0, step: 0.1,
label: "Gaussian noise amplitude"
})Two-Stage Filter Pipeline
Two-Stage Filter Pipeline
animation
analytics-ml
data-quality
Try Change Stage 1: Median window (odd) from 5 to 11.
Observe Watch RMSE for Exp-only and Combined after pressing Play.
Explain Describe how Combined: Median + Exp responds to step changes at 20 and 40.
Technical boundaries. The tool generates 60 samples with step changes, a sine signal, noise, and spikes. Exponential smoothing uses the median output; Exp-only uses the noisy signal.
NoteTwo-Stage Filter Pipeline
{
const prng3 = (i) => ((Math.sin(i * 83.7 + 419.3) * 27182.8182) % 1 + 1) % 1;
const n = 60;
const mw = comb_medWindow % 2 === 0 ? comb_medWindow + 1 : comb_medWindow;
// Clean signal with step + sine
const clean = Array.from({length: n}, (_, i) => {
const base = i < 20 ? 22 : i < 40 ? 25 : 22;
return base + 1.2 * Math.sin(i * 0.25);
});
// Corrupt signal
const noisy = clean.map((v, i) => {
let val = v + (prng3(i) - 0.5) * 2 * comb_noiseAmp;
if (prng3(i + 300) < comb_spikeRate) {
val += (prng3(i + 600) > 0.5 ? 1 : -1) * 12;
}
return val;
});
// Stage 1: Median only
const medBuf2 = [];
const afterMedian = noisy.map(v => {
medBuf2.push(v);
if (medBuf2.length > mw) medBuf2.shift();
const sorted = medBuf2.slice().sort((a, b) => a - b);
const m = Math.floor(sorted.length / 2);
return sorted.length % 2 === 0 ? (sorted[m - 1] + sorted[m]) / 2 : sorted[m];
});
// Stage 2: Exponential smoothing on median output
const combined = [];
let sm = afterMedian[0];
for (let i = 0; i < n; i++) {
sm = comb_expAlpha * afterMedian[i] + (1 - comb_expAlpha) * sm;
combined.push(sm);
}
// Also compute exp-only (no median stage) for comparison
const expOnly = [];
let se = noisy[0];
for (let i = 0; i < n; i++) {
se = comb_expAlpha * noisy[i] + (1 - comb_expAlpha) * se;
expOnly.push(se);
}
const width = 620;
const height = 300;
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, ...combined, ...expOnly];
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 combPath = combined.map((v, i) => `${i === 0 ? "M" : "L"}${xS(i).toFixed(1)},${yS(v).toFixed(1)}`).join(" ");
const expOnlyPath = expOnly.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(" ");
let combErr = 0, expErr = 0;
for (let i = 0; i < n; i++) {
combErr += (combined[i] - clean[i]) ** 2;
expErr += (expOnly[i] - clean[i]) ** 2;
}
const combRmse = Math.sqrt(combErr / n).toFixed(3);
const expRmse = Math.sqrt(expErr / 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)">Combined Filter: Median(${mw}) then Exp(alpha=${comb_expAlpha.toFixed(2)})</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" stroke-dasharray="6,3"/>
<path d="${noisyPath}" fill="none" stroke="#7F8C8D" stroke-width="0.6" opacity="0.4"/>
<path d="${expOnlyPath}" fill="none" stroke="#E74C3C" stroke-width="1.2" stroke-dasharray="3,2"/>
<path d="${combPath}" fill="none" stroke="#2C3E50" stroke-width="2.5"/>
<text x="${margin.left + plotW / 2}" y="${height - 5}" text-anchor="middle" font-size="10" fill="var(--bs-body-color)">Sample (signal has step changes at 20 and 40)</text>
<rect x="${margin.left + 5}" y="${margin.top + 5}" width="185" height="66" 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" 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.6" opacity="0.4"/>
<text x="${margin.left + 32}" y="${margin.top + 35}" font-size="9" fill="var(--bs-body-color)">Noisy + spikes</text>
<line x1="${margin.left + 10}" y1="${margin.top + 45}" x2="${margin.left + 28}" y2="${margin.top + 45}" stroke="#E74C3C" stroke-width="1.2" stroke-dasharray="3,2"/>
<text x="${margin.left + 32}" y="${margin.top + 49}" font-size="9" fill="var(--bs-body-color)">Exp smoothing only (no median)</text>
<line x1="${margin.left + 10}" y1="${margin.top + 59}" x2="${margin.left + 28}" y2="${margin.top + 59}" stroke="#2C3E50" stroke-width="2.5"/>
<text x="${margin.left + 32}" y="${margin.top + 63}" font-size="9" fill="var(--bs-body-color)">Combined: Median + Exp</text>
</svg>
<p style="margin: 0.5rem 0 0 0; font-size: 0.9em;">
<strong>RMSE:</strong>
Exp-only = <span style="color:#E74C3C; font-weight:bold;">${expRmse}</span> |
Combined = <span style="color:#2C3E50; font-weight:bold;">${combRmse}</span>
${parseFloat(combRmse) < parseFloat(expRmse) ? " -- The two-stage pipeline outperforms single exponential smoothing." : " -- Similar performance; try increasing spike rate to see the benefit of the median stage."}
The median stage removes spikes that would otherwise corrupt the exponential smoother's state.
</p>
</div>`;
}