viewof emaAlpha = Inputs.range([0.05, 0.9], {value: 0.3, step: 0.05, label: "EMA Alpha (smoothing factor)"})
viewof txPower = Inputs.range([-75, -45], {value: -59, step: 1, label: "TX Power at 1m (dBm)"})
viewof pathLossN = Inputs.range([1.5, 4.0], {value: 2.5, step: 0.1, label: "Path Loss Exponent (n)"})
viewof simScenario = Inputs.select(["Approaching (far to near)", "Departing (near to far)", "Stationary with noise", "Walk-by pattern"], {value: "Approaching (far to near)", label: "Movement Scenario"})RSSI Smoothing and Zone Classification
RSSI Smoothing and Zone Classification
animation
bluetooth-ble
python
Try Keep Movement Scenario on Approaching (far to near) and raise EMA Alpha (smoothing factor) from 0.3. Compare the smoothed curve with the raw samples.
Observe With the default alpha of 0.3, Effective Window is 5.7 samples and 95% Response Time is 8.4s (at 1Hz). Final Zone reads IMMEDIATE for the default approaching sequence.
Explain After the first sample, EMA Alpha (smoothing factor) weights each new reading and the remaining weight keeps the previous smoothed value. The smoothed signal sets the zone, while TX Power at 1m (dBm) and Path Loss Exponent (n) set the distance estimate.
Technical boundaries. Each scenario is a fixed sequence of 15 samples, and the response-time display assumes one sample per second. Distance uses 10 raised to ((TX power − smoothed RSSI) / (10 × n)); zone selection instead uses strict thresholds above -55 and -70 dBm.
NoteTry It: RSSI Smoothing and Zone Classification
Experiment with EMA smoothing parameters and see how they affect proximity zone detection. Adjust the alpha value and watch the smoothed RSSI converge, then observe zone classification in real time.
emaSimResult = {
const scenarios = {
"Approaching (far to near)": [-80, -77, -74, -72, -69, -66, -63, -60, -57, -55, -52, -50, -48, -46, -44],
"Departing (near to far)": [-45, -47, -50, -52, -55, -58, -61, -64, -67, -70, -73, -75, -78, -80, -82],
"Stationary with noise": [-62, -58, -65, -60, -63, -67, -59, -64, -61, -66, -58, -63, -60, -65, -62],
"Walk-by pattern": [-80, -75, -68, -60, -52, -48, -45, -43, -45, -48, -53, -60, -68, -75, -82]
};
const rawSamples = scenarios[simScenario];
const steps = [];
let smoothed = rawSamples[0];
for (let i = 0; i < rawSamples.length; i++) {
if (i === 0) {
smoothed = rawSamples[0];
} else {
smoothed = emaAlpha * rawSamples[i] + (1 - emaAlpha) * smoothed;
}
const distance = Math.pow(10, (txPower - smoothed) / (10 * pathLossN));
let zone, zoneColor;
if (smoothed > -55) { zone = "IMMEDIATE"; zoneColor = "#E74C3C"; }
else if (smoothed > -70) { zone = "NEAR"; zoneColor = "#E67E22"; }
else { zone = "FAR"; zoneColor = "#3498DB"; }
steps.push({idx: i, raw: rawSamples[i], smoothed: Math.round(smoothed * 10) / 10, distance: Math.round(distance * 100) / 100, zone, zoneColor});
}
const chartW = 600;
const chartH = 200;
const padL = 50;
const padR = 20;
const padT = 20;
const padB = 30;
const plotW = chartW - padL - padR;
const plotH = chartH - padT - padB;
const minRSSI = -90;
const maxRSSI = -35;
const xStep = plotW / (steps.length - 1);
const yScale = (val) => padT + plotH - ((val - minRSSI) / (maxRSSI - minRSSI)) * plotH;
const rawPoints = steps.map((s, i) => `${padL + i * xStep},${yScale(s.raw)}`).join(" ");
const smoothPoints = steps.map((s, i) => `${padL + i * xStep},${yScale(s.smoothed)}`).join(" ");
const threshImm = yScale(-55);
const threshNear = yScale(-70);
const effWindow = (2 / emaAlpha - 1).toFixed(1);
const responseTime = (Math.log(0.05) / Math.log(1 - emaAlpha)).toFixed(1);
return html`<div style="font-family: Arial, sans-serif; background: #f8f9fa; border-radius: 8px; padding: 16px;">
<div style="display: flex; gap: 16px; margin-bottom: 12px; flex-wrap: wrap;">
<div style="background: white; border-radius: 6px; padding: 10px; flex: 1; min-width: 120px; border-top: 3px solid #2C3E50;">
<div style="font-size: 11px; color: #7F8C8D;">Effective Window</div>
<div style="font-size: 18px; font-weight: bold; color: #2C3E50;">${effWindow} samples</div>
</div>
<div style="background: white; border-radius: 6px; padding: 10px; flex: 1; min-width: 120px; border-top: 3px solid #16A085;">
<div style="font-size: 11px; color: #7F8C8D;">95% Response Time</div>
<div style="font-size: 18px; font-weight: bold; color: #16A085;">${responseTime}s (at 1Hz)</div>
</div>
<div style="background: white; border-radius: 6px; padding: 10px; flex: 1; min-width: 120px; border-top: 3px solid #E67E22;">
<div style="font-size: 11px; color: #7F8C8D;">Final Zone</div>
<div style="font-size: 18px; font-weight: bold; color: ${steps[steps.length - 1].zoneColor};">${steps[steps.length - 1].zone}</div>
</div>
</div>
<svg viewBox="0 0 ${chartW} ${chartH}" style="width: 100%; max-width: 600px; background: white; border-radius: 6px; border: 1px solid #e0e0e0;">
<rect x="${padL}" y="${threshImm}" width="${plotW}" height="${threshNear - threshImm}" fill="#E67E22" opacity="0.08"/>
<rect x="${padL}" y="${padT}" width="${plotW}" height="${threshImm - padT}" fill="#E74C3C" opacity="0.08"/>
<rect x="${padL}" y="${threshNear}" width="${plotW}" height="${padT + plotH - threshNear}" fill="#3498DB" opacity="0.08"/>
<line x1="${padL}" y1="${threshImm}" x2="${padL + plotW}" y2="${threshImm}" stroke="#E74C3C" stroke-dasharray="4,4" opacity="0.5"/>
<line x1="${padL}" y1="${threshNear}" x2="${padL + plotW}" y2="${threshNear}" stroke="#3498DB" stroke-dasharray="4,4" opacity="0.5"/>
<text x="${padL + 4}" y="${threshImm - 4}" font-size="9" fill="#E74C3C">-55 dBm (immediate)</text>
<text x="${padL + 4}" y="${threshNear - 4}" font-size="9" fill="#3498DB">-70 dBm (near/far)</text>
${steps.map((s, i) => `<line x1="${padL + i * xStep}" y1="${padT}" x2="${padL + i * xStep}" y2="${padT + plotH}" stroke="#eee" stroke-width="0.5"/>`).join("")}
<polyline points="${rawPoints}" fill="none" stroke="#7F8C8D" stroke-width="1.5" stroke-dasharray="3,3" opacity="0.6"/>
<polyline points="${smoothPoints}" fill="none" stroke="#2C3E50" stroke-width="2.5"/>
${steps.map((s, i) => `<circle cx="${padL + i * xStep}" cy="${yScale(s.raw)}" r="3" fill="#7F8C8D" opacity="0.5"/>`).join("")}
${steps.map((s, i) => `<circle cx="${padL + i * xStep}" cy="${yScale(s.smoothed)}" r="4" fill="${s.zoneColor}"/>`).join("")}
<text x="${padL - 5}" y="${padT + 5}" font-size="9" fill="#7F8C8D" text-anchor="end">${maxRSSI}</text>
<text x="${padL - 5}" y="${padT + plotH}" font-size="9" fill="#7F8C8D" text-anchor="end">${minRSSI}</text>
<text x="${padL + plotW / 2}" y="${chartH - 2}" font-size="10" fill="#7F8C8D" text-anchor="middle">Sample Index</text>
</svg>
<div style="display: flex; gap: 16px; margin-top: 8px; font-size: 11px; color: #7F8C8D;">
<span><span style="display: inline-block; width: 20px; height: 2px; background: #7F8C8D; vertical-align: middle; border-top: 1px dashed #7F8C8D;"></span> Raw RSSI</span>
<span><span style="display: inline-block; width: 20px; height: 3px; background: #2C3E50; vertical-align: middle;"></span> Smoothed RSSI</span>
</div>
<div style="margin-top: 12px; border: 1px solid #e0e0e0; border-radius: 4px;">
<table style="width: 100%; font-size: 12px; border-collapse: collapse;">
<thead><tr style="background: #2C3E50; color: white;">
<th style="padding: 4px 8px; text-align: left;">#</th>
<th style="padding: 4px 8px; text-align: right;">Raw RSSI</th>
<th style="padding: 4px 8px; text-align: right;">Smoothed</th>
<th style="padding: 4px 8px; text-align: right;">Distance</th>
<th style="padding: 4px 8px; text-align: center;">Zone</th>
</tr></thead>
<tbody>
${steps.map(s => html`<tr style="border-bottom: 1px solid #f0f0f0;">
<td style="padding: 3px 8px;">${s.idx + 1}</td>
<td style="padding: 3px 8px; text-align: right;">${s.raw} dBm</td>
<td style="padding: 3px 8px; text-align: right; font-weight: bold;">${s.smoothed} dBm</td>
<td style="padding: 3px 8px; text-align: right;">${s.distance}m</td>
<td style="padding: 3px 8px; text-align: center;"><span style="background: ${s.zoneColor}; color: white; padding: 1px 8px; border-radius: 8px; font-size: 11px;">${s.zone}</span></td>
</tr>`)}
</tbody>
</table>
</div>
</div>`;
}