diff --git a/docs/results/2026-11-22-calibration/baseline.txt b/docs/results/2026-11-22-calibration/baseline.txt new file mode 100644 index 0000000..0234601 --- /dev/null +++ b/docs/results/2026-11-22-calibration/baseline.txt @@ -0,0 +1,8 @@ +Baseline: 2026-11-22-live-113837, 11:45 to 11:48 local time (UTC+2), empty room +BLE readings: 1819 (10.1/s), devices: 17 +Strongest device median: -86 dBm; devices with median above -80 dBm: 0 +All readings: median -88 dBm, 99th percentile -70 dBm, max -68 dBm +Readings by manufacturer: 0x0006 1584, none 134, 0x0942 83, 0x0CCB 16, 0x079A 1, 0x0075 1 +Devices by median (no addresses): -86 dBm/718 adv/0x0006, -87 dBm/471 adv/0x0006, -88 dBm/24 adv/none, -89 dBm/1 adv/none, -91 dBm/83 adv/0x0942, -94 dBm/33 adv/none, -95 dBm/2 adv/0x0006, -96 dBm/39 adv/none, -96 dBm/16 adv/0x0CCB, -96 dBm/37 adv/none, -97 dBm/373 adv/0x0006, -98 dBm/1 adv/0x0075, -100 dBm/1 adv/0x079A, -100 dBm/6 adv/0x0006, -102 dBm/9 adv/0x0006, -102 dBm/1 adv/0x0006, -104 dBm/4 adv/0x0006 +Spectrum messages: 162, sweeps per message (median): 43 +Channels active in >= 2% of sweeps: [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 47, 48, 49, 50, 51, 52, 53, 54, 55] diff --git a/docs/results/2026-11-22-calibration/make_summary.py b/docs/results/2026-11-22-calibration/make_summary.py new file mode 100644 index 0000000..8cef699 --- /dev/null +++ b/docs/results/2026-11-22-calibration/make_summary.py @@ -0,0 +1,164 @@ +"""Summary table and poster graph for the 2026-11-22 calibration sessions. + +Run from the repo root (needs the raw sessions in data/raw/, which stay on the laptop): + python docs/results/2026-11-22-calibration/make_summary.py + +Writes summary.csv, baseline.txt and rssi_vs_distance.png next to this file. +No hashed addresses are written: each row is one device at one distance. +""" + +from __future__ import annotations + +import json +import sys +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt +import pandas as pd + +sys.path.insert(0, ".") +from ml.features import CHANNEL_COLS, load_ble, load_spectrum # noqa: E402 + +RAW = Path("data/raw") +OUT = Path(__file__).resolve().parent +CAIRO = timezone(timedelta(hours=2)) + +# model label in session.json -> name on the graph +DEVICES = { + "phone B": "Phone B (iPhone)", + "phone B class": "Phone B class (iPhone)", + "phone C class": "Phone C class (Android, +1 dBm, 100 ms)", + "earpiece B class": "Earpiece B class (Oraimo, case open)", +} +BASELINE = ("2026-11-22-live-113837", (11, 45), (11, 48)) + +# Brand tokens (docs/brand/tokens.json, light theme). +INK, INK_MUTED, LINE, PAPER = "#0E1726", "#4B5871", "#D3DAE4", "#FFFFFF" +SOFT = {"near": "#FBE2E3", "mid": "#FBEED3", "far": "#E2EBFA"} +BAND_INK = {"near": "#CC2F3B", "mid": "#B87400", "far": "#2563C9"} +BAND_EDGES = {"near": (0.0, 1.0), "mid": (1.0, 2.0), "far": (2.0, 3.75)} +STYLE = { + "phone B": dict(color="#15243F", marker="o", ls="-"), + "phone B class": dict(color="#0B7A68", marker="s", ls="-"), + "phone C class": dict(color="#75839A", marker="^", ls="--"), + "earpiece B class": dict(color="#6B3FA0", marker="D", ls="-."), +} + + +def calibration_rows() -> pd.DataFrame: + rows = [] + for d in sorted(RAW.glob("2026-11-22-calib-*")): + if "incomplete" in d.name or not (d / "session.json").exists(): + continue + meta = json.loads((d / "session.json").read_text(encoding="utf-8")) + ble = load_ble(d) + secs = (ble["ts"].max() - ble["ts"].min()) / 1000 + for addr, lab in meta["devices"].items(): + if lab.get("model") not in DEVICES: + continue + t = ble[ble["addr"] == addr] + rows.append( + { + "device": lab["model"], + "type": lab["type"], + "state": lab.get("state", ""), + "distance_m": lab["distance_m"], + "band": lab["band"], + "session_s": round(secs), + "readings": len(t), + "readings_per_s": round(len(t) / secs, 2), + "rssi_median": t["rssi"].median(), + "rssi_p25": t["rssi"].quantile(0.25), + "rssi_p75": t["rssi"].quantile(0.75), + "rssi_max": t["rssi"].max(), + "mfr": t["mfr"].mode().iat[0] or "none", + } + ) + return pd.DataFrame(rows).sort_values(["device", "distance_m"]).reset_index(drop=True) + + +def baseline_summary() -> tuple[str, float]: + name, (h0, m0), (h1, m1) = BASELINE + start = int(datetime(2026, 11, 22, h0, m0, tzinfo=CAIRO).timestamp() * 1000) + end = int(datetime(2026, 11, 22, h1, m1, tzinfo=CAIRO).timestamp() * 1000) + ble, spec = load_ble(RAW / name), load_spectrum(RAW / name) + ble = ble[(ble["ts"] >= start) & (ble["ts"] < end)] + spec = spec[(spec["ts"] >= start) & (spec["ts"] < end)] + per_dev = ble.groupby("addr").agg(n=("rssi", "size"), med=("rssi", "median"), mfr=("mfr", "first")) + per_dev = per_dev.sort_values("med", ascending=False) + frac = spec[CHANNEL_COLS].sum() / spec["sweeps"].sum() + active = [int(c[2:]) for c in frac[frac >= 0.02].index] + lines = [ + f"Baseline: {name}, 11:{m0:02d} to 11:{m1:02d} local time (UTC+2), empty room", + f"BLE readings: {len(ble)} ({len(ble) / 180:.1f}/s), devices: {len(per_dev)}", + f"Strongest device median: {per_dev['med'].max():.0f} dBm; devices with median above -80 dBm: {(per_dev['med'] > -80).sum()}", + f"All readings: median {ble['rssi'].median():.0f} dBm, 99th percentile {ble['rssi'].quantile(0.99):.0f} dBm, max {ble['rssi'].max()} dBm", + "Readings by manufacturer: " + + ", ".join(f"{k or 'none'} {v}" for k, v in ble["mfr"].value_counts().items()), + "Devices by median (no addresses): " + + ", ".join(f"{r.med:.0f} dBm/{r.n} adv/{r.mfr or 'none'}" for r in per_dev.itertuples()), + f"Spectrum messages: {len(spec)}, sweeps per message (median): {spec['sweeps'].median():.0f}", + f"Channels active in >= 2% of sweeps: {active}", + ] + return "\n".join(lines) + "\n", float(per_dev["med"].max()) + + +def plot(df: pd.DataFrame, baseline_top: float) -> None: + plt.rcParams.update( + { + "font.family": ["IBM Plex Sans", "Segoe UI", "DejaVu Sans"], + "font.size": 15, + "axes.edgecolor": INK_MUTED, + "axes.labelcolor": INK, + "xtick.color": INK_MUTED, + "ytick.color": INK_MUTED, + } + ) + fig, ax = plt.subplots(figsize=(12, 8.2), dpi=200) + fig.patch.set_facecolor(PAPER) + ymin, ymax = -104, -55 + for band, (x0, x1) in BAND_EDGES.items(): + ax.axvspan(x0, x1, color=SOFT[band], zorder=0, lw=0) + ax.text((x0 + x1) / 2, ymax - 1.5, band.upper(), ha="center", va="top", + fontsize=15, fontweight="bold", color=BAND_INK[band]) + ax.axhline(baseline_top, color=INK_MUTED, lw=1.5, ls=":", zorder=1) + ax.text(3.72, baseline_top + 0.6, "strongest device in the empty room", ha="right", + va="bottom", fontsize=12, color=INK_MUTED) + for model, label in DEVICES.items(): + d = df[df["device"] == model] + s = STYLE[model] + ax.plot(d["distance_m"], d["rssi_median"], color=s["color"], ls=s["ls"], lw=2.5, + marker=s["marker"], ms=9, label=label, zorder=3) + ax.set_xlim(0, 3.75) + ax.set_ylim(ymin, ymax) + ax.set_xticks([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]) + ax.set_xlabel("Distance from the unit (m)") + ax.set_ylabel("Median RSSI over 60 s (dBm)") + ax.grid(axis="y", color=LINE, lw=0.8, zorder=0) + for side in ("top", "right"): + ax.spines[side].set_visible(False) + ax.legend(loc="upper center", bbox_to_anchor=(0.5, -0.13), ncol=2, frameon=False, fontsize=13) + ax.set_title("Signal strength vs distance, unit N1, 2026-11-22", loc="left", + fontsize=19, fontweight="bold", color=INK, pad=14) + fig.text(0.01, 0.01, "Smartwatch (Huawei, connected to its phone) was not heard at 0.5 m and is not shown. " + "Phone B class was not heard at 2 m.", fontsize=11, color=INK_MUTED) + fig.tight_layout(rect=(0, 0.03, 1, 1)) + fig.savefig(OUT / "rssi_vs_distance.png", facecolor=PAPER) + + +def main() -> None: + df = calibration_rows() + df.to_csv(OUT / "summary.csv", index=False) + text, top = baseline_summary() + (OUT / "baseline.txt").write_text(text, encoding="utf-8") + plot(df, top) + print(df.to_string(index=False)) + print(text) + + +if __name__ == "__main__": + main() diff --git a/docs/results/2026-11-22-calibration/report.md b/docs/results/2026-11-22-calibration/report.md new file mode 100644 index 0000000..e4812ef --- /dev/null +++ b/docs/results/2026-11-22-calibration/report.md @@ -0,0 +1,62 @@ +# Calibration: RSSI vs distance, four devices + +Date: 2026-11-22. Unit N1 at the edge of the fab lab bench. One 60 s session per device per distance, device on the desk, screen on (phones) or case open (earpiece). Bands as in `docs/data-protocol.md`: near under 1 m, mid 1 to 2 m, far 2 to 3.5 m. Times are local (UTC+2). + +Files in this folder: + +- `summary.csv`: one row per device and distance (readings, median, quartiles, max, manufacturer). No addresses. +- `baseline.txt`: the empty-room baseline, 11:45 to 11:48. +- `rssi_vs_distance.png`: the poster graph. +- `make_summary.py`: makes all three from `data/raw/` (run from the repo root). + +![Median RSSI vs distance](rssi_vs_distance.png) + +## Devices + +| Device | What it is | Manufacturer ID | Heard to | +| --- | --- | --- | --- | +| Phone B | Fab lab iPhone | `0x004C` (Apple) | 3.5 m | +| Phone B class | A classmate's iPhone | `0x004C` (Apple) | 1.5 m | +| Phone C class | Android phone advertising from nRF Connect at +1 dBm, 100 ms interval, no manufacturer data | none | 3.5 m (weak) | +| Earpiece B class | Oraimo buds, case open | `0x0942` (Oraimo) | 3.5 m | +| Watch A class | Huawei smartwatch, connected to its phone | none seen | not heard | + +## Median RSSI (dBm) + +| Distance | Band | Phone B | Phone B class | Phone C class | Earpiece B class | +| --- | --- | --- | --- | --- | --- | +| 0.5 m | near | -64 | -61 | -65 | -61 | +| 1.0 m | mid | -66 | -74 | -78 | -72 | +| 1.5 m | mid | -78 | -86 | -95 | -85 | +| 2.0 m | mid | -79 | not heard | -95 | -78 | +| 2.5 m | far | -78 | | -98 | -83 | +| 3.0 m | far | -79 | | -92 (8 readings) | -77 | +| 3.5 m | far | -79 | | -99 | -80 | + +Readings per second at 0.5 m: phone B 2.8, phone B class 2.7, phone C class 6.4, earpiece 1.5. + +## Baseline (empty room, 11:45 to 11:48) + +- 10.1 BLE readings a second from 17 devices. The strongest had a median of -86 dBm. No device had a median above -80 dBm, and no single reading was above -68 dBm. +- One Oraimo (`0x0942`) device was heard in the baseline at a median of -91 dBm. It may be the same buds in their closed case somewhere in the room. Check where the buds were before using this baseline for the earpiece. +- Spectrum: 43 sweeps per message. Channels active in at least 2% of sweeps were 8 to 28 and 47 to 55 (2408 to 2428 MHz and 2447 to 2455 MHz), two fixed blocks, which is the Wi-Fi shape. Nothing else was active. + +## Watch + +The Huawei watch was not heard while it was connected to its phone: no `0x027D` (Huawei) advertisement in any of the four watch runs at 0.5 m (one complete, three stopped early). A connected watch usually stops advertising. Next test: the same 0.5 m session with the watch's Bluetooth link dropped (phone Bluetooth off), and a spectrum-only check of whether its link traffic shows on the nRF scan. + +## What this means + +1. **Near is easy, mid and far are not, from RSSI alone.** Every device drops 13 to 30 dB between 0.5 m and 1.5 m. Past 1.5 m, phone B and the earpiece stay flat around -77 to -83 dBm, so 2 m and 3.5 m look the same. The distance band model will need more than the median (spread, readings per second, spectrum) and more sessions, and design requirement 3 (80% band accuracy) is at risk for mid vs far. +2. **Devices differ more than distances do.** At 1.5 m the four devices span 17 dB (-78 to -95). A single RSSI threshold per band will not work across devices. The type model's answer should feed the band decision. +3. **Phone C class is the weakest phone even at +1 dBm.** Past 1 m it sits below the strongest empty-room device. Its 3 m session has only 8 readings against 106 at 3.5 m, so that point is not trustworthy. Repeat 3 m. +4. **Phone B class drops out at 2 m.** Two 2 m runs were stopped and the complete one has no Apple address stronger than -94 dBm. The iPhone may have changed its address or gone quiet with the screen locked. Repeat 2 to 3.5 m with the screen kept awake. +5. **Range (design requirement 6, 3 m).** Phone B and the earpiece were heard at 3.5 m. Phone C was heard weakly. Phone B class and the watch were not. Not yet a pass. + +These are single sessions per point, so no held-out accuracy can be computed yet. + +## Changes from this run + +- `0x0942` (Oraimo) added to `KNOWN_MFR` in `ml/features.py`, so the earpiece gets its own manufacturer column. + +Sessions left out: the `-incomplete-*` folders (stopped early), `phone-phone-a-0.5m` and `earpiece-earpiece-a-0.5m` (no `session.json`), and `phone-phone-b-class-2m` (target not found). diff --git a/docs/results/2026-11-22-calibration/rssi_vs_distance.png b/docs/results/2026-11-22-calibration/rssi_vs_distance.png new file mode 100644 index 0000000..9a8e98a Binary files /dev/null and b/docs/results/2026-11-22-calibration/rssi_vs_distance.png differ diff --git a/docs/results/2026-11-22-calibration/summary.csv b/docs/results/2026-11-22-calibration/summary.csv new file mode 100644 index 0000000..f182cd0 --- /dev/null +++ b/docs/results/2026-11-22-calibration/summary.csv @@ -0,0 +1,25 @@ +device,type,state,distance_m,band,session_s,readings,readings_per_s,rssi_median,rssi_p25,rssi_p75,rssi_max,mfr +earpiece B class,earpiece,case open,0.5,near,59,87,1.46,-61.0,-70.0,-59.0,-43,0x0942 +earpiece B class,earpiece,case open,1.0,mid,60,88,1.48,-72.0,-78.25,-68.0,-49,0x0942 +earpiece B class,earpiece,case open,1.5,mid,60,81,1.36,-85.0,-88.0,-81.0,-67,0x0942 +earpiece B class,earpiece,case open,2.0,mid,60,60,0.99,-78.0,-80.0,-66.0,-59,0x0942 +earpiece B class,earpiece,case open,2.5,far,60,73,1.22,-83.0,-86.0,-74.0,-66,0x0942 +earpiece B class,earpiece,case open,3.0,far,60,68,1.13,-77.0,-80.0,-68.75,-60,0x0942 +earpiece B class,earpiece,case open,3.5,far,59,50,0.84,-80.0,-82.75,-76.5,-64,0x0942 +phone B,phone,screen on,0.5,near,60,167,2.78,-64.0,-66.0,-60.0,-46,0x004C +phone B,phone,screen on,1.0,mid,60,157,2.61,-66.0,-70.0,-56.0,-47,0x004C +phone B,phone,screen on,1.5,mid,60,159,2.65,-78.0,-82.0,-70.5,-55,0x004C +phone B,phone,screen on,2.0,mid,59,160,2.71,-79.0,-82.0,-75.0,-55,0x004C +phone B,phone,screen on,2.5,far,60,164,2.72,-78.0,-83.0,-74.0,-57,0x004C +phone B,phone,screen on,3.0,far,60,141,2.35,-79.0,-84.0,-75.0,-58,0x004C +phone B,phone,screen on,3.5,far,60,133,2.2,-79.0,-88.0,-76.0,-60,0x004C +phone B class,phone,screen on,0.5,near,60,163,2.72,-61.0,-63.0,-60.0,-46,0x004C +phone B class,phone,screen on,1.0,mid,59,163,2.76,-74.0,-76.0,-62.0,-57,0x004C +phone B class,phone,screen on,1.5,mid,60,145,2.42,-86.0,-90.0,-80.0,-70,0x004C +phone C class,phone,screen on,0.5,near,59,377,6.35,-65.0,-67.0,-53.0,-47,none +phone C class,phone,screen on,1.0,mid,60,380,6.39,-78.0,-80.0,-68.0,-61,none +phone C class,phone,screen on,1.5,mid,58,257,4.45,-95.0,-98.0,-87.0,-77,none +phone C class,phone,screen on,2.0,mid,58,137,2.36,-95.0,-100.0,-92.0,-79,none +phone C class,phone,screen on,2.5,far,61,193,3.17,-98.0,-100.0,-89.0,-80,none +phone C class,phone,screen on,3.0,far,59,8,0.13,-91.5,-92.25,-90.5,-88,none +phone C class,phone,screen on,3.5,far,60,106,1.75,-99.0,-100.0,-98.0,-86,none diff --git a/ml/features.py b/ml/features.py index 2bc289e..3fe8f5e 100644 --- a/ml/features.py +++ b/ml/features.py @@ -47,6 +47,7 @@ "0x0006": "microsoft", "0x038F": "xiaomi", "0x027D": "huawei", + "0x0942": "oraimo", } # ---------------------------------------------------------------------------