mirror of
https://github.com/ruvnet/RuView
synced 2026-06-19 11:53:19 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 85417b84a6 | |||
| 4fc491dea5 | |||
| 4f6780f884 | |||
| 085af0c2be | |||
| f4e636aaa2 | |||
| 582d51aed6 | |||
| b31efe5e92 | |||
| f03b484dd1 | |||
| 7a75277d58 | |||
| 73ce72d39c | |||
| 4e9e92d713 | |||
| 28368b2c70 | |||
| 4bb8c3303f | |||
| b9778c5ad2 |
+2
-1
@@ -248,4 +248,5 @@ v1/src/sensing/mac_wifi
|
||||
**/node_modules/
|
||||
|
||||
# Local build scripts
|
||||
firmware/esp32-csi-node/build_firmware.bat
|
||||
firmware/esp32-csi-node/build_firmware.batdata/
|
||||
models/
|
||||
|
||||
@@ -7,32 +7,32 @@
|
||||
</p>
|
||||
|
||||
> **Beta Software** — Under active development. APIs and firmware may change. Known limitations:
|
||||
> - No pre-trained model weights are provided; training from scratch is required
|
||||
> - ESP32-C3 and original ESP32 are not supported (single-core, insufficient for CSI DSP)
|
||||
> - Single ESP32 deployments have limited spatial resolution — use 2+ nodes or add a [Cognitum Seed](https://cognitum.one) for best results
|
||||
> - Multi-person counting (n_persons) may overcount in single-occupancy scenarios ([#348](https://github.com/ruvnet/RuView/issues/348))
|
||||
> - Camera-free pose accuracy is limited (2.5% PCK@20) — camera-labeled data significantly improves accuracy
|
||||
>
|
||||
> Contributions and bug reports welcome at [Issues](https://github.com/ruvnet/RuView/issues).
|
||||
|
||||
## **See through walls with WiFi + Ai** ##
|
||||
## **See through walls with WiFi** ##
|
||||
|
||||
**Perceive the world through signals.** No cameras. No wearables. No Internet. Just physics.
|
||||
**Turn ordinary WiFi into a sensing system.** Detect people, measure breathing and heart rate, track movement, and monitor rooms — through walls, in the dark, with no cameras or wearables. Just physics.
|
||||
|
||||
### π RuView is an edge AI perception system that learns directly from the environment around it.
|
||||
### π RuView is a WiFi sensing platform that turns radio signals into spatial intelligence.
|
||||
|
||||
Instead of relying on cameras or cloud models, it observes whatever signals exist in a space such as WiFi, radio waves across the spectrum, motion patterns, vibration, sound, or other sensory inputs and builds an understanding of what is happening locally.
|
||||
Every WiFi router already fills your space with radio waves. When people move, breathe, or even sit still, they disturb those waves in measurable ways. RuView captures these disturbances using Channel State Information (CSI) from low-cost ESP32 sensors and turns them into actionable data: who's there, what they're doing, and whether they're okay.
|
||||
|
||||
Built on top of [RuVector](https://github.com/ruvnet/ruvector/) Self Learning Vector Memory system and [Cognitum.One](https://Cognitum.One) , the project became widely known for its implementation of WiFi DensePose — a sensing technique first explored in academic research such as Carnegie Mellon University's *DensePose From WiFi* work. That research demonstrated that WiFi signals can be used to reconstruct human pose.
|
||||
**What it senses:**
|
||||
- **Presence and occupancy** — detect people through walls, count them, track entries and exits
|
||||
- **Vital signs** — breathing rate and heart rate, contactless, while sleeping or sitting
|
||||
- **Activity recognition** — walking, sitting, gestures, falls — from temporal CSI patterns
|
||||
- **Environment mapping** — RF fingerprinting identifies rooms, detects moved furniture, spots new objects
|
||||
- **Sleep quality** — overnight monitoring with sleep stage classification and apnea screening
|
||||
|
||||
RuView extends that concept into a practical edge system. By analyzing Channel State Information (CSI) disturbances caused by human movement, RuView reconstructs body position, breathing rate, heart rate, and presence in real time using physics-based signal processing and machine learning.
|
||||
Built on [RuVector](https://github.com/ruvnet/ruvector/) and [Cognitum Seed](https://cognitum.one), RuView runs entirely on edge hardware — an ESP32 mesh (as low as $9 per node) paired with a Cognitum Seed for persistent memory, cryptographic attestation, and AI integration. No cloud, no cameras, no internet required.
|
||||
|
||||
Unlike research systems that rely on synchronized cameras for training, RuView is designed to operate entirely from radio signals and self-learned embeddings at the edge.
|
||||
The system learns each environment locally using spiking neural networks that adapt in under 30 seconds, with multi-frequency mesh scanning across 6 WiFi channels that uses your neighbors' routers as free radar illuminators. Every measurement is cryptographically attested via an Ed25519 witness chain.
|
||||
|
||||
The system runs entirely on inexpensive hardware such as an ESP32 sensor mesh (as low as ~$1 per node). Small programmable edge modules analyze signals locally and learn the RF signature of a room over time, allowing the system to separate the environment from the activity happening inside it.
|
||||
|
||||
Because RuView learns in proximity to the signals it observes, it improves as it operates. Each deployment develops a local model of its surroundings and continuously adapts without requiring cameras, labeled data, or cloud infrastructure.
|
||||
|
||||
In practice this means ordinary environments gain a new kind of spatial awareness. Rooms, buildings, and devices begin to sense presence, movement, and vital activity using the signals that already fill the space.
|
||||
RuView also supports pose estimation (17 COCO keypoints via the WiFlow architecture), trained entirely without cameras using 10 sensor signals — a technique pioneered from the original *DensePose From WiFi* research at Carnegie Mellon University.
|
||||
|
||||
### Built for low-power edge applications
|
||||
|
||||
@@ -54,25 +54,39 @@ In practice this means ordinary environments gain a new kind of spatial awarenes
|
||||
> | **Heart rate** | Bandpass 0.8-2.0 Hz → zero-crossing BPM | 40-120 BPM |
|
||||
> | **Presence sensing** | Trained model + PIR fusion — 100% accuracy | 0.012 ms latency |
|
||||
> | **Through-wall** | Fresnel zone geometry + multipath modeling | Up to 5m depth |
|
||||
> | **Edge intelligence** | 8-dim feature vectors + RVF store on Cognitum Seed | $27 total BOM |
|
||||
> | **Edge intelligence** | 8-dim feature vectors + RVF store on Cognitum Seed | $140 total BOM |
|
||||
> | **Camera-free training** | 10 sensor signals, no labels needed | 84s on M4 Pro |
|
||||
> | **Multi-frequency mesh** | Channel hopping across 6 bands, neighbor APs as illuminators | 3x sensing bandwidth |
|
||||
|
||||
```bash
|
||||
# 30 seconds to live sensing — no toolchain required
|
||||
# Option 1: Docker (simulated data, no hardware needed)
|
||||
docker pull ruvnet/wifi-densepose:latest
|
||||
docker run -p 3000:3000 ruvnet/wifi-densepose:latest
|
||||
# Open http://localhost:3000
|
||||
|
||||
# Option 2: Live sensing with ESP32-S3 hardware ($9)
|
||||
# Flash firmware, provision WiFi, and start sensing:
|
||||
python -m esptool --chip esp32s3 --port COM9 --baud 460800 \
|
||||
write_flash 0x0 bootloader.bin 0x8000 partition-table.bin \
|
||||
0xf000 ota_data_initial.bin 0x20000 esp32-csi-node.bin
|
||||
python firmware/esp32-csi-node/provision.py --port COM9 \
|
||||
--ssid "YourWiFi" --password "secret" --target-ip 192.168.1.20
|
||||
|
||||
# Option 3: Full system with Cognitum Seed ($140)
|
||||
# ESP32 streams CSI → bridge forwards to Seed for persistent storage + kNN + witness chain
|
||||
node scripts/rf-scan.js --port 5006 # Live RF room scan
|
||||
node scripts/snn-csi-processor.js --port 5006 # SNN real-time learning
|
||||
node scripts/mincut-person-counter.js --port 5006 # Correct person counting
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> **CSI-capable hardware required.** Pose estimation, vital signs, and through-wall sensing rely on Channel State Information (CSI) — per-subcarrier amplitude and phase data that standard consumer WiFi does not expose. You need CSI-capable hardware (ESP32-S3 or a research NIC) for full functionality. Consumer WiFi laptops can only provide RSSI-based presence detection, which is significantly less capable.
|
||||
> **CSI-capable hardware recommended.** Presence, vital signs, through-wall sensing, and all advanced capabilities require Channel State Information (CSI) from an ESP32-S3 ($9) or research NIC. The Docker image runs with simulated data for evaluation. Consumer WiFi laptops provide RSSI-only presence detection.
|
||||
|
||||
> **Hardware options** for live CSI capture:
|
||||
>
|
||||
> | Option | Hardware | Cost | Full CSI | Capabilities |
|
||||
> |--------|----------|------|----------|-------------|
|
||||
> | **ESP32 + Cognitum Seed** (recommended) | ESP32-S3 + Cognitum Seed (Pi Zero 2 W) | ~$27 | Yes | Pose, breathing, heartbeat, motion, presence + persistent vector store, kNN search, witness chain, MCP proxy |
|
||||
> | **ESP32 + Cognitum Seed** (recommended) | ESP32-S3 + [Cognitum Seed](https://cognitum.one) | ~$140 | Yes | Pose, breathing, heartbeat, motion, presence + persistent vector store, kNN search, witness chain, MCP proxy |
|
||||
> | **ESP32 Mesh** | 3-6x ESP32-S3 + WiFi router | ~$54 | Yes | Pose, breathing, heartbeat, motion, presence |
|
||||
> | **Research NIC** | Intel 5300 / Atheros AR9580 | ~$50-100 | Yes | Full CSI with 3x3 MIMO |
|
||||
> | **Any WiFi** | Windows, macOS, or Linux laptop | $0 | No | RSSI-only: coarse presence and motion |
|
||||
@@ -81,16 +95,67 @@ docker run -p 3000:3000 ruvnet/wifi-densepose:latest
|
||||
>
|
||||
---
|
||||
|
||||
### What's New in v0.5.4
|
||||
### What's New in v0.5.5
|
||||
|
||||
<details open>
|
||||
<summary><strong>Advanced Sensing: SNN + MinCut + WiFlow + Multi-Frequency Mesh</strong></summary>
|
||||
|
||||
**v0.5.5 adds four new sensing capabilities** built on the [ruvector](https://github.com/ruvnet/ruvector) ecosystem:
|
||||
|
||||
| Capability | What it does | ADR |
|
||||
|-----------|-------------|-----|
|
||||
| **Spiking Neural Network** | Adapts to your room in <30s with STDP online learning — no labels, no batches, 16-160x less compute | [ADR-074](docs/adr/ADR-074-spiking-neural-csi-sensing.md) |
|
||||
| **MinCut Person Counting** | Stoer-Wagner min-cut on subcarrier correlation graph — **fixes #348** (was always 4, now correct) | [ADR-075](docs/adr/ADR-075-mincut-person-separation.md) |
|
||||
| **CNN Spectrogram Embeddings** | Treat CSI as a 64×20 image → 128-dim embedding for environment fingerprinting (0.95+ similarity) | [ADR-076](docs/adr/ADR-076-csi-spectrogram-embeddings.md) |
|
||||
| **WiFlow SOTA Architecture** | TCN + axial attention + pose decoder → 17 COCO keypoints, 1.8M params (881 KB at 4-bit) | [ADR-072](docs/adr/ADR-072-wiflow-architecture.md) |
|
||||
| **Multi-Frequency Mesh** | Channel hopping across 6 bands, neighbor WiFi as passive radar illuminators | [ADR-073](docs/adr/ADR-073-multifrequency-mesh-scan.md) |
|
||||
|
||||
```bash
|
||||
# Live RF room scan (spectrum visualization)
|
||||
node scripts/rf-scan.js --port 5006 --duration 30
|
||||
|
||||
# Correct person counting (fixes #348)
|
||||
node scripts/mincut-person-counter.js --port 5006
|
||||
|
||||
# SNN real-time adaptation
|
||||
node scripts/snn-csi-processor.js --port 5006
|
||||
|
||||
# CNN spectrogram embeddings
|
||||
node scripts/csi-spectrogram.js --replay data/recordings/*.csi.jsonl
|
||||
|
||||
# WiFlow 17-keypoint pose training
|
||||
node scripts/train-wiflow.js --data data/recordings/*.csi.jsonl
|
||||
|
||||
# Enable channel hopping on ESP32
|
||||
python firmware/esp32-csi-node/provision.py --port COM9 --hop-channels "1,6,11"
|
||||
```
|
||||
|
||||
**Validated benchmarks:**
|
||||
|
||||
| Metric | v0.5.4 | v0.5.5 |
|
||||
|--------|--------|--------|
|
||||
| Person counting | Broken (always 4) | **Correct** (MinCut, 24/24) |
|
||||
| WiFi channels | 1 | **6** (multi-freq hopping) |
|
||||
| Null subcarriers | 19% blocked | **16%** (frequency diversity) |
|
||||
| Pose model | 16K params (FC only) | **1.8M params** (WiFlow) |
|
||||
| Online adaptation | None | **<30s** (SNN STDP) |
|
||||
| Fingerprint dims | 8 | **128** (CNN spectrogram) |
|
||||
| Multi-node fusion | Average | **GATv2 attention** |
|
||||
| New scripts | 0 | **15+** |
|
||||
| New ADRs | 3 | **8** (069-076) |
|
||||
|
||||
</details>
|
||||
|
||||
### What's New in v0.5.4
|
||||
|
||||
<details>
|
||||
<summary><strong>Cognitum Seed Integration + Camera-Free Pose Training</strong></summary>
|
||||
|
||||
**v0.5.4 transforms RuView from a real-time sensing tool into a persistent edge AI system.** Your ESP32 now remembers what it senses, learns without cameras, and proves its data cryptographically.
|
||||
|
||||
| Capability | Details | Hardware |
|
||||
|-----------|---------|----------|
|
||||
| **Persistent vector store** | Every sensing event stored as searchable 8-dim vector in RVF format | ESP32 + [Cognitum Seed](https://cognitum.one) ($27) |
|
||||
| **Persistent vector store** | Every sensing event stored as searchable 8-dim vector in RVF format | ESP32 + [Cognitum Seed](https://cognitum.one) ($140) |
|
||||
| **kNN similarity search** | "Find the 10 most similar states to right now" — anomaly detection, fingerprinting | Cognitum Seed |
|
||||
| **Witness chain** | SHA-256 tamper-evident audit trail for every measurement (1,747 entries validated) | Cognitum Seed |
|
||||
| **Camera-free pose training** | 17 COCO keypoints from 10 sensor signals — PIR, RSSI triangulation, subcarrier asymmetry, vibration, BME280 | 2x ESP32 + Seed |
|
||||
@@ -119,18 +184,24 @@ node scripts/train-camera-free.js --data data/recordings/pretrain-*.csi.jsonl
|
||||
node scripts/benchmark-ruvllm.js --model models/csi-ruvllm
|
||||
```
|
||||
|
||||
**Validated benchmarks (M4 Pro):**
|
||||
**Benchmarks — validated on real hardware (Apple M4 Pro + ESP32-S3 + Cognitum Seed):**
|
||||
|
||||
| Metric | Value |
|
||||
|--------|-------|
|
||||
| Training time | 84.4s (2,360 augmented samples) |
|
||||
| Contrastive improvement | 33.9% |
|
||||
| Presence accuracy | 100% |
|
||||
| Inference latency | 0.012 ms |
|
||||
| Throughput | 171,472 emb/s |
|
||||
| Model size (4-bit) | 8 KB |
|
||||
| Skeleton violations | 0 / 100 frames |
|
||||
| Rust tests | 1,463 passed |
|
||||
| What we measured | Result | Why it matters |
|
||||
|-----------------|--------|---------------|
|
||||
| **Presence detection** | **100% accuracy** | Never misses a person, never false alarms |
|
||||
| **Person counting** | **24/24 correct** (MinCut) | Fixed the #1 user-reported issue |
|
||||
| **Inference speed** | **0.012 ms** per embedding | 83,000x faster than real-time |
|
||||
| **Throughput** | **171,472 embeddings/sec** | One Mac Mini handles 1,700+ ESP32 nodes |
|
||||
| **Training time** | **84 seconds** | From zero to trained model in under 2 minutes |
|
||||
| **Contrastive learning** | **33.9% improvement** | Model learns meaningful patterns from CSI |
|
||||
| **Model size** | **8 KB** (4-bit quantized) | Fits in ESP32 SRAM — no server needed |
|
||||
| **Skeleton physics** | **0 violations** in 100 frames | Every pose is anatomically valid |
|
||||
| **Pose keypoints** | **17 COCO keypoints** | Full body pose, no camera required |
|
||||
| **WiFi channels** | **6 simultaneous** | 3x more sensing data than single-channel |
|
||||
| **Online adaptation** | **<30 seconds** (SNN) | Learns a new room without retraining |
|
||||
| **Witness chain** | **2,547 entries** verified | Cryptographic proof every measurement is real |
|
||||
| **Test suite** | **1,463 tests passed** | Rock-solid foundation |
|
||||
| **Total hardware cost** | **$140** | ESP32 ($9) + [Cognitum Seed](https://cognitum.one) ($131) |
|
||||
|
||||
See [ADR-069](docs/adr/ADR-069-cognitum-seed-csi-pipeline.md), [ADR-071](docs/adr/ADR-071-ruvllm-training-pipeline.md), and the [Cognitum Seed tutorial](docs/tutorials/cognitum-seed-pretraining.md) for full details.
|
||||
|
||||
@@ -1117,7 +1188,8 @@ Download a pre-built binary — no build toolchain needed:
|
||||
|
||||
| Release | What's included | Tag |
|
||||
|---------|-----------------|-----|
|
||||
| [v0.5.4](https://github.com/ruvnet/RuView/releases/tag/v0.5.4-esp32) | **Latest** — Cognitum Seed integration ([ADR-069](docs/adr/ADR-069-cognitum-seed-csi-pipeline.md)), 8-dim feature vectors at 1 Hz, RVF vector store ingest, witness chain attestation, security hardening | `v0.5.4-esp32` |
|
||||
| [v0.5.5](https://github.com/ruvnet/RuView/releases/tag/v0.5.5-esp32) | **Latest** — SNN + MinCut (fixes #348) + CNN spectrogram + WiFlow 1.8M architecture + multi-freq mesh (6 channels) + graph transformer | `v0.5.5-esp32` |
|
||||
| [v0.5.4](https://github.com/ruvnet/RuView/releases/tag/v0.5.4-esp32) | Cognitum Seed integration ([ADR-069](docs/adr/ADR-069-cognitum-seed-csi-pipeline.md)), 8-dim feature vectors, RVF store, witness chain, security hardening | `v0.5.4-esp32` |
|
||||
| [v0.5.0](https://github.com/ruvnet/RuView/releases/tag/v0.5.0-esp32) | mmWave sensor fusion ([ADR-063](docs/adr/ADR-063-mmwave-sensor-fusion.md)), auto-detect MR60BHA2/LD2410, 48-byte fused vitals, all v0.4.3.1 fixes | `v0.5.0-esp32` |
|
||||
| [v0.4.3.1](https://github.com/ruvnet/RuView/releases/tag/v0.4.3.1-esp32) | Fall detection fix ([#263](https://github.com/ruvnet/RuView/issues/263)), 4MB flash ([#265](https://github.com/ruvnet/RuView/issues/265)), watchdog fix ([#266](https://github.com/ruvnet/RuView/issues/266)) | `v0.4.3.1-esp32` |
|
||||
| [v0.4.1](https://github.com/ruvnet/RuView/releases/tag/v0.4.1-esp32) | CSI build fix, compile guard, AMOLED display, edge intelligence ([ADR-057](docs/adr/ADR-057-firmware-csi-build-guard.md)) | `v0.4.1-esp32` |
|
||||
@@ -1166,7 +1238,7 @@ Nodes can also hop across WiFi channels (1, 6, 11) to increase sensing bandwidth
|
||||
|
||||
### Cognitum Seed integration (ADR-069)
|
||||
|
||||
Connect an ESP32 to a [Cognitum Seed](https://cognitum.one) (Pi Zero 2 W, ~$15) for persistent vector storage, kNN search, cryptographic witness chain, and AI-accessible MCP proxy:
|
||||
Connect an ESP32 to a [Cognitum Seed](https://cognitum.one) ($131) for persistent vector storage, kNN search, cryptographic witness chain, and AI-accessible MCP proxy:
|
||||
|
||||
```
|
||||
ESP32-S3 ($9) ──UDP──> Host bridge ──HTTPS──> Cognitum Seed ($15)
|
||||
|
||||
@@ -176,6 +176,20 @@ Channel hopping may cause the ESP32 to lose connection to the home AP (ruv.net o
|
||||
|
||||
3. **Wider bandwidth (HT40)**: using 40 MHz channels doubles subcarrier count per channel. Rejected because: (a) HT40 requires a secondary channel, reducing available channels for hopping; (b) many neighbor APs use HT20, so their illumination only covers 20 MHz.
|
||||
|
||||
## SNN Integration (ADR-074)
|
||||
|
||||
Multi-frequency scanning produces subcarrier data across 6 channels, creating temporal patterns that are well-suited for spiking neural network processing. ADR-074 introduces an SNN with STDP learning that consumes the multi-channel CSI stream.
|
||||
|
||||
**Key interactions with multi-frequency data:**
|
||||
|
||||
1. **Null diversity as SNN input**: subcarriers that are null on one channel but active on another produce a distinctive spike pattern (spikes only during certain channel dwells). STDP learns to associate these cross-channel patterns with specific objects or zones — something a single-channel SNN cannot do.
|
||||
|
||||
2. **Channel-interleaved temporal coding**: because each node dwells on 3 channels in a 750ms rotation, the SNN receives subcarrier data in a repeating temporal pattern (ch1 → ch2 → ch3 → ch1 ...). The SNN's LIF membrane dynamics integrate spikes across the rotation, naturally performing cross-channel fusion through temporal summation. A hidden neuron that receives spikes from subcarrier 15 on channel 1 AND subcarrier 15 on channel 6 will fire more strongly than one receiving either alone.
|
||||
|
||||
3. **Expanded input mode**: on the server (not constrained by ESP32 memory), the SNN can use 384 input neurons (6 channels x 64 subcarriers) instead of 128. This provides maximum spectral diversity per frame but requires ~150 KB of weight storage. The `snn-csi-processor.js` script supports this via the `--hidden` flag to scale the network.
|
||||
|
||||
4. **Illuminator fingerprinting**: different neighbor APs have different beamforming patterns and power levels. The SNN learns which subcarrier patterns belong to which illuminator, enabling it to distinguish AP-specific signatures from human-caused perturbations. This is especially useful for the NETGEAR dual-AP setup on channel 9, where two illuminators from different positions create stereo-like RF coverage.
|
||||
|
||||
## References
|
||||
|
||||
- ADR-018: CSI binary frame format
|
||||
@@ -183,5 +197,6 @@ Channel hopping may cause the ESP32 to lose connection to the home AP (ruv.net o
|
||||
- ADR-039: Edge processing pipeline
|
||||
- ADR-060: Channel override provisioning
|
||||
- ADR-069: Cognitum Seed CSI pipeline
|
||||
- ADR-074: Spiking neural network for CSI sensing
|
||||
- IEEE 802.11-2020, Section 21 (OFDM PHY)
|
||||
- ESP-IDF CSI Guide: https://docs.espressif.com/projects/esp-idf/en/v5.4/esp32s3/api-guides/wifi.html#wi-fi-channel-state-information
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
# ADR-074: Spiking Neural Network for CSI Sensing
|
||||
|
||||
| Field | Value |
|
||||
|-------------|--------------------------------------------|
|
||||
| **Status** | Proposed |
|
||||
| **Date** | 2026-04-02 |
|
||||
| **Authors** | ruv |
|
||||
| **Depends** | ADR-018 (binary frame), ADR-029 (channel hopping), ADR-069 (Cognitum Seed), ADR-073 (multi-frequency mesh) |
|
||||
|
||||
## Context
|
||||
|
||||
The current WiFi-DensePose CSI sensing pipeline uses two approaches for interpreting subcarrier data:
|
||||
|
||||
1. **Static thresholds** — presence detection fires when subcarrier variance exceeds a fixed value. This works in calibrated environments but fails when the RF landscape changes (furniture moved, new objects, temperature drift). Recalibration requires manual intervention or batch retraining.
|
||||
|
||||
2. **Batch-trained FC encoder** — the neural network in `wifi-densepose-nn` maps CSI frames to 8-dimensional feature vectors. It requires labeled training data, offline training epochs, and model deployment. The encoder cannot adapt to a new environment without collecting new data and retraining.
|
||||
|
||||
Neither approach handles online adaptation. When an ESP32 node is deployed in a new room, the first hours produce noisy, unreliable output until the thresholds are tuned or a model is trained. In disaster scenarios (ADR MAT), there is no time for calibration.
|
||||
|
||||
**Spiking Neural Networks (SNNs)** offer an alternative. Unlike traditional ANNs that process continuous values in batch mode, SNNs communicate through discrete spike events and learn online via Spike-Timing-Dependent Plasticity (STDP). This is a natural fit for CSI data:
|
||||
|
||||
- CSI subcarrier amplitudes are temporal signals sampled at 12-22 fps
|
||||
- Amplitude changes (not absolute values) carry the information about motion, breathing, and presence
|
||||
- STDP learns temporal correlations between subcarriers without labels
|
||||
- Event-driven processing means idle rooms (no motion) consume near-zero compute
|
||||
|
||||
The `@ruvector/spiking-neural` package (vendored at `vendor/ruvector/npm/packages/spiking-neural/`) provides production-ready LIF neurons, STDP learning, lateral inhibition, and SIMD-optimized vector math in pure JavaScript with zero dependencies.
|
||||
|
||||
## Decision
|
||||
|
||||
Integrate `@ruvector/spiking-neural` into the CSI sensing pipeline as an online unsupervised pattern learner that runs alongside the existing FC encoder. The SNN provides real-time adaptation while the FC encoder provides stable baseline predictions.
|
||||
|
||||
### Network Architecture
|
||||
|
||||
```
|
||||
CSI Frame (128 subcarriers)
|
||||
|
|
||||
v
|
||||
[ Rate Encoding ] -----> 128 input neurons (one per subcarrier)
|
||||
| amplitude delta -> spike rate
|
||||
v
|
||||
[ LIF Hidden Layer ] ---> 64 hidden neurons (tau=20ms)
|
||||
| STDP learns subcarrier correlations
|
||||
| lateral inhibition -> sparse codes
|
||||
v
|
||||
[ LIF Output Layer ] ---> 8 output neurons
|
||||
|
|
||||
v
|
||||
presence | motion | breathing | heart_rate | phase_var | persons | fall | rssi
|
||||
```
|
||||
|
||||
**Layer parameters:**
|
||||
|
||||
| Layer | Neurons | tau (ms) | v_thresh (mV) | Function |
|
||||
|-------|---------|----------|---------------|----------|
|
||||
| Input | 128 | N/A | N/A | Rate-coded spike generation from subcarrier deltas |
|
||||
| Hidden | 64 | 20.0 | -50.0 | STDP learns correlated subcarrier groups |
|
||||
| Output | 8 | 25.0 | -50.0 | Each neuron specializes in one sensing modality |
|
||||
|
||||
**Synapse parameters:**
|
||||
|
||||
| Connection | Count | a_plus | a_minus | w_init | Lateral Inhibition |
|
||||
|------------|-------|--------|---------|--------|-------------------|
|
||||
| Input -> Hidden | 8,192 | 0.005 | 0.005 | 0.3 | No |
|
||||
| Hidden -> Output | 512 | 0.003 | 0.003 | 0.2 | Yes (strength=15.0) |
|
||||
|
||||
Total synapses: 8,704. At 4 bytes per weight, this is 34 KB — fits in ESP32 SRAM.
|
||||
|
||||
### Input Encoding
|
||||
|
||||
CSI amplitudes are converted to spike rates using rate coding:
|
||||
|
||||
1. Compute per-subcarrier amplitude: `amp[i] = sqrt(I[i]^2 + Q[i]^2)` from the ADR-018 binary frame
|
||||
2. Compute amplitude delta from previous frame: `delta[i] = |amp[i] - prev_amp[i]|`
|
||||
3. Normalize deltas to [0, 1] range: `norm[i] = min(delta[i] / max_delta, 1.0)`
|
||||
4. Feed `norm` to `rateEncoding(norm, dt, max_rate)` which produces Poisson spikes
|
||||
|
||||
Higher amplitude changes produce more spikes. Static subcarriers (no motion) produce few or no spikes. This is the key energy advantage: an empty room generates almost no spikes, so the SNN does almost no work.
|
||||
|
||||
### STDP Learning Rule
|
||||
|
||||
STDP strengthens connections between neurons that fire together (within a time window) and weakens connections between neurons that fire out of sync:
|
||||
|
||||
- **LTP (Long-Term Potentiation)**: if a presynaptic neuron fires before a postsynaptic neuron within 20ms, the weight increases by `a_plus * exp(-dt/tau_stdp)`
|
||||
- **LTD (Long-Term Depression)**: if a postsynaptic neuron fires before a presynaptic neuron, the weight decreases by `a_minus * exp(-dt/tau_stdp)`
|
||||
|
||||
Over time, this causes the hidden layer neurons to specialize. Subcarriers that consistently change together (e.g., subcarriers 10-20 affected by a person walking through zone A) become strongly connected to the same hidden neuron. Different motion patterns activate different hidden neuron clusters.
|
||||
|
||||
### Lateral Inhibition (Winner-Take-All)
|
||||
|
||||
The output layer uses lateral inhibition with strength 15.0. When one output neuron fires, it suppresses all others. This forces each output neuron to specialize in a distinct pattern:
|
||||
|
||||
- Output 0: presence (any subcarrier activity above baseline)
|
||||
- Output 1: motion (widespread subcarrier changes, high spike rate)
|
||||
- Output 2: breathing (periodic 0.1-0.5 Hz modulation on chest-area subcarriers)
|
||||
- Output 3: heart rate (periodic 0.8-2.0 Hz modulation, lower amplitude than breathing)
|
||||
- Output 4: phase variance (phase instability across subcarriers)
|
||||
- Output 5: person count (number of distinct active subcarrier clusters)
|
||||
- Output 6: fall (sudden high-amplitude burst followed by silence)
|
||||
- Output 7: RSSI trend (overall signal strength change)
|
||||
|
||||
The neuron-to-label mapping is not fixed by training. Instead, the mapping is discovered by observing which output neuron fires most for each known condition during an optional calibration phase. If no calibration is available, the output is reported as raw spike counts per output neuron, and downstream consumers (Cognitum Seed, SONA) interpret the patterns.
|
||||
|
||||
### Integration with Existing Pipeline
|
||||
|
||||
The SNN does not replace the FC encoder. It runs in parallel:
|
||||
|
||||
```
|
||||
CSI Frame ----+----> FC Encoder --------> 8-dim feature vector (stable, trained)
|
||||
|
|
||||
+----> SNN (STDP) --------> 8-dim spike rate vector (adaptive, online)
|
||||
|
|
||||
+----> SONA Adapter -------> Weighted fusion of both signals
|
||||
```
|
||||
|
||||
SONA (Self-Optimizing Neural Architecture) receives both signals and learns which source is more reliable for each output dimension. In a new environment where the FC encoder has not been retrained, SONA automatically weights the SNN output higher because it adapts faster. As the FC encoder is retrained on local data, SONA shifts weight back toward it.
|
||||
|
||||
### Energy and Compute Budget
|
||||
|
||||
| Metric | FC Encoder | SNN (STDP) | Ratio |
|
||||
|--------|-----------|------------|-------|
|
||||
| Compute per frame (idle room) | 8,192 MACs | ~50 spike events | ~160x less |
|
||||
| Compute per frame (active room) | 8,192 MACs | ~500 spike events | ~16x less |
|
||||
| Memory | 34 KB weights | 34 KB weights | Equal |
|
||||
| Adaptation | Offline retraining | Online, continuous | SNN wins |
|
||||
| Stability | High (frozen weights) | Lower (weights drift) | FC wins |
|
||||
| Latency to first useful output | Hours (needs training data) | ~30 seconds | SNN wins |
|
||||
|
||||
The SNN's event-driven nature means it processes only spikes, not every subcarrier on every frame. In an idle room with no motion, subcarrier deltas are near zero, spike rates drop to near zero, and the SNN consumes negligible compute. This is ideal for battery-powered or thermally constrained deployments (ESP32, Cognitum Seed Pi Zero).
|
||||
|
||||
### Deployment Targets
|
||||
|
||||
| Platform | Runtime | Notes |
|
||||
|----------|---------|-------|
|
||||
| Node.js server | `require('@ruvector/spiking-neural')` | Primary. Receives UDP frames, runs SNN. |
|
||||
| Cognitum Seed (Pi Zero) | Node.js ARM | 34 KB model fits. ~0.06ms per step at 100 neurons. |
|
||||
| ESP32-S3 (WASM) | wasm3 interpreter | Optional. SNN weights exported as flat Float32Array. |
|
||||
| Browser | WebAssembly or JS | Via `wifi-densepose-wasm` crate's JS bindings. |
|
||||
|
||||
### Multi-Channel SNN (ADR-073 Integration)
|
||||
|
||||
With multi-frequency mesh scanning (ADR-073), the SNN input expands:
|
||||
|
||||
- **Single-channel mode**: 128 input neurons (64 subcarriers x 2 for I/Q or amplitude/phase)
|
||||
- **Multi-channel mode**: 128 input neurons, but the subcarrier index rotates across channels. Each channel's subcarriers map to the same neuron indices, but at different time slots. The SNN's temporal dynamics naturally integrate cross-channel information because STDP operates across time.
|
||||
|
||||
Alternatively, for maximum spectral diversity, a wider SNN (384 input neurons for 6 channels x 64 subcarriers) can be used on the server where memory is not constrained.
|
||||
|
||||
## Performance Targets
|
||||
|
||||
| Metric | Target | Method |
|
||||
|--------|--------|--------|
|
||||
| SNN step latency | <0.1ms | 128-64-8 network, ~8,700 synapses |
|
||||
| STDP convergence | <30 seconds | ~360 frames at 12 fps, patterns stabilize |
|
||||
| Output accuracy (after adaptation) | >80% | Compared to manually labeled ground truth |
|
||||
| Memory footprint | <50 KB | Weights + neuron state |
|
||||
| Idle room spike rate | <10 spikes/frame | Event-driven: near-zero compute when nothing moves |
|
||||
| Adaptation to new environment | <2 minutes | STDP relearns subcarrier correlations |
|
||||
|
||||
## Risks
|
||||
|
||||
### Weight Drift
|
||||
|
||||
STDP learning never stops. In a stable environment, weights can slowly drift as the network over-fits to the current RF landscape. Mitigation: implement weight decay (multiply all weights by 0.999 per second) and clamp weights to [w_min, w_max].
|
||||
|
||||
### Output Neuron Reassignment
|
||||
|
||||
If the RF environment changes significantly (new furniture, different room), output neurons may reassign their specialization. The mapping from output neuron index to label (presence, motion, etc.) may change. Mitigation: periodically log the output neuron activity and detect reassignment events. Downstream consumers should use the spike pattern, not the neuron index, for classification.
|
||||
|
||||
### Interference with FC Encoder
|
||||
|
||||
If SONA naively averages the SNN and FC encoder outputs, a poorly adapted SNN could degrade overall accuracy. Mitigation: SONA uses confidence-weighted fusion. The SNN output includes a confidence signal (total spike count / expected spike count). Low confidence = low weight.
|
||||
|
||||
### STDP Learning Rate Sensitivity
|
||||
|
||||
If `a_plus` and `a_minus` are too high, the SNN oscillates and never converges. If too low, adaptation takes too long. The default values (0.005 and 0.003) are conservative. The script includes a `--learning-rate` flag for tuning.
|
||||
|
||||
## Alternatives Considered
|
||||
|
||||
1. **Online gradient descent on FC encoder** — backprop through the FC network with each new frame. Rejected because: (a) requires a loss function, which requires labels; (b) continuous gradient updates on a small model lead to catastrophic forgetting of the pretrained representations.
|
||||
|
||||
2. **Adaptive thresholds only** — replace fixed thresholds with exponentially-weighted moving averages. Rejected because: (a) single-variable thresholds cannot capture multi-subcarrier correlations; (b) no representation learning — each subcarrier is still processed independently.
|
||||
|
||||
3. **Reservoir computing (Echo State Network)** — use a fixed random recurrent network as a temporal feature extractor. Partially viable, but: (a) requires a linear readout layer trained with labels; (b) the random reservoir does not adapt to the specific RF environment.
|
||||
|
||||
4. **Train SNN with supervision** — use surrogate gradient methods to train the SNN on labeled data. Rejected because: (a) defeats the purpose of online unsupervised learning; (b) the `@ruvector/spiking-neural` package does not implement surrogate gradients.
|
||||
|
||||
## Implementation
|
||||
|
||||
The integration is implemented in `scripts/snn-csi-processor.js`, a standalone Node.js script that:
|
||||
|
||||
1. Receives live CSI frames via UDP (port 5006, ADR-018 binary format)
|
||||
2. Decodes subcarrier I/Q data and computes amplitude deltas
|
||||
3. Feeds deltas through rate encoding into the SNN
|
||||
4. Applies STDP learning on every frame (online, unsupervised)
|
||||
5. Maps output neuron spike counts to sensing labels
|
||||
6. Prints real-time ASCII visualization of SNN activity
|
||||
7. Optionally forwards learned patterns to Cognitum Seed
|
||||
|
||||
## References
|
||||
|
||||
- ADR-018: CSI binary frame format
|
||||
- ADR-029: Channel hopping infrastructure
|
||||
- ADR-069: Cognitum Seed CSI pipeline
|
||||
- ADR-073: Multi-frequency mesh scanning
|
||||
- Maass, W. (1997). "Networks of spiking neurons: The third generation of neural network models." Neural Networks, 10(9), 1659-1671.
|
||||
- Bi, G. & Poo, M. (1998). "Synaptic modifications in cultured hippocampal neurons: Dependence on spike timing." Journal of Neuroscience, 18(24), 10464-10472.
|
||||
- `@ruvector/spiking-neural` v1.0.1 — LIF, STDP, lateral inhibition, SIMD
|
||||
@@ -0,0 +1,195 @@
|
||||
# ADR-075: Min-Cut Based Person Separation from Subcarrier Correlation
|
||||
|
||||
- **Status:** Proposed
|
||||
- **Date:** 2026-04-02
|
||||
- **Issue:** #348 — `n_persons` always reports 4 regardless of actual occupancy
|
||||
- **Depends on:** ADR-016 (RuVector integration), ADR-041 (person tracking), ADR-073 (multifrequency mesh scan)
|
||||
|
||||
## Context
|
||||
|
||||
### The Bug
|
||||
|
||||
Issue #348 reports that the ESP32 firmware's multi-person counting always reports
|
||||
`n_persons = 4`. The root cause is in the WASM edge module
|
||||
`sig_mincut_person_match.rs`, which uses a fixed `MAX_PERSONS = 4` constant and a
|
||||
threshold-based variance classifier to populate person slots. The classifier bins
|
||||
subcarriers into "dynamic" vs "static" using a single fixed variance threshold
|
||||
(`DYNAMIC_VAR_THRESH = 0.15`). In practice:
|
||||
|
||||
1. The threshold is miscalibrated for real-world CSI data — almost any room with
|
||||
multipath reflections pushes a majority of subcarriers above 0.15 variance.
|
||||
2. The subcarrier-to-person assignment uses a greedy Hungarian-lite matcher that
|
||||
fills all 4 slots once there are >= 4 dynamic subcarriers (which is nearly
|
||||
always the case).
|
||||
3. There is no mechanism to determine how many independent movers exist — the
|
||||
algorithm assumes all 4 slots should be filled.
|
||||
|
||||
### Prior Art
|
||||
|
||||
The Rust crate `ruvector-mincut` (vendored at `vendor/ruvector/crates/ruvector-mincut/`)
|
||||
implements a full dynamic min-cut algorithm with O(n^{o(1)}) amortized update time,
|
||||
Stoer-Wagner exact min-cut, and online edge insert/delete. It is already integrated
|
||||
in the training pipeline (`wifi-densepose-train/src/metrics.rs`) via
|
||||
`DynamicPersonMatcher`.
|
||||
|
||||
### WiFi Sensing Insight
|
||||
|
||||
When a person moves through a room, they perturb the Fresnel zones of specific
|
||||
subcarrier frequencies. Subcarriers whose Fresnel zones overlap the person's body
|
||||
change **together** — their amplitudes are temporally correlated. When two people
|
||||
move independently, they create two **separate** groups of correlated subcarriers.
|
||||
This correlation structure forms a natural graph partitioning problem.
|
||||
|
||||
## Decision
|
||||
|
||||
Replace the fixed-threshold person counter with a spectral min-cut algorithm
|
||||
operating on the subcarrier temporal correlation graph. This runs in the bridge
|
||||
script (`scripts/mincut-person-counter.js`) or on Cognitum Seed, and feeds the
|
||||
corrected person count back to the feature vector before ingest.
|
||||
|
||||
### Algorithm
|
||||
|
||||
1. **Sliding window accumulation**: Maintain the last 2 seconds of subcarrier
|
||||
amplitude data (~40 frames at 20 fps). Each frame provides a 64-element
|
||||
amplitude vector (one per subcarrier).
|
||||
|
||||
2. **Pairwise Pearson correlation**: For all subcarrier pairs (i, j), compute
|
||||
the Pearson correlation coefficient over the sliding window:
|
||||
|
||||
```
|
||||
r(i,j) = cov(amp_i, amp_j) / (std(amp_i) * std(amp_j))
|
||||
```
|
||||
|
||||
This produces a 64x64 correlation matrix.
|
||||
|
||||
3. **Graph construction**: Build a weighted undirected graph:
|
||||
- **Nodes** = subcarriers (64 for single-antenna ESP32-S3, up to 128 for dual)
|
||||
- **Edges** = pairs with |r(i,j)| > 0.3 (correlation threshold)
|
||||
- **Weight** = |r(i,j)| (correlation strength)
|
||||
- Discard null subcarriers (amplitude consistently near zero)
|
||||
- Expected: ~1500-2500 edges for 64 active subcarriers
|
||||
|
||||
4. **Iterative Stoer-Wagner min-cut**: Apply the Stoer-Wagner algorithm to find
|
||||
the global minimum cut. If the min-cut weight is below a separation threshold
|
||||
(empirically 2.0), the cut represents a real boundary between independent
|
||||
movers. Split the graph at the cut and recurse on each partition.
|
||||
|
||||
5. **Person count**: The number of partitions after all valid cuts = number of
|
||||
independent movers = person count. A single connected component with high
|
||||
internal correlation and no low-weight cut = 1 person (or 0 if variance is
|
||||
also low).
|
||||
|
||||
6. **Empty room detection**: If the total variance across all subcarriers is
|
||||
below a noise floor threshold, report 0 persons regardless of graph structure.
|
||||
|
||||
### Stoer-Wagner Algorithm
|
||||
|
||||
Stoer-Wagner finds the exact global minimum cut of an undirected weighted graph
|
||||
in O(V * E) time using a sequence of "minimum cut phases":
|
||||
|
||||
```
|
||||
function stoerWagner(G):
|
||||
best_cut = infinity
|
||||
while |V(G)| > 1:
|
||||
(s, t, cut_of_phase) = minimumCutPhase(G)
|
||||
if cut_of_phase < best_cut:
|
||||
best_cut = cut_of_phase
|
||||
best_partition = partition induced by t
|
||||
merge(s, t) // contract vertices s and t
|
||||
return best_cut, best_partition
|
||||
|
||||
function minimumCutPhase(G):
|
||||
A = {arbitrary start vertex}
|
||||
while A != V(G):
|
||||
z = vertex most tightly connected to A
|
||||
// "most tightly connected" = max sum of edge weights to A
|
||||
add z to A
|
||||
s = second-to-last vertex added
|
||||
t = last vertex added (most tightly connected)
|
||||
cut_of_phase = sum of weights of edges incident to t
|
||||
return (s, t, cut_of_phase)
|
||||
```
|
||||
|
||||
For V=64 subcarriers and E~2000 edges, this runs in ~8 million operations,
|
||||
well under 1ms on modern hardware and under 10ms even on ESP32-S3.
|
||||
|
||||
### Integration Points
|
||||
|
||||
```
|
||||
ESP32 Node 1 ──UDP 5006──┐
|
||||
├──> mincut-person-counter.js ──> corrected n_persons
|
||||
ESP32 Node 2 ──UDP 5006──┘ │
|
||||
├──> seed_csi_bridge.py (feature dim 5 override)
|
||||
└──> csi-graph-visualizer.js (debug view)
|
||||
```
|
||||
|
||||
The person counter runs as a standalone Node.js process alongside the existing
|
||||
`rf-scan.js` and `seed_csi_bridge.py` bridge scripts. It can also replay
|
||||
recorded `.csi.jsonl` files for offline analysis.
|
||||
|
||||
## Alternatives Considered
|
||||
|
||||
### 1. Threshold-based peak counting (current, broken)
|
||||
|
||||
Count subcarriers with variance above a threshold, then cluster by proximity.
|
||||
**Problem:** threshold is environment-dependent, miscalibrates easily, and
|
||||
cannot distinguish correlated from independent motion.
|
||||
|
||||
### 2. PCA / spectral clustering on correlation matrix
|
||||
|
||||
Compute eigenvectors of the correlation matrix; the number of large eigenvalues
|
||||
indicates the number of independent sources. **Problem:** requires choosing an
|
||||
eigenvalue gap threshold, which is as fragile as the current variance threshold.
|
||||
Also does not give per-person subcarrier assignments.
|
||||
|
||||
### 3. Min-cut on correlation graph (this ADR)
|
||||
|
||||
**Advantages:**
|
||||
- Directly models the physical structure (Fresnel zone groupings)
|
||||
- Threshold-free person counting (cut weight is a natural separation metric)
|
||||
- Produces per-person subcarrier groups as a side effect
|
||||
- Stoer-Wagner is simple to implement (~100 lines) and runs in polynomial time
|
||||
- Already validated in Rust via `ruvector-mincut` integration
|
||||
|
||||
## Performance
|
||||
|
||||
| Metric | Value |
|
||||
|--------|-------|
|
||||
| Graph size | V=64, E~2000 |
|
||||
| Stoer-Wagner complexity | O(V * E) = O(128,000) per cut |
|
||||
| Iterative cuts (max 4) | O(512,000) total |
|
||||
| Wall time (Node.js) | < 5 ms per 2-second window |
|
||||
| Wall time (Rust/WASM) | < 0.5 ms |
|
||||
| Memory | ~32 KB for correlation matrix + graph |
|
||||
| Sliding window | 2 seconds = ~40 frames * 64 subcarriers * 8 bytes = 20 KB |
|
||||
|
||||
## Consequences
|
||||
|
||||
### Positive
|
||||
|
||||
- Fixes #348: person count now reflects actual independent movers
|
||||
- Robust across environments (no per-room threshold calibration)
|
||||
- Per-person subcarrier groups enable per-person feature extraction
|
||||
- Graph visualization aids debugging and room mapping
|
||||
- Algorithm is well-understood (Stoer-Wagner, 1997)
|
||||
|
||||
### Negative
|
||||
|
||||
- Adds a new process to the sensing pipeline
|
||||
- 2-second latency for person count changes (sliding window)
|
||||
- Correlation-based: cannot detect stationary persons (no motion = no signal)
|
||||
- Assumes independent motion — two people walking in sync may be counted as one
|
||||
|
||||
### Migration
|
||||
|
||||
1. Deploy `scripts/mincut-person-counter.js` alongside existing bridge
|
||||
2. Override feature vector dimension 5 (`n_persons`) with corrected count
|
||||
3. Once validated, port Stoer-Wagner to C for direct ESP32-S3 firmware integration
|
||||
4. Deprecate the fixed-threshold `PersonMatcher` in `sig_mincut_person_match.rs`
|
||||
|
||||
## References
|
||||
|
||||
- Stoer, M. & Wagner, F. (1997). "A Simple Min-Cut Algorithm." JACM 44(4).
|
||||
- `vendor/ruvector/crates/ruvector-mincut/src/algorithm/mod.rs` — DynamicMinCut API
|
||||
- `rust-port/.../sig_mincut_person_match.rs` — current (broken) WASM edge matcher
|
||||
- `scripts/rf-scan.js` — CSI packet parsing and subcarrier classification
|
||||
@@ -0,0 +1,259 @@
|
||||
# ADR-076: CSI Spectrogram Embeddings via CNN + Graph Transformer
|
||||
|
||||
| Field | Value |
|
||||
|-------------|--------------------------------------------|
|
||||
| **Status** | Proposed |
|
||||
| **Date** | 2026-04-02 |
|
||||
| **Authors** | ruv |
|
||||
| **Depends** | ADR-018 (binary frame), ADR-024 (AETHER contrastive embeddings), ADR-029 (RuvSense), ADR-069 (Cognitum Seed bridge), ADR-073 (multi-frequency mesh scan) |
|
||||
|
||||
## Context
|
||||
|
||||
The current CSI processing pipeline extracts an 8-dimensional hand-crafted feature vector per frame: mean amplitude, amplitude variance, max amplitude, mean phase, phase variance, bandwidth, spectral centroid, and RSSI. These features are effective for basic presence detection and room fingerprinting but discard the rich spatial-frequency structure present in the raw subcarrier data.
|
||||
|
||||
A single CSI frame from an ESP32-S3 contains 64 subcarriers (or 128 in HT40 mode), each with I/Q components. When stacked over time, 20 consecutive frames form a **64x20 subcarrier-by-time matrix** — effectively a grayscale spectrogram image. This matrix encodes:
|
||||
|
||||
1. **Frequency-selective fading** — metal objects create persistent null zones at specific subcarrier indices (visible as dark vertical stripes)
|
||||
2. **Doppler signatures** — human motion produces time-varying amplitude patterns across subcarriers (visible as horizontal wave patterns)
|
||||
3. **Multipath structure** — room geometry creates characteristic interference patterns unique to each environment
|
||||
4. **Activity fingerprints** — walking, sitting, breathing, and falling produce distinct 2D texture patterns in the subcarrier-time matrix
|
||||
|
||||
These 2D structural patterns are invisible to the 8-dim feature vector, which collapses all subcarrier information into scalar statistics. A CNN embedding can preserve this spatial structure.
|
||||
|
||||
### Existing Vendor Libraries
|
||||
|
||||
**@ruvector/cnn** (v0.1.0) provides:
|
||||
- WASM-based CNN feature extraction (~5ms per 224x224 image, ~900KB model)
|
||||
- Configurable embedding dimension (default 512, we use 128 for compact storage)
|
||||
- L2-normalized embeddings with cosine similarity search
|
||||
- Contrastive training via InfoNCE and triplet loss
|
||||
- SIMD-optimized layer operations (batch norm, global average pooling, ReLU)
|
||||
- Works in both Node.js and browser environments
|
||||
|
||||
**ruvector-graph-transformer** provides:
|
||||
- Sublinear O(n log n) graph attention via LSH bucketing and PPR sampling
|
||||
- Proof-gated mutation substrate for verified computations
|
||||
- Temporal causal attention with Granger causality (relevant for CSI time series)
|
||||
- Manifold attention on product spaces S^n x H^m x R^k
|
||||
|
||||
**@ruvector/graph-wasm** (v2.0.2) provides:
|
||||
- Neo4j-compatible property graph database in WASM
|
||||
- Node/edge creation with arbitrary properties and embeddings
|
||||
- Hyperedge support for multi-node relationships
|
||||
- Cypher query language
|
||||
|
||||
### Current Limitations of 8-dim Features
|
||||
|
||||
| Limitation | Impact |
|
||||
|------------|--------|
|
||||
| No subcarrier-level information | Cannot distinguish frequency-selective vs broadband fading |
|
||||
| No temporal pattern encoding | Walking gait (periodic) looks identical to random motion (aperiodic) |
|
||||
| No 2D structure | Room fingerprint reduced to 8 numbers; two rooms with similar statistics are indistinguishable |
|
||||
| No cross-subcarrier correlation | Cannot detect standing waves, node patterns, or multipath clusters |
|
||||
| Poor kNN discrimination | 8 dimensions provides limited hypersphere surface area for separating environments |
|
||||
|
||||
## Decision
|
||||
|
||||
Treat the CSI subcarrier-by-time matrix as a grayscale spectrogram image and apply CNN embedding to produce a 128-dimensional representation that preserves 2D spatial-frequency structure. Use a graph transformer to fuse embeddings across multiple ESP32 nodes.
|
||||
|
||||
### Architecture
|
||||
|
||||
```
|
||||
ESP32 Node 1 ESP32 Node 2
|
||||
| |
|
||||
v v
|
||||
UDP 5006 UDP 5006
|
||||
| |
|
||||
v v
|
||||
[64 subcarriers] [64 subcarriers]
|
||||
[20-frame window] [20-frame window]
|
||||
| |
|
||||
v v
|
||||
64x20 amplitude 64x20 amplitude
|
||||
matrix (grayscale) matrix (grayscale)
|
||||
| |
|
||||
v v
|
||||
@ruvector/cnn @ruvector/cnn
|
||||
CnnEmbedder CnnEmbedder
|
||||
| |
|
||||
v v
|
||||
128-dim vector 128-dim vector
|
||||
| |
|
||||
+-------+ +----------+
|
||||
| |
|
||||
v v
|
||||
Graph Transformer (2-node graph)
|
||||
Edge weight = cross-node correlation
|
||||
|
|
||||
v
|
||||
Fused 128-dim vector
|
||||
|
|
||||
+-------+-------+
|
||||
| |
|
||||
v v
|
||||
Cognitum Seed kNN Search
|
||||
(128-dim store) (similar rooms)
|
||||
```
|
||||
|
||||
### Step 1: CSI-to-Spectrogram Conversion
|
||||
|
||||
Each ESP32 transmits CSI frames via UDP in ADR-018 binary format. The `iq_hex` field contains I/Q pairs for each subcarrier (2 bytes per subcarrier: I + Q as unsigned 8-bit values).
|
||||
|
||||
```
|
||||
Amplitude[sc] = sqrt(I[sc]^2 + Q[sc]^2)
|
||||
```
|
||||
|
||||
A sliding window of 20 frames produces a 64x20 matrix. Normalization to 0-255 grayscale:
|
||||
|
||||
```
|
||||
pixel[sc][t] = clamp(255 * (amplitude[sc][t] - min) / (max - min), 0, 255)
|
||||
```
|
||||
|
||||
Where `min` and `max` are computed over the entire 64x20 window for per-window contrast normalization. This ensures the CNN sees the relative structure regardless of absolute signal strength (which varies with distance, TX power, and environmental absorption).
|
||||
|
||||
### Step 2: CNN Embedding
|
||||
|
||||
The 64x20 grayscale matrix is resized to the CNN's expected input size (224x224 via nearest-neighbor upsampling, since we want to preserve the discrete subcarrier structure rather than blur it with bilinear interpolation). The input is replicated across 3 channels (RGB) since @ruvector/cnn expects RGB input.
|
||||
|
||||
Configuration:
|
||||
- **Input**: 224x224x3 (upsampled from 64x20, grayscale replicated to RGB)
|
||||
- **Embedding dimension**: 128 (reduced from default 512 for compact storage and faster kNN)
|
||||
- **Normalization**: L2-enabled (cosine similarity = dot product on unit sphere)
|
||||
- **Latency**: ~5ms per window on modern hardware
|
||||
|
||||
The 128-dim embedding encodes the 2D structure of the spectrogram: null zones, Doppler patterns, multipath signatures, and activity textures.
|
||||
|
||||
### Step 3: Graph Transformer for Multi-Node Fusion
|
||||
|
||||
With 2 ESP32 nodes (generalizable to N), we construct a graph:
|
||||
|
||||
```
|
||||
Nodes: {Node_1, Node_2}
|
||||
Edges: {(Node_1, Node_2, weight=cross_correlation)}
|
||||
Node features: 128-dim CNN embedding per node
|
||||
```
|
||||
|
||||
The graph attention mechanism learns which node is more informative for each prediction:
|
||||
|
||||
1. **Query/Key/Value** from each node's 128-dim embedding
|
||||
2. **Edge weight** = Pearson cross-correlation between the two nodes' raw amplitude vectors (captures how much their CSI observations agree)
|
||||
3. **Attention score** = softmax(Q_i * K_j / sqrt(d) + edge_weight_bias)
|
||||
4. **Output** = weighted sum of value vectors
|
||||
|
||||
This produces a fused 128-dim vector that combines both nodes' perspectives, automatically weighting the node with cleaner signal (higher SNR, less fading) more heavily.
|
||||
|
||||
**Generalization to 3+ nodes**: Adding a third ESP32 adds one node and 2 edges to the graph. The attention mechanism handles variable-size graphs without architecture changes.
|
||||
|
||||
### Step 4: Storage and Search
|
||||
|
||||
The fused 128-dim embedding is stored in Cognitum Seed (ADR-069) alongside the existing 8-dim features:
|
||||
|
||||
| Store | Dimension | Content | Use Case |
|
||||
|-------|-----------|---------|----------|
|
||||
| `csi-features` | 8-dim | Hand-crafted statistics | Fast presence detection |
|
||||
| `csi-spectrograms` | 128-dim | CNN spectrogram embedding | Environment fingerprinting, anomaly detection |
|
||||
| `csi-spectrograms-fused` | 128-dim | Graph-fused multi-node embedding | Cross-viewpoint room signature |
|
||||
|
||||
kNN search on the 128-dim store finds past spectrograms that "look like" the current one:
|
||||
- **Environment fingerprinting**: "What room does this RF pattern match?"
|
||||
- **Cross-room transfer**: "Which training room is most similar to this deployment room?"
|
||||
- **Anomaly detection**: Low similarity to all known patterns = unknown environment or novel activity
|
||||
- **Temporal segmentation**: Similarity drops = activity transition boundaries
|
||||
|
||||
### Comparison: 8-dim vs 128-dim vs Combined
|
||||
|
||||
| Property | 8-dim hand-crafted | 128-dim CNN | Combined |
|
||||
|----------|-------------------|-------------|----------|
|
||||
| Subcarrier structure | Lost | Preserved | Both available |
|
||||
| Temporal patterns | Lost | Preserved (20-frame window) | Both |
|
||||
| Computation | ~0.1ms | ~5ms | ~5ms |
|
||||
| Storage per vector | 32 bytes | 512 bytes | 544 bytes |
|
||||
| kNN discrimination | Low (8-dim curse) | High (128-dim surface) | Highest |
|
||||
| Interpretability | High (named features) | Low (learned) | Mixed |
|
||||
| Training required | No | Optional (pre-trained works) | Optional |
|
||||
| Multi-node fusion | Average/max | Graph attention | Graph attention |
|
||||
|
||||
### Contrastive Training (Optional Enhancement)
|
||||
|
||||
The CNN embedding works out-of-the-box with the pre-trained weights. For domain-specific improvements, contrastive training with CSI data:
|
||||
|
||||
1. **Positive pairs**: Same room, different time windows (should embed similarly)
|
||||
2. **Negative pairs**: Different rooms or different activities (should embed differently)
|
||||
3. **Loss**: InfoNCE with temperature 0.07 (standard SimCLR)
|
||||
4. **Augmentation**: Time-shift (slide window by 1-5 frames), subcarrier dropout (zero 10% of rows), amplitude jitter (multiply by uniform [0.8, 1.2])
|
||||
|
||||
This teaches the CNN that "same room at different times" should produce similar embeddings, while "different rooms" should produce different embeddings.
|
||||
|
||||
## Consequences
|
||||
|
||||
### Positive
|
||||
|
||||
1. **Richer representation**: 128 dimensions capture 2D structure that 8 dimensions cannot
|
||||
2. **Environment fingerprinting**: kNN on spectrograms can distinguish rooms that look identical in 8-dim feature space
|
||||
3. **Activity detection**: Temporal patterns (gait periodicity, breathing frequency) are encoded in the spectrogram texture
|
||||
4. **Multi-node fusion**: Graph attention automatically weights the most informative node, improving robustness to single-node occlusion or interference
|
||||
5. **Incremental adoption**: 128-dim store operates alongside 8-dim store; no migration needed
|
||||
6. **Browser-compatible**: WASM-based CNN runs in the sensing-server UI for live visualization
|
||||
|
||||
### Negative
|
||||
|
||||
1. **5ms latency per window**: Acceptable for 1.3 Hz update rate (750ms rotation from ADR-073), but constrains real-time applications
|
||||
2. **900KB model download**: One-time cost, cached after first load
|
||||
3. **128-dim storage**: 16x more bytes per vector than 8-dim; mitigated by the fact that we store one embedding per 20-frame window (not per frame)
|
||||
4. **Opaque embeddings**: Unlike named 8-dim features, CNN embeddings are not human-interpretable
|
||||
5. **Input size mismatch**: 64x20 matrix must be upsampled to 224x224; nearest-neighbor preserves structure but wastes computation on padded regions
|
||||
|
||||
### Risks and Mitigations
|
||||
|
||||
| Risk | Mitigation |
|
||||
|------|------------|
|
||||
| CNN embeddings not discriminative enough for CSI | Contrastive fine-tuning on CSI spectrograms; fall back to 8-dim if 128-dim kNN recall is worse |
|
||||
| Graph transformer overhead for 2-node graph | Lightweight attention (single head, no MLP); O(1) for 2 nodes |
|
||||
| Upsampling artifacts from 64x20 to 224x224 | Nearest-neighbor preserves discrete structure; consider training a smaller CNN on native 64x20 input |
|
||||
| WASM initialization delay | Call `init()` at server startup, not per-request |
|
||||
|
||||
## Implementation
|
||||
|
||||
### Files
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `scripts/csi-spectrogram.js` | CSI-to-spectrogram pipeline with CNN embedding, ASCII visualization, Cognitum Seed ingest |
|
||||
| `scripts/mesh-graph-transformer.js` | Multi-node graph attention fusion using @ruvector/graph-wasm |
|
||||
| `docs/adr/ADR-076-csi-spectrogram-embeddings.md` | This ADR |
|
||||
|
||||
### Dependencies
|
||||
|
||||
| Package | Version | Source |
|
||||
|---------|---------|--------|
|
||||
| `@ruvector/cnn` | 0.1.0 | `vendor/ruvector/npm/packages/ruvector-cnn/` |
|
||||
| `@ruvector/graph-wasm` | 2.0.2 | `vendor/ruvector/npm/packages/graph-wasm/` |
|
||||
|
||||
### Data Format
|
||||
|
||||
CSI JSONL frames from `data/recordings/pretrain-1775182186.csi.jsonl`:
|
||||
|
||||
```json
|
||||
{
|
||||
"timestamp": 1775182186.123,
|
||||
"node_id": 1,
|
||||
"magic": 3289481217,
|
||||
"size": 148,
|
||||
"rssi": -45,
|
||||
"type": "CSI",
|
||||
"iq_hex": "00000f030d030e040d030d030d030c020d020d01...",
|
||||
"subcarriers": 64
|
||||
}
|
||||
```
|
||||
|
||||
`iq_hex` encoding: 2 hex characters per byte, 4 hex characters per subcarrier (I byte + Q byte). Total length = `subcarriers * 4` hex characters.
|
||||
|
||||
## References
|
||||
|
||||
- ADR-018: Binary CSI frame format
|
||||
- ADR-024: AETHER contrastive CSI embeddings (Rust-side)
|
||||
- ADR-029: RuvSense multistatic sensing mode
|
||||
- ADR-069: Cognitum Seed RVF ingest bridge
|
||||
- ADR-073: Multi-frequency mesh scanning
|
||||
- SimCLR: Chen et al., "A Simple Framework for Contrastive Learning of Visual Representations" (2020)
|
||||
- GATv2: Brody et al., "How Attentive are Graph Attention Networks?" (2021)
|
||||
@@ -0,0 +1,284 @@
|
||||
# ADR-077: Novel RF Sensing Applications
|
||||
|
||||
**Status:** Accepted
|
||||
**Date:** 2026-04-02
|
||||
**Authors:** ruv
|
||||
**Depends on:** ADR-018 (CSI binary protocol), ADR-073 (multifrequency mesh scan), ADR-075 (MinCut person separation), ADR-076 (CSI spectrogram embeddings)
|
||||
|
||||
## Context
|
||||
|
||||
The existing ESP32 CSI + Cognitum Seed infrastructure collects rich multi-modal data:
|
||||
- 2 ESP32-S3 nodes streaming CSI at ~22 fps each (64-128 subcarriers, channel hopping ch 1/3/5/6/9/11)
|
||||
- Vitals extraction: breathing rate, heart rate, motion energy, presence score (1 Hz per node)
|
||||
- 8-dimensional feature vectors per frame
|
||||
- Cognitum Seed with BME280 (temp/humidity/pressure), PIR, reed switch, vibration sensor
|
||||
|
||||
No new hardware is required. All 6 applications below derive novel insights from data already being collected via the ADR-018 binary protocol over UDP port 5006.
|
||||
|
||||
## Decision
|
||||
|
||||
Implement 6 novel RF sensing applications as standalone Node.js scripts that process live UDP or replayed `.csi.jsonl` recordings.
|
||||
|
||||
---
|
||||
|
||||
## Application 1: Sleep Quality Monitoring
|
||||
|
||||
### Input
|
||||
Breathing rate (BR) and heart rate (HR) time series from vitals packets (0xC5110002), sampled at ~1 Hz per node over 6-8 hours.
|
||||
|
||||
### Algorithm
|
||||
Sliding window analysis (5-minute windows, 1-minute stride) classifying sleep stages:
|
||||
|
||||
| Stage | BR (BPM) | BR Variance | HR Pattern | Motion |
|
||||
|-------|----------|-------------|------------|--------|
|
||||
| **Deep (N3)** | 6-12 | Very low (<2.0) | Slow, regular | None |
|
||||
| **Light (N1/N2)** | 12-18 | Moderate (2.0-8.0) | Normal | Minimal |
|
||||
| **REM** | 15-25 | High (>8.0), irregular | Elevated | Eyes only (low CSI motion) |
|
||||
| **Awake** | >18 or <6 | Any | Variable | Moderate-high |
|
||||
|
||||
Each 5-minute window is scored by:
|
||||
1. Compute BR mean and variance within the window
|
||||
2. Compute HR mean and coefficient of variation (CV)
|
||||
3. Compute motion energy mean (from vitals `motion_energy` field)
|
||||
4. Classify stage using threshold hierarchy: Awake > REM > Light > Deep
|
||||
|
||||
### Output
|
||||
- Real-time sleep stage classification
|
||||
- ASCII hypnogram (time vs. stage)
|
||||
- Summary: total sleep time, sleep efficiency (TST / time in bed), time per stage
|
||||
- Optional JSON for health app integration
|
||||
|
||||
### Validation
|
||||
Overnight recording (`overnight-1775217646.csi.jsonl`, 113k frames, ~40 min) should show:
|
||||
- Transition from active (awake) to resting states
|
||||
- Decreased motion energy over time
|
||||
- BR stabilization in sleeping segments
|
||||
|
||||
### Clinical Relevance
|
||||
Consumer-grade sleep tracking without wearables. RF-based sensing avoids compliance issues (forgotten wristbands, dead batteries). Not diagnostic; informational only.
|
||||
|
||||
---
|
||||
|
||||
## Application 2: Breathing Disorder Screening (Apnea Detection)
|
||||
|
||||
### Input
|
||||
Breathing rate time series from vitals packets at ~1 Hz.
|
||||
|
||||
### Algorithm
|
||||
Detect respiratory events in the BR time series:
|
||||
|
||||
| Event | Definition | Duration |
|
||||
|-------|-----------|----------|
|
||||
| **Apnea** | BR drops below 3 BPM (effective cessation) | >= 10 seconds |
|
||||
| **Hypopnea** | BR drops > 50% from 5-min rolling baseline | >= 10 seconds |
|
||||
|
||||
Scoring:
|
||||
1. Maintain 5-minute rolling baseline BR (exponential moving average)
|
||||
2. Flag apnea when BR < 3 BPM for >= 10 consecutive seconds
|
||||
3. Flag hypopnea when BR < 50% of baseline for >= 10 consecutive seconds
|
||||
4. Compute AHI (Apnea-Hypopnea Index) = total events / hours monitored
|
||||
|
||||
| AHI | Severity |
|
||||
|-----|----------|
|
||||
| < 5 | Normal |
|
||||
| 5-15 | Mild |
|
||||
| 15-30 | Moderate |
|
||||
| > 30 | Severe |
|
||||
|
||||
### Output
|
||||
- Per-event log: type (apnea/hypopnea), start time, duration, BR during event
|
||||
- Hourly AHI and overall AHI
|
||||
- Severity classification
|
||||
- Alert on severe events (consecutive apneas > 30s)
|
||||
|
||||
### Clinical Relevance
|
||||
Pre-screening tool for obstructive sleep apnea (OSA). Provides motivation for clinical polysomnography referral. Not a diagnostic device; informational pre-screen only.
|
||||
|
||||
---
|
||||
|
||||
## Application 3: Emotional State / Stress Detection
|
||||
|
||||
### Input
|
||||
Heart rate time series from vitals packets at ~1 Hz.
|
||||
|
||||
### Algorithm
|
||||
Heart Rate Variability (HRV) analysis:
|
||||
|
||||
1. **RMSSD** (Root Mean Square of Successive Differences):
|
||||
- Compute successive HR differences within 5-minute windows
|
||||
- RMSSD = sqrt(mean(diff^2))
|
||||
- High RMSSD = high vagal tone = relaxed
|
||||
- Low RMSSD = sympathetic dominance = stressed
|
||||
|
||||
2. **LF/HF Ratio** (via FFT on 5-minute HR windows):
|
||||
- LF band: 0.04-0.15 Hz (sympathetic + parasympathetic)
|
||||
- HF band: 0.15-0.40 Hz (parasympathetic)
|
||||
- High LF/HF (> 2.0) = stressed
|
||||
- Low LF/HF (< 1.0) = relaxed
|
||||
|
||||
3. **Stress Score** (0-100):
|
||||
- `score = 50 * (1 - RMSSD_norm) + 50 * LF_HF_norm`
|
||||
- Where `RMSSD_norm` = RMSSD / max_expected_RMSSD (capped at 1.0)
|
||||
- And `LF_HF_norm` = min(LF_HF / 4.0, 1.0)
|
||||
|
||||
### Output
|
||||
- Real-time stress score (0-100)
|
||||
- RMSSD and LF/HF ratio per window
|
||||
- ASCII trend chart over hours
|
||||
- Activity context correlation (motion level vs. stress)
|
||||
|
||||
### Validation
|
||||
- Periods of activity (walking, working) should correlate with higher stress scores
|
||||
- Quiet rest should show lower scores
|
||||
- Sleeping should show lowest scores (high HRV, low LF/HF)
|
||||
|
||||
---
|
||||
|
||||
## Application 4: Gait Analysis / Movement Disorder Detection
|
||||
|
||||
### Input
|
||||
- Motion energy time series from vitals packets
|
||||
- CSI phase variance from raw CSI frames (0xC5110001)
|
||||
- Cross-node RSSI from vitals packets
|
||||
|
||||
### Algorithm
|
||||
|
||||
1. **Cadence Extraction**: FFT on motion_energy within 5-second sliding windows
|
||||
- Walking cadence: dominant frequency 0.8-2.0 Hz (normal: ~1.0 Hz = 120 steps/min)
|
||||
- Running: > 2.0 Hz
|
||||
- Stationary: no dominant peak
|
||||
|
||||
2. **Stride Regularity**: Autocorrelation of motion_energy
|
||||
- Regular walking: strong autocorrelation peak at step period
|
||||
- Irregularity score = 1 - (peak_height / baseline)
|
||||
|
||||
3. **Asymmetry Detection**: Compare motion energy oscillation between two ESP32 nodes
|
||||
- Symmetric gait: both nodes see similar oscillation period and amplitude
|
||||
- Asymmetry index = |period_node1 - period_node2| / mean_period
|
||||
|
||||
4. **Tremor Detection**: High-frequency phase variance analysis
|
||||
- Compute phase variance per subcarrier in 2-second windows
|
||||
- Tremor band: 3-8 Hz component in phase variance time series
|
||||
- Parkinsonian tremor: 4-6 Hz, resting
|
||||
- Essential tremor: 5-8 Hz, action
|
||||
|
||||
### Output
|
||||
- Cadence (steps/min)
|
||||
- Stride regularity score (0-1)
|
||||
- Asymmetry index (0 = symmetric, 1 = highly asymmetric)
|
||||
- Tremor score and dominant frequency
|
||||
- Walking vs. stationary classification
|
||||
|
||||
### Validation
|
||||
Overnight data should show clear stationary periods with no cadence detected. Any walking segments should show cadence in the 0.8-2.0 Hz range.
|
||||
|
||||
---
|
||||
|
||||
## Application 5: Material/Object Change Detection
|
||||
|
||||
### Input
|
||||
Per-subcarrier amplitude from raw CSI frames (0xC5110001).
|
||||
|
||||
### Algorithm
|
||||
|
||||
1. **Baseline Establishment** (first 10 minutes or configurable):
|
||||
- Record mean amplitude per subcarrier (Welford online mean)
|
||||
- Record null pattern: which subcarriers are below null threshold (amplitude < 2.0)
|
||||
|
||||
2. **Change Detection** (sliding 30-second windows):
|
||||
- Compare current null pattern to baseline
|
||||
- New nulls appearing = new metal object blocking RF path
|
||||
- Existing nulls disappearing = metal object removed
|
||||
- Null position shifted = object moved
|
||||
- Amplitude change without null change = non-metal material (wood, water, glass)
|
||||
|
||||
3. **Material Classification** heuristic:
|
||||
- Metal: sharp null (amplitude drops to near 0 on specific subcarriers)
|
||||
- Water/human: broad amplitude reduction across many subcarriers
|
||||
- Wood/plastic: minimal amplitude change, mostly phase shift
|
||||
- Glass: frequency-selective (affects higher subcarriers more)
|
||||
|
||||
### Output
|
||||
- Change events with timestamp, type (add/remove/move), affected subcarrier range
|
||||
- Estimated material category
|
||||
- Null pattern delta visualization (ASCII)
|
||||
- Event timeline for monitoring
|
||||
|
||||
### Validation
|
||||
Overnight data has 19% null baseline. Changes in null pattern over the recording period indicate environment changes (doors opening/closing, person entering/leaving).
|
||||
|
||||
---
|
||||
|
||||
## Application 6: Room Environment Fingerprinting
|
||||
|
||||
### Input
|
||||
- 8-dimensional feature vectors from feature packets (0xC5110003)
|
||||
- Motion energy and presence score from vitals packets
|
||||
|
||||
### Algorithm
|
||||
|
||||
1. **Online Clustering** using running k-means (k=5, updateable centroids):
|
||||
- Each incoming 8-dim feature vector is assigned to nearest centroid
|
||||
- Centroid updated via exponential moving average (alpha=0.01)
|
||||
- New cluster created if distance to all centroids exceeds threshold
|
||||
|
||||
2. **State Labeling** (heuristic from vitals correlation):
|
||||
- Cluster with lowest motion_energy = "empty/sleeping"
|
||||
- Cluster with highest motion_energy = "active/walking"
|
||||
- Intermediate clusters = "resting", "working", "transitional"
|
||||
|
||||
3. **Transition Tracking**:
|
||||
- Build state transition matrix (from_state -> to_state counts)
|
||||
- Detect anomalous transitions (rare in historical data)
|
||||
|
||||
4. **Daily Profile**:
|
||||
- Aggregate state durations per hour
|
||||
- Compare across days for routine detection
|
||||
|
||||
### Output
|
||||
- Current room state and confidence
|
||||
- State timeline (ASCII)
|
||||
- Transition matrix
|
||||
- Daily pattern profile
|
||||
- Anomaly score (deviation from established daily pattern)
|
||||
|
||||
### Validation
|
||||
Overnight recording should show 2-3 stable clusters corresponding to activity periods at different times. Transitions should be infrequent and correspond to real behavioral changes.
|
||||
|
||||
---
|
||||
|
||||
## Implementation
|
||||
|
||||
All scripts share common infrastructure:
|
||||
- ADR-018 binary packet parsing (same as rf-scan.js, mincut-person-counter.js)
|
||||
- JSONL replay via readline interface
|
||||
- Live UDP via dgram
|
||||
- Pure Node.js, no external dependencies
|
||||
- CLI: `--replay <file>` for offline, `--port <N>` for live, `--json` for programmatic output
|
||||
|
||||
| Script | Primary Packets | Key Algorithm |
|
||||
|--------|----------------|---------------|
|
||||
| `sleep-monitor.js` | vitals (0xC5110002) | BR/HR window classification |
|
||||
| `apnea-detector.js` | vitals (0xC5110002) | BR pause detection, AHI scoring |
|
||||
| `stress-monitor.js` | vitals (0xC5110002) | HRV RMSSD + FFT LF/HF |
|
||||
| `gait-analyzer.js` | vitals + raw CSI | FFT cadence + phase tremor |
|
||||
| `material-detector.js` | raw CSI (0xC5110001) | Null pattern baseline + delta |
|
||||
| `room-fingerprint.js` | feature (0xC5110003) + vitals | Online k-means clustering |
|
||||
|
||||
## Consequences
|
||||
|
||||
### Positive
|
||||
- 6 new sensing applications from existing hardware (zero additional cost)
|
||||
- All offline-capable via JSONL replay (no live hardware needed for development)
|
||||
- Pure JS, no native dependencies, runs on any platform with Node.js
|
||||
- Each script is standalone and composable
|
||||
|
||||
### Negative
|
||||
- Vitals accuracy depends on ESP32 CSI quality (RSSI, multipath)
|
||||
- HRV analysis at 1 Hz HR sampling is coarse compared to ECG
|
||||
- Material classification is heuristic, not definitive
|
||||
- Sleep staging without EEG is approximate (consumer-grade accuracy)
|
||||
|
||||
### Risks
|
||||
- Users may misinterpret health-related outputs as clinical diagnoses
|
||||
- Mitigation: all scripts include disclaimers in output headers
|
||||
@@ -0,0 +1,354 @@
|
||||
# ADR-078: Multi-Frequency Mesh Sensing Applications
|
||||
|
||||
| Field | Value |
|
||||
|-------------|--------------------------------------------|
|
||||
| **Status** | Proposed |
|
||||
| **Date** | 2026-04-02 |
|
||||
| **Authors** | ruv |
|
||||
| **Depends** | ADR-018 (binary frame), ADR-029 (channel hopping), ADR-073 (multi-frequency mesh scan) |
|
||||
|
||||
## Context
|
||||
|
||||
ADR-073 established multi-frequency mesh scanning: 2 ESP32-S3 nodes hopping across 6 WiFi channels (1, 3, 5, 6, 9, 11) with 9 neighbor WiFi networks as passive illuminators. This ADR defines 5 sensing applications that are **unique to multi-frequency mesh scanning** and impossible with single-channel WiFi sensing.
|
||||
|
||||
### Why Multi-Frequency is Required
|
||||
|
||||
Single-channel WiFi sensing captures CSI on one frequency (e.g., channel 5 at 2432 MHz). This provides amplitude and phase across ~52-64 OFDM subcarriers within a 20 MHz bandwidth. Multi-frequency mesh scanning extends this to 6 channels spanning 2412-2462 MHz (50 MHz total), with each channel providing independent multipath observations. The applications below exploit the frequency dimension that single-channel sensing cannot access.
|
||||
|
||||
### Available Infrastructure
|
||||
|
||||
| Resource | Detail |
|
||||
|----------|--------|
|
||||
| Node 1 (COM7) | ESP32-S3, channels 1, 6, 11 (non-overlapping), 200ms dwell |
|
||||
| Node 2 | ESP32-S3, channels 3, 5, 9 (interleaved, near neighbor APs), 200ms dwell |
|
||||
| Neighbor APs | 9 networks across channels 3, 5, 6, 9, 11 |
|
||||
| Data transport | UDP port 5006, ADR-018 binary format |
|
||||
| Recorded data | `data/recordings/overnight-*.csi.jsonl` |
|
||||
|
||||
### Neighbor AP Illuminator Table
|
||||
|
||||
| SSID | Channel | Freq (MHz) | Signal (%) | Role |
|
||||
|------|---------|------------|------------|------|
|
||||
| ruv.net | 5 | 2432 | 100 | Primary illuminator |
|
||||
| Cohen-Guest | 5 | 2432 | 100 | Co-channel illuminator |
|
||||
| COGECO-21B20 | 11 | 2462 | 100 | High-freq illuminator |
|
||||
| HP M255 LaserJet | 5 | 2432 | 94 | Device fingerprinting target |
|
||||
| conclusion mesh | 3 | 2422 | 44 | Low-freq illuminator |
|
||||
| NETGEAR72 | 9 | 2452 | 42 | Mid-high illuminator |
|
||||
| NETGEAR72-Guest | 9 | 2452 | 42 | Co-channel illuminator |
|
||||
| COGECO-4321 | 11 | 2462 | 30 | Weak high-freq illuminator |
|
||||
| Innanen | 6 | 2437 | 19 | Weak center-band illuminator |
|
||||
|
||||
## Decision
|
||||
|
||||
Implement 5 multi-frequency-specific sensing applications, each as a standalone Node.js script in `scripts/`.
|
||||
|
||||
---
|
||||
|
||||
## Application 1: RF Tomographic Imaging
|
||||
|
||||
### Principle
|
||||
|
||||
Each WiFi channel "sees" through the room differently because multipath interference patterns are frequency-dependent. A 2 cm path length difference produces a null at 2432 MHz but constructive interference at 2412 MHz. With 6 channels x 2 nodes, we have 12 independent RF path observations through the room.
|
||||
|
||||
RF tomography back-projects attenuation along each transmitter-receiver path. Where paths overlap with high attenuation, there is an absorbing object (person, furniture, wall). Where paths show low attenuation, the space is clear.
|
||||
|
||||
### Algorithm
|
||||
|
||||
```
|
||||
For each CSI frame:
|
||||
1. Compute path attenuation = RSSI_free_space - RSSI_measured
|
||||
2. For each cell in a 10x10 room grid:
|
||||
a. Compute the cell's distance to the TX->RX line (perpendicular distance)
|
||||
b. Weight contribution by 1/distance (cells near the path contribute more)
|
||||
3. Accumulate weighted attenuation across all frames, channels, and node pairs
|
||||
4. Normalize: cells with high accumulated attenuation = absorbers (people/objects)
|
||||
```
|
||||
|
||||
Uses the Algebraic Reconstruction Technique (ART) for iterative refinement, or simple backprojection for real-time display.
|
||||
|
||||
### Resolution
|
||||
|
||||
- Theoretical: ~lambda/2 = 6 cm (at 2.4 GHz)
|
||||
- Practical with 2 nodes: ~20 cm (limited by node geometry)
|
||||
- Frequency diversity gain: sqrt(6) improvement over single-channel = ~2.4x
|
||||
|
||||
### Why Single-Channel Cannot Do This
|
||||
|
||||
Single-channel provides only 1 frequency observation per path. Frequency-selective fading means a single channel may show zero attenuation through a person (if the path happens to be at a constructive interference point). Multiple channels provide independent attenuation measurements through the same spatial path, enabling reliable detection.
|
||||
|
||||
### Script
|
||||
|
||||
`scripts/rf-tomography.js`
|
||||
|
||||
---
|
||||
|
||||
## Application 2: Passive Bistatic Radar
|
||||
|
||||
### Principle
|
||||
|
||||
Neighbor WiFi APs transmit continuously and uncontrollably. The ESP32 nodes capture CSI from these transmissions, which includes phase and amplitude modulated by objects in the room. Each neighbor AP acts as a free "illuminator of opportunity" at a known position and frequency.
|
||||
|
||||
This is the same principle used by military passive radar systems (e.g., the Ukrainian Kolchuga, Czech VERA-NG) that use FM radio and TV transmitters to detect aircraft without emitting any signals themselves. Here we use WiFi APs instead of broadcast towers, and detect people instead of aircraft.
|
||||
|
||||
### Algorithm
|
||||
|
||||
```
|
||||
For each neighbor AP (identified by BSSID/channel):
|
||||
1. Track CSI phase progression across consecutive frames
|
||||
2. Compute Doppler shift: fd = d(phase)/dt / (2*pi)
|
||||
- Positive Doppler = target moving toward the AP
|
||||
- Negative Doppler = target moving away
|
||||
3. Compute range from subcarrier phase slope:
|
||||
- tau = d(phase)/d(subcarrier_freq) / (2*pi)
|
||||
- range = c * tau (where c = speed of light)
|
||||
4. Build range-Doppler map per AP
|
||||
5. Fuse multi-static detections:
|
||||
- Each AP provides a range ellipse (locus of constant TX->target->RX delay)
|
||||
- Intersection of 3+ ellipses = target position
|
||||
```
|
||||
|
||||
### Multi-Static Geometry
|
||||
|
||||
With 3+ neighbor APs as transmitters and 2 ESP32 receivers, we have 6+ bistatic pairs. Each pair constrains the target to an ellipse. The intersection provides 2D position.
|
||||
|
||||
```
|
||||
AP1 (ch5) AP2 (ch11)
|
||||
\ /
|
||||
\ TARGET /
|
||||
\ /|\ /
|
||||
\ / | \ /
|
||||
ESP32_1 ---*--+--*--- ESP32_2
|
||||
/ \ | / \
|
||||
/ \|/ \
|
||||
/ TARGET \
|
||||
/ \
|
||||
AP3 (ch3) AP4 (ch9)
|
||||
```
|
||||
|
||||
### Why Single-Channel Cannot Do This
|
||||
|
||||
Single-channel only captures CSI from APs on that one channel. With channel 5, you see ruv.net and Cohen-Guest, but miss COGECO-21B20 (ch11), conclusion mesh (ch3), NETGEAR72 (ch9). Multi-frequency scanning captures illumination from all 9 APs across 6 channels, providing the geometric diversity needed for position triangulation.
|
||||
|
||||
### Script
|
||||
|
||||
`scripts/passive-radar.js`
|
||||
|
||||
---
|
||||
|
||||
## Application 3: Frequency-Selective Material Classification
|
||||
|
||||
### Principle
|
||||
|
||||
Different materials interact with 2.4 GHz WiFi signals differently, and critically, their absorption/reflection varies with frequency:
|
||||
|
||||
| Material | Attenuation Pattern | Frequency Dependence |
|
||||
|----------|--------------------|--------------------|
|
||||
| Metal | Total reflection, deep null | Frequency-flat (blocks all equally) |
|
||||
| Water/Human body | Strong absorption | Increases with frequency (dielectric loss ~ f^2) |
|
||||
| Wood | Mild attenuation | Increases with frequency (moisture content) |
|
||||
| Glass | Low attenuation | Nearly frequency-flat |
|
||||
| Drywall | Low-moderate attenuation | Slight frequency dependence |
|
||||
| Concrete | Moderate-high attenuation | Increases with frequency |
|
||||
|
||||
### Algorithm
|
||||
|
||||
```
|
||||
For each subcarrier index i across all channels:
|
||||
1. Measure attenuation A(i, ch) on each channel
|
||||
2. Compute frequency selectivity:
|
||||
- Flat ratio = std(A across channels) / mean(A across channels)
|
||||
- Slope = linear regression of A vs frequency
|
||||
3. Classify:
|
||||
- Flat ratio < 0.1 AND high attenuation -> Metal
|
||||
- Flat ratio < 0.1 AND low attenuation -> Glass/Air
|
||||
- Positive slope (A increases with freq) AND high A -> Water/Human
|
||||
- Positive slope AND moderate A -> Wood
|
||||
- High variance across channels -> Complex scatterer
|
||||
```
|
||||
|
||||
### Physics Basis
|
||||
|
||||
At 2.4 GHz, water's complex permittivity is epsilon_r = 77 - j10. The imaginary component (loss) increases with frequency within the WiFi band. Metal is a perfect conductor regardless of frequency. Glass (epsilon_r ~ 6 - j0.1) has negligible loss at all WiFi frequencies.
|
||||
|
||||
The 50 MHz span (2412-2462 MHz) is only ~2% of the carrier frequency, but this is sufficient to detect the frequency-dependent absorption signature of water-bearing materials (human body, wet wood, potted plants) versus frequency-flat materials (metal, glass).
|
||||
|
||||
### Why Single-Channel Cannot Do This
|
||||
|
||||
Material classification requires measuring how attenuation varies with frequency. A single channel provides only one frequency point -- there is no frequency axis to measure against. Multi-frequency scanning provides 6 frequency points spanning 50 MHz, enabling slope and variance computation.
|
||||
|
||||
### Script
|
||||
|
||||
`scripts/material-classifier.js`
|
||||
|
||||
---
|
||||
|
||||
## Application 4: Through-Wall Motion Detection
|
||||
|
||||
### Principle
|
||||
|
||||
Lower WiFi frequencies penetrate walls better than higher frequencies. At 2.4 GHz, wall attenuation for a standard drywall+stud partition is approximately:
|
||||
|
||||
| Channel | Freq (MHz) | Drywall Loss (dB) | Concrete Loss (dB) |
|
||||
|---------|------------|-------------------|-------------------|
|
||||
| 1 | 2412 | 2.5 | 8.0 |
|
||||
| 6 | 2437 | 2.6 | 8.3 |
|
||||
| 11 | 2462 | 2.7 | 8.6 |
|
||||
|
||||
The absolute differences are small (~0.2 dB), but with 6 channels we can:
|
||||
|
||||
1. **Baseline the wall's frequency-dependent attenuation profile** during a calibration period (no one behind the wall)
|
||||
2. **Detect changes above baseline** that indicate motion behind the wall
|
||||
3. **Weight lower channels more heavily** since they have better through-wall SNR
|
||||
4. **Cross-validate** across channels: real through-wall motion appears on all channels (with frequency-dependent amplitude), while interference/noise typically appears on only one channel
|
||||
|
||||
### Algorithm
|
||||
|
||||
```
|
||||
Calibration phase (60 seconds, no motion behind wall):
|
||||
For each channel ch:
|
||||
baseline_mean[ch] = mean(CSI amplitude over calibration)
|
||||
baseline_std[ch] = std(CSI amplitude over calibration)
|
||||
|
||||
Detection phase:
|
||||
For each frame on channel ch:
|
||||
1. Compute deviation = |current_amplitude - baseline_mean[ch]| / baseline_std[ch]
|
||||
2. Channel weight = f(penetration_quality[ch])
|
||||
3. Per-channel score = deviation * weight
|
||||
|
||||
Fused score = weighted sum across channels
|
||||
Alert if fused_score > threshold for N consecutive frames
|
||||
```
|
||||
|
||||
### Why Single-Channel Cannot Do This
|
||||
|
||||
Single-channel through-wall detection suffers from high false-positive rates because it cannot distinguish wall effects from motion. With multi-frequency, we can:
|
||||
|
||||
1. Characterize the wall's frequency response during calibration
|
||||
2. Subtract the wall effect per channel
|
||||
3. Cross-validate detections across channels (real motion is coherent across frequencies; noise is not)
|
||||
|
||||
The frequency diversity provides a ~2.4x improvement in detection SNR (sqrt(6) independent observations).
|
||||
|
||||
### Script
|
||||
|
||||
`scripts/through-wall-detector.js`
|
||||
|
||||
---
|
||||
|
||||
## Application 5: Device Fingerprinting via RF Emissions
|
||||
|
||||
### Principle
|
||||
|
||||
Every electronic device has unique RF characteristics visible in the WiFi spectrum. When a device transmits (or even when its internal oscillators radiate EMI), it modulates nearby WiFi signals in device-specific ways:
|
||||
|
||||
- **WiFi APs**: each AP has unique transmit power, phase noise, and clock drift characteristics
|
||||
- **Printers**: the HP M255 LaserJet creates specific subcarrier patterns when printing (motor EMI)
|
||||
- **Microwave ovens**: 2.45 GHz magnetron radiates across channels 8-11, creating distinctive wideband interference
|
||||
- **Bluetooth devices**: 2.4 GHz frequency-hopping creates transient spikes across channels
|
||||
|
||||
### Algorithm
|
||||
|
||||
```
|
||||
Learning phase:
|
||||
For each known device (from WiFi scan SSID/BSSID correlation):
|
||||
1. Record CSI patterns when device is active vs inactive
|
||||
2. Compute per-channel signature:
|
||||
- Mean amplitude profile across subcarriers
|
||||
- Variance profile (active devices increase variance on specific subcarriers)
|
||||
- Phase noise characteristics
|
||||
3. Store signature as device fingerprint
|
||||
|
||||
Detection phase:
|
||||
For each analysis window:
|
||||
1. Compute current CSI profile per channel
|
||||
2. Correlate against stored fingerprints
|
||||
3. Report device activity: "HP printer active (confidence 0.87)"
|
||||
```
|
||||
|
||||
### Multi-Frequency Advantage
|
||||
|
||||
Different devices affect different channels:
|
||||
|
||||
- HP printer (ch5): affects subcarriers 20-40 on channel 5 during print jobs
|
||||
- NETGEAR72 router (ch9): creates clock-drift correlated phase patterns on channel 9
|
||||
- Microwave: broadband interference strongest on channels 9-11
|
||||
|
||||
Single-channel sensing only sees devices that affect that one channel. Multi-frequency scanning observes the full 2412-2462 MHz band, detecting device activity regardless of which channel the device operates on.
|
||||
|
||||
### Script
|
||||
|
||||
`scripts/device-fingerprint.js`
|
||||
|
||||
---
|
||||
|
||||
## Implementation
|
||||
|
||||
### Shared Infrastructure
|
||||
|
||||
All 5 scripts share common infrastructure:
|
||||
|
||||
| Component | Detail |
|
||||
|-----------|--------|
|
||||
| Packet format | ADR-018 binary (UDP) or .csi.jsonl (replay) |
|
||||
| IQ parsing | `parseIqHex()` for JSONL, `parseCSIFrame()` for binary UDP |
|
||||
| Channel assignment | From binary freq field, or simulated round-robin for legacy JSONL |
|
||||
| Node positions | Configurable, default: Node 1 at (0,0), Node 2 at (3,0) meters |
|
||||
| Visualization | ASCII Unicode block characters and box drawing |
|
||||
|
||||
### Scripts
|
||||
|
||||
| Script | Application | Lines | Key Algorithm |
|
||||
|--------|------------|-------|---------------|
|
||||
| `scripts/rf-tomography.js` | RF Tomographic Imaging | ~500 | ART backprojection |
|
||||
| `scripts/passive-radar.js` | Passive Bistatic Radar | ~500 | Range-Doppler + multi-static fusion |
|
||||
| `scripts/material-classifier.js` | Material Classification | ~450 | Frequency-selective attenuation analysis |
|
||||
| `scripts/through-wall-detector.js` | Through-Wall Detection | ~400 | Baselined multi-channel anomaly detection |
|
||||
| `scripts/device-fingerprint.js` | Device Fingerprinting | ~450 | Per-channel signature correlation |
|
||||
|
||||
### Data Requirements
|
||||
|
||||
- **Live mode**: UDP port 5006, 2 ESP32 nodes channel-hopping per ADR-073
|
||||
- **Replay mode**: `--replay <file.csi.jsonl>` with overnight recordings
|
||||
- **Calibration**: through-wall detector requires 60s calibration with `--calibrate`
|
||||
|
||||
## Performance Targets
|
||||
|
||||
| Application | Latency | Update Rate | Accuracy Target |
|
||||
|-------------|---------|-------------|-----------------|
|
||||
| RF Tomography | <100ms per frame | 1 Hz image update | 20 cm spatial resolution |
|
||||
| Passive Radar | <200ms per frame | 2 Hz range-Doppler | 1 m range, 0.1 m/s velocity |
|
||||
| Material Classification | <500ms per window | 0.5 Hz classification | 70% correct material ID |
|
||||
| Through-Wall Detection | <100ms per frame | 2 Hz detection | 90% true positive, <10% false positive |
|
||||
| Device Fingerprinting | <1s per window | 0.2 Hz activity update | 80% correct device ID |
|
||||
|
||||
## Risks
|
||||
|
||||
### Limited Frequency Span
|
||||
|
||||
The 50 MHz span (2412-2462 MHz) is only 2% of the carrier frequency. Material classification accuracy depends on the attenuation slope being measurable within this narrow range. Mitigation: use long averaging windows (5-10 seconds) to improve SNR of frequency-dependent measurements.
|
||||
|
||||
### Node Geometry
|
||||
|
||||
2 nodes provide limited spatial diversity for tomographic imaging. The backprojection is essentially 1D along the node-to-node axis, with poor resolution perpendicular to it. Mitigation: neighbor APs provide additional geometric diversity for passive radar mode.
|
||||
|
||||
### Legacy Data Compatibility
|
||||
|
||||
Overnight recordings (`data/recordings/overnight-*.csi.jsonl`) were captured before multi-frequency scanning was deployed and lack channel/frequency fields. Scripts simulate channel assignment for replay. Full multi-frequency data requires re-recording with channel hopping enabled.
|
||||
|
||||
### Phase Calibration
|
||||
|
||||
Passive radar requires accurate phase tracking across consecutive frames. ESP32 CSI phase includes a random offset per channel hop that must be removed. Mitigation: use phase-difference between consecutive frames rather than absolute phase.
|
||||
|
||||
## Alternatives Considered
|
||||
|
||||
1. **5 GHz multi-frequency**: rejected -- no 5 GHz APs visible in environment, no free illuminators.
|
||||
2. **UWB (ultra-wideband)**: rejected -- ESP32-S3 does not support UWB. Would require additional hardware (DW1000/DW3000 modules).
|
||||
3. **Dedicated radar hardware**: rejected -- multi-frequency WiFi sensing achieves similar capabilities using existing infrastructure at zero additional cost.
|
||||
|
||||
## References
|
||||
|
||||
- Wilson, J. & Patwari, N. (2010). "Radio Tomographic Imaging with Wireless Networks." IEEE Trans. Mobile Computing.
|
||||
- Colone, F. et al. (2012). "WiFi-Based Passive Bistatic Radar: Data Processing Schemes and Experimental Results." IEEE Trans. Aerospace and Electronic Systems.
|
||||
- Adib, F. & Katabi, D. (2013). "See Through Walls with WiFi!" ACM SIGCOMM.
|
||||
- Banerjee, A. et al. (2014). "RF-based material identification using WiFi signals." ACM MobiCom.
|
||||
@@ -0,0 +1,410 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-077: Breathing Disorder Screening — Apnea/Hypopnea Detection
|
||||
*
|
||||
* Monitors breathing rate time series for respiratory events (pauses > 10s)
|
||||
* and computes AHI (Apnea-Hypopnea Index) for pre-screening.
|
||||
*
|
||||
* DISCLAIMER: This is a pre-screening tool, NOT a clinical diagnostic device.
|
||||
* Consult a physician and pursue polysomnography for diagnosis.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/apnea-detector.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/apnea-detector.js --port 5006
|
||||
* node scripts/apnea-detector.js --replay FILE --json
|
||||
*
|
||||
* ADR: docs/adr/ADR-077-novel-rf-sensing-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
json: { type: 'boolean', default: false },
|
||||
interval: { type: 'string', short: 'i', default: '5000' },
|
||||
'apnea-threshold': { type: 'string', default: '3.0' },
|
||||
'hypopnea-drop': { type: 'string', default: '0.5' },
|
||||
'min-duration': { type: 'string', default: '10' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const APNEA_THRESH = parseFloat(args['apnea-threshold']); // BR below this = apnea
|
||||
const HYPOPNEA_DROP = parseFloat(args['hypopnea-drop']); // 50% drop from baseline
|
||||
const MIN_DURATION_SEC = parseInt(args['min-duration'], 10); // min event duration
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ADR-018 packet constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const VITALS_MAGIC = 0xC5110002;
|
||||
const FUSED_MAGIC = 0xC5110004;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Apnea detector engine
|
||||
// ---------------------------------------------------------------------------
|
||||
class ApneaDetector {
|
||||
constructor(opts) {
|
||||
this.apneaThresh = opts.apneaThresh;
|
||||
this.hypopneaDrop = opts.hypopneaDrop;
|
||||
this.minDurationSec = opts.minDurationSec;
|
||||
|
||||
// Rolling baseline (exponential moving average, 5-min window)
|
||||
this.baselineBR = null;
|
||||
this.baselineAlpha = 0.005; // slow adaptation
|
||||
|
||||
// Event tracking
|
||||
this.events = []; // { type, startTs, endTs, durationSec, avgBR }
|
||||
this.currentEvent = null; // in-progress event
|
||||
this.eventSamples = []; // BR samples during current event
|
||||
|
||||
// Time tracking
|
||||
this.startTime = null;
|
||||
this.lastTime = null;
|
||||
this.totalSamples = 0;
|
||||
|
||||
// Per-hour tracking
|
||||
this.hourlyEvents = new Map(); // hour_index -> count
|
||||
}
|
||||
|
||||
ingest(timestamp, br) {
|
||||
if (!this.startTime) this.startTime = timestamp;
|
||||
this.lastTime = timestamp;
|
||||
this.totalSamples++;
|
||||
|
||||
// Update baseline (only with "normal" breathing)
|
||||
if (br > this.apneaThresh * 2 && (!this.baselineBR || br < this.baselineBR * 2)) {
|
||||
if (this.baselineBR === null) {
|
||||
this.baselineBR = br;
|
||||
} else {
|
||||
this.baselineBR = this.baselineBR * (1 - this.baselineAlpha) + br * this.baselineAlpha;
|
||||
}
|
||||
}
|
||||
|
||||
// Detect events
|
||||
const isApnea = br < this.apneaThresh;
|
||||
const isHypopnea = this.baselineBR && br < this.baselineBR * (1 - this.hypopneaDrop) && !isApnea;
|
||||
const inEvent = isApnea || isHypopnea;
|
||||
|
||||
if (inEvent) {
|
||||
if (!this.currentEvent) {
|
||||
// Start new event
|
||||
this.currentEvent = {
|
||||
type: isApnea ? 'apnea' : 'hypopnea',
|
||||
startTs: timestamp,
|
||||
};
|
||||
this.eventSamples = [br];
|
||||
} else {
|
||||
this.eventSamples.push(br);
|
||||
// Upgrade hypopnea to apnea if BR drops further
|
||||
if (isApnea && this.currentEvent.type === 'hypopnea') {
|
||||
this.currentEvent.type = 'apnea';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Event ended
|
||||
if (this.currentEvent) {
|
||||
const duration = timestamp - this.currentEvent.startTs;
|
||||
if (duration >= this.minDurationSec) {
|
||||
const avgBR = this.eventSamples.reduce((a, b) => a + b, 0) / this.eventSamples.length;
|
||||
const event = {
|
||||
type: this.currentEvent.type,
|
||||
startTs: this.currentEvent.startTs,
|
||||
endTs: timestamp,
|
||||
durationSec: duration,
|
||||
avgBR,
|
||||
};
|
||||
this.events.push(event);
|
||||
|
||||
// Track hourly
|
||||
const hourIdx = Math.floor((this.currentEvent.startTs - this.startTime) / 3600);
|
||||
this.hourlyEvents.set(hourIdx, (this.hourlyEvents.get(hourIdx) || 0) + 1);
|
||||
}
|
||||
this.currentEvent = null;
|
||||
this.eventSamples = [];
|
||||
}
|
||||
}
|
||||
|
||||
return { isApnea, isHypopnea, baseline: this.baselineBR, br };
|
||||
}
|
||||
|
||||
getAHI() {
|
||||
const hours = this.lastTime && this.startTime
|
||||
? (this.lastTime - this.startTime) / 3600
|
||||
: 0;
|
||||
if (hours < 0.01) return { ahi: 0, hours, events: 0, severity: 'Insufficient data' };
|
||||
|
||||
const totalEvents = this.events.length;
|
||||
const ahi = totalEvents / hours;
|
||||
|
||||
let severity;
|
||||
if (ahi < 5) severity = 'Normal';
|
||||
else if (ahi < 15) severity = 'Mild';
|
||||
else if (ahi < 30) severity = 'Moderate';
|
||||
else severity = 'Severe';
|
||||
|
||||
return { ahi, hours, events: totalEvents, severity };
|
||||
}
|
||||
|
||||
getHourlyAHI() {
|
||||
const result = [];
|
||||
for (const [hour, count] of [...this.hourlyEvents.entries()].sort((a, b) => a[0] - b[0])) {
|
||||
result.push({ hour, events: count, ahi: count }); // events per 1 hour
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
getEventSummary() {
|
||||
const apneas = this.events.filter(e => e.type === 'apnea');
|
||||
const hypopneas = this.events.filter(e => e.type === 'hypopnea');
|
||||
|
||||
return {
|
||||
totalEvents: this.events.length,
|
||||
apneas: apneas.length,
|
||||
hypopneas: hypopneas.length,
|
||||
avgApneaDuration: apneas.length > 0
|
||||
? apneas.reduce((s, e) => s + e.durationSec, 0) / apneas.length : 0,
|
||||
avgHypopneaDuration: hypopneas.length > 0
|
||||
? hypopneas.reduce((s, e) => s + e.durationSec, 0) / hypopneas.length : 0,
|
||||
maxDuration: this.events.length > 0
|
||||
? Math.max(...this.events.map(e => e.durationSec)) : 0,
|
||||
baselineBR: this.baselineBR || 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Packet parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseVitalsJsonl(record) {
|
||||
if (record.type !== 'vitals') return null;
|
||||
return { timestamp: record.timestamp, nodeId: record.node_id, br: record.breathing_bpm || 0 };
|
||||
}
|
||||
|
||||
function parseVitalsUdp(buf) {
|
||||
if (buf.length < 32) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== VITALS_MAGIC && magic !== FUSED_MAGIC) return null;
|
||||
return {
|
||||
timestamp: Date.now() / 1000,
|
||||
nodeId: buf.readUInt8(4),
|
||||
br: buf.readUInt16LE(6) / 100,
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const detector = new ApneaDetector({
|
||||
apneaThresh: APNEA_THRESH,
|
||||
hypopneaDrop: HYPOPNEA_DROP,
|
||||
minDurationSec: MIN_DURATION_SEC,
|
||||
});
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let vitalsCount = 0;
|
||||
let lastPrintTs = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
|
||||
const v = parseVitalsJsonl(record);
|
||||
if (!v) continue;
|
||||
|
||||
const state = detector.ingest(v.timestamp, v.br);
|
||||
vitalsCount++;
|
||||
|
||||
// Print new events immediately
|
||||
const lastEvent = detector.events.length > 0 ? detector.events[detector.events.length - 1] : null;
|
||||
if (lastEvent && lastEvent.endTs === v.timestamp) {
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
type: 'event',
|
||||
event_type: lastEvent.type,
|
||||
start: lastEvent.startTs,
|
||||
end: lastEvent.endTs,
|
||||
duration_sec: +lastEvent.durationSec.toFixed(1),
|
||||
avg_br: +lastEvent.avgBR.toFixed(2),
|
||||
}));
|
||||
} else {
|
||||
const ts = new Date(lastEvent.startTs * 1000).toISOString().slice(11, 19);
|
||||
const tag = lastEvent.type === 'apnea' ? '!! APNEA ' : '~ HYPOPNEA';
|
||||
console.log(`[${ts}] ${tag} | ${lastEvent.durationSec.toFixed(1)}s | avg BR ${lastEvent.avgBR.toFixed(1)} BPM`);
|
||||
}
|
||||
}
|
||||
|
||||
// Periodic status
|
||||
const tsMs = v.timestamp * 1000;
|
||||
if (tsMs - lastPrintTs >= INTERVAL_MS * 2) {
|
||||
if (!JSON_OUTPUT) {
|
||||
const ahi = detector.getAHI();
|
||||
const ts = new Date(v.timestamp * 1000).toISOString().slice(11, 19);
|
||||
console.log(`[${ts}] BR ${v.br.toFixed(1)} | baseline ${(state.baseline || 0).toFixed(1)} | AHI ${ahi.ahi.toFixed(1)} (${ahi.severity}) | ${ahi.events} events / ${ahi.hours.toFixed(2)} hrs`);
|
||||
}
|
||||
lastPrintTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Final summary
|
||||
const ahi = detector.getAHI();
|
||||
const summary = detector.getEventSummary();
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
type: 'summary',
|
||||
ahi: +ahi.ahi.toFixed(2),
|
||||
severity: ahi.severity,
|
||||
hours: +ahi.hours.toFixed(3),
|
||||
...summary,
|
||||
hourly: detector.getHourlyAHI(),
|
||||
}));
|
||||
} else {
|
||||
console.log('\n' + '='.repeat(60));
|
||||
console.log('APNEA SCREENING SUMMARY');
|
||||
console.log('DISCLAIMER: Pre-screening only. Consult a physician.');
|
||||
console.log('='.repeat(60));
|
||||
console.log(`Monitored: ${ahi.hours.toFixed(2)} hours (${vitalsCount} samples)`);
|
||||
console.log(`AHI: ${ahi.ahi.toFixed(1)} events/hour`);
|
||||
console.log(`Severity: ${ahi.severity}`);
|
||||
console.log(`Total events: ${summary.totalEvents}`);
|
||||
console.log(` Apneas: ${summary.apneas} (avg ${summary.avgApneaDuration.toFixed(1)}s)`);
|
||||
console.log(` Hypopneas: ${summary.hypopneas} (avg ${summary.avgHypopneaDuration.toFixed(1)}s)`);
|
||||
console.log(` Longest event: ${summary.maxDuration.toFixed(1)}s`);
|
||||
console.log(`Baseline BR: ${summary.baselineBR.toFixed(1)} BPM`);
|
||||
|
||||
const hourly = detector.getHourlyAHI();
|
||||
if (hourly.length > 0) {
|
||||
console.log('\nHourly breakdown:');
|
||||
for (const h of hourly) {
|
||||
const bar = '\u2588'.repeat(Math.min(h.events, 40));
|
||||
console.log(` Hour ${h.hour}: ${bar} ${h.events} events (AHI ${h.ahi})`);
|
||||
}
|
||||
}
|
||||
|
||||
// Event timeline
|
||||
if (detector.events.length > 0 && detector.events.length <= 50) {
|
||||
console.log('\nEvent timeline:');
|
||||
for (const e of detector.events) {
|
||||
const ts = new Date(e.startTs * 1000).toISOString().slice(11, 19);
|
||||
const tag = e.type === 'apnea' ? 'APNEA ' : 'HYPOPNEA';
|
||||
console.log(` [${ts}] ${tag} ${e.durationSec.toFixed(1)}s (BR ${e.avgBR.toFixed(1)})`);
|
||||
}
|
||||
} else if (detector.events.length > 50) {
|
||||
console.log(`\n(${detector.events.length} events total, showing first/last 5)`);
|
||||
for (const e of detector.events.slice(0, 5)) {
|
||||
const ts = new Date(e.startTs * 1000).toISOString().slice(11, 19);
|
||||
console.log(` [${ts}] ${e.type.padEnd(8)} ${e.durationSec.toFixed(1)}s`);
|
||||
}
|
||||
console.log(' ...');
|
||||
for (const e of detector.events.slice(-5)) {
|
||||
const ts = new Date(e.startTs * 1000).toISOString().slice(11, 19);
|
||||
console.log(` [${ts}] ${e.type.padEnd(8)} ${e.durationSec.toFixed(1)}s`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live UDP mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const detector = new ApneaDetector({
|
||||
apneaThresh: APNEA_THRESH,
|
||||
hypopneaDrop: HYPOPNEA_DROP,
|
||||
minDurationSec: MIN_DURATION_SEC,
|
||||
});
|
||||
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', (buf) => {
|
||||
const v = parseVitalsUdp(buf);
|
||||
if (!v) return;
|
||||
|
||||
const state = detector.ingest(v.timestamp, v.br);
|
||||
|
||||
// Alert on new events
|
||||
const lastEvent = detector.events.length > 0 ? detector.events[detector.events.length - 1] : null;
|
||||
if (lastEvent && Math.abs(lastEvent.endTs - v.timestamp) < 2) {
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
type: 'event', event_type: lastEvent.type,
|
||||
duration_sec: +lastEvent.durationSec.toFixed(1),
|
||||
avg_br: +lastEvent.avgBR.toFixed(2),
|
||||
}));
|
||||
} else {
|
||||
const tag = lastEvent.type === 'apnea' ? '!! APNEA' : '~ HYPOPNEA';
|
||||
console.log(`${tag} | ${lastEvent.durationSec.toFixed(1)}s | avg BR ${lastEvent.avgBR.toFixed(1)}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
if (!JSON_OUTPUT) {
|
||||
const ahi = detector.getAHI();
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
console.log('=== APNEA SCREENING (ADR-077) ===');
|
||||
console.log('DISCLAIMER: Pre-screening only. Not a diagnostic device.');
|
||||
console.log('');
|
||||
console.log(`AHI: ${ahi.ahi.toFixed(1)} events/hour | Severity: ${ahi.severity}`);
|
||||
console.log(`Events: ${ahi.events} in ${ahi.hours.toFixed(2)} hours`);
|
||||
console.log(`Baseline BR: ${(detector.baselineBR || 0).toFixed(1)} BPM`);
|
||||
|
||||
if (detector.events.length > 0) {
|
||||
console.log('\nRecent events:');
|
||||
for (const e of detector.events.slice(-5)) {
|
||||
const ts = new Date(e.startTs * 1000).toISOString().slice(11, 19);
|
||||
console.log(` [${ts}] ${e.type.padEnd(8)} ${e.durationSec.toFixed(1)}s (BR ${e.avgBR.toFixed(1)})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, INTERVAL_MS);
|
||||
|
||||
server.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Apnea Detector listening on UDP :${PORT}`);
|
||||
console.log('DISCLAIMER: Pre-screening only. Consult a physician.\n');
|
||||
}
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
const ahi = detector.getAHI();
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`\nSession AHI: ${ahi.ahi.toFixed(1)} (${ahi.severity}) | ${ahi.events} events / ${ahi.hours.toFixed(2)} hrs`);
|
||||
}
|
||||
server.close();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,674 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-075: CSI Subcarrier Correlation Graph Visualizer
|
||||
*
|
||||
* ASCII visualization of the subcarrier correlation graph used by the
|
||||
* min-cut person counter. Shows per-person subcarrier clusters, graph
|
||||
* connectivity, and correlation heatmap in real-time.
|
||||
*
|
||||
* Usage:
|
||||
* # Live from ESP32 nodes via UDP
|
||||
* node scripts/csi-graph-visualizer.js --port 5006
|
||||
*
|
||||
* # Replay from recorded CSI data
|
||||
* node scripts/csi-graph-visualizer.js --replay data/recordings/pretrain-1775182186.csi.jsonl
|
||||
*
|
||||
* # Show correlation heatmap only
|
||||
* node scripts/csi-graph-visualizer.js --replay FILE --mode heatmap
|
||||
*
|
||||
* ADR: docs/adr/ADR-075-mincut-person-separation.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
interval: { type: 'string', short: 'i', default: '2000' },
|
||||
window: { type: 'string', short: 'w', default: '2000' },
|
||||
mode: { type: 'string', short: 'm', default: 'all' },
|
||||
node: { type: 'string', short: 'n', default: '0' },
|
||||
'corr-threshold': { type: 'string', default: '0.3' },
|
||||
'cut-threshold': { type: 'string', default: '2.0' },
|
||||
'var-floor': { type: 'string', default: '0.5' },
|
||||
width: { type: 'string', default: '80' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const WINDOW_MS = parseInt(args.window, 10);
|
||||
const CORR_THRESHOLD = parseFloat(args['corr-threshold']);
|
||||
const CUT_THRESHOLD = parseFloat(args['cut-threshold']);
|
||||
const VAR_FLOOR = parseFloat(args['var-floor']);
|
||||
const MODE = args.mode; // 'all', 'heatmap', 'clusters', 'spectrum'
|
||||
const TARGET_NODE = parseInt(args.node, 10);
|
||||
const WIDTH = parseInt(args.width, 10);
|
||||
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
// Color palette for person clusters (ANSI 256)
|
||||
const PERSON_COLORS = [
|
||||
'\x1b[31m', // red
|
||||
'\x1b[32m', // green
|
||||
'\x1b[34m', // blue
|
||||
'\x1b[33m', // yellow
|
||||
'\x1b[35m', // magenta
|
||||
'\x1b[36m', // cyan
|
||||
'\x1b[91m', // bright red
|
||||
'\x1b[92m', // bright green
|
||||
];
|
||||
const RESET = '\x1b[0m';
|
||||
const DIM = '\x1b[2m';
|
||||
const BOLD = '\x1b[1m';
|
||||
|
||||
// Heatmap characters (11 levels of intensity)
|
||||
const HEAT = [' ', '\u2591', '\u2591', '\u2592', '\u2592', '\u2593', '\u2593', '\u2588', '\u2588', '\u2588', '\u2588'];
|
||||
|
||||
// Bar chart characters
|
||||
const BARS = ['\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588'];
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sliding window (same as mincut-person-counter.js)
|
||||
// ---------------------------------------------------------------------------
|
||||
class SubcarrierWindow {
|
||||
constructor(maxAgeMs) {
|
||||
this.maxAgeMs = maxAgeMs;
|
||||
this.frames = [];
|
||||
this.nSubcarriers = 0;
|
||||
}
|
||||
|
||||
push(timestamp, amplitudes) {
|
||||
this.nSubcarriers = amplitudes.length;
|
||||
this.frames.push({ timestamp, amplitudes: Float64Array.from(amplitudes) });
|
||||
const cutoff = timestamp - this.maxAgeMs;
|
||||
while (this.frames.length > 0 && this.frames[0].timestamp < cutoff) {
|
||||
this.frames.shift();
|
||||
}
|
||||
}
|
||||
|
||||
get length() { return this.frames.length; }
|
||||
|
||||
correlationMatrix() {
|
||||
const nFrames = this.frames.length;
|
||||
const nSc = this.nSubcarriers;
|
||||
if (nFrames < 5 || nSc === 0) return null;
|
||||
|
||||
const mean = new Float64Array(nSc);
|
||||
const std = new Float64Array(nSc);
|
||||
|
||||
for (let f = 0; f < nFrames; f++) {
|
||||
const amp = this.frames[f].amplitudes;
|
||||
for (let i = 0; i < nSc; i++) mean[i] += amp[i];
|
||||
}
|
||||
for (let i = 0; i < nSc; i++) mean[i] /= nFrames;
|
||||
|
||||
for (let f = 0; f < nFrames; f++) {
|
||||
const amp = this.frames[f].amplitudes;
|
||||
for (let i = 0; i < nSc; i++) {
|
||||
const d = amp[i] - mean[i];
|
||||
std[i] += d * d;
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < nSc; i++) std[i] = Math.sqrt(std[i] / (nFrames - 1));
|
||||
|
||||
const activeIndices = [];
|
||||
for (let i = 0; i < nSc; i++) {
|
||||
if (std[i] > VAR_FLOOR) activeIndices.push(i);
|
||||
}
|
||||
|
||||
const n = activeIndices.length;
|
||||
if (n < 2) return { matrix: null, n: 0, activeIndices, mean, std };
|
||||
|
||||
const matrix = new Float64Array(n * n);
|
||||
for (let ai = 0; ai < n; ai++) {
|
||||
matrix[ai * n + ai] = 1.0;
|
||||
const si = activeIndices[ai];
|
||||
for (let aj = ai + 1; aj < n; aj++) {
|
||||
const sj = activeIndices[aj];
|
||||
let cov = 0;
|
||||
for (let f = 0; f < nFrames; f++) {
|
||||
const amp = this.frames[f].amplitudes;
|
||||
cov += (amp[si] - mean[si]) * (amp[sj] - mean[sj]);
|
||||
}
|
||||
cov /= (nFrames - 1);
|
||||
const denom = std[si] * std[sj];
|
||||
const r = denom > 1e-10 ? cov / denom : 0;
|
||||
matrix[ai * n + aj] = r;
|
||||
matrix[aj * n + ai] = r;
|
||||
}
|
||||
}
|
||||
|
||||
return { matrix, n, activeIndices, mean, std };
|
||||
}
|
||||
|
||||
/** Get latest amplitudes */
|
||||
latestAmplitudes() {
|
||||
if (this.frames.length === 0) return null;
|
||||
return this.frames[this.frames.length - 1].amplitudes;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Graph + Stoer-Wagner (minimal copy from mincut-person-counter.js)
|
||||
// ---------------------------------------------------------------------------
|
||||
class WeightedGraph {
|
||||
constructor(n) {
|
||||
this.n = n;
|
||||
this.adj = new Array(n);
|
||||
for (let i = 0; i < n; i++) this.adj[i] = new Map();
|
||||
this.edgeCount = 0;
|
||||
}
|
||||
addEdge(u, v, w) {
|
||||
if (u === v) return;
|
||||
if (!this.adj[u].has(v)) this.edgeCount++;
|
||||
this.adj[u].set(v, w);
|
||||
this.adj[v].set(u, w);
|
||||
}
|
||||
static fromCorrelation(matrix, n, threshold) {
|
||||
const g = new WeightedGraph(n);
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = i + 1; j < n; j++) {
|
||||
const r = Math.abs(matrix[i * n + j]);
|
||||
if (r > threshold) g.addEdge(i, j, r);
|
||||
}
|
||||
}
|
||||
return g;
|
||||
}
|
||||
connectedComponents() {
|
||||
const visited = new Uint8Array(this.n);
|
||||
const components = [];
|
||||
for (let start = 0; start < this.n; start++) {
|
||||
if (visited[start]) continue;
|
||||
const comp = [];
|
||||
const queue = [start];
|
||||
visited[start] = 1;
|
||||
while (queue.length > 0) {
|
||||
const u = queue.shift();
|
||||
comp.push(u);
|
||||
for (const [v] of this.adj[u]) {
|
||||
if (!visited[v]) { visited[v] = 1; queue.push(v); }
|
||||
}
|
||||
}
|
||||
components.push(comp);
|
||||
}
|
||||
return components;
|
||||
}
|
||||
subgraph(vertices) {
|
||||
const newIdx = new Map();
|
||||
vertices.forEach((v, i) => newIdx.set(v, i));
|
||||
const sub = new WeightedGraph(vertices.length);
|
||||
for (const u of vertices) {
|
||||
for (const [v, w] of this.adj[u]) {
|
||||
if (newIdx.has(v) && u < v) sub.addEdge(newIdx.get(u), newIdx.get(v), w);
|
||||
}
|
||||
}
|
||||
return { graph: sub, mapping: vertices };
|
||||
}
|
||||
}
|
||||
|
||||
function stoerWagner(graph) {
|
||||
const n = graph.n;
|
||||
if (n <= 1) return { minCutValue: Infinity, partition: [Array.from({length: n}, (_, i) => i), []] };
|
||||
|
||||
const adj = new Array(n);
|
||||
for (let i = 0; i < n; i++) adj[i] = new Map(graph.adj[i]);
|
||||
const groups = new Array(n);
|
||||
for (let i = 0; i < n; i++) groups[i] = [i];
|
||||
|
||||
let activeVertices = Array.from({length: n}, (_, i) => i);
|
||||
let bestCut = Infinity;
|
||||
let bestPartitionSide = null;
|
||||
|
||||
while (activeVertices.length > 1) {
|
||||
const key = new Float64Array(n);
|
||||
const inA = new Uint8Array(n);
|
||||
let s = -1, t = -1;
|
||||
|
||||
for (let iter = 0; iter < activeVertices.length; iter++) {
|
||||
let best = -1, bestKey = -Infinity;
|
||||
for (const v of activeVertices) {
|
||||
if (!inA[v] && key[v] > bestKey) { bestKey = key[v]; best = v; }
|
||||
}
|
||||
if (best === -1) {
|
||||
for (const v of activeVertices) { if (!inA[v]) { best = v; break; } }
|
||||
}
|
||||
s = t; t = best; inA[best] = 1;
|
||||
if (adj[best]) {
|
||||
for (const [nb, w] of adj[best]) {
|
||||
if (activeVertices.includes(nb) && !inA[nb]) key[nb] += w;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let cutOfPhase = 0;
|
||||
if (adj[t]) {
|
||||
for (const [nb, w] of adj[t]) {
|
||||
if (activeVertices.includes(nb) && nb !== t) cutOfPhase += w;
|
||||
}
|
||||
}
|
||||
|
||||
if (s === -1 || t === -1) break;
|
||||
if (cutOfPhase < bestCut) { bestCut = cutOfPhase; bestPartitionSide = [...groups[t]]; }
|
||||
|
||||
if (adj[t]) {
|
||||
for (const [nb, w] of adj[t]) {
|
||||
if (nb === s) continue;
|
||||
const ex = adj[s].get(nb) || 0;
|
||||
adj[s].set(nb, ex + w);
|
||||
adj[nb].delete(t);
|
||||
adj[nb].set(s, ex + w);
|
||||
}
|
||||
}
|
||||
adj[s].delete(t);
|
||||
groups[s] = groups[s].concat(groups[t]);
|
||||
groups[t] = [];
|
||||
activeVertices = activeVertices.filter(v => v !== t);
|
||||
}
|
||||
|
||||
if (!bestPartitionSide || bestPartitionSide.length === 0) {
|
||||
return { minCutValue: Infinity, partition: [Array.from({length: n}, (_, i) => i), []] };
|
||||
}
|
||||
const sideSet = new Set(bestPartitionSide);
|
||||
const sideA = [], sideB = [];
|
||||
for (let i = 0; i < n; i++) { (sideSet.has(i) ? sideA : sideB).push(i); }
|
||||
return { minCutValue: bestCut, partition: [sideA, sideB] };
|
||||
}
|
||||
|
||||
function separatePersons(graph, cutThreshold, maxPersons) {
|
||||
const components = graph.connectedComponents();
|
||||
const personGroups = [];
|
||||
for (const comp of components) {
|
||||
if (comp.length < 2) continue;
|
||||
_split(graph, comp, cutThreshold, maxPersons, personGroups);
|
||||
}
|
||||
return personGroups;
|
||||
}
|
||||
|
||||
function _split(graph, vertices, cutThreshold, maxPersons, result) {
|
||||
if (vertices.length < 2 || result.length >= maxPersons) {
|
||||
if (vertices.length >= 2) result.push(vertices);
|
||||
return;
|
||||
}
|
||||
const { graph: sub, mapping } = graph.subgraph(vertices);
|
||||
const { minCutValue, partition } = stoerWagner(sub);
|
||||
if (minCutValue >= cutThreshold || partition[0].length === 0 || partition[1].length === 0) {
|
||||
result.push(vertices);
|
||||
return;
|
||||
}
|
||||
_split(graph, partition[0].map(i => mapping[i]), cutThreshold, maxPersons, result);
|
||||
_split(graph, partition[1].map(i => mapping[i]), cutThreshold, maxPersons, result);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Visualization renderers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Render correlation heatmap (downsampled to fit terminal width).
|
||||
* Rows and columns = active subcarrier indices.
|
||||
*/
|
||||
function renderHeatmap(corr, width) {
|
||||
if (!corr || !corr.matrix) return [' (insufficient data for heatmap)'];
|
||||
const { matrix, n, activeIndices } = corr;
|
||||
|
||||
const lines = [];
|
||||
lines.push(`${BOLD}Correlation Heatmap${RESET} (${n} active subcarriers, threshold=${CORR_THRESHOLD})`);
|
||||
|
||||
// Downsample if needed
|
||||
const maxCols = Math.min(n, width - 8);
|
||||
const step = Math.max(1, Math.ceil(n / maxCols));
|
||||
const displayN = Math.ceil(n / step);
|
||||
|
||||
// Header row: subcarrier indices
|
||||
let header = ' ';
|
||||
for (let j = 0; j < displayN; j++) {
|
||||
const sc = activeIndices[j * step];
|
||||
header += (sc < 10 ? `${sc} ` : `${sc}`).slice(0, 2);
|
||||
}
|
||||
lines.push(DIM + header + RESET);
|
||||
|
||||
for (let i = 0; i < displayN; i++) {
|
||||
const sc = activeIndices[i * step];
|
||||
let row = ` ${String(sc).padStart(3)} `;
|
||||
|
||||
for (let j = 0; j < displayN; j++) {
|
||||
const ii = i * step, jj = j * step;
|
||||
const val = Math.abs(matrix[ii * n + jj]);
|
||||
const level = Math.min(10, Math.floor(val * 10));
|
||||
|
||||
if (val > CORR_THRESHOLD) {
|
||||
row += `\x1b[33m${HEAT[level]}${RESET} `;
|
||||
} else {
|
||||
row += `${DIM}${HEAT[level]}${RESET} `;
|
||||
}
|
||||
}
|
||||
lines.push(row);
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render subcarrier spectrum bar with person cluster coloring.
|
||||
*/
|
||||
function renderSpectrum(window, personGroups, activeIndices) {
|
||||
const amp = window.latestAmplitudes();
|
||||
if (!amp) return [' (no data)'];
|
||||
|
||||
const lines = [];
|
||||
const nSc = window.nSubcarriers;
|
||||
|
||||
// Build subcarrier-to-person mapping
|
||||
const scToPerson = new Int8Array(nSc).fill(-1);
|
||||
if (personGroups && activeIndices) {
|
||||
for (let p = 0; p < personGroups.length; p++) {
|
||||
for (const graphIdx of personGroups[p]) {
|
||||
if (graphIdx < activeIndices.length) {
|
||||
scToPerson[activeIndices[graphIdx]] = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find max amplitude for normalization
|
||||
let maxAmp = 0;
|
||||
for (let i = 0; i < nSc; i++) {
|
||||
if (amp[i] > maxAmp) maxAmp = amp[i];
|
||||
}
|
||||
if (maxAmp === 0) maxAmp = 1;
|
||||
|
||||
lines.push(`${BOLD}Spectrum${RESET} (${nSc} subcarriers, colored by person cluster)`);
|
||||
|
||||
// Render bar
|
||||
let bar = ' ';
|
||||
for (let i = 0; i < nSc; i++) {
|
||||
const level = Math.floor((amp[i] / maxAmp) * 7.99);
|
||||
const ch = BARS[Math.max(0, Math.min(7, level))];
|
||||
const personIdx = scToPerson[i];
|
||||
if (personIdx >= 0 && personIdx < PERSON_COLORS.length) {
|
||||
bar += PERSON_COLORS[personIdx] + ch + RESET;
|
||||
} else {
|
||||
bar += DIM + ch + RESET;
|
||||
}
|
||||
}
|
||||
lines.push(bar);
|
||||
|
||||
// Legend
|
||||
let legend = ' ';
|
||||
for (let i = 0; i < nSc; i++) {
|
||||
const p = scToPerson[i];
|
||||
if (p >= 0 && p < PERSON_COLORS.length) {
|
||||
legend += PERSON_COLORS[p] + (p + 1) + RESET;
|
||||
} else {
|
||||
legend += DIM + '.' + RESET;
|
||||
}
|
||||
}
|
||||
lines.push(legend);
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render cluster summary with per-person statistics.
|
||||
*/
|
||||
function renderClusters(personGroups, activeIndices, corr) {
|
||||
if (!personGroups || personGroups.length === 0) {
|
||||
return [' No person clusters detected'];
|
||||
}
|
||||
|
||||
const lines = [];
|
||||
lines.push(`${BOLD}Person Clusters${RESET} (${personGroups.length} detected)`);
|
||||
|
||||
for (let p = 0; p < personGroups.length; p++) {
|
||||
const group = personGroups[p];
|
||||
const color = p < PERSON_COLORS.length ? PERSON_COLORS[p] : '';
|
||||
|
||||
// Map back to subcarrier indices
|
||||
const scIds = group.map(i => activeIndices[i]);
|
||||
const scStr = scIds.length <= 16
|
||||
? scIds.join(', ')
|
||||
: scIds.slice(0, 14).join(', ') + `, ...+${scIds.length - 14}`;
|
||||
|
||||
// Compute intra-cluster average correlation
|
||||
let avgCorr = 0, count = 0;
|
||||
if (corr && corr.matrix) {
|
||||
for (let i = 0; i < group.length; i++) {
|
||||
for (let j = i + 1; j < group.length; j++) {
|
||||
avgCorr += Math.abs(corr.matrix[group[i] * corr.n + group[j]]);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count > 0) avgCorr /= count;
|
||||
}
|
||||
|
||||
lines.push(` ${color}Person ${p + 1}${RESET}: ${group.length} subcarriers, avg intra-corr=${avgCorr.toFixed(3)}`);
|
||||
lines.push(` ${DIM}SC: [${scStr}]${RESET}`);
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render graph connectivity summary.
|
||||
*/
|
||||
function renderGraphStats(graph, corr) {
|
||||
if (!graph) return [' (no graph)'];
|
||||
|
||||
const lines = [];
|
||||
const components = graph.connectedComponents();
|
||||
const density = graph.n > 1 ? (2 * graph.edgeCount) / (graph.n * (graph.n - 1)) : 0;
|
||||
|
||||
lines.push(`${BOLD}Graph${RESET}: ${graph.n} nodes, ${graph.edgeCount} edges, density=${density.toFixed(3)}, components=${components.length}`);
|
||||
|
||||
// Degree distribution summary
|
||||
const degrees = new Array(graph.n);
|
||||
let minDeg = Infinity, maxDeg = 0, sumDeg = 0;
|
||||
for (let i = 0; i < graph.n; i++) {
|
||||
degrees[i] = graph.adj[i].size;
|
||||
if (degrees[i] < minDeg) minDeg = degrees[i];
|
||||
if (degrees[i] > maxDeg) maxDeg = degrees[i];
|
||||
sumDeg += degrees[i];
|
||||
}
|
||||
const avgDeg = graph.n > 0 ? sumDeg / graph.n : 0;
|
||||
lines.push(` Degree: min=${minDeg} max=${maxDeg} avg=${avgDeg.toFixed(1)}`);
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Full render
|
||||
// ---------------------------------------------------------------------------
|
||||
function render(window, nodeId) {
|
||||
const corr = window.correlationMatrix();
|
||||
const lines = [];
|
||||
|
||||
const ts = new Date().toISOString().slice(11, 19);
|
||||
lines.push(`${BOLD}ADR-075 CSI Graph Visualizer${RESET} [${ts}] Node ${nodeId} | ${window.length} frames`);
|
||||
lines.push('═'.repeat(WIDTH));
|
||||
|
||||
let graph = null;
|
||||
let personGroups = null;
|
||||
let activeIndices = corr ? corr.activeIndices : [];
|
||||
|
||||
if (corr && corr.matrix && corr.n >= 2) {
|
||||
graph = WeightedGraph.fromCorrelation(corr.matrix, corr.n, CORR_THRESHOLD);
|
||||
personGroups = separatePersons(graph, CUT_THRESHOLD, 8);
|
||||
}
|
||||
|
||||
const personCount = personGroups ? personGroups.length : 0;
|
||||
lines.push(`${BOLD}Persons: ${personCount}${RESET} | Active subcarriers: ${activeIndices.length}/${window.nSubcarriers}`);
|
||||
lines.push('');
|
||||
|
||||
if (MODE === 'all' || MODE === 'spectrum') {
|
||||
lines.push(...renderSpectrum(window, personGroups, activeIndices));
|
||||
lines.push('');
|
||||
}
|
||||
|
||||
if (MODE === 'all' || MODE === 'clusters') {
|
||||
lines.push(...renderClusters(personGroups, activeIndices, corr));
|
||||
lines.push('');
|
||||
}
|
||||
|
||||
if (MODE === 'all' || MODE === 'heatmap') {
|
||||
lines.push(...renderHeatmap(corr, WIDTH));
|
||||
lines.push('');
|
||||
}
|
||||
|
||||
if (graph) {
|
||||
lines.push(...renderGraphStats(graph, corr));
|
||||
}
|
||||
|
||||
lines.push('═'.repeat(WIDTH));
|
||||
lines.push(`${DIM}Thresholds: corr=${CORR_THRESHOLD} cut=${CUT_THRESHOLD} var-floor=${VAR_FLOOR}${RESET}`);
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Packet parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseIqHex(iqHex, nSubcarriers) {
|
||||
const bytes = Buffer.from(iqHex, 'hex');
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = 2 + sc * 2;
|
||||
if (offset + 1 >= bytes.length) break;
|
||||
let I = bytes[offset]; let Q = bytes[offset + 1];
|
||||
if (I > 127) I -= 256;
|
||||
if (Q > 127) Q -= 256;
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
return amplitudes;
|
||||
}
|
||||
|
||||
function parseUdpPacket(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const nSubcarriers = buf.readUInt16LE(6);
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = HEADER_SIZE + sc * 2;
|
||||
if (offset + 1 >= buf.length) break;
|
||||
const I = buf.readInt8(offset);
|
||||
const Q = buf.readInt8(offset + 1);
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
return { nodeId, nSubcarriers, amplitudes, timestamp: Date.now() };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main: live mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const windows = new Map();
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', (buf) => {
|
||||
const frame = parseUdpPacket(buf);
|
||||
if (!frame) return;
|
||||
if (!windows.has(frame.nodeId)) {
|
||||
windows.set(frame.nodeId, new SubcarrierWindow(WINDOW_MS));
|
||||
}
|
||||
windows.get(frame.nodeId).push(frame.timestamp, frame.amplitudes);
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
process.stdout.write('\x1b[2J\x1b[H');
|
||||
for (const [nodeId, window] of windows) {
|
||||
if (TARGET_NODE !== 0 && nodeId !== TARGET_NODE) continue;
|
||||
console.log(render(window, nodeId));
|
||||
console.log();
|
||||
}
|
||||
if (windows.size === 0) {
|
||||
console.log('Waiting for CSI frames on UDP port ' + PORT + '...');
|
||||
}
|
||||
}, INTERVAL_MS);
|
||||
|
||||
server.bind(PORT, () => {
|
||||
console.log(`CSI Graph Visualizer listening on UDP port ${PORT}`);
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main: replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const windows = new Map();
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let lastRenderTs = 0;
|
||||
let frameCount = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
if (record.type !== 'raw_csi' || !record.iq_hex) continue;
|
||||
|
||||
const nSc = record.subcarriers || 64;
|
||||
const amplitudes = parseIqHex(record.iq_hex, nSc);
|
||||
const nodeId = record.node_id;
|
||||
const tsMs = record.timestamp * 1000;
|
||||
|
||||
if (!windows.has(nodeId)) {
|
||||
windows.set(nodeId, new SubcarrierWindow(WINDOW_MS));
|
||||
}
|
||||
windows.get(nodeId).push(tsMs, amplitudes);
|
||||
frameCount++;
|
||||
|
||||
if (lastRenderTs === 0) lastRenderTs = tsMs;
|
||||
if (tsMs - lastRenderTs >= INTERVAL_MS) {
|
||||
process.stdout.write('\x1b[2J\x1b[H');
|
||||
for (const [nid, window] of windows) {
|
||||
if (TARGET_NODE !== 0 && nid !== TARGET_NODE) continue;
|
||||
console.log(render(window, nid));
|
||||
console.log();
|
||||
}
|
||||
lastRenderTs = tsMs;
|
||||
|
||||
// Small delay for visual effect during replay
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
}
|
||||
}
|
||||
|
||||
// Final render
|
||||
console.log();
|
||||
console.log('═'.repeat(WIDTH));
|
||||
console.log(`${BOLD}Replay complete${RESET}: ${frameCount} frames`);
|
||||
for (const [nodeId, window] of windows) {
|
||||
if (TARGET_NODE !== 0 && nodeId !== TARGET_NODE) continue;
|
||||
console.log();
|
||||
console.log(render(window, nodeId));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry point
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,672 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-076: CSI Spectrogram Embedding Pipeline
|
||||
*
|
||||
* Converts raw CSI frames into 128-dim CNN embeddings by treating the
|
||||
* subcarrier x time matrix as a grayscale spectrogram image.
|
||||
*
|
||||
* Modes:
|
||||
* --live Listen on UDP for real-time CSI frames
|
||||
* --file FILE Read from a .csi.jsonl recording
|
||||
* --ascii Print ASCII spectrogram visualization
|
||||
* --ingest Send 128-dim embeddings to Cognitum Seed
|
||||
* --knn K Find K most similar past spectrograms
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/csi-spectrogram.js --file data/recordings/pretrain-1775182186.csi.jsonl --ascii
|
||||
* node scripts/csi-spectrogram.js --live --port 5006 --ingest --seed-url https://169.254.42.1:8443
|
||||
* node scripts/csi-spectrogram.js --file data/recordings/pretrain-1775182186.csi.jsonl --knn 5
|
||||
*
|
||||
* ADR: docs/adr/ADR-076-csi-spectrogram-embeddings.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
file: { type: 'string', short: 'f' },
|
||||
live: { type: 'boolean', default: false },
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
ascii: { type: 'boolean', default: false },
|
||||
ingest: { type: 'boolean', default: false },
|
||||
knn: { type: 'string', short: 'k' },
|
||||
'seed-url': { type: 'string', default: 'https://169.254.42.1:8443' },
|
||||
'seed-token': { type: 'string', default: '' },
|
||||
window: { type: 'string', short: 'w', default: '20' },
|
||||
stride: { type: 'string', short: 's', default: '10' },
|
||||
dim: { type: 'string', short: 'd', default: '128' },
|
||||
json: { type: 'boolean', default: false },
|
||||
limit: { type: 'string', short: 'l' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const WINDOW_SIZE = parseInt(args.window, 10); // frames per spectrogram
|
||||
const STRIDE = parseInt(args.stride, 10); // frames between windows
|
||||
const EMBED_DIM = parseInt(args.dim, 10); // CNN output dimension
|
||||
const KNN_K = args.knn ? parseInt(args.knn, 10) : 0;
|
||||
const LIMIT = args.limit ? parseInt(args.limit, 10) : Infinity;
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
|
||||
// ADR-018 packet constants
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
// CNN input size (ruvector/cnn expects 224x224 RGB)
|
||||
const CNN_INPUT_SIZE = 224;
|
||||
|
||||
// ASCII visualization characters (8 intensity levels)
|
||||
const BARS = [' ', '\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588'];
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// IQ Hex Parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Parse iq_hex string into subcarrier amplitudes.
|
||||
* Format: 4 hex chars per subcarrier (I byte + Q byte).
|
||||
* @param {string} iqHex - Hex-encoded I/Q data
|
||||
* @param {number} nSubcarriers - Expected number of subcarriers
|
||||
* @returns {Float32Array} Amplitude per subcarrier
|
||||
*/
|
||||
function parseIqHex(iqHex, nSubcarriers) {
|
||||
const amps = new Float32Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = sc * 4;
|
||||
if (offset + 4 > iqHex.length) break;
|
||||
const iVal = parseInt(iqHex.substring(offset, offset + 2), 16);
|
||||
const qVal = parseInt(iqHex.substring(offset + 2, offset + 4), 16);
|
||||
amps[sc] = Math.sqrt(iVal * iVal + qVal * qVal);
|
||||
}
|
||||
return amps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an ADR-018 binary UDP packet into subcarrier amplitudes.
|
||||
* @param {Buffer} buf - Raw UDP packet
|
||||
* @returns {{ nodeId: number, rssi: number, nSubcarriers: number, amplitudes: Float32Array } | null}
|
||||
*/
|
||||
function parseBinaryFrame(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const rssi = buf.readInt8(5);
|
||||
const nSubcarriers = buf.readUInt16LE(6);
|
||||
const payloadSize = buf.readUInt16LE(8);
|
||||
|
||||
if (buf.length < HEADER_SIZE + payloadSize) return null;
|
||||
|
||||
const amps = new Float32Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const off = HEADER_SIZE + sc * 2;
|
||||
if (off + 2 > buf.length) break;
|
||||
const iVal = buf[off];
|
||||
const qVal = buf[off + 1];
|
||||
amps[sc] = Math.sqrt(iVal * iVal + qVal * qVal);
|
||||
}
|
||||
|
||||
return { nodeId, rssi, nSubcarriers, amplitudes: amps };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Spectrogram Window
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
class SpectrogramWindow {
|
||||
/**
|
||||
* @param {number} nSubcarriers - Number of subcarriers per frame
|
||||
* @param {number} windowSize - Number of time frames per window
|
||||
*/
|
||||
constructor(nSubcarriers, windowSize) {
|
||||
this.nSubcarriers = nSubcarriers;
|
||||
this.windowSize = windowSize;
|
||||
/** @type {Float32Array[]} Ring buffer of amplitude vectors */
|
||||
this.frames = [];
|
||||
this.totalPushed = 0;
|
||||
}
|
||||
|
||||
/** Push a new amplitude vector. */
|
||||
push(amplitudes) {
|
||||
if (amplitudes.length !== this.nSubcarriers) {
|
||||
// Pad or truncate to expected size
|
||||
const padded = new Float32Array(this.nSubcarriers);
|
||||
padded.set(amplitudes.subarray(0, Math.min(amplitudes.length, this.nSubcarriers)));
|
||||
this.frames.push(padded);
|
||||
} else {
|
||||
this.frames.push(new Float32Array(amplitudes));
|
||||
}
|
||||
if (this.frames.length > this.windowSize) {
|
||||
this.frames.shift();
|
||||
}
|
||||
this.totalPushed++;
|
||||
}
|
||||
|
||||
/** @returns {boolean} True when window is full */
|
||||
isFull() {
|
||||
return this.frames.length >= this.windowSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subcarrier x time matrix as a flat grayscale image (0-255).
|
||||
* Layout: row-major, rows = subcarriers, cols = time frames.
|
||||
* @returns {{ pixels: Uint8Array, width: number, height: number }}
|
||||
*/
|
||||
toGrayscale() {
|
||||
const h = this.nSubcarriers;
|
||||
const w = this.windowSize;
|
||||
const pixels = new Uint8Array(h * w);
|
||||
|
||||
// Find min/max across entire window for normalization
|
||||
let min = Infinity;
|
||||
let max = -Infinity;
|
||||
for (let t = 0; t < w; t++) {
|
||||
const frame = this.frames[t];
|
||||
for (let sc = 0; sc < h; sc++) {
|
||||
const v = frame[sc];
|
||||
if (v < min) min = v;
|
||||
if (v > max) max = v;
|
||||
}
|
||||
}
|
||||
|
||||
const range = max - min || 1;
|
||||
for (let sc = 0; sc < h; sc++) {
|
||||
for (let t = 0; t < w; t++) {
|
||||
const v = this.frames[t][sc];
|
||||
pixels[sc * w + t] = Math.round(255 * (v - min) / range);
|
||||
}
|
||||
}
|
||||
|
||||
return { pixels, width: w, height: h };
|
||||
}
|
||||
|
||||
/**
|
||||
* Upsample grayscale to CNN input size using nearest-neighbor interpolation.
|
||||
* Replicates to 3-channel RGB as required by @ruvector/cnn.
|
||||
* @returns {Uint8Array} RGB pixel data (CNN_INPUT_SIZE * CNN_INPUT_SIZE * 3)
|
||||
*/
|
||||
toCnnInput() {
|
||||
const { pixels, width, height } = this.toGrayscale();
|
||||
const out = new Uint8Array(CNN_INPUT_SIZE * CNN_INPUT_SIZE * 3);
|
||||
|
||||
for (let y = 0; y < CNN_INPUT_SIZE; y++) {
|
||||
const srcY = Math.min(Math.floor(y * height / CNN_INPUT_SIZE), height - 1);
|
||||
for (let x = 0; x < CNN_INPUT_SIZE; x++) {
|
||||
const srcX = Math.min(Math.floor(x * width / CNN_INPUT_SIZE), width - 1);
|
||||
const gray = pixels[srcY * width + srcX];
|
||||
const dstIdx = (y * CNN_INPUT_SIZE + x) * 3;
|
||||
out[dstIdx] = gray;
|
||||
out[dstIdx + 1] = gray;
|
||||
out[dstIdx + 2] = gray;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ASCII Visualization
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Print an ASCII spectrogram of the current window.
|
||||
* Rows = subcarrier index (downsampled), columns = time.
|
||||
*/
|
||||
function printAsciiSpectrogram(window, meta = {}) {
|
||||
const { pixels, width, height } = window.toGrayscale();
|
||||
|
||||
// Downsample rows to fit terminal (max 32 rows)
|
||||
const maxRows = Math.min(height, 32);
|
||||
const rowStep = Math.ceil(height / maxRows);
|
||||
|
||||
const lines = [];
|
||||
lines.push(`--- Spectrogram [${height}sc x ${width}t] node=${meta.nodeId || '?'} rssi=${meta.rssi || '?'} ---`);
|
||||
|
||||
for (let r = 0; r < maxRows; r++) {
|
||||
const sc = r * rowStep;
|
||||
const label = String(sc).padStart(3);
|
||||
let row = `sc${label} |`;
|
||||
for (let t = 0; t < width; t++) {
|
||||
const v = pixels[sc * width + t];
|
||||
const level = Math.min(Math.floor(v / 29), BARS.length - 1);
|
||||
row += BARS[level];
|
||||
}
|
||||
row += '|';
|
||||
lines.push(row);
|
||||
}
|
||||
|
||||
lines.push(` ${''.padStart(width + 2, '-')}`);
|
||||
lines.push(` t=0${''.padStart(width - 6)}t=${width - 1}`);
|
||||
console.log(lines.join('\n'));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CNN Embedding
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
let cnnEmbedder = null;
|
||||
let cnnInitialized = false;
|
||||
|
||||
/**
|
||||
* Initialize the CNN embedder from vendor WASM.
|
||||
*/
|
||||
async function initCnn() {
|
||||
if (cnnInitialized) return;
|
||||
|
||||
// Load WASM bindings directly to work around the CnnEmbedder wrapper bug:
|
||||
// The wrapper's constructor calls `new wasm.WasmCnnEmbedder(wasmConfig)` which
|
||||
// consumes (destroys) the EmbedderConfig pointer, then tries to read
|
||||
// `wasmConfig.embedding_dim` from the now-null pointer. We use the WASM
|
||||
// classes directly and track the dimension ourselves.
|
||||
const wasmPath = path.resolve(
|
||||
__dirname, '..', 'vendor', 'ruvector', 'npm', 'packages', 'ruvector-cnn'
|
||||
);
|
||||
const wasmModule = require(path.join(wasmPath, 'ruvector_cnn_wasm.js'));
|
||||
const wasmBuffer = fs.readFileSync(path.join(wasmPath, 'ruvector_cnn_wasm_bg.wasm'));
|
||||
await wasmModule.default(wasmBuffer);
|
||||
|
||||
const config = new wasmModule.EmbedderConfig();
|
||||
config.input_size = CNN_INPUT_SIZE;
|
||||
config.embedding_dim = EMBED_DIM;
|
||||
config.normalize = true;
|
||||
|
||||
// Save dim before construction (constructor consumes config)
|
||||
const savedDim = EMBED_DIM;
|
||||
const inner = new wasmModule.WasmCnnEmbedder(config);
|
||||
|
||||
// Wrap in a compatible interface
|
||||
cnnEmbedder = {
|
||||
_inner: inner,
|
||||
embeddingDim: savedDim,
|
||||
extract(imageData, width, height) {
|
||||
return new Float32Array(inner.extract(imageData, width, height));
|
||||
},
|
||||
cosineSimilarity(a, b) {
|
||||
return inner.cosine_similarity(a, b);
|
||||
},
|
||||
};
|
||||
|
||||
cnnInitialized = true;
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`[cnn] Initialized: embeddingDim=${savedDim}, inputSize=${CNN_INPUT_SIZE}x${CNN_INPUT_SIZE}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract CNN embedding from a spectrogram window.
|
||||
* @param {SpectrogramWindow} window
|
||||
* @returns {Float32Array} 128-dim embedding
|
||||
*/
|
||||
function extractEmbedding(window) {
|
||||
const rgbPixels = window.toCnnInput();
|
||||
return cnnEmbedder.extract(rgbPixels, CNN_INPUT_SIZE, CNN_INPUT_SIZE);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Embedding Store (in-memory kNN)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
class EmbeddingStore {
|
||||
constructor() {
|
||||
/** @type {{ embedding: Float32Array, timestamp: number, nodeId: number, windowIdx: number }[]} */
|
||||
this.entries = [];
|
||||
}
|
||||
|
||||
add(embedding, meta) {
|
||||
this.entries.push({ embedding, ...meta });
|
||||
}
|
||||
|
||||
/**
|
||||
* Find k nearest neighbors by cosine similarity.
|
||||
* @param {Float32Array} query
|
||||
* @param {number} k
|
||||
* @returns {{ index: number, similarity: number, meta: object }[]}
|
||||
*/
|
||||
knn(query, k) {
|
||||
const scores = this.entries.map((entry, index) => ({
|
||||
index,
|
||||
similarity: cosineSimilarity(query, entry.embedding),
|
||||
timestamp: entry.timestamp,
|
||||
nodeId: entry.nodeId,
|
||||
windowIdx: entry.windowIdx,
|
||||
}));
|
||||
scores.sort((a, b) => b.similarity - a.similarity);
|
||||
return scores.slice(0, k);
|
||||
}
|
||||
|
||||
get size() { return this.entries.length; }
|
||||
}
|
||||
|
||||
function cosineSimilarity(a, b) {
|
||||
let dot = 0, normA = 0, normB = 0;
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
dot += a[i] * b[i];
|
||||
normA += a[i] * a[i];
|
||||
normB += b[i] * b[i];
|
||||
}
|
||||
const denom = Math.sqrt(normA) * Math.sqrt(normB);
|
||||
return denom > 0 ? dot / denom : 0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Cognitum Seed Ingest
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Send a 128-dim embedding to Cognitum Seed's RVF vector store.
|
||||
* @param {Float32Array} embedding
|
||||
* @param {object} meta
|
||||
*/
|
||||
async function ingestToSeed(embedding, meta) {
|
||||
const seedUrl = args['seed-url'];
|
||||
const token = args['seed-token'] || process.env.SEED_TOKEN;
|
||||
if (!token) {
|
||||
console.error('[seed] No token provided (--seed-token or $SEED_TOKEN)');
|
||||
return;
|
||||
}
|
||||
|
||||
const https = require('https');
|
||||
const payload = JSON.stringify({
|
||||
store: 'csi-spectrograms',
|
||||
vectors: [{
|
||||
id: `spectrogram-${meta.nodeId}-${meta.windowIdx}`,
|
||||
values: Array.from(embedding),
|
||||
metadata: {
|
||||
node_id: meta.nodeId,
|
||||
timestamp: meta.timestamp,
|
||||
window_idx: meta.windowIdx,
|
||||
rssi: meta.rssi,
|
||||
subcarriers: meta.nSubcarriers,
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = new URL('/v1/vectors/upsert', seedUrl);
|
||||
const req = https.request(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Length': Buffer.byteLength(payload),
|
||||
},
|
||||
rejectUnauthorized: false,
|
||||
}, (res) => {
|
||||
let body = '';
|
||||
res.on('data', (chunk) => body += chunk);
|
||||
res.on('end', () => {
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
resolve(JSON.parse(body));
|
||||
} else {
|
||||
reject(new Error(`Seed HTTP ${res.statusCode}: ${body}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
req.on('error', reject);
|
||||
req.write(payload);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// File Mode: Read JSONL Recording
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function processFile(filePath) {
|
||||
await initCnn();
|
||||
|
||||
const store = new EmbeddingStore();
|
||||
const windows = new Map(); // nodeId -> SpectrogramWindow
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let windowCount = 0;
|
||||
let lastNodeId = 0;
|
||||
let lastRssi = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (frameCount >= LIMIT) break;
|
||||
|
||||
let frame;
|
||||
try {
|
||||
frame = JSON.parse(line);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
|
||||
const nodeId = frame.node_id || 0;
|
||||
const nSubcarriers = frame.subcarriers || 64;
|
||||
const iqHex = frame.iq_hex || '';
|
||||
|
||||
if (!iqHex) continue;
|
||||
|
||||
const amplitudes = parseIqHex(iqHex, nSubcarriers);
|
||||
lastNodeId = nodeId;
|
||||
lastRssi = frame.rssi || 0;
|
||||
|
||||
if (!windows.has(nodeId)) {
|
||||
windows.set(nodeId, new SpectrogramWindow(nSubcarriers, WINDOW_SIZE));
|
||||
}
|
||||
|
||||
const win = windows.get(nodeId);
|
||||
win.push(amplitudes);
|
||||
frameCount++;
|
||||
|
||||
// Check if this window is ready and stride condition met
|
||||
if (win.isFull() && (win.totalPushed - WINDOW_SIZE) % STRIDE === 0) {
|
||||
const t0 = Date.now();
|
||||
const embedding = extractEmbedding(win);
|
||||
const embedMs = Date.now() - t0;
|
||||
|
||||
const meta = {
|
||||
timestamp: frame.timestamp,
|
||||
nodeId,
|
||||
windowIdx: windowCount,
|
||||
rssi: frame.rssi || 0,
|
||||
nSubcarriers,
|
||||
};
|
||||
|
||||
store.add(embedding, meta);
|
||||
|
||||
if (args.ascii) {
|
||||
printAsciiSpectrogram(win, { nodeId, rssi: frame.rssi });
|
||||
}
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
type: 'embedding',
|
||||
windowIdx: windowCount,
|
||||
nodeId,
|
||||
dim: embedding.length,
|
||||
embedMs,
|
||||
embedding: Array.from(embedding).map(v => +v.toFixed(6)),
|
||||
}));
|
||||
} else {
|
||||
const embSnippet = Array.from(embedding.subarray(0, 4)).map(v => v.toFixed(4)).join(', ');
|
||||
console.log(`[window ${windowCount}] node=${nodeId} embed=[${embSnippet}, ...] (${embedMs}ms)`);
|
||||
}
|
||||
|
||||
// kNN search against previous windows
|
||||
if (KNN_K > 0 && store.size > 1) {
|
||||
const neighbors = store.knn(embedding, KNN_K + 1);
|
||||
// Skip self (first result)
|
||||
const results = neighbors.filter(n => n.windowIdx !== windowCount).slice(0, KNN_K);
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({ type: 'knn', query: windowCount, results }));
|
||||
} else {
|
||||
console.log(` kNN(${KNN_K}): ${results.map(r => `w${r.windowIdx}(${r.similarity.toFixed(3)})`).join(' ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Cognitum Seed ingest
|
||||
if (args.ingest) {
|
||||
try {
|
||||
await ingestToSeed(embedding, meta);
|
||||
if (!JSON_OUTPUT) console.log(` -> ingested to Seed`);
|
||||
} catch (err) {
|
||||
console.error(` -> Seed ingest failed: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
windowCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`\nProcessed ${frameCount} frames -> ${windowCount} spectrogram windows`);
|
||||
console.log(`Store contains ${store.size} embeddings of dimension ${EMBED_DIM}`);
|
||||
}
|
||||
|
||||
return store;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live Mode: UDP Listener
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function processLive() {
|
||||
await initCnn();
|
||||
|
||||
const store = new EmbeddingStore();
|
||||
const windows = new Map();
|
||||
let windowCount = 0;
|
||||
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', async (msg, rinfo) => {
|
||||
// Try binary ADR-018 format first
|
||||
let parsed = parseBinaryFrame(msg);
|
||||
let nodeId, nSubcarriers, amplitudes, rssi;
|
||||
|
||||
if (parsed) {
|
||||
nodeId = parsed.nodeId;
|
||||
nSubcarriers = parsed.nSubcarriers;
|
||||
amplitudes = parsed.amplitudes;
|
||||
rssi = parsed.rssi;
|
||||
} else {
|
||||
// Try JSONL format
|
||||
try {
|
||||
const frame = JSON.parse(msg.toString());
|
||||
nodeId = frame.node_id || 0;
|
||||
nSubcarriers = frame.subcarriers || 64;
|
||||
amplitudes = parseIqHex(frame.iq_hex || '', nSubcarriers);
|
||||
rssi = frame.rssi || 0;
|
||||
} catch {
|
||||
return; // Unknown format
|
||||
}
|
||||
}
|
||||
|
||||
if (!windows.has(nodeId)) {
|
||||
windows.set(nodeId, new SpectrogramWindow(nSubcarriers, WINDOW_SIZE));
|
||||
}
|
||||
|
||||
const win = windows.get(nodeId);
|
||||
win.push(amplitudes);
|
||||
|
||||
if (win.isFull() && (win.totalPushed - WINDOW_SIZE) % STRIDE === 0) {
|
||||
const t0 = Date.now();
|
||||
const embedding = extractEmbedding(win);
|
||||
const embedMs = Date.now() - t0;
|
||||
|
||||
const meta = {
|
||||
timestamp: Date.now() / 1000,
|
||||
nodeId,
|
||||
windowIdx: windowCount,
|
||||
rssi,
|
||||
nSubcarriers,
|
||||
};
|
||||
|
||||
store.add(embedding, meta);
|
||||
|
||||
if (args.ascii) {
|
||||
printAsciiSpectrogram(win, { nodeId, rssi });
|
||||
}
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
type: 'embedding',
|
||||
windowIdx: windowCount,
|
||||
nodeId,
|
||||
dim: embedding.length,
|
||||
embedMs,
|
||||
embedding: Array.from(embedding).map(v => +v.toFixed(6)),
|
||||
}));
|
||||
} else {
|
||||
const embSnippet = Array.from(embedding.subarray(0, 4)).map(v => v.toFixed(4)).join(', ');
|
||||
console.log(`[window ${windowCount}] node=${nodeId} rssi=${rssi} embed=[${embSnippet}, ...] (${embedMs}ms)`);
|
||||
}
|
||||
|
||||
if (KNN_K > 0 && store.size > 1) {
|
||||
const neighbors = store.knn(embedding, KNN_K + 1);
|
||||
const results = neighbors.filter(n => n.windowIdx !== windowCount).slice(0, KNN_K);
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(` kNN(${KNN_K}): ${results.map(r => `w${r.windowIdx}(${r.similarity.toFixed(3)})`).join(' ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (args.ingest) {
|
||||
try {
|
||||
await ingestToSeed(embedding, meta);
|
||||
} catch (err) {
|
||||
console.error(` -> Seed ingest failed: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
windowCount++;
|
||||
}
|
||||
});
|
||||
|
||||
server.on('listening', () => {
|
||||
const addr = server.address();
|
||||
console.log(`[live] Listening for CSI on UDP ${addr.address}:${addr.port}`);
|
||||
console.log(`[live] Window: ${WINDOW_SIZE} frames, stride: ${STRIDE}, embed dim: ${EMBED_DIM}`);
|
||||
if (KNN_K > 0) console.log(`[live] kNN search: k=${KNN_K}`);
|
||||
if (args.ingest) console.log(`[live] Ingesting to Cognitum Seed at ${args['seed-url']}`);
|
||||
});
|
||||
|
||||
server.bind(PORT);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function main() {
|
||||
if (!args.file && !args.live) {
|
||||
console.error('Usage: node scripts/csi-spectrogram.js --file <path> [--ascii] [--knn K]');
|
||||
console.error(' node scripts/csi-spectrogram.js --live [--port 5006] [--ingest]');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (args.file) {
|
||||
const filePath = path.resolve(args.file);
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
await processFile(filePath);
|
||||
} else {
|
||||
await processLive();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('Fatal:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,715 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Device Fingerprinting via RF Emissions — Multi-Frequency Mesh Application
|
||||
*
|
||||
* Identifies electronic devices by their unique RF characteristics across
|
||||
* multiple WiFi channels. Each device creates distinctive subcarrier patterns:
|
||||
*
|
||||
* - WiFi APs: unique transmit power, phase noise, clock drift
|
||||
* - Printers: motor EMI creates specific subcarrier modulation
|
||||
* - Microwaves: 2.45 GHz magnetron radiates across channels 8-11
|
||||
* - Bluetooth: frequency-hopping creates transient spikes
|
||||
*
|
||||
* Correlates WiFi scan SSID/signal with CSI patterns to build per-device
|
||||
* fingerprints, then detects when devices become active or inactive.
|
||||
*
|
||||
* Requires multi-frequency mesh scanning (ADR-073): 2 ESP32 nodes hopping
|
||||
* across channels 1, 3, 5, 6, 9, 11.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/device-fingerprint.js
|
||||
* node scripts/device-fingerprint.js --port 5006 --duration 120
|
||||
* node scripts/device-fingerprint.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/device-fingerprint.js --learn 30
|
||||
*
|
||||
* ADR: docs/adr/ADR-078-multifreq-mesh-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
duration: { type: 'string', short: 'd' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
interval: { type: 'string', short: 'i', default: '5000' },
|
||||
learn: { type: 'string', short: 'l', default: '20' },
|
||||
json: { type: 'boolean', default: false },
|
||||
'save-fingerprints': { type: 'string' },
|
||||
'load-fingerprints': { type: 'string' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const DURATION_MS = args.duration ? parseInt(args.duration, 10) * 1000 : null;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const LEARN_DURATION = parseInt(args.learn, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
const CHANNEL_FREQ = {};
|
||||
for (let ch = 1; ch <= 13; ch++) CHANNEL_FREQ[ch] = 2412 + (ch - 1) * 5;
|
||||
|
||||
const NODE1_CHANNELS = [1, 6, 11];
|
||||
const NODE2_CHANNELS = [3, 5, 9];
|
||||
|
||||
// Known devices from WiFi scan (these are the devices we can fingerprint)
|
||||
const KNOWN_DEVICES = [
|
||||
{ id: 'ruv-net', ssid: 'ruv.net', channel: 5, signal: 100, type: 'router' },
|
||||
{ id: 'cohen-guest', ssid: 'Cohen-Guest', channel: 5, signal: 100, type: 'router' },
|
||||
{ id: 'cogeco-21b20', ssid: 'COGECO-21B20', channel: 11, signal: 100, type: 'router' },
|
||||
{ id: 'hp-printer', ssid: 'DIRECT-fa-HP M255 LaserJet', channel: 5, signal: 94, type: 'printer' },
|
||||
{ id: 'conclusion', ssid: 'conclusion mesh', channel: 3, signal: 44, type: 'mesh-node' },
|
||||
{ id: 'netgear72', ssid: 'NETGEAR72', channel: 9, signal: 42, type: 'router' },
|
||||
{ id: 'cogeco-4321', ssid: 'COGECO-4321', channel: 11, signal: 30, type: 'router' },
|
||||
{ id: 'innanen', ssid: 'Innanen', channel: 6, signal: 19, type: 'router' },
|
||||
];
|
||||
|
||||
// Activity states
|
||||
const ACTIVITY = {
|
||||
UNKNOWN: 'unknown',
|
||||
ACTIVE: 'active',
|
||||
IDLE: 'idle',
|
||||
CHANGED: 'changed',
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Device fingerprint
|
||||
// ---------------------------------------------------------------------------
|
||||
class DeviceFingerprint {
|
||||
constructor(device) {
|
||||
this.device = device;
|
||||
this.id = device.id;
|
||||
this.channel = device.channel;
|
||||
|
||||
// Per-subcarrier signature (learned during training)
|
||||
this.baselineMean = null; // Float64Array
|
||||
this.baselineStd = null; // Float64Array
|
||||
this.varianceProfile = null; // Float64Array - characteristic variance pattern
|
||||
this.nSub = 0;
|
||||
this.trainCount = 0;
|
||||
|
||||
// Welford accumulators for training
|
||||
this._sum = null;
|
||||
this._sumSq = null;
|
||||
this._varSum = null;
|
||||
this._varSumSq = null;
|
||||
this._frameAmps = []; // store recent frames for variance computation
|
||||
|
||||
// Runtime state
|
||||
this.activity = ACTIVITY.UNKNOWN;
|
||||
this.lastScore = 0;
|
||||
this.lastSeen = 0;
|
||||
this.activityHistory = [];
|
||||
this.maxHistory = 30;
|
||||
}
|
||||
|
||||
/** Ingest a training frame */
|
||||
train(amplitudes) {
|
||||
const n = amplitudes.length;
|
||||
if (!this._sum) {
|
||||
this.nSub = n;
|
||||
this._sum = new Float64Array(n);
|
||||
this._sumSq = new Float64Array(n);
|
||||
}
|
||||
|
||||
this.trainCount++;
|
||||
for (let i = 0; i < n && i < this.nSub; i++) {
|
||||
this._sum[i] += amplitudes[i];
|
||||
this._sumSq[i] += amplitudes[i] * amplitudes[i];
|
||||
}
|
||||
|
||||
// Keep last 10 frames for variance profile
|
||||
this._frameAmps.push(new Float64Array(amplitudes));
|
||||
if (this._frameAmps.length > 10) this._frameAmps.shift();
|
||||
}
|
||||
|
||||
/** Finalize training */
|
||||
finalizeTrain() {
|
||||
if (this.trainCount < 3 || !this._sum) return false;
|
||||
|
||||
this.baselineMean = new Float64Array(this.nSub);
|
||||
this.baselineStd = new Float64Array(this.nSub);
|
||||
|
||||
for (let i = 0; i < this.nSub; i++) {
|
||||
this.baselineMean[i] = this._sum[i] / this.trainCount;
|
||||
const variance = (this._sumSq[i] / this.trainCount) - (this.baselineMean[i] ** 2);
|
||||
this.baselineStd[i] = Math.sqrt(Math.max(0, variance));
|
||||
if (this.baselineStd[i] < 0.1) this.baselineStd[i] = 0.1;
|
||||
}
|
||||
|
||||
// Compute variance profile from stored frames
|
||||
if (this._frameAmps.length >= 3) {
|
||||
this.varianceProfile = new Float64Array(this.nSub);
|
||||
for (let i = 0; i < this.nSub; i++) {
|
||||
let sum = 0, sumSq = 0;
|
||||
for (const frame of this._frameAmps) {
|
||||
sum += frame[i];
|
||||
sumSq += frame[i] * frame[i];
|
||||
}
|
||||
const n = this._frameAmps.length;
|
||||
const mean = sum / n;
|
||||
this.varianceProfile[i] = (sumSq / n) - (mean * mean);
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up training data
|
||||
this._sum = null;
|
||||
this._sumSq = null;
|
||||
this._frameAmps = [];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Score a new frame against this device's fingerprint.
|
||||
* Returns a similarity score (0 = no match, 1 = perfect match).
|
||||
*/
|
||||
score(amplitudes) {
|
||||
if (!this.baselineMean) return 0;
|
||||
|
||||
const n = Math.min(amplitudes.length, this.nSub);
|
||||
let matchScore = 0;
|
||||
let count = 0;
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
// Normalized difference from baseline
|
||||
const diff = Math.abs(amplitudes[i] - this.baselineMean[i]);
|
||||
const normalizedDiff = diff / this.baselineStd[i];
|
||||
|
||||
// Score: 1.0 if within 1 std, decreasing beyond
|
||||
const subScore = Math.exp(-0.5 * normalizedDiff * normalizedDiff);
|
||||
matchScore += subScore;
|
||||
count++;
|
||||
}
|
||||
|
||||
return count > 0 ? matchScore / count : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect activity change.
|
||||
* Compare current frame's variance against baseline variance profile.
|
||||
*/
|
||||
detectActivity(amplitudes, timestamp) {
|
||||
const similarity = this.score(amplitudes);
|
||||
this.lastScore = similarity;
|
||||
this.lastSeen = timestamp;
|
||||
|
||||
// Activity thresholds
|
||||
const prevActivity = this.activity;
|
||||
if (similarity > 0.7) {
|
||||
this.activity = ACTIVITY.ACTIVE;
|
||||
} else if (similarity > 0.4) {
|
||||
this.activity = ACTIVITY.CHANGED;
|
||||
} else {
|
||||
this.activity = ACTIVITY.IDLE;
|
||||
}
|
||||
|
||||
// Record transitions
|
||||
if (prevActivity !== this.activity && prevActivity !== ACTIVITY.UNKNOWN) {
|
||||
this.activityHistory.push({
|
||||
timestamp,
|
||||
from: prevActivity,
|
||||
to: this.activity,
|
||||
score: similarity.toFixed(3),
|
||||
});
|
||||
if (this.activityHistory.length > this.maxHistory) this.activityHistory.shift();
|
||||
}
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
ssid: this.device.ssid,
|
||||
type: this.device.type,
|
||||
channel: this.channel,
|
||||
activity: this.activity,
|
||||
similarity: similarity.toFixed(3),
|
||||
changed: prevActivity !== this.activity && prevActivity !== ACTIVITY.UNKNOWN,
|
||||
};
|
||||
}
|
||||
|
||||
/** Export fingerprint for persistence */
|
||||
exportFingerprint() {
|
||||
return {
|
||||
id: this.id,
|
||||
device: this.device,
|
||||
nSub: this.nSub,
|
||||
trainCount: this.trainCount,
|
||||
baselineMean: this.baselineMean ? Array.from(this.baselineMean) : null,
|
||||
baselineStd: this.baselineStd ? Array.from(this.baselineStd) : null,
|
||||
varianceProfile: this.varianceProfile ? Array.from(this.varianceProfile) : null,
|
||||
};
|
||||
}
|
||||
|
||||
/** Import fingerprint from saved data */
|
||||
importFingerprint(data) {
|
||||
this.nSub = data.nSub;
|
||||
this.trainCount = data.trainCount;
|
||||
this.baselineMean = data.baselineMean ? new Float64Array(data.baselineMean) : null;
|
||||
this.baselineStd = data.baselineStd ? new Float64Array(data.baselineStd) : null;
|
||||
this.varianceProfile = data.varianceProfile ? new Float64Array(data.varianceProfile) : null;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Device fingerprint manager
|
||||
// ---------------------------------------------------------------------------
|
||||
class FingerprintManager {
|
||||
constructor(learnDuration) {
|
||||
this.learnDuration = learnDuration;
|
||||
this.fingerprints = new Map(); // id -> DeviceFingerprint
|
||||
this.learning = true;
|
||||
this.startTime = null;
|
||||
this.totalFrames = 0;
|
||||
|
||||
// Initialize fingerprints for known devices
|
||||
for (const device of KNOWN_DEVICES) {
|
||||
this.fingerprints.set(device.id, new DeviceFingerprint(device));
|
||||
}
|
||||
}
|
||||
|
||||
ingestFrame(channel, amplitudes, timestamp) {
|
||||
this.totalFrames++;
|
||||
if (!this.startTime) this.startTime = timestamp;
|
||||
|
||||
// Learning phase: train fingerprints for devices on this channel
|
||||
if (this.learning) {
|
||||
for (const fp of this.fingerprints.values()) {
|
||||
if (fp.channel === channel) {
|
||||
fp.train(amplitudes);
|
||||
}
|
||||
}
|
||||
|
||||
if (timestamp - this.startTime >= this.learnDuration) {
|
||||
// Finalize all fingerprints
|
||||
let trained = 0;
|
||||
for (const fp of this.fingerprints.values()) {
|
||||
if (fp.finalizeTrain()) trained++;
|
||||
}
|
||||
this.learning = false;
|
||||
return { event: 'learn_complete', trained, total: this.fingerprints.size };
|
||||
}
|
||||
|
||||
return { event: 'learning', elapsed: timestamp - this.startTime, duration: this.learnDuration };
|
||||
}
|
||||
|
||||
// Detection phase: score all devices on this channel
|
||||
const results = [];
|
||||
for (const fp of this.fingerprints.values()) {
|
||||
if (fp.channel === channel) {
|
||||
const result = fp.detectActivity(amplitudes, timestamp);
|
||||
results.push(result);
|
||||
}
|
||||
}
|
||||
|
||||
return { event: 'detect', results };
|
||||
}
|
||||
|
||||
/** Get current device activity summary */
|
||||
getSummary() {
|
||||
const devices = [];
|
||||
for (const fp of this.fingerprints.values()) {
|
||||
devices.push({
|
||||
id: fp.id,
|
||||
ssid: fp.device.ssid,
|
||||
type: fp.device.type,
|
||||
channel: fp.channel,
|
||||
activity: fp.activity,
|
||||
similarity: fp.lastScore.toFixed(3),
|
||||
trained: fp.baselineMean !== null,
|
||||
trainFrames: fp.trainCount,
|
||||
transitions: fp.activityHistory.length,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
learning: this.learning,
|
||||
totalFrames: this.totalFrames,
|
||||
devices: devices.sort((a, b) => parseFloat(b.similarity) - parseFloat(a.similarity)),
|
||||
};
|
||||
}
|
||||
|
||||
/** Save fingerprints to file */
|
||||
saveFingerprints(filePath) {
|
||||
const data = {};
|
||||
for (const [id, fp] of this.fingerprints) {
|
||||
if (fp.baselineMean) {
|
||||
data[id] = fp.exportFingerprint();
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
|
||||
return Object.keys(data).length;
|
||||
}
|
||||
|
||||
/** Load fingerprints from file */
|
||||
loadFingerprints(filePath) {
|
||||
if (!fs.existsSync(filePath)) return 0;
|
||||
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
let loaded = 0;
|
||||
for (const [id, fpData] of Object.entries(data)) {
|
||||
if (this.fingerprints.has(id)) {
|
||||
this.fingerprints.get(id).importFingerprint(fpData);
|
||||
loaded++;
|
||||
}
|
||||
}
|
||||
if (loaded > 0) this.learning = false;
|
||||
return loaded;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CSI parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseIqHex(iqHex, nSubcarriers) {
|
||||
const bytes = Buffer.from(iqHex, 'hex');
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = 2 + sc * 2;
|
||||
if (offset + 1 >= bytes.length) break;
|
||||
let I = bytes[offset];
|
||||
let Q = bytes[offset + 1];
|
||||
if (I > 127) I -= 256;
|
||||
if (Q > 127) Q -= 256;
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
return amplitudes;
|
||||
}
|
||||
|
||||
function parseCSIFrame(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const nSubcarriers = buf.readUInt16LE(6);
|
||||
const freqMhz = buf.readUInt32LE(8);
|
||||
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = HEADER_SIZE + sc * 2;
|
||||
if (offset + 1 >= buf.length) break;
|
||||
const I = buf.readInt8(offset);
|
||||
const Q = buf.readInt8(offset + 1);
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
let channel = 0;
|
||||
if (freqMhz >= 2412 && freqMhz <= 2484) {
|
||||
channel = freqMhz === 2484 ? 14 : Math.round((freqMhz - 2412) / 5) + 1;
|
||||
}
|
||||
|
||||
return { nodeId, nSubcarriers, freqMhz, amplitudes, channel };
|
||||
}
|
||||
|
||||
const nodeChannelIdx = { 1: 0, 2: 0 };
|
||||
function assignChannel(nodeId) {
|
||||
const channels = nodeId === 1 ? NODE1_CHANNELS : NODE2_CHANNELS;
|
||||
const ch = channels[nodeChannelIdx[nodeId] % channels.length];
|
||||
nodeChannelIdx[nodeId]++;
|
||||
return ch;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Visualization
|
||||
// ---------------------------------------------------------------------------
|
||||
function renderDeviceTable(manager) {
|
||||
const summary = manager.getSummary();
|
||||
const lines = [];
|
||||
|
||||
lines.push('');
|
||||
lines.push(' DEVICE FINGERPRINTING — RF EMISSIONS ANALYSIS');
|
||||
lines.push(' ' + '='.repeat(60));
|
||||
lines.push('');
|
||||
|
||||
if (summary.learning) {
|
||||
const elapsed = manager.startTime ? Date.now() / 1000 - manager.startTime : 0;
|
||||
const progress = Math.min(100, (elapsed / manager.learnDuration) * 100);
|
||||
const barLen = Math.floor(progress / 2);
|
||||
const bar = '\u2588'.repeat(barLen) + '\u2591'.repeat(50 - barLen);
|
||||
lines.push(` Learning device signatures: [${bar}] ${progress.toFixed(0)}%`);
|
||||
lines.push(` Frames: ${summary.totalFrames}`);
|
||||
lines.push('');
|
||||
}
|
||||
|
||||
// Device activity table
|
||||
const activitySymbol = {
|
||||
[ACTIVITY.ACTIVE]: '[ON] ',
|
||||
[ACTIVITY.IDLE]: '[off]',
|
||||
[ACTIVITY.CHANGED]: '[CHG]',
|
||||
[ACTIVITY.UNKNOWN]: '[ ? ]',
|
||||
};
|
||||
|
||||
lines.push(' Device Type Ch Similarity Status');
|
||||
lines.push(' ' + '-'.repeat(65));
|
||||
|
||||
for (const dev of summary.devices) {
|
||||
const status = activitySymbol[dev.activity] || '[ ? ]';
|
||||
const trained = dev.trained ? '' : ' (untrained)';
|
||||
lines.push(
|
||||
` ${dev.ssid.substring(0, 28).padEnd(30)} ${dev.type.padEnd(10)} ${String(dev.channel).padStart(2)} ` +
|
||||
`${dev.similarity.padStart(7)} ${status}${trained}`
|
||||
);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function renderTimeline(manager) {
|
||||
const summary = manager.getSummary();
|
||||
const lines = [];
|
||||
|
||||
lines.push('');
|
||||
lines.push(' Activity Transitions:');
|
||||
lines.push(' ' + '-'.repeat(50));
|
||||
|
||||
let hasTransitions = false;
|
||||
for (const dev of summary.devices) {
|
||||
const fp = manager.fingerprints.get(dev.id);
|
||||
if (fp && fp.activityHistory.length > 0) {
|
||||
hasTransitions = true;
|
||||
const recent = fp.activityHistory.slice(-3);
|
||||
for (const t of recent) {
|
||||
const time = new Date(t.timestamp * 1000).toISOString().substring(11, 19);
|
||||
lines.push(` ${time} ${dev.ssid.substring(0, 20).padEnd(20)} ${t.from} -> ${t.to} (score=${t.score})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasTransitions) {
|
||||
lines.push(' (no transitions detected yet)');
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function renderChannelActivity(manager) {
|
||||
const summary = manager.getSummary();
|
||||
const lines = [];
|
||||
|
||||
lines.push('');
|
||||
lines.push(' Per-Channel Device Activity:');
|
||||
|
||||
const channels = [...new Set(summary.devices.map(d => d.channel))].sort((a, b) => a - b);
|
||||
for (const ch of channels) {
|
||||
const devs = summary.devices.filter(d => d.channel === ch);
|
||||
const activeCount = devs.filter(d => d.activity === ACTIVITY.ACTIVE).length;
|
||||
lines.push(` ch${ch} (${CHANNEL_FREQ[ch]} MHz): ${activeCount}/${devs.length} devices active`);
|
||||
for (const dev of devs) {
|
||||
const bar = '\u2588'.repeat(Math.floor(parseFloat(dev.similarity) * 20));
|
||||
lines.push(` ${dev.ssid.substring(0, 18).padEnd(18)} ${bar} ${dev.similarity}`);
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Global state
|
||||
// ---------------------------------------------------------------------------
|
||||
const manager = new FingerprintManager(LEARN_DURATION);
|
||||
let lastDisplayMs = 0;
|
||||
|
||||
// Load saved fingerprints if specified
|
||||
if (args['load-fingerprints']) {
|
||||
const loaded = manager.loadFingerprints(args['load-fingerprints']);
|
||||
if (!JSON_OUTPUT) console.log(`Loaded ${loaded} fingerprints from ${args['load-fingerprints']}`);
|
||||
}
|
||||
|
||||
function displayUpdate() {
|
||||
if (JSON_OUTPUT) {
|
||||
const summary = manager.getSummary();
|
||||
console.log(JSON.stringify({
|
||||
timestamp: Date.now() / 1000,
|
||||
learning: summary.learning,
|
||||
totalFrames: summary.totalFrames,
|
||||
devices: summary.devices.map(d => ({
|
||||
id: d.id, ssid: d.ssid, activity: d.activity,
|
||||
similarity: d.similarity, channel: d.channel,
|
||||
})),
|
||||
}));
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
console.log(renderDeviceTable(manager));
|
||||
console.log(renderTimeline(manager));
|
||||
console.log(renderChannelActivity(manager));
|
||||
console.log('');
|
||||
console.log(` Total frames: ${manager.totalFrames}`);
|
||||
console.log(' Press Ctrl+C to exit');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const sock = dgram.createSocket('udp4');
|
||||
|
||||
sock.on('message', (buf) => {
|
||||
if (buf.length < 4) return;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return;
|
||||
|
||||
const frame = parseCSIFrame(buf);
|
||||
if (!frame) return;
|
||||
|
||||
const result = manager.ingestFrame(frame.channel, frame.amplitudes, Date.now() / 1000);
|
||||
|
||||
// Announce learning completion
|
||||
if (result && result.event === 'learn_complete' && !JSON_OUTPUT) {
|
||||
console.log(`\nLearning complete! Trained ${result.trained}/${result.total} device fingerprints`);
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
if (now - lastDisplayMs >= INTERVAL_MS) {
|
||||
displayUpdate();
|
||||
lastDisplayMs = now;
|
||||
}
|
||||
});
|
||||
|
||||
sock.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Device Fingerprinter listening on UDP port ${PORT}`);
|
||||
console.log(`Learning duration: ${LEARN_DURATION}s`);
|
||||
console.log(`Known devices: ${KNOWN_DEVICES.length}`);
|
||||
console.log('Waiting for CSI frames...');
|
||||
}
|
||||
});
|
||||
|
||||
if (DURATION_MS) {
|
||||
setTimeout(() => {
|
||||
displayUpdate();
|
||||
if (args['save-fingerprints']) {
|
||||
const saved = manager.saveFingerprints(args['save-fingerprints']);
|
||||
if (!JSON_OUTPUT) console.log(`Saved ${saved} fingerprints to ${args['save-fingerprints']}`);
|
||||
}
|
||||
sock.close();
|
||||
process.exit(0);
|
||||
}, DURATION_MS);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
let windowCount = 0;
|
||||
let learnComplete = false;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
if (record.type !== 'raw_csi' || !record.iq_hex) continue;
|
||||
|
||||
const amplitudes = parseIqHex(record.iq_hex, record.subcarriers || 64);
|
||||
const channel = record.channel || assignChannel(record.node_id);
|
||||
|
||||
const result = manager.ingestFrame(channel, amplitudes, record.timestamp);
|
||||
frameCount++;
|
||||
|
||||
if (result && result.event === 'learn_complete' && !learnComplete) {
|
||||
learnComplete = true;
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`\nLearning complete at t=${record.timestamp.toFixed(1)}s`);
|
||||
console.log(`Trained ${result.trained}/${result.total} device fingerprints`);
|
||||
console.log('');
|
||||
}
|
||||
}
|
||||
|
||||
const tsMs = record.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
windowCount++;
|
||||
const summary = manager.getSummary();
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
window: windowCount,
|
||||
timestamp: record.timestamp,
|
||||
learning: summary.learning,
|
||||
devices: summary.devices.map(d => ({
|
||||
id: d.id, activity: d.activity, similarity: d.similarity,
|
||||
})),
|
||||
}));
|
||||
} else if (!summary.learning) {
|
||||
// Compact per-window output
|
||||
const active = summary.devices.filter(d => d.activity === ACTIVITY.ACTIVE);
|
||||
const changed = summary.devices.filter(d => d.activity === ACTIVITY.CHANGED);
|
||||
let line = ` [${String(windowCount).padStart(4)}] t=${record.timestamp.toFixed(1)}s active: `;
|
||||
line += active.length > 0
|
||||
? active.map(d => `${d.ssid.substring(0, 15)}(${d.similarity})`).join(', ')
|
||||
: '(none)';
|
||||
if (changed.length > 0) {
|
||||
line += ' changed: ' + changed.map(d => d.ssid.substring(0, 12)).join(', ');
|
||||
}
|
||||
console.log(line);
|
||||
}
|
||||
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Save fingerprints if requested
|
||||
if (args['save-fingerprints']) {
|
||||
const saved = manager.saveFingerprints(args['save-fingerprints']);
|
||||
if (!JSON_OUTPUT) console.log(`\nSaved ${saved} fingerprints to ${args['save-fingerprints']}`);
|
||||
}
|
||||
|
||||
// Final summary
|
||||
if (!JSON_OUTPUT) {
|
||||
const summary = manager.getSummary();
|
||||
console.log('');
|
||||
console.log('='.repeat(60));
|
||||
console.log('DEVICE FINGERPRINT SUMMARY');
|
||||
console.log('='.repeat(60));
|
||||
console.log(renderDeviceTable(manager));
|
||||
console.log(renderTimeline(manager));
|
||||
|
||||
// Statistics
|
||||
const trained = summary.devices.filter(d => d.trained).length;
|
||||
const active = summary.devices.filter(d => d.activity === ACTIVITY.ACTIVE).length;
|
||||
console.log('');
|
||||
console.log(` Trained fingerprints: ${trained}/${summary.devices.length}`);
|
||||
console.log(` Currently active: ${active}/${summary.devices.length}`);
|
||||
console.log(` Total frames: ${frameCount}`);
|
||||
console.log(` Analysis windows: ${windowCount}`);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry point
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,534 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-077: Gait Analysis / Movement Disorder Detection
|
||||
*
|
||||
* Extracts walking cadence, stride regularity, asymmetry, and tremor indicators
|
||||
* from CSI motion energy and phase variance time series.
|
||||
*
|
||||
* DISCLAIMER: This is an informational tool, NOT a medical device.
|
||||
* Do not use for clinical diagnosis of movement disorders.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/gait-analyzer.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/gait-analyzer.js --port 5006
|
||||
* node scripts/gait-analyzer.js --replay FILE --json
|
||||
*
|
||||
* ADR: docs/adr/ADR-077-novel-rf-sensing-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
json: { type: 'boolean', default: false },
|
||||
interval: { type: 'string', short: 'i', default: '5000' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ADR-018 packet constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const VITALS_MAGIC = 0xC5110002;
|
||||
const FUSED_MAGIC = 0xC5110004;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Simple FFT (radix-2 DIT)
|
||||
// ---------------------------------------------------------------------------
|
||||
function fft(re, im) {
|
||||
const n = re.length;
|
||||
if (n <= 1) return;
|
||||
|
||||
for (let i = 1, j = 0; i < n; i++) {
|
||||
let bit = n >> 1;
|
||||
for (; j & bit; bit >>= 1) j ^= bit;
|
||||
j ^= bit;
|
||||
if (i < j) {
|
||||
[re[i], re[j]] = [re[j], re[i]];
|
||||
[im[i], im[j]] = [im[j], im[i]];
|
||||
}
|
||||
}
|
||||
|
||||
for (let len = 2; len <= n; len *= 2) {
|
||||
const half = len / 2;
|
||||
const angle = -2 * Math.PI / len;
|
||||
const wRe = Math.cos(angle);
|
||||
const wIm = Math.sin(angle);
|
||||
|
||||
for (let i = 0; i < n; i += len) {
|
||||
let curRe = 1, curIm = 0;
|
||||
for (let j = 0; j < half; j++) {
|
||||
const tRe = curRe * re[i + j + half] - curIm * im[i + j + half];
|
||||
const tIm = curRe * im[i + j + half] + curIm * re[i + j + half];
|
||||
re[i + j + half] = re[i + j] - tRe;
|
||||
im[i + j + half] = im[i + j] - tIm;
|
||||
re[i + j] += tRe;
|
||||
im[i + j] += tIm;
|
||||
const newCurRe = curRe * wRe - curIm * wIm;
|
||||
curIm = curRe * wIm + curIm * wRe;
|
||||
curRe = newCurRe;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function nextPow2(n) {
|
||||
let p = 1;
|
||||
while (p < n) p *= 2;
|
||||
return p;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Gait analysis engine
|
||||
// ---------------------------------------------------------------------------
|
||||
class GaitAnalyzer {
|
||||
constructor() {
|
||||
// Per-node time series buffers (5-second windows at ~22 fps or ~1 Hz vitals)
|
||||
this.motionBuffers = new Map(); // nodeId -> [{ timestamp, motion }]
|
||||
this.phaseVarBuffers = new Map(); // nodeId -> [{ timestamp, phaseVar }]
|
||||
this.maxAge = 5.0; // seconds
|
||||
this.results = [];
|
||||
}
|
||||
|
||||
pushMotion(nodeId, timestamp, motion) {
|
||||
if (!this.motionBuffers.has(nodeId)) this.motionBuffers.set(nodeId, []);
|
||||
const buf = this.motionBuffers.get(nodeId);
|
||||
buf.push({ timestamp, motion });
|
||||
const cutoff = timestamp - this.maxAge;
|
||||
while (buf.length > 0 && buf[0].timestamp < cutoff) buf.shift();
|
||||
}
|
||||
|
||||
pushPhaseVar(nodeId, timestamp, phaseVar) {
|
||||
if (!this.phaseVarBuffers.has(nodeId)) this.phaseVarBuffers.set(nodeId, []);
|
||||
const buf = this.phaseVarBuffers.get(nodeId);
|
||||
buf.push({ timestamp, phaseVar });
|
||||
const cutoff = timestamp - this.maxAge;
|
||||
while (buf.length > 0 && buf[0].timestamp < cutoff) buf.shift();
|
||||
}
|
||||
|
||||
analyze(timestamp) {
|
||||
const perNode = {};
|
||||
let bestCadence = 0;
|
||||
let bestRegularity = 0;
|
||||
const cadences = [];
|
||||
|
||||
for (const [nodeId, buf] of this.motionBuffers) {
|
||||
if (buf.length < 5) {
|
||||
perNode[nodeId] = { cadence: 0, regularity: 0, state: 'insufficient data' };
|
||||
continue;
|
||||
}
|
||||
|
||||
const motionValues = buf.map(b => b.motion);
|
||||
|
||||
// Estimate sampling rate
|
||||
const duration = buf[buf.length - 1].timestamp - buf[0].timestamp;
|
||||
const fs = duration > 0 ? buf.length / duration : 1;
|
||||
|
||||
// FFT for cadence
|
||||
const nfft = nextPow2(Math.max(motionValues.length, 32));
|
||||
const re = new Float64Array(nfft);
|
||||
const im = new Float64Array(nfft);
|
||||
|
||||
const mean = motionValues.reduce((a, b) => a + b, 0) / motionValues.length;
|
||||
for (let i = 0; i < motionValues.length; i++) {
|
||||
const hann = 0.5 * (1 - Math.cos(2 * Math.PI * i / (motionValues.length - 1)));
|
||||
re[i] = (motionValues[i] - mean) * hann;
|
||||
}
|
||||
|
||||
fft(re, im);
|
||||
|
||||
// Find dominant frequency in walking range (0.8 - 2.5 Hz)
|
||||
const freqRes = fs / nfft;
|
||||
let peakPower = 0, peakFreq = 0;
|
||||
let totalPower = 0;
|
||||
|
||||
for (let k = 1; k < nfft / 2; k++) {
|
||||
const freq = k * freqRes;
|
||||
const power = re[k] * re[k] + im[k] * im[k];
|
||||
totalPower += power;
|
||||
|
||||
if (freq >= 0.8 && freq <= 2.5 && power > peakPower) {
|
||||
peakPower = power;
|
||||
peakFreq = freq;
|
||||
}
|
||||
}
|
||||
|
||||
const cadence = peakFreq * 60; // steps per minute (each leg cycle)
|
||||
const regularity = totalPower > 0 ? peakPower / totalPower : 0;
|
||||
|
||||
// Autocorrelation for stride regularity
|
||||
const autoCorr = this._autocorrelation(motionValues);
|
||||
const strideRegularity = autoCorr > 0 ? autoCorr : 0;
|
||||
|
||||
// State classification
|
||||
let state;
|
||||
if (mean < 1.0) state = 'stationary';
|
||||
else if (peakFreq >= 0.8 && peakFreq <= 2.0 && regularity > 0.1) state = 'walking';
|
||||
else if (peakFreq > 2.0 && regularity > 0.1) state = 'running';
|
||||
else state = 'moving (irregular)';
|
||||
|
||||
perNode[nodeId] = {
|
||||
cadence: +cadence.toFixed(1),
|
||||
cadenceHz: +peakFreq.toFixed(3),
|
||||
regularity: +regularity.toFixed(3),
|
||||
strideRegularity: +strideRegularity.toFixed(3),
|
||||
meanMotion: +mean.toFixed(3),
|
||||
state,
|
||||
samples: buf.length,
|
||||
fps: +fs.toFixed(1),
|
||||
};
|
||||
|
||||
if (cadence > bestCadence) bestCadence = cadence;
|
||||
if (regularity > bestRegularity) bestRegularity = regularity;
|
||||
if (peakFreq > 0) cadences.push(cadence);
|
||||
}
|
||||
|
||||
// Cross-node asymmetry (if 2+ nodes)
|
||||
let asymmetry = 0;
|
||||
const nodeKeys = Object.keys(perNode);
|
||||
if (nodeKeys.length >= 2) {
|
||||
const c0 = perNode[nodeKeys[0]].cadenceHz;
|
||||
const c1 = perNode[nodeKeys[1]].cadenceHz;
|
||||
const meanC = (c0 + c1) / 2;
|
||||
asymmetry = meanC > 0 ? Math.abs(c0 - c1) / meanC : 0;
|
||||
}
|
||||
|
||||
// Tremor detection from phase variance
|
||||
let tremorScore = 0;
|
||||
let tremorFreq = 0;
|
||||
for (const [, buf] of this.phaseVarBuffers) {
|
||||
if (buf.length < 10) continue;
|
||||
|
||||
const values = buf.map(b => b.phaseVar);
|
||||
const duration = buf[buf.length - 1].timestamp - buf[0].timestamp;
|
||||
const fs = duration > 0 ? buf.length / duration : 1;
|
||||
|
||||
const nfft = nextPow2(Math.max(values.length, 32));
|
||||
const re = new Float64Array(nfft);
|
||||
const im = new Float64Array(nfft);
|
||||
const mean = values.reduce((a, b) => a + b, 0) / values.length;
|
||||
for (let i = 0; i < values.length; i++) re[i] = values[i] - mean;
|
||||
fft(re, im);
|
||||
|
||||
const freqRes = fs / nfft;
|
||||
let tPeak = 0, tFreq = 0;
|
||||
for (let k = 1; k < nfft / 2; k++) {
|
||||
const freq = k * freqRes;
|
||||
const power = re[k] * re[k] + im[k] * im[k];
|
||||
if (freq >= 3.0 && freq <= 8.0 && power > tPeak) {
|
||||
tPeak = power;
|
||||
tFreq = freq;
|
||||
}
|
||||
}
|
||||
if (tPeak > tremorScore) {
|
||||
tremorScore = tPeak;
|
||||
tremorFreq = tFreq;
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize tremor score to 0-1 range (heuristic)
|
||||
const tremorNorm = Math.min(tremorScore / 100, 1.0);
|
||||
|
||||
const result = {
|
||||
timestamp,
|
||||
cadence: +bestCadence.toFixed(1),
|
||||
regularity: +bestRegularity.toFixed(3),
|
||||
asymmetry: +asymmetry.toFixed(3),
|
||||
tremorScore: +tremorNorm.toFixed(3),
|
||||
tremorFreqHz: +tremorFreq.toFixed(2),
|
||||
perNode,
|
||||
overallState: this._overallState(perNode),
|
||||
};
|
||||
|
||||
this.results.push(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
_autocorrelation(values) {
|
||||
const n = values.length;
|
||||
if (n < 4) return 0;
|
||||
|
||||
const mean = values.reduce((a, b) => a + b, 0) / n;
|
||||
let denom = 0;
|
||||
for (let i = 0; i < n; i++) denom += (values[i] - mean) ** 2;
|
||||
if (denom < 0.001) return 0;
|
||||
|
||||
// Check autocorrelation at lag = n/4 to n/2 (typical stride period range)
|
||||
let bestCorr = 0;
|
||||
const minLag = Math.max(2, Math.floor(n / 4));
|
||||
const maxLag = Math.floor(n / 2);
|
||||
|
||||
for (let lag = minLag; lag <= maxLag; lag++) {
|
||||
let num = 0;
|
||||
for (let i = 0; i < n - lag; i++) {
|
||||
num += (values[i] - mean) * (values[i + lag] - mean);
|
||||
}
|
||||
const corr = num / denom;
|
||||
if (corr > bestCorr) bestCorr = corr;
|
||||
}
|
||||
|
||||
return bestCorr;
|
||||
}
|
||||
|
||||
_overallState(perNode) {
|
||||
const states = Object.values(perNode).map(n => n.state);
|
||||
if (states.includes('walking')) return 'walking';
|
||||
if (states.includes('running')) return 'running';
|
||||
if (states.includes('moving (irregular)')) return 'moving';
|
||||
return 'stationary';
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Packet parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseVitalsJsonl(record) {
|
||||
if (record.type !== 'vitals') return null;
|
||||
return {
|
||||
timestamp: record.timestamp,
|
||||
nodeId: record.node_id,
|
||||
motion: record.motion_energy || 0,
|
||||
};
|
||||
}
|
||||
|
||||
function parseCsiJsonl(record) {
|
||||
if (record.type !== 'raw_csi' || !record.iq_hex) return null;
|
||||
const nSc = record.subcarriers || 64;
|
||||
const bytes = Buffer.from(record.iq_hex, 'hex');
|
||||
|
||||
// Compute phase variance across subcarriers
|
||||
let phaseSum = 0, phaseSqSum = 0, count = 0;
|
||||
for (let sc = 0; sc < nSc; sc++) {
|
||||
const offset = 2 + sc * 2;
|
||||
if (offset + 1 >= bytes.length) break;
|
||||
let I = bytes[offset]; if (I > 127) I -= 256;
|
||||
let Q = bytes[offset + 1]; if (Q > 127) Q -= 256;
|
||||
const phase = Math.atan2(Q, I);
|
||||
phaseSum += phase;
|
||||
phaseSqSum += phase * phase;
|
||||
count++;
|
||||
}
|
||||
|
||||
const phaseMean = count > 0 ? phaseSum / count : 0;
|
||||
const phaseVar = count > 1 ? (phaseSqSum / count - phaseMean * phaseMean) : 0;
|
||||
|
||||
return {
|
||||
timestamp: record.timestamp,
|
||||
nodeId: record.node_id,
|
||||
phaseVar: Math.abs(phaseVar),
|
||||
};
|
||||
}
|
||||
|
||||
function parseVitalsUdp(buf) {
|
||||
if (buf.length < 32) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== VITALS_MAGIC && magic !== FUSED_MAGIC) return null;
|
||||
return {
|
||||
timestamp: Date.now() / 1000,
|
||||
nodeId: buf.readUInt8(4),
|
||||
motion: buf.readFloatLE(16),
|
||||
};
|
||||
}
|
||||
|
||||
function parseCsiUdp(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const nSc = buf.readUInt16LE(6);
|
||||
|
||||
let phaseSum = 0, phaseSqSum = 0, count = 0;
|
||||
for (let sc = 0; sc < nSc; sc++) {
|
||||
const offset = HEADER_SIZE + sc * 2;
|
||||
if (offset + 1 >= buf.length) break;
|
||||
const I = buf.readInt8(offset);
|
||||
const Q = buf.readInt8(offset + 1);
|
||||
const phase = Math.atan2(Q, I);
|
||||
phaseSum += phase;
|
||||
phaseSqSum += phase * phase;
|
||||
count++;
|
||||
}
|
||||
|
||||
const phaseMean = count > 0 ? phaseSum / count : 0;
|
||||
const phaseVar = count > 1 ? (phaseSqSum / count - phaseMean * phaseMean) : 0;
|
||||
|
||||
return { timestamp: Date.now() / 1000, nodeId, phaseVar: Math.abs(phaseVar) };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Display
|
||||
// ---------------------------------------------------------------------------
|
||||
function formatResult(result) {
|
||||
const lines = [];
|
||||
const ts = new Date(result.timestamp * 1000).toISOString().slice(11, 19);
|
||||
lines.push(`[${ts}] ${result.overallState.toUpperCase()}`);
|
||||
lines.push(` Cadence: ${result.cadence} steps/min`);
|
||||
lines.push(` Regularity: ${result.regularity}`);
|
||||
lines.push(` Asymmetry: ${result.asymmetry}`);
|
||||
lines.push(` Tremor: ${result.tremorScore} (${result.tremorFreqHz} Hz)`);
|
||||
|
||||
for (const [nodeId, node] of Object.entries(result.perNode)) {
|
||||
lines.push(` Node ${nodeId}: ${node.state} | ${node.cadence} spm | regularity ${node.regularity} | ${node.samples} samples @ ${node.fps} fps`);
|
||||
}
|
||||
|
||||
// Flags
|
||||
const flags = [];
|
||||
if (result.asymmetry > 0.3) flags.push('HIGH ASYMMETRY');
|
||||
if (result.tremorScore > 0.3) flags.push(`TREMOR DETECTED (${result.tremorFreqHz} Hz)`);
|
||||
if (result.cadence > 0 && result.cadence < 50) flags.push('SLOW CADENCE');
|
||||
if (flags.length > 0) lines.push(` ** ${flags.join(' | ')} **`);
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const analyzer = new GaitAnalyzer();
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
|
||||
const v = parseVitalsJsonl(record);
|
||||
if (v) {
|
||||
analyzer.pushMotion(v.nodeId, v.timestamp, v.motion);
|
||||
frameCount++;
|
||||
}
|
||||
|
||||
const csi = parseCsiJsonl(record);
|
||||
if (csi) {
|
||||
analyzer.pushPhaseVar(csi.nodeId, csi.timestamp, csi.phaseVar);
|
||||
}
|
||||
|
||||
const ts = (v || csi);
|
||||
if (!ts) continue;
|
||||
|
||||
const tsMs = ts.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
const result = analyzer.analyze(ts.timestamp);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify(result));
|
||||
} else {
|
||||
console.log(formatResult(result));
|
||||
}
|
||||
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Summary
|
||||
if (!JSON_OUTPUT && analyzer.results.length > 0) {
|
||||
console.log('\n' + '='.repeat(60));
|
||||
console.log('GAIT ANALYSIS SUMMARY');
|
||||
console.log('DISCLAIMER: Informational only. Not a medical device.');
|
||||
console.log('='.repeat(60));
|
||||
|
||||
const states = {};
|
||||
let totalCadence = 0, cadenceCount = 0;
|
||||
let maxTremor = 0;
|
||||
|
||||
for (const r of analyzer.results) {
|
||||
states[r.overallState] = (states[r.overallState] || 0) + 1;
|
||||
if (r.cadence > 0) {
|
||||
totalCadence += r.cadence;
|
||||
cadenceCount++;
|
||||
}
|
||||
if (r.tremorScore > maxTremor) maxTremor = r.tremorScore;
|
||||
}
|
||||
|
||||
console.log('Activity distribution:');
|
||||
for (const [state, count] of Object.entries(states)) {
|
||||
const pct = ((count / analyzer.results.length) * 100).toFixed(1);
|
||||
const bar = '\u2588'.repeat(Math.round(pct / 2));
|
||||
console.log(` ${state.padEnd(15)} ${bar.padEnd(50)} ${pct}%`);
|
||||
}
|
||||
|
||||
if (cadenceCount > 0) {
|
||||
console.log(`\nAverage walking cadence: ${(totalCadence / cadenceCount).toFixed(1)} steps/min`);
|
||||
}
|
||||
console.log(`Max tremor score: ${maxTremor.toFixed(3)}`);
|
||||
console.log(`Analysis windows: ${analyzer.results.length}`);
|
||||
console.log(`Processed ${frameCount} vitals packets`);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live UDP mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const analyzer = new GaitAnalyzer();
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', (buf) => {
|
||||
const v = parseVitalsUdp(buf);
|
||||
if (v) analyzer.pushMotion(v.nodeId, v.timestamp, v.motion);
|
||||
|
||||
const csi = parseCsiUdp(buf);
|
||||
if (csi) analyzer.pushPhaseVar(csi.nodeId, csi.timestamp, csi.phaseVar);
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
const result = analyzer.analyze(Date.now() / 1000);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify(result));
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
console.log('=== GAIT ANALYZER (ADR-077) ===');
|
||||
console.log('DISCLAIMER: Informational only. Not a medical device.\n');
|
||||
console.log(formatResult(result));
|
||||
}
|
||||
}, INTERVAL_MS);
|
||||
|
||||
server.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Gait Analyzer listening on UDP :${PORT}`);
|
||||
}
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => { server.close(); process.exit(0); });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,613 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Frequency-Selective Material Classification — Multi-Frequency Mesh Application
|
||||
*
|
||||
* Compares CSI null/attenuation patterns across 6 WiFi channels to classify
|
||||
* materials in the room. Different materials absorb WiFi at different rates
|
||||
* depending on frequency:
|
||||
*
|
||||
* Metal: blocks all frequencies equally (frequency-flat null)
|
||||
* Water: absorbs strongly, increasing with frequency (dielectric loss)
|
||||
* Wood: mild attenuation, increases with frequency (moisture)
|
||||
* Glass: low attenuation, nearly frequency-flat
|
||||
* Human: 60-70% water, strong frequency-dependent absorption
|
||||
*
|
||||
* Requires multi-frequency mesh scanning (ADR-073): 2 ESP32 nodes hopping
|
||||
* across channels 1, 3, 5, 6, 9, 11.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/material-classifier.js
|
||||
* node scripts/material-classifier.js --port 5006 --duration 60
|
||||
* node scripts/material-classifier.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
*
|
||||
* ADR: docs/adr/ADR-078-multifreq-mesh-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
duration: { type: 'string', short: 'd' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
interval: { type: 'string', short: 'i', default: '5000' },
|
||||
json: { type: 'boolean', default: false },
|
||||
window: { type: 'string', short: 'w', default: '20' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const DURATION_MS = args.duration ? parseInt(args.duration, 10) * 1000 : null;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const WINDOW_FRAMES = parseInt(args.window, 10);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
const CHANNEL_FREQ = {};
|
||||
for (let ch = 1; ch <= 13; ch++) CHANNEL_FREQ[ch] = 2412 + (ch - 1) * 5;
|
||||
|
||||
const NODE1_CHANNELS = [1, 6, 11];
|
||||
const NODE2_CHANNELS = [3, 5, 9];
|
||||
|
||||
// Material classification thresholds
|
||||
const NULL_THRESHOLD = 2.0;
|
||||
|
||||
// Material types
|
||||
const MATERIAL = {
|
||||
METAL: { name: 'Metal', char: '#', desc: 'Total block, frequency-flat' },
|
||||
WATER: { name: 'Water', char: '~', desc: 'Strong absorption, freq-dependent' },
|
||||
HUMAN: { name: 'Human', char: '@', desc: '60-70% water, strong freq-dependent' },
|
||||
WOOD: { name: 'Wood', char: '|', desc: 'Mild attenuation, freq-increasing' },
|
||||
GLASS: { name: 'Glass', char: ':', desc: 'Low attenuation, frequency-flat' },
|
||||
AIR: { name: 'Air', char: '.', desc: 'Minimal attenuation' },
|
||||
COMPLEX: { name: 'Complex', char: '?', desc: 'Mixed/unclassifiable' },
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per-channel amplitude accumulator
|
||||
// ---------------------------------------------------------------------------
|
||||
class ChannelAccumulator {
|
||||
constructor() {
|
||||
// channel -> { amplitudes: Float64Array[], count: number }
|
||||
this.channels = new Map();
|
||||
}
|
||||
|
||||
ingest(channel, amplitudes) {
|
||||
if (!this.channels.has(channel)) {
|
||||
this.channels.set(channel, {
|
||||
sum: new Float64Array(amplitudes.length),
|
||||
sumSq: new Float64Array(amplitudes.length),
|
||||
count: 0,
|
||||
nSub: amplitudes.length,
|
||||
});
|
||||
}
|
||||
|
||||
const ch = this.channels.get(channel);
|
||||
ch.count++;
|
||||
for (let i = 0; i < amplitudes.length && i < ch.nSub; i++) {
|
||||
ch.sum[i] += amplitudes[i];
|
||||
ch.sumSq[i] += amplitudes[i] * amplitudes[i];
|
||||
}
|
||||
}
|
||||
|
||||
/** Get mean amplitude per subcarrier per channel */
|
||||
getMeans() {
|
||||
const means = new Map();
|
||||
for (const [channel, ch] of this.channels) {
|
||||
if (ch.count === 0) continue;
|
||||
const mean = new Float64Array(ch.nSub);
|
||||
for (let i = 0; i < ch.nSub; i++) {
|
||||
mean[i] = ch.sum[i] / ch.count;
|
||||
}
|
||||
means.set(channel, { mean, count: ch.count, nSub: ch.nSub });
|
||||
}
|
||||
return means;
|
||||
}
|
||||
|
||||
/** Get variance per subcarrier per channel */
|
||||
getVariances() {
|
||||
const variances = new Map();
|
||||
for (const [channel, ch] of this.channels) {
|
||||
if (ch.count < 2) continue;
|
||||
const variance = new Float64Array(ch.nSub);
|
||||
for (let i = 0; i < ch.nSub; i++) {
|
||||
const mean = ch.sum[i] / ch.count;
|
||||
variance[i] = (ch.sumSq[i] / ch.count) - (mean * mean);
|
||||
}
|
||||
variances.set(channel, variance);
|
||||
}
|
||||
return variances;
|
||||
}
|
||||
|
||||
/** Get active channel list sorted by frequency */
|
||||
getActiveChannels() {
|
||||
return [...this.channels.keys()]
|
||||
.filter(ch => this.channels.get(ch).count > 0)
|
||||
.sort((a, b) => a - b);
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.channels.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Material classifier
|
||||
// ---------------------------------------------------------------------------
|
||||
class MaterialClassifier {
|
||||
constructor() {
|
||||
this.accumulator = new ChannelAccumulator();
|
||||
this.frameCount = 0;
|
||||
this.classifications = [];
|
||||
}
|
||||
|
||||
ingestFrame(channel, amplitudes) {
|
||||
this.accumulator.ingest(channel, amplitudes);
|
||||
this.frameCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Classify each subcarrier group by comparing attenuation across channels.
|
||||
*
|
||||
* For each subcarrier index:
|
||||
* 1. Collect mean amplitude on each channel
|
||||
* 2. Compute frequency selectivity metrics:
|
||||
* - Flat ratio = std / mean (low = frequency-flat)
|
||||
* - Slope = linear regression of amplitude vs frequency
|
||||
* - Mean level = overall attenuation (high = strong absorber)
|
||||
* 3. Decision tree:
|
||||
* - All channels null -> Metal (frequency-flat total block)
|
||||
* - Flat ratio < 0.15 AND mean < 3.0 -> Metal
|
||||
* - Flat ratio < 0.15 AND mean > 8.0 -> Glass/Air
|
||||
* - Negative slope (amp decreases with freq) AND mean < 6.0 -> Water/Human
|
||||
* - Negative slope AND mean 6.0-8.0 -> Wood
|
||||
* - High variance across channels -> Complex
|
||||
*/
|
||||
classify() {
|
||||
const means = this.accumulator.getMeans();
|
||||
const channels = this.accumulator.getActiveChannels();
|
||||
|
||||
if (channels.length < 2) {
|
||||
return { error: 'Need at least 2 channels for material classification', channels: channels.length };
|
||||
}
|
||||
|
||||
const nSub = Math.min(...[...means.values()].map(m => m.nSub));
|
||||
const freqs = channels.map(ch => CHANNEL_FREQ[ch] || 2432);
|
||||
|
||||
const results = [];
|
||||
const materialCounts = {};
|
||||
for (const m of Object.values(MATERIAL)) materialCounts[m.name] = 0;
|
||||
|
||||
for (let sc = 0; sc < nSub; sc++) {
|
||||
// Collect amplitudes across channels for this subcarrier
|
||||
const amps = channels.map(ch => means.get(ch).mean[sc]);
|
||||
|
||||
// Is this a null on all channels?
|
||||
const allNull = amps.every(a => a < NULL_THRESHOLD);
|
||||
const anyNull = amps.some(a => a < NULL_THRESHOLD);
|
||||
|
||||
// Mean amplitude
|
||||
const meanAmp = amps.reduce((a, b) => a + b, 0) / amps.length;
|
||||
|
||||
// Standard deviation
|
||||
const variance = amps.reduce((a, b) => a + (b - meanAmp) ** 2, 0) / amps.length;
|
||||
const stdAmp = Math.sqrt(variance);
|
||||
|
||||
// Flat ratio (coefficient of variation)
|
||||
const flatRatio = meanAmp > 0.01 ? stdAmp / meanAmp : 0;
|
||||
|
||||
// Frequency slope: linear regression of amplitude vs frequency
|
||||
let sumF = 0, sumA = 0, sumFF = 0, sumFA = 0;
|
||||
for (let i = 0; i < channels.length; i++) {
|
||||
sumF += freqs[i];
|
||||
sumA += amps[i];
|
||||
sumFF += freqs[i] * freqs[i];
|
||||
sumFA += freqs[i] * amps[i];
|
||||
}
|
||||
const nCh = channels.length;
|
||||
const meanF = sumF / nCh;
|
||||
const denomF = sumFF - sumF * meanF;
|
||||
const slope = Math.abs(denomF) > 1e-6
|
||||
? (sumFA - sumF * (sumA / nCh)) / denomF
|
||||
: 0;
|
||||
|
||||
// Normalized slope (per MHz)
|
||||
const slopePerMHz = slope;
|
||||
|
||||
// Classification decision tree
|
||||
let material;
|
||||
if (allNull) {
|
||||
material = MATERIAL.METAL;
|
||||
} else if (flatRatio < 0.15 && meanAmp < 3.0) {
|
||||
material = MATERIAL.METAL;
|
||||
} else if (flatRatio < 0.15 && meanAmp > 10.0) {
|
||||
material = MATERIAL.AIR;
|
||||
} else if (flatRatio < 0.15 && meanAmp > 6.0) {
|
||||
material = MATERIAL.GLASS;
|
||||
} else if (slopePerMHz < -0.005 && meanAmp < 5.0) {
|
||||
// Amplitude decreases with frequency = frequency-dependent absorption
|
||||
material = MATERIAL.HUMAN;
|
||||
} else if (slopePerMHz < -0.003 && meanAmp < 8.0) {
|
||||
material = MATERIAL.WATER;
|
||||
} else if (slopePerMHz < -0.001 && meanAmp >= 5.0) {
|
||||
material = MATERIAL.WOOD;
|
||||
} else if (flatRatio > 0.5) {
|
||||
material = MATERIAL.COMPLEX;
|
||||
} else {
|
||||
material = MATERIAL.AIR;
|
||||
}
|
||||
|
||||
materialCounts[material.name]++;
|
||||
results.push({
|
||||
subcarrier: sc,
|
||||
material: material.name,
|
||||
char: material.char,
|
||||
meanAmp: meanAmp.toFixed(1),
|
||||
flatRatio: flatRatio.toFixed(3),
|
||||
slopePerMHz: slopePerMHz.toFixed(5),
|
||||
amps: amps.map(a => a.toFixed(1)),
|
||||
});
|
||||
}
|
||||
|
||||
this.classifications = results;
|
||||
|
||||
return {
|
||||
channels,
|
||||
nSubcarriers: nSub,
|
||||
frameCount: this.frameCount,
|
||||
materialCounts,
|
||||
classifications: results,
|
||||
};
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.accumulator.reset();
|
||||
this.frameCount = 0;
|
||||
this.classifications = [];
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CSI parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseIqHex(iqHex, nSubcarriers) {
|
||||
const bytes = Buffer.from(iqHex, 'hex');
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = 2 + sc * 2;
|
||||
if (offset + 1 >= bytes.length) break;
|
||||
let I = bytes[offset];
|
||||
let Q = bytes[offset + 1];
|
||||
if (I > 127) I -= 256;
|
||||
if (Q > 127) Q -= 256;
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
return amplitudes;
|
||||
}
|
||||
|
||||
function parseCSIFrame(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const nSubcarriers = buf.readUInt16LE(6);
|
||||
const freqMhz = buf.readUInt32LE(8);
|
||||
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = HEADER_SIZE + sc * 2;
|
||||
if (offset + 1 >= buf.length) break;
|
||||
const I = buf.readInt8(offset);
|
||||
const Q = buf.readInt8(offset + 1);
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
let channel = 0;
|
||||
if (freqMhz >= 2412 && freqMhz <= 2484) {
|
||||
channel = freqMhz === 2484 ? 14 : Math.round((freqMhz - 2412) / 5) + 1;
|
||||
}
|
||||
|
||||
return { nodeId, nSubcarriers, freqMhz, amplitudes, channel };
|
||||
}
|
||||
|
||||
const nodeChannelIdx = { 1: 0, 2: 0 };
|
||||
function assignChannel(nodeId) {
|
||||
const channels = nodeId === 1 ? NODE1_CHANNELS : NODE2_CHANNELS;
|
||||
const ch = channels[nodeChannelIdx[nodeId] % channels.length];
|
||||
nodeChannelIdx[nodeId]++;
|
||||
return ch;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Visualization
|
||||
// ---------------------------------------------------------------------------
|
||||
function renderMaterialMap(result) {
|
||||
const { classifications, channels, nSubcarriers, materialCounts } = result;
|
||||
if (!classifications || classifications.length === 0) return ' No classifications available';
|
||||
|
||||
const lines = [];
|
||||
lines.push('');
|
||||
lines.push(' FREQUENCY-SELECTIVE MATERIAL CLASSIFICATION');
|
||||
lines.push(' ' + '='.repeat(55));
|
||||
lines.push('');
|
||||
|
||||
// Material map: one char per subcarrier
|
||||
lines.push(' Subcarrier Material Map (1 char = 1 subcarrier):');
|
||||
let mapRow = ' ';
|
||||
for (let i = 0; i < classifications.length; i++) {
|
||||
mapRow += classifications[i].char;
|
||||
if ((i + 1) % 64 === 0) {
|
||||
lines.push(mapRow);
|
||||
mapRow = ' ';
|
||||
}
|
||||
}
|
||||
if (mapRow.trim()) lines.push(mapRow);
|
||||
|
||||
lines.push('');
|
||||
lines.push(' Legend:');
|
||||
for (const m of Object.values(MATERIAL)) {
|
||||
const count = materialCounts[m.name] || 0;
|
||||
const pct = nSubcarriers > 0 ? (count / nSubcarriers * 100).toFixed(1) : '0.0';
|
||||
lines.push(` ${m.char} = ${m.name.padEnd(8)} (${pct}%) ${m.desc}`);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function renderFrequencyProfile(result) {
|
||||
const { classifications, channels } = result;
|
||||
if (!classifications || channels.length < 2) return '';
|
||||
|
||||
const lines = [];
|
||||
lines.push('');
|
||||
lines.push(' Frequency Profile (mean amplitude per channel):');
|
||||
lines.push(' ' + '-'.repeat(50));
|
||||
|
||||
// Compute mean per channel across all subcarriers
|
||||
const channelMeans = {};
|
||||
for (const ch of channels) channelMeans[ch] = { sum: 0, count: 0 };
|
||||
|
||||
for (const cls of classifications) {
|
||||
for (let i = 0; i < channels.length && i < cls.amps.length; i++) {
|
||||
channelMeans[channels[i]].sum += parseFloat(cls.amps[i]);
|
||||
channelMeans[channels[i]].count++;
|
||||
}
|
||||
}
|
||||
|
||||
const BARS = '\u2581\u2582\u2583\u2584\u2585\u2586\u2587\u2588';
|
||||
let maxMean = 0;
|
||||
for (const ch of channels) {
|
||||
const m = channelMeans[ch].count > 0 ? channelMeans[ch].sum / channelMeans[ch].count : 0;
|
||||
if (m > maxMean) maxMean = m;
|
||||
}
|
||||
if (maxMean === 0) maxMean = 1;
|
||||
|
||||
for (const ch of channels) {
|
||||
const mean = channelMeans[ch].count > 0 ? channelMeans[ch].sum / channelMeans[ch].count : 0;
|
||||
const freq = CHANNEL_FREQ[ch] || 0;
|
||||
const barLen = Math.floor((mean / maxMean) * 30);
|
||||
const bar = BARS[7].repeat(barLen);
|
||||
lines.push(` ch${String(ch).padStart(2)} (${freq} MHz): ${bar} ${mean.toFixed(1)}`);
|
||||
}
|
||||
|
||||
// Slope analysis
|
||||
const freqs = channels.map(ch => CHANNEL_FREQ[ch]);
|
||||
const means = channels.map(ch => {
|
||||
const c = channelMeans[ch];
|
||||
return c.count > 0 ? c.sum / c.count : 0;
|
||||
});
|
||||
|
||||
let sumF = 0, sumA = 0, sumFF = 0, sumFA = 0;
|
||||
for (let i = 0; i < channels.length; i++) {
|
||||
sumF += freqs[i]; sumA += means[i];
|
||||
sumFF += freqs[i] * freqs[i]; sumFA += freqs[i] * means[i];
|
||||
}
|
||||
const nCh = channels.length;
|
||||
const meanF = sumF / nCh;
|
||||
const denomF = sumFF - sumF * meanF;
|
||||
const slope = Math.abs(denomF) > 1e-6 ? (sumFA - sumF * (sumA / nCh)) / denomF : 0;
|
||||
|
||||
lines.push('');
|
||||
if (slope < -0.003) {
|
||||
lines.push(' Overall trend: DECREASING with frequency (water/organic absorption)');
|
||||
} else if (slope > 0.003) {
|
||||
lines.push(' Overall trend: INCREASING with frequency (unusual, possible reflection)');
|
||||
} else {
|
||||
lines.push(' Overall trend: FLAT across frequency (metal or air dominant)');
|
||||
}
|
||||
lines.push(` Slope: ${(slope * 1000).toFixed(3)} amplitude/GHz`);
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function renderDetailedSubcarriers(result) {
|
||||
const { classifications, channels } = result;
|
||||
if (!classifications) return '';
|
||||
|
||||
const lines = [];
|
||||
lines.push('');
|
||||
lines.push(' Notable Subcarriers (high frequency selectivity):');
|
||||
lines.push(' ' + '-'.repeat(60));
|
||||
lines.push(' SC# Material Mean Flat Slope/MHz Per-channel amps');
|
||||
|
||||
// Find most interesting subcarriers (high flat ratio or steep slope)
|
||||
const interesting = classifications
|
||||
.filter(c => parseFloat(c.flatRatio) > 0.3 || Math.abs(parseFloat(c.slopePerMHz)) > 0.005)
|
||||
.sort((a, b) => parseFloat(b.flatRatio) - parseFloat(a.flatRatio))
|
||||
.slice(0, 15);
|
||||
|
||||
for (const cls of interesting) {
|
||||
const amps = cls.amps.join(' ');
|
||||
lines.push(` ${String(cls.subcarrier).padStart(3)} ${cls.material.padEnd(8)} ` +
|
||||
`${cls.meanAmp.padStart(5)} ${cls.flatRatio} ${cls.slopePerMHz.padStart(9)} [${amps}]`);
|
||||
}
|
||||
|
||||
if (interesting.length === 0) {
|
||||
lines.push(' (no highly frequency-selective subcarriers detected)');
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Global state
|
||||
// ---------------------------------------------------------------------------
|
||||
const classifier = new MaterialClassifier();
|
||||
let lastDisplayMs = 0;
|
||||
|
||||
function processFrame(channel, amplitudes) {
|
||||
classifier.ingestFrame(channel, amplitudes);
|
||||
}
|
||||
|
||||
function displayUpdate() {
|
||||
const result = classifier.classify();
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
timestamp: Date.now() / 1000,
|
||||
channels: result.channels,
|
||||
frameCount: result.frameCount,
|
||||
materialCounts: result.materialCounts,
|
||||
topClassifications: (result.classifications || [])
|
||||
.filter(c => c.material !== 'Air')
|
||||
.slice(0, 20)
|
||||
.map(c => ({ sc: c.subcarrier, material: c.material, meanAmp: c.meanAmp })),
|
||||
}));
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
console.log(renderMaterialMap(result));
|
||||
console.log(renderFrequencyProfile(result));
|
||||
console.log(renderDetailedSubcarriers(result));
|
||||
console.log('');
|
||||
console.log(` Frames: ${result.frameCount} | Channels: ${(result.channels || []).length}`);
|
||||
console.log(' Press Ctrl+C to exit');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const sock = dgram.createSocket('udp4');
|
||||
|
||||
sock.on('message', (buf) => {
|
||||
if (buf.length < 4) return;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return;
|
||||
|
||||
const frame = parseCSIFrame(buf);
|
||||
if (!frame) return;
|
||||
|
||||
processFrame(frame.channel, frame.amplitudes);
|
||||
|
||||
const now = Date.now();
|
||||
if (now - lastDisplayMs >= INTERVAL_MS) {
|
||||
displayUpdate();
|
||||
lastDisplayMs = now;
|
||||
}
|
||||
});
|
||||
|
||||
sock.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Material Classifier listening on UDP port ${PORT}`);
|
||||
console.log('Waiting for multi-channel CSI frames...');
|
||||
}
|
||||
});
|
||||
|
||||
if (DURATION_MS) {
|
||||
setTimeout(() => { displayUpdate(); sock.close(); process.exit(0); }, DURATION_MS);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
let windowCount = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
if (record.type !== 'raw_csi' || !record.iq_hex) continue;
|
||||
|
||||
const amplitudes = parseIqHex(record.iq_hex, record.subcarriers || 64);
|
||||
const channel = record.channel || assignChannel(record.node_id);
|
||||
|
||||
processFrame(channel, amplitudes);
|
||||
frameCount++;
|
||||
|
||||
const tsMs = record.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
windowCount++;
|
||||
const result = classifier.classify();
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
window: windowCount, timestamp: record.timestamp,
|
||||
materialCounts: result.materialCounts,
|
||||
}));
|
||||
} else {
|
||||
console.log(`\n${'='.repeat(60)}`);
|
||||
console.log(`Window ${windowCount} | t=${record.timestamp.toFixed(1)}s | frames=${frameCount}`);
|
||||
console.log('='.repeat(60));
|
||||
console.log(renderMaterialMap(result));
|
||||
console.log(renderFrequencyProfile(result));
|
||||
}
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Final
|
||||
if (!JSON_OUTPUT) {
|
||||
const result = classifier.classify();
|
||||
console.log(`\n${'='.repeat(60)}`);
|
||||
console.log('FINAL MATERIAL CLASSIFICATION');
|
||||
console.log('='.repeat(60));
|
||||
console.log(renderMaterialMap(result));
|
||||
console.log(renderFrequencyProfile(result));
|
||||
console.log(renderDetailedSubcarriers(result));
|
||||
console.log(`\nProcessed ${frameCount} frames in ${windowCount} windows`);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry point
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,474 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-077: Material/Object Change Detection
|
||||
*
|
||||
* Monitors CSI subcarrier null patterns to detect when objects (metal, water,
|
||||
* wood, glass) are introduced, removed, or moved in the sensing area.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/material-detector.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/material-detector.js --port 5006
|
||||
* node scripts/material-detector.js --replay FILE --json
|
||||
* node scripts/material-detector.js --replay FILE --baseline-time 120
|
||||
*
|
||||
* ADR: docs/adr/ADR-077-novel-rf-sensing-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
json: { type: 'boolean', default: false },
|
||||
interval: { type: 'string', short: 'i', default: '5000' },
|
||||
'baseline-time': { type: 'string', default: '60' },
|
||||
'null-threshold': { type: 'string', default: '2.0' },
|
||||
'change-threshold': { type: 'string', default: '3' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const BASELINE_SEC = parseInt(args['baseline-time'], 10);
|
||||
const NULL_THRESHOLD = parseFloat(args['null-threshold']);
|
||||
const CHANGE_THRESHOLD = parseInt(args['change-threshold'], 10); // min subcarriers changed
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ADR-018 packet constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Subcarrier null pattern tracker
|
||||
// ---------------------------------------------------------------------------
|
||||
class NullPatternTracker {
|
||||
constructor(nSubcarriers) {
|
||||
this.nSc = nSubcarriers || 64;
|
||||
|
||||
// Baseline (Welford mean per subcarrier)
|
||||
this.baselineMean = new Float64Array(256);
|
||||
this.baselineCount = new Uint32Array(256);
|
||||
this.baselineEstablished = false;
|
||||
this.baselineNulls = new Set();
|
||||
|
||||
// Current window state
|
||||
this.currentAmps = new Float64Array(256);
|
||||
this.currentCount = 0;
|
||||
|
||||
// Events
|
||||
this.events = [];
|
||||
this.startTime = null;
|
||||
this.lastTime = null;
|
||||
}
|
||||
|
||||
updateBaseline(amplitudes) {
|
||||
const n = amplitudes.length;
|
||||
this.nSc = n;
|
||||
for (let i = 0; i < n; i++) {
|
||||
this.baselineCount[i]++;
|
||||
const delta = amplitudes[i] - this.baselineMean[i];
|
||||
this.baselineMean[i] += delta / this.baselineCount[i];
|
||||
}
|
||||
}
|
||||
|
||||
finalizeBaseline() {
|
||||
this.baselineNulls = new Set();
|
||||
for (let i = 0; i < this.nSc; i++) {
|
||||
if (this.baselineMean[i] < NULL_THRESHOLD) {
|
||||
this.baselineNulls.add(i);
|
||||
}
|
||||
}
|
||||
this.baselineEstablished = true;
|
||||
}
|
||||
|
||||
updateCurrent(amplitudes) {
|
||||
const n = amplitudes.length;
|
||||
// Exponential moving average for current window
|
||||
const alpha = 0.1;
|
||||
if (this.currentCount === 0) {
|
||||
for (let i = 0; i < n; i++) this.currentAmps[i] = amplitudes[i];
|
||||
} else {
|
||||
for (let i = 0; i < n; i++) {
|
||||
this.currentAmps[i] = this.currentAmps[i] * (1 - alpha) + amplitudes[i] * alpha;
|
||||
}
|
||||
}
|
||||
this.currentCount++;
|
||||
}
|
||||
|
||||
detectChanges(timestamp) {
|
||||
if (!this.baselineEstablished || this.currentCount < 5) return null;
|
||||
|
||||
const currentNulls = new Set();
|
||||
for (let i = 0; i < this.nSc; i++) {
|
||||
if (this.currentAmps[i] < NULL_THRESHOLD) {
|
||||
currentNulls.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Find differences
|
||||
const newNulls = []; // appeared (something blocking)
|
||||
const removedNulls = []; // disappeared (object removed)
|
||||
const shiftedNulls = []; // nearby shifts
|
||||
|
||||
for (const sc of currentNulls) {
|
||||
if (!this.baselineNulls.has(sc)) newNulls.push(sc);
|
||||
}
|
||||
for (const sc of this.baselineNulls) {
|
||||
if (!currentNulls.has(sc)) removedNulls.push(sc);
|
||||
}
|
||||
|
||||
// Detect shifts (null moved by 1-3 subcarriers)
|
||||
for (const newSc of newNulls) {
|
||||
for (const rmSc of removedNulls) {
|
||||
if (Math.abs(newSc - rmSc) <= 3) {
|
||||
shiftedNulls.push({ from: rmSc, to: newSc });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Amplitude changes (non-null subcarriers with significant amplitude shift)
|
||||
const ampChanges = [];
|
||||
for (let i = 0; i < this.nSc; i++) {
|
||||
if (this.baselineMean[i] > NULL_THRESHOLD && this.currentAmps[i] > NULL_THRESHOLD) {
|
||||
const ratio = this.currentAmps[i] / this.baselineMean[i];
|
||||
if (ratio < 0.5 || ratio > 2.0) {
|
||||
ampChanges.push({ sc: i, baseline: this.baselineMean[i], current: this.currentAmps[i], ratio });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Material classification
|
||||
let material = 'unknown';
|
||||
if (newNulls.length > 0) {
|
||||
// Null pattern indicates metal
|
||||
if (newNulls.length <= 5) material = 'metal (small object)';
|
||||
else if (newNulls.length <= 15) material = 'metal (medium)';
|
||||
else material = 'metal (large)';
|
||||
} else if (ampChanges.length > this.nSc * 0.3) {
|
||||
// Broad amplitude change = water or human
|
||||
const avgRatio = ampChanges.reduce((s, c) => s + c.ratio, 0) / ampChanges.length;
|
||||
material = avgRatio < 1 ? 'water/human (absorption)' : 'reflective surface';
|
||||
} else if (ampChanges.length > 0 && ampChanges.length <= this.nSc * 0.1) {
|
||||
material = 'wood/plastic (minimal)';
|
||||
}
|
||||
|
||||
const totalChanges = newNulls.length + removedNulls.length + ampChanges.length;
|
||||
|
||||
// Only report if significant changes
|
||||
if (totalChanges < CHANGE_THRESHOLD) {
|
||||
return {
|
||||
timestamp,
|
||||
changeDetected: false,
|
||||
currentNullCount: currentNulls.size,
|
||||
baselineNullCount: this.baselineNulls.size,
|
||||
};
|
||||
}
|
||||
|
||||
// Determine event type
|
||||
let eventType;
|
||||
if (shiftedNulls.length > 0) eventType = 'moved';
|
||||
else if (newNulls.length > removedNulls.length) eventType = 'added';
|
||||
else if (removedNulls.length > newNulls.length) eventType = 'removed';
|
||||
else eventType = 'changed';
|
||||
|
||||
const event = {
|
||||
timestamp,
|
||||
changeDetected: true,
|
||||
eventType,
|
||||
material,
|
||||
newNulls: newNulls.length,
|
||||
removedNulls: removedNulls.length,
|
||||
shiftedNulls: shiftedNulls.length,
|
||||
ampChanges: ampChanges.length,
|
||||
newNullRange: newNulls.length > 0 ? [Math.min(...newNulls), Math.max(...newNulls)] : null,
|
||||
removedNullRange: removedNulls.length > 0 ? [Math.min(...removedNulls), Math.max(...removedNulls)] : null,
|
||||
currentNullCount: currentNulls.size,
|
||||
baselineNullCount: this.baselineNulls.size,
|
||||
nullDelta: currentNulls.size - this.baselineNulls.size,
|
||||
};
|
||||
|
||||
this.events.push(event);
|
||||
return event;
|
||||
}
|
||||
|
||||
renderNullMap() {
|
||||
const chars = [];
|
||||
for (let i = 0; i < this.nSc; i++) {
|
||||
if (this.currentAmps[i] < NULL_THRESHOLD) {
|
||||
if (this.baselineNulls.has(i)) chars.push('_'); // baseline null
|
||||
else chars.push('X'); // new null
|
||||
} else if (this.baselineNulls.has(i)) {
|
||||
chars.push('O'); // removed null
|
||||
} else {
|
||||
chars.push('\u2581'); // normal
|
||||
}
|
||||
}
|
||||
return chars.join('');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Multi-node manager
|
||||
// ---------------------------------------------------------------------------
|
||||
class MaterialDetector {
|
||||
constructor() {
|
||||
this.trackers = new Map(); // nodeId -> NullPatternTracker
|
||||
this.startTime = null;
|
||||
this.allEvents = [];
|
||||
}
|
||||
|
||||
ingestCSI(nodeId, timestamp, amplitudes) {
|
||||
if (!this.startTime) this.startTime = timestamp;
|
||||
|
||||
if (!this.trackers.has(nodeId)) {
|
||||
this.trackers.set(nodeId, new NullPatternTracker(amplitudes.length));
|
||||
}
|
||||
const tracker = this.trackers.get(nodeId);
|
||||
tracker.lastTime = timestamp;
|
||||
if (!tracker.startTime) tracker.startTime = timestamp;
|
||||
|
||||
// Baseline phase
|
||||
const elapsed = timestamp - tracker.startTime;
|
||||
if (elapsed < BASELINE_SEC) {
|
||||
tracker.updateBaseline(amplitudes);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Finalize baseline on transition
|
||||
if (!tracker.baselineEstablished) {
|
||||
tracker.finalizeBaseline();
|
||||
}
|
||||
|
||||
tracker.updateCurrent(amplitudes);
|
||||
return null; // actual detection happens on analyze() call
|
||||
}
|
||||
|
||||
analyze(timestamp) {
|
||||
const results = {};
|
||||
for (const [nodeId, tracker] of this.trackers) {
|
||||
const result = tracker.detectChanges(timestamp);
|
||||
if (result) {
|
||||
result.nodeId = nodeId;
|
||||
results[nodeId] = result;
|
||||
if (result.changeDetected) {
|
||||
this.allEvents.push(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Packet parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseCsiJsonl(record) {
|
||||
if (record.type !== 'raw_csi' || !record.iq_hex) return null;
|
||||
const nSc = record.subcarriers || 64;
|
||||
const bytes = Buffer.from(record.iq_hex, 'hex');
|
||||
const amplitudes = new Float64Array(nSc);
|
||||
|
||||
for (let sc = 0; sc < nSc; sc++) {
|
||||
const offset = 2 + sc * 2;
|
||||
if (offset + 1 >= bytes.length) break;
|
||||
let I = bytes[offset]; if (I > 127) I -= 256;
|
||||
let Q = bytes[offset + 1]; if (Q > 127) Q -= 256;
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
return { timestamp: record.timestamp, nodeId: record.node_id, amplitudes };
|
||||
}
|
||||
|
||||
function parseCsiUdp(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const nSc = buf.readUInt16LE(6);
|
||||
const amplitudes = new Float64Array(nSc);
|
||||
|
||||
for (let sc = 0; sc < nSc; sc++) {
|
||||
const offset = HEADER_SIZE + sc * 2;
|
||||
if (offset + 1 >= buf.length) break;
|
||||
const I = buf.readInt8(offset);
|
||||
const Q = buf.readInt8(offset + 1);
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
return { timestamp: Date.now() / 1000, nodeId, amplitudes };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const detector = new MaterialDetector();
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
let baselineReported = new Set();
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
|
||||
const csi = parseCsiJsonl(record);
|
||||
if (!csi) continue;
|
||||
|
||||
detector.ingestCSI(csi.nodeId, csi.timestamp, csi.amplitudes);
|
||||
frameCount++;
|
||||
|
||||
// Report baseline completion
|
||||
for (const [nodeId, tracker] of detector.trackers) {
|
||||
if (tracker.baselineEstablished && !baselineReported.has(nodeId)) {
|
||||
baselineReported.add(nodeId);
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Node ${nodeId}: baseline established (${tracker.baselineNulls.size} nulls, ${((tracker.baselineNulls.size / tracker.nSc) * 100).toFixed(0)}%)`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const tsMs = csi.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
const results = detector.analyze(csi.timestamp);
|
||||
|
||||
for (const [nodeId, result] of Object.entries(results)) {
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify(result));
|
||||
} else if (result.changeDetected) {
|
||||
const ts = new Date(csi.timestamp * 1000).toISOString().slice(11, 19);
|
||||
console.log(`[${ts}] Node ${nodeId}: ${result.eventType.toUpperCase()} | ${result.material} | nulls ${result.baselineNullCount} -> ${result.currentNullCount} (delta ${result.nullDelta > 0 ? '+' : ''}${result.nullDelta})`);
|
||||
if (result.newNullRange) console.log(` New nulls: sc ${result.newNullRange[0]}-${result.newNullRange[1]} (${result.newNulls} subcarriers)`);
|
||||
if (result.removedNullRange) console.log(` Removed nulls: sc ${result.removedNullRange[0]}-${result.removedNullRange[1]} (${result.removedNulls} subcarriers)`);
|
||||
if (result.ampChanges > 0) console.log(` Amplitude changes: ${result.ampChanges} subcarriers`);
|
||||
}
|
||||
}
|
||||
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Summary
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log('\n' + '='.repeat(60));
|
||||
console.log('MATERIAL/OBJECT CHANGE DETECTION SUMMARY');
|
||||
console.log('='.repeat(60));
|
||||
|
||||
for (const [nodeId, tracker] of detector.trackers) {
|
||||
console.log(`\nNode ${nodeId}:`);
|
||||
console.log(` Baseline nulls: ${tracker.baselineNulls.size} / ${tracker.nSc} (${((tracker.baselineNulls.size / tracker.nSc) * 100).toFixed(0)}%)`);
|
||||
console.log(` Current map: ${tracker.renderNullMap()}`);
|
||||
console.log(` Legend: _ = baseline null, X = new null, O = removed null, \u2581 = normal`);
|
||||
}
|
||||
|
||||
console.log(`\nTotal change events: ${detector.allEvents.length}`);
|
||||
if (detector.allEvents.length > 0) {
|
||||
const types = {};
|
||||
const materials = {};
|
||||
for (const e of detector.allEvents) {
|
||||
types[e.eventType] = (types[e.eventType] || 0) + 1;
|
||||
materials[e.material] = (materials[e.material] || 0) + 1;
|
||||
}
|
||||
console.log('Event types:');
|
||||
for (const [t, c] of Object.entries(types)) console.log(` ${t}: ${c}`);
|
||||
console.log('Materials:');
|
||||
for (const [m, c] of Object.entries(materials)) console.log(` ${m}: ${c}`);
|
||||
}
|
||||
|
||||
console.log(`\nProcessed ${frameCount} CSI frames`);
|
||||
} else {
|
||||
console.log(JSON.stringify({
|
||||
type: 'summary',
|
||||
events: detector.allEvents.length,
|
||||
frames: frameCount,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live UDP mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const detector = new MaterialDetector();
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', (buf) => {
|
||||
const csi = parseCsiUdp(buf);
|
||||
if (csi) detector.ingestCSI(csi.nodeId, csi.timestamp, csi.amplitudes);
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
const results = detector.analyze(Date.now() / 1000);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
for (const result of Object.values(results)) {
|
||||
console.log(JSON.stringify(result));
|
||||
}
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
console.log('=== MATERIAL/OBJECT DETECTOR (ADR-077) ===\n');
|
||||
|
||||
for (const [nodeId, tracker] of detector.trackers) {
|
||||
if (!tracker.baselineEstablished) {
|
||||
const elapsed = tracker.lastTime ? tracker.lastTime - tracker.startTime : 0;
|
||||
console.log(`Node ${nodeId}: establishing baseline... ${elapsed.toFixed(0)}/${BASELINE_SEC}s`);
|
||||
} else {
|
||||
console.log(`Node ${nodeId}: ${tracker.renderNullMap()}`);
|
||||
console.log(` Baseline: ${tracker.baselineNulls.size} nulls | Current: ${[...Array(tracker.nSc)].filter((_, i) => tracker.currentAmps[i] < NULL_THRESHOLD).length} nulls`);
|
||||
}
|
||||
}
|
||||
|
||||
if (detector.allEvents.length > 0) {
|
||||
console.log('\nRecent events:');
|
||||
for (const e of detector.allEvents.slice(-5)) {
|
||||
const ts = new Date(e.timestamp * 1000).toISOString().slice(11, 19);
|
||||
console.log(` [${ts}] ${e.eventType} | ${e.material} | delta ${e.nullDelta}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\nTotal events: ${detector.allEvents.length}`);
|
||||
}
|
||||
}, INTERVAL_MS);
|
||||
|
||||
server.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Material Detector listening on UDP :${PORT} (baseline: ${BASELINE_SEC}s)`);
|
||||
}
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => { server.close(); process.exit(0); });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,666 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-076: Multi-Node Graph Transformer for CSI Fusion
|
||||
*
|
||||
* Builds a graph from multiple ESP32 nodes and applies graph attention to
|
||||
* fuse their CSI feature vectors (either 8-dim hand-crafted or 128-dim CNN)
|
||||
* into a single multi-viewpoint representation.
|
||||
*
|
||||
* The graph structure:
|
||||
* - Each ESP32 node = graph node with a feature vector
|
||||
* - Edge between nodes weighted by cross-node correlation
|
||||
* - Attention learns which node to trust more per prediction
|
||||
*
|
||||
* Modes:
|
||||
* --live Listen on UDP for real-time multi-node CSI
|
||||
* --file FILE Read from a .csi.jsonl recording with multiple node_ids
|
||||
* --dim DIM Feature dimension (8 for hand-crafted, 128 for CNN)
|
||||
* --heads H Number of attention heads (default: 4)
|
||||
* --json JSON output
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/mesh-graph-transformer.js --file data/recordings/pretrain-1775182186.csi.jsonl
|
||||
* node scripts/mesh-graph-transformer.js --live --port 5006 --dim 128
|
||||
*
|
||||
* ADR: docs/adr/ADR-076-csi-spectrogram-embeddings.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
file: { type: 'string', short: 'f' },
|
||||
live: { type: 'boolean', default: false },
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
dim: { type: 'string', short: 'd', default: '8' },
|
||||
heads: { type: 'string', short: 'h', default: '4' },
|
||||
window: { type: 'string', short: 'w', default: '20' },
|
||||
json: { type: 'boolean', default: false },
|
||||
limit: { type: 'string', short: 'l' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const FEAT_DIM = parseInt(args.dim, 10);
|
||||
const NUM_HEADS = parseInt(args.heads, 10);
|
||||
const WINDOW_SIZE = parseInt(args.window, 10);
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const LIMIT = args.limit ? parseInt(args.limit, 10) : Infinity;
|
||||
const JSON_OUTPUT = args.json;
|
||||
|
||||
// ADR-018 packet constants
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// IQ Parsing (shared with csi-spectrogram.js)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function parseIqHex(iqHex, nSubcarriers) {
|
||||
const amps = new Float32Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = sc * 4;
|
||||
if (offset + 4 > iqHex.length) break;
|
||||
const iVal = parseInt(iqHex.substring(offset, offset + 2), 16);
|
||||
const qVal = parseInt(iqHex.substring(offset + 2, offset + 4), 16);
|
||||
amps[sc] = Math.sqrt(iVal * iVal + qVal * qVal);
|
||||
}
|
||||
return amps;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 8-dim Hand-Crafted Feature Extraction
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Extract 8-dim feature vector from subcarrier amplitudes.
|
||||
* Matches the features used by seed_csi_bridge.py (ADR-069).
|
||||
* @param {Float32Array} amplitudes
|
||||
* @param {number} rssi
|
||||
* @returns {Float32Array}
|
||||
*/
|
||||
function extract8DimFeatures(amplitudes, rssi) {
|
||||
const n = amplitudes.length;
|
||||
if (n === 0) return new Float32Array(8);
|
||||
|
||||
let sum = 0, sumSq = 0, maxAmp = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
const v = amplitudes[i];
|
||||
sum += v;
|
||||
sumSq += v * v;
|
||||
if (v > maxAmp) maxAmp = v;
|
||||
}
|
||||
const mean = sum / n;
|
||||
const variance = sumSq / n - mean * mean;
|
||||
|
||||
// Phase: approximate from I/Q sign pattern (simplified)
|
||||
const phaseMean = 0; // Would need raw I/Q for true phase
|
||||
const phaseVariance = 0;
|
||||
|
||||
// Bandwidth: number of subcarriers above noise floor
|
||||
const noiseFloor = mean * 0.1;
|
||||
let bw = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
if (amplitudes[i] > noiseFloor) bw++;
|
||||
}
|
||||
|
||||
// Spectral centroid
|
||||
let weightedSum = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
weightedSum += i * amplitudes[i];
|
||||
}
|
||||
const centroid = sum > 0 ? weightedSum / sum : n / 2;
|
||||
|
||||
return new Float32Array([
|
||||
mean,
|
||||
variance,
|
||||
maxAmp,
|
||||
phaseMean,
|
||||
phaseVariance,
|
||||
bw / n, // normalized bandwidth
|
||||
centroid / n, // normalized centroid
|
||||
Math.abs(rssi) / 100, // normalized RSSI
|
||||
]);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Graph Attention Layer (Pure JS, no WASM dependency)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Multi-head graph attention network (GATv2-style).
|
||||
*
|
||||
* For a graph with N nodes each having D-dimensional features:
|
||||
* 1. Project features to Q, K, V using learned weights
|
||||
* 2. Compute attention scores with edge weight bias
|
||||
* 3. Aggregate via softmax-weighted sum
|
||||
* 4. Produce fused D-dimensional output
|
||||
*/
|
||||
class GraphAttentionLayer {
|
||||
/**
|
||||
* @param {number} inputDim - Feature dimension per node
|
||||
* @param {number} numHeads - Number of attention heads
|
||||
*/
|
||||
constructor(inputDim, numHeads) {
|
||||
this.inputDim = inputDim;
|
||||
this.numHeads = numHeads;
|
||||
this.headDim = Math.max(1, Math.floor(inputDim / numHeads));
|
||||
|
||||
// Initialize projection weights (Xavier uniform)
|
||||
this.Wq = this._initWeights(inputDim, this.headDim * numHeads);
|
||||
this.Wk = this._initWeights(inputDim, this.headDim * numHeads);
|
||||
this.Wv = this._initWeights(inputDim, this.headDim * numHeads);
|
||||
this.Wo = this._initWeights(this.headDim * numHeads, inputDim);
|
||||
|
||||
// Edge weight bias scale
|
||||
this.edgeBiasScale = 0.5;
|
||||
}
|
||||
|
||||
/** Xavier-uniform weight initialization. */
|
||||
_initWeights(rows, cols) {
|
||||
const limit = Math.sqrt(6 / (rows + cols));
|
||||
const w = new Float32Array(rows * cols);
|
||||
for (let i = 0; i < w.length; i++) {
|
||||
w[i] = (Math.random() * 2 - 1) * limit;
|
||||
}
|
||||
return { data: w, rows, cols };
|
||||
}
|
||||
|
||||
/** Matrix-vector multiply: out = W * x. */
|
||||
_matvec(W, x) {
|
||||
const out = new Float32Array(W.rows);
|
||||
for (let r = 0; r < W.rows; r++) {
|
||||
let sum = 0;
|
||||
for (let c = 0; c < W.cols; c++) {
|
||||
sum += W.data[r * W.cols + c] * x[c];
|
||||
}
|
||||
out[r] = sum;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute attention-fused output for a set of nodes.
|
||||
*
|
||||
* @param {Float32Array[]} nodeFeatures - Array of D-dim feature vectors, one per node
|
||||
* @param {Map<string, number>} edgeWeights - Map of "i-j" -> weight (cross-correlation)
|
||||
* @returns {{ fused: Float32Array, attentionWeights: number[][] }}
|
||||
*/
|
||||
forward(nodeFeatures, edgeWeights) {
|
||||
const N = nodeFeatures.length;
|
||||
if (N === 0) return { fused: new Float32Array(this.inputDim), attentionWeights: [] };
|
||||
if (N === 1) return { fused: new Float32Array(nodeFeatures[0]), attentionWeights: [[1.0]] };
|
||||
|
||||
const D = this.headDim;
|
||||
const H = this.numHeads;
|
||||
|
||||
// Project to Q, K, V for each node
|
||||
const queries = nodeFeatures.map(f => this._matvec(this.Wq, f));
|
||||
const keys = nodeFeatures.map(f => this._matvec(this.Wk, f));
|
||||
const values = nodeFeatures.map(f => this._matvec(this.Wv, f));
|
||||
|
||||
// Compute per-head attention scores with edge bias
|
||||
const scale = 1 / Math.sqrt(D);
|
||||
const allAttentionWeights = [];
|
||||
|
||||
// Aggregate output per node (we produce a fused vector for each node)
|
||||
const nodeOutputs = [];
|
||||
|
||||
for (let i = 0; i < N; i++) {
|
||||
const headOutputs = [];
|
||||
|
||||
for (let h = 0; h < H; h++) {
|
||||
const hOff = h * D;
|
||||
|
||||
// Compute attention scores from node i to all other nodes
|
||||
const scores = new Float32Array(N);
|
||||
for (let j = 0; j < N; j++) {
|
||||
let dot = 0;
|
||||
for (let d = 0; d < D; d++) {
|
||||
dot += queries[i][hOff + d] * keys[j][hOff + d];
|
||||
}
|
||||
// Add edge weight bias
|
||||
const edgeKey = i < j ? `${i}-${j}` : `${j}-${i}`;
|
||||
const ew = edgeWeights.get(edgeKey) || 0;
|
||||
scores[j] = dot * scale + ew * this.edgeBiasScale;
|
||||
}
|
||||
|
||||
// Softmax
|
||||
let maxScore = -Infinity;
|
||||
for (let j = 0; j < N; j++) {
|
||||
if (scores[j] > maxScore) maxScore = scores[j];
|
||||
}
|
||||
let sumExp = 0;
|
||||
const attn = new Float32Array(N);
|
||||
for (let j = 0; j < N; j++) {
|
||||
attn[j] = Math.exp(scores[j] - maxScore);
|
||||
sumExp += attn[j];
|
||||
}
|
||||
for (let j = 0; j < N; j++) {
|
||||
attn[j] /= sumExp;
|
||||
}
|
||||
|
||||
if (i === 0 && h === 0) {
|
||||
allAttentionWeights.push(Array.from(attn));
|
||||
}
|
||||
|
||||
// Weighted sum of values
|
||||
const headOut = new Float32Array(D);
|
||||
for (let j = 0; j < N; j++) {
|
||||
for (let d = 0; d < D; d++) {
|
||||
headOut[d] += attn[j] * values[j][hOff + d];
|
||||
}
|
||||
}
|
||||
headOutputs.push(headOut);
|
||||
}
|
||||
|
||||
// Concatenate heads
|
||||
const concat = new Float32Array(H * D);
|
||||
for (let h = 0; h < H; h++) {
|
||||
concat.set(headOutputs[h], h * D);
|
||||
}
|
||||
|
||||
// Project back to input dimension
|
||||
nodeOutputs.push(this._matvec(this.Wo, concat));
|
||||
}
|
||||
|
||||
// Fuse all node outputs via mean pooling
|
||||
const fused = new Float32Array(this.inputDim);
|
||||
for (let i = 0; i < N; i++) {
|
||||
for (let d = 0; d < this.inputDim; d++) {
|
||||
fused[d] += nodeOutputs[i][d] / N;
|
||||
}
|
||||
}
|
||||
|
||||
return { fused, attentionWeights: allAttentionWeights };
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Cross-Node Correlation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Compute Pearson correlation between two amplitude vectors.
|
||||
* Used as edge weight in the graph.
|
||||
*/
|
||||
function pearsonCorrelation(a, b) {
|
||||
const n = Math.min(a.length, b.length);
|
||||
if (n === 0) return 0;
|
||||
|
||||
let sumA = 0, sumB = 0, sumAB = 0, sumA2 = 0, sumB2 = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
sumA += a[i];
|
||||
sumB += b[i];
|
||||
sumAB += a[i] * b[i];
|
||||
sumA2 += a[i] * a[i];
|
||||
sumB2 += b[i] * b[i];
|
||||
}
|
||||
|
||||
const num = n * sumAB - sumA * sumB;
|
||||
const den = Math.sqrt((n * sumA2 - sumA * sumA) * (n * sumB2 - sumB * sumB));
|
||||
return den > 0 ? num / den : 0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Graph Builder
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Build and maintain a graph of ESP32 nodes.
|
||||
* Stores the latest feature vector per node and computes edge weights.
|
||||
*/
|
||||
class MeshGraph {
|
||||
constructor(featDim, numHeads) {
|
||||
this.featDim = featDim;
|
||||
/** @type {Map<number, { features: Float32Array, amplitudes: Float32Array, rssi: number, timestamp: number }>} */
|
||||
this.nodes = new Map();
|
||||
this.attention = new GraphAttentionLayer(featDim, numHeads);
|
||||
this.fusionCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a node's features.
|
||||
* @param {number} nodeId
|
||||
* @param {Float32Array} features - D-dim feature vector
|
||||
* @param {Float32Array} amplitudes - Raw subcarrier amplitudes (for cross-correlation)
|
||||
* @param {number} rssi
|
||||
* @param {number} timestamp
|
||||
*/
|
||||
updateNode(nodeId, features, amplitudes, rssi, timestamp) {
|
||||
this.nodes.set(nodeId, { features, amplitudes, rssi, timestamp });
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute edge weights between all node pairs.
|
||||
* @returns {Map<string, number>}
|
||||
*/
|
||||
computeEdgeWeights() {
|
||||
const weights = new Map();
|
||||
const nodeIds = Array.from(this.nodes.keys()).sort();
|
||||
|
||||
for (let i = 0; i < nodeIds.length; i++) {
|
||||
for (let j = i + 1; j < nodeIds.length; j++) {
|
||||
const a = this.nodes.get(nodeIds[i]);
|
||||
const b = this.nodes.get(nodeIds[j]);
|
||||
const corr = pearsonCorrelation(a.amplitudes, b.amplitudes);
|
||||
weights.set(`${i}-${j}`, corr);
|
||||
}
|
||||
}
|
||||
|
||||
return weights;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run graph attention to produce a fused feature vector.
|
||||
* @returns {{ fused: Float32Array, attentionWeights: number[][], nodeIds: number[], edgeWeights: Map<string, number> } | null}
|
||||
*/
|
||||
fuse() {
|
||||
if (this.nodes.size < 2) return null;
|
||||
|
||||
const nodeIds = Array.from(this.nodes.keys()).sort();
|
||||
const features = nodeIds.map(id => this.nodes.get(id).features);
|
||||
const edgeWeights = this.computeEdgeWeights();
|
||||
|
||||
const { fused, attentionWeights } = this.attention.forward(features, edgeWeights);
|
||||
this.fusionCount++;
|
||||
|
||||
return { fused, attentionWeights, nodeIds, edgeWeights };
|
||||
}
|
||||
|
||||
/** Pretty-print graph state. */
|
||||
toString() {
|
||||
const nodeIds = Array.from(this.nodes.keys()).sort();
|
||||
const lines = [`Graph: ${nodeIds.length} nodes [${nodeIds.join(', ')}]`];
|
||||
|
||||
if (nodeIds.length >= 2) {
|
||||
const edgeWeights = this.computeEdgeWeights();
|
||||
for (const [key, weight] of edgeWeights) {
|
||||
const [i, j] = key.split('-').map(Number);
|
||||
lines.push(` Edge ${nodeIds[i]}->${nodeIds[j]}: correlation=${weight.toFixed(4)}`);
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Optional: Graph-WASM Visualization
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
let graphDb = null;
|
||||
|
||||
/**
|
||||
* Initialize @ruvector/graph-wasm for persistent graph storage.
|
||||
* Optional -- only used if the WASM file exists.
|
||||
*/
|
||||
async function initGraphDb() {
|
||||
try {
|
||||
const graphWasmPath = path.resolve(
|
||||
__dirname, '..', 'vendor', 'ruvector', 'npm', 'packages', 'graph-wasm'
|
||||
);
|
||||
const graphWasm = require(graphWasmPath);
|
||||
await graphWasm.default();
|
||||
graphDb = new graphWasm.GraphDB('cosine');
|
||||
if (!JSON_OUTPUT) console.log('[graph-wasm] Initialized persistent graph DB');
|
||||
return true;
|
||||
} catch {
|
||||
if (!JSON_OUTPUT) console.log('[graph-wasm] Not available, using in-memory graph only');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist the mesh graph to @ruvector/graph-wasm.
|
||||
* @param {MeshGraph} mesh
|
||||
* @param {object} fusionResult
|
||||
*/
|
||||
function persistToGraphDb(mesh, fusionResult) {
|
||||
if (!graphDb) return;
|
||||
|
||||
const { nodeIds, edgeWeights, fused, attentionWeights } = fusionResult;
|
||||
|
||||
// Create/update nodes
|
||||
for (const nodeId of nodeIds) {
|
||||
const node = mesh.nodes.get(nodeId);
|
||||
const existingId = `esp32-node-${nodeId}`;
|
||||
try { graphDb.deleteNode(existingId); } catch { /* ignore */ }
|
||||
graphDb.createNode(['ESP32', 'SensingNode'], {
|
||||
id: existingId,
|
||||
node_id: nodeId,
|
||||
rssi: node.rssi,
|
||||
timestamp: node.timestamp,
|
||||
feature_dim: mesh.featDim,
|
||||
});
|
||||
}
|
||||
|
||||
// Create edges with correlation weights
|
||||
for (const [key, weight] of edgeWeights) {
|
||||
const [i, j] = key.split('-').map(Number);
|
||||
try {
|
||||
graphDb.createEdge(
|
||||
`esp32-node-${nodeIds[i]}`,
|
||||
`esp32-node-${nodeIds[j]}`,
|
||||
'CSI_CORRELATION',
|
||||
{ weight, fusion_count: mesh.fusionCount }
|
||||
);
|
||||
} catch { /* ignore duplicate edges */ }
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// File Mode
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function processFile(filePath) {
|
||||
await initGraphDb();
|
||||
|
||||
const mesh = new MeshGraph(FEAT_DIM, NUM_HEADS);
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let fusionCount = 0;
|
||||
const nodeFrameCounts = new Map();
|
||||
|
||||
for await (const line of rl) {
|
||||
if (frameCount >= LIMIT) break;
|
||||
|
||||
let frame;
|
||||
try {
|
||||
frame = JSON.parse(line);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
|
||||
const nodeId = frame.node_id || 0;
|
||||
const nSubcarriers = frame.subcarriers || 64;
|
||||
const iqHex = frame.iq_hex || '';
|
||||
if (!iqHex) continue;
|
||||
|
||||
const amplitudes = parseIqHex(iqHex, nSubcarriers);
|
||||
const rssi = frame.rssi || 0;
|
||||
|
||||
// Extract feature vector based on configured dimension
|
||||
let features;
|
||||
if (FEAT_DIM === 8) {
|
||||
features = extract8DimFeatures(amplitudes, rssi);
|
||||
} else {
|
||||
// For CNN embeddings, we need the csi-spectrogram.js pipeline.
|
||||
// In file mode without CNN, use padded 8-dim features as a placeholder.
|
||||
const base = extract8DimFeatures(amplitudes, rssi);
|
||||
features = new Float32Array(FEAT_DIM);
|
||||
features.set(base.subarray(0, Math.min(8, FEAT_DIM)));
|
||||
}
|
||||
|
||||
mesh.updateNode(nodeId, features, amplitudes, rssi, frame.timestamp || 0);
|
||||
frameCount++;
|
||||
|
||||
const nc = (nodeFrameCounts.get(nodeId) || 0) + 1;
|
||||
nodeFrameCounts.set(nodeId, nc);
|
||||
|
||||
// Attempt fusion every WINDOW_SIZE frames (when we have data from multiple nodes)
|
||||
if (frameCount % WINDOW_SIZE === 0 && mesh.nodes.size >= 2) {
|
||||
const result = mesh.fuse();
|
||||
if (result) {
|
||||
fusionCount++;
|
||||
persistToGraphDb(mesh, result);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
type: 'fusion',
|
||||
fusionIdx: fusionCount,
|
||||
nodeIds: result.nodeIds,
|
||||
edgeWeights: Object.fromEntries(result.edgeWeights),
|
||||
attentionWeights: result.attentionWeights,
|
||||
fused: Array.from(result.fused).map(v => +v.toFixed(6)),
|
||||
}));
|
||||
} else {
|
||||
console.log(`\n[fusion ${fusionCount}] ${mesh.toString()}`);
|
||||
if (result.attentionWeights.length > 0) {
|
||||
const aw = result.attentionWeights[0].map(w => w.toFixed(3));
|
||||
console.log(` Attention (head 0): [${aw.join(', ')}]`);
|
||||
}
|
||||
const fusedSnippet = Array.from(result.fused.subarray(0, 4)).map(v => v.toFixed(4)).join(', ');
|
||||
console.log(` Fused: [${fusedSnippet}, ...] (dim=${FEAT_DIM})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`\nProcessed ${frameCount} frames from ${nodeFrameCounts.size} nodes`);
|
||||
console.log(`Produced ${fusionCount} fusions with ${NUM_HEADS}-head attention`);
|
||||
for (const [nodeId, count] of nodeFrameCounts) {
|
||||
console.log(` Node ${nodeId}: ${count} frames`);
|
||||
}
|
||||
if (graphDb) {
|
||||
const stats = graphDb.stats();
|
||||
console.log(`Graph DB: ${stats.nodeCount} nodes, ${stats.edgeCount} edges`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live Mode
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function processLive() {
|
||||
await initGraphDb();
|
||||
|
||||
const mesh = new MeshGraph(FEAT_DIM, NUM_HEADS);
|
||||
let frameCount = 0;
|
||||
let fusionCount = 0;
|
||||
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', (msg) => {
|
||||
let nodeId, nSubcarriers, amplitudes, rssi;
|
||||
|
||||
// Try binary ADR-018 format
|
||||
if (msg.length >= HEADER_SIZE && msg.readUInt32LE(0) === CSI_MAGIC) {
|
||||
nodeId = msg.readUInt8(4);
|
||||
rssi = msg.readInt8(5);
|
||||
nSubcarriers = msg.readUInt16LE(6);
|
||||
amplitudes = new Float32Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const off = HEADER_SIZE + sc * 2;
|
||||
if (off + 2 > msg.length) break;
|
||||
amplitudes[sc] = Math.sqrt(msg[off] ** 2 + msg[off + 1] ** 2);
|
||||
}
|
||||
} else {
|
||||
// Try JSONL
|
||||
try {
|
||||
const frame = JSON.parse(msg.toString());
|
||||
nodeId = frame.node_id || 0;
|
||||
nSubcarriers = frame.subcarriers || 64;
|
||||
amplitudes = parseIqHex(frame.iq_hex || '', nSubcarriers);
|
||||
rssi = frame.rssi || 0;
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let features;
|
||||
if (FEAT_DIM === 8) {
|
||||
features = extract8DimFeatures(amplitudes, rssi);
|
||||
} else {
|
||||
const base = extract8DimFeatures(amplitudes, rssi);
|
||||
features = new Float32Array(FEAT_DIM);
|
||||
features.set(base.subarray(0, Math.min(8, FEAT_DIM)));
|
||||
}
|
||||
|
||||
mesh.updateNode(nodeId, features, amplitudes, rssi, Date.now() / 1000);
|
||||
frameCount++;
|
||||
|
||||
if (frameCount % WINDOW_SIZE === 0 && mesh.nodes.size >= 2) {
|
||||
const result = mesh.fuse();
|
||||
if (result) {
|
||||
fusionCount++;
|
||||
persistToGraphDb(mesh, result);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
type: 'fusion',
|
||||
fusionIdx: fusionCount,
|
||||
nodeIds: result.nodeIds,
|
||||
edgeWeights: Object.fromEntries(result.edgeWeights),
|
||||
attentionWeights: result.attentionWeights,
|
||||
fused: Array.from(result.fused).map(v => +v.toFixed(6)),
|
||||
}));
|
||||
} else {
|
||||
console.log(`[fusion ${fusionCount}] nodes=${result.nodeIds.join(',')}` +
|
||||
` corr=${Array.from(result.edgeWeights.values()).map(v => v.toFixed(3)).join(',')}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
server.on('listening', () => {
|
||||
const addr = server.address();
|
||||
console.log(`[live] Mesh graph transformer on UDP ${addr.address}:${addr.port}`);
|
||||
console.log(`[live] Feature dim: ${FEAT_DIM}, heads: ${NUM_HEADS}, window: ${WINDOW_SIZE}`);
|
||||
});
|
||||
|
||||
server.bind(PORT);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function main() {
|
||||
if (!args.file && !args.live) {
|
||||
console.error('Usage: node scripts/mesh-graph-transformer.js --file <path> [--dim 8|128] [--heads 4]');
|
||||
console.error(' node scripts/mesh-graph-transformer.js --live [--port 5006] [--dim 128]');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (args.file) {
|
||||
const filePath = path.resolve(args.file);
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
await processFile(filePath);
|
||||
} else {
|
||||
await processLive();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('Fatal:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,766 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-075: Min-Cut Person Counter — Subcarrier correlation graph partitioning
|
||||
*
|
||||
* Fixes issue #348: n_persons always shows 4. Instead of threshold-based
|
||||
* counting, builds a subcarrier correlation graph and uses Stoer-Wagner
|
||||
* min-cut to find naturally independent groups of correlated subcarriers.
|
||||
* Each group = one person's Fresnel zone perturbation.
|
||||
*
|
||||
* Usage:
|
||||
* # Live from ESP32 nodes via UDP
|
||||
* node scripts/mincut-person-counter.js --port 5006
|
||||
*
|
||||
* # Replay from recorded CSI data
|
||||
* node scripts/mincut-person-counter.js --replay data/recordings/pretrain-1775182186.csi.jsonl
|
||||
*
|
||||
* # JSON output for piping to seed bridge
|
||||
* node scripts/mincut-person-counter.js --replay FILE --json
|
||||
*
|
||||
* # Override feature vector dim 5 and forward to seed bridge
|
||||
* node scripts/mincut-person-counter.js --port 5006 --forward 5007
|
||||
*
|
||||
* ADR: docs/adr/ADR-075-mincut-person-separation.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
json: { type: 'boolean', default: false },
|
||||
forward: { type: 'string', short: 'f' },
|
||||
interval: { type: 'string', short: 'i', default: '2000' },
|
||||
window: { type: 'string', short: 'w', default: '2000' },
|
||||
'corr-threshold': { type: 'string', default: '0.3' },
|
||||
'cut-threshold': { type: 'string', default: '2.0' },
|
||||
'var-floor': { type: 'string', default: '0.5' },
|
||||
'max-persons': { type: 'string', default: '8' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const WINDOW_MS = parseInt(args.window, 10);
|
||||
const CORR_THRESHOLD = parseFloat(args['corr-threshold']);
|
||||
const CUT_THRESHOLD = parseFloat(args['cut-threshold']);
|
||||
const VAR_FLOOR = parseFloat(args['var-floor']);
|
||||
const MAX_PERSONS = parseInt(args['max-persons'], 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const FORWARD_PORT = args.forward ? parseInt(args.forward, 10) : null;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ADR-018 packet constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per-node sliding window of subcarrier amplitudes
|
||||
// ---------------------------------------------------------------------------
|
||||
class SubcarrierWindow {
|
||||
constructor(maxAgeMs) {
|
||||
this.maxAgeMs = maxAgeMs;
|
||||
this.frames = []; // { timestamp, amplitudes: Float64Array }
|
||||
this.nSubcarriers = 0;
|
||||
}
|
||||
|
||||
push(timestamp, amplitudes) {
|
||||
this.nSubcarriers = amplitudes.length;
|
||||
this.frames.push({ timestamp, amplitudes: Float64Array.from(amplitudes) });
|
||||
this._prune(timestamp);
|
||||
}
|
||||
|
||||
_prune(now) {
|
||||
const cutoff = now - this.maxAgeMs;
|
||||
while (this.frames.length > 0 && this.frames[0].timestamp < cutoff) {
|
||||
this.frames.shift();
|
||||
}
|
||||
}
|
||||
|
||||
get length() { return this.frames.length; }
|
||||
|
||||
/**
|
||||
* Compute pairwise Pearson correlation matrix for all subcarrier pairs.
|
||||
* Returns { matrix: Float64Array (n*n row-major), n, activeIndices }
|
||||
*/
|
||||
correlationMatrix() {
|
||||
const nFrames = this.frames.length;
|
||||
const nSc = this.nSubcarriers;
|
||||
if (nFrames < 5 || nSc === 0) return null;
|
||||
|
||||
// Compute mean and std for each subcarrier
|
||||
const mean = new Float64Array(nSc);
|
||||
const std = new Float64Array(nSc);
|
||||
|
||||
for (let f = 0; f < nFrames; f++) {
|
||||
const amp = this.frames[f].amplitudes;
|
||||
for (let i = 0; i < nSc; i++) {
|
||||
mean[i] += amp[i];
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < nSc; i++) mean[i] /= nFrames;
|
||||
|
||||
for (let f = 0; f < nFrames; f++) {
|
||||
const amp = this.frames[f].amplitudes;
|
||||
for (let i = 0; i < nSc; i++) {
|
||||
const d = amp[i] - mean[i];
|
||||
std[i] += d * d;
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < nSc; i++) {
|
||||
std[i] = Math.sqrt(std[i] / (nFrames - 1));
|
||||
}
|
||||
|
||||
// Filter out null/static subcarriers (std below noise floor)
|
||||
const activeIndices = [];
|
||||
for (let i = 0; i < nSc; i++) {
|
||||
if (std[i] > VAR_FLOOR) {
|
||||
activeIndices.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
const n = activeIndices.length;
|
||||
if (n < 2) return { matrix: null, n: 0, activeIndices };
|
||||
|
||||
// Compute Pearson correlation for active pairs
|
||||
const matrix = new Float64Array(n * n);
|
||||
|
||||
for (let ai = 0; ai < n; ai++) {
|
||||
matrix[ai * n + ai] = 1.0; // self-correlation
|
||||
const si = activeIndices[ai];
|
||||
|
||||
for (let aj = ai + 1; aj < n; aj++) {
|
||||
const sj = activeIndices[aj];
|
||||
|
||||
let cov = 0;
|
||||
for (let f = 0; f < nFrames; f++) {
|
||||
const amp = this.frames[f].amplitudes;
|
||||
cov += (amp[si] - mean[si]) * (amp[sj] - mean[sj]);
|
||||
}
|
||||
cov /= (nFrames - 1);
|
||||
|
||||
const denom = std[si] * std[sj];
|
||||
const r = denom > 1e-10 ? cov / denom : 0;
|
||||
|
||||
matrix[ai * n + aj] = r;
|
||||
matrix[aj * n + ai] = r;
|
||||
}
|
||||
}
|
||||
|
||||
return { matrix, n, activeIndices };
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Weighted undirected graph (adjacency list)
|
||||
// ---------------------------------------------------------------------------
|
||||
class WeightedGraph {
|
||||
constructor(n) {
|
||||
this.n = n;
|
||||
// adj[i] = Map<j, weight>
|
||||
this.adj = new Array(n);
|
||||
for (let i = 0; i < n; i++) this.adj[i] = new Map();
|
||||
this.edgeCount = 0;
|
||||
}
|
||||
|
||||
addEdge(u, v, w) {
|
||||
if (u === v) return;
|
||||
if (!this.adj[u].has(v)) this.edgeCount++;
|
||||
this.adj[u].set(v, w);
|
||||
this.adj[v].set(u, w);
|
||||
}
|
||||
|
||||
/** Build graph from correlation matrix, keeping edges above threshold */
|
||||
static fromCorrelation(matrix, n, threshold) {
|
||||
const g = new WeightedGraph(n);
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = i + 1; j < n; j++) {
|
||||
const r = Math.abs(matrix[i * n + j]);
|
||||
if (r > threshold) {
|
||||
g.addEdge(i, j, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find connected components via BFS.
|
||||
* Returns array of arrays: each inner array = vertex indices in component.
|
||||
*/
|
||||
connectedComponents() {
|
||||
const visited = new Uint8Array(this.n);
|
||||
const components = [];
|
||||
|
||||
for (let start = 0; start < this.n; start++) {
|
||||
if (visited[start]) continue;
|
||||
const comp = [];
|
||||
const queue = [start];
|
||||
visited[start] = 1;
|
||||
|
||||
while (queue.length > 0) {
|
||||
const u = queue.shift();
|
||||
comp.push(u);
|
||||
for (const [v] of this.adj[u]) {
|
||||
if (!visited[v]) {
|
||||
visited[v] = 1;
|
||||
queue.push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
components.push(comp);
|
||||
}
|
||||
return components;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a subgraph containing only the specified vertices.
|
||||
* Returns a new WeightedGraph with vertices relabeled 0..vertices.length-1,
|
||||
* plus a mapping array from new index to original index.
|
||||
*/
|
||||
subgraph(vertices) {
|
||||
const newIdx = new Map();
|
||||
vertices.forEach((v, i) => newIdx.set(v, i));
|
||||
|
||||
const sub = new WeightedGraph(vertices.length);
|
||||
for (const u of vertices) {
|
||||
for (const [v, w] of this.adj[u]) {
|
||||
if (newIdx.has(v) && u < v) {
|
||||
sub.addEdge(newIdx.get(u), newIdx.get(v), w);
|
||||
}
|
||||
}
|
||||
}
|
||||
return { graph: sub, mapping: vertices };
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Stoer-Wagner minimum cut algorithm
|
||||
//
|
||||
// Finds the global minimum s-t cut of an undirected weighted graph.
|
||||
// Complexity: O(V * E) using adjacency list with priority tracking.
|
||||
//
|
||||
// Reference: Stoer & Wagner (1997), "A Simple Min-Cut Algorithm", JACM.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Run one "minimum cut phase" of Stoer-Wagner.
|
||||
*
|
||||
* Starting from an arbitrary vertex, greedily add the most tightly connected
|
||||
* vertex to the growing set A until all vertices are absorbed.
|
||||
*
|
||||
* @param {number} n - Number of active vertices
|
||||
* @param {Map<number, Map<number, number>>} adj - Adjacency: adj[u].get(v) = weight
|
||||
* @param {number[]} activeVertices - List of active vertex IDs
|
||||
* @returns {{ s: number, t: number, cutOfPhase: number }}
|
||||
*/
|
||||
function minimumCutPhase(n, adj, activeVertices) {
|
||||
// key[v] = sum of edge weights from v to vertices already in A
|
||||
const key = new Float64Array(n);
|
||||
const inA = new Uint8Array(n);
|
||||
const active = new Uint8Array(n);
|
||||
for (const v of activeVertices) active[v] = 1;
|
||||
|
||||
let s = -1, t = -1;
|
||||
|
||||
for (let iter = 0; iter < activeVertices.length; iter++) {
|
||||
// Find vertex not in A with maximum key value
|
||||
let best = -1, bestKey = -Infinity;
|
||||
for (const v of activeVertices) {
|
||||
if (!inA[v] && key[v] > bestKey) {
|
||||
bestKey = key[v];
|
||||
best = v;
|
||||
}
|
||||
}
|
||||
|
||||
// On first iteration when all keys are 0, just pick the first active vertex
|
||||
if (best === -1) {
|
||||
for (const v of activeVertices) {
|
||||
if (!inA[v]) { best = v; break; }
|
||||
}
|
||||
}
|
||||
|
||||
s = t;
|
||||
t = best;
|
||||
inA[best] = 1;
|
||||
|
||||
// Update keys: for each neighbor of best, increase key
|
||||
if (adj[best]) {
|
||||
for (const [neighbor, weight] of adj[best]) {
|
||||
if (active[neighbor] && !inA[neighbor]) {
|
||||
key[neighbor] += weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cut of the phase = sum of edges from t to all other active vertices
|
||||
let cutOfPhase = 0;
|
||||
if (adj[t]) {
|
||||
for (const [neighbor, weight] of adj[t]) {
|
||||
if (active[neighbor] && neighbor !== t) {
|
||||
cutOfPhase += weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { s, t, cutOfPhase };
|
||||
}
|
||||
|
||||
/**
|
||||
* Stoer-Wagner global minimum cut.
|
||||
*
|
||||
* @param {WeightedGraph} graph
|
||||
* @returns {{ minCutValue: number, partition: [number[], number[]] }}
|
||||
* partition[0] = vertices on one side, partition[1] = vertices on the other side
|
||||
*/
|
||||
function stoerWagner(graph) {
|
||||
const n = graph.n;
|
||||
if (n <= 1) return { minCutValue: Infinity, partition: [Array.from({length: n}, (_, i) => i), []] };
|
||||
|
||||
// Build mutable adjacency (Map-based for efficient merge)
|
||||
const adj = new Array(n);
|
||||
for (let i = 0; i < n; i++) adj[i] = new Map(graph.adj[i]);
|
||||
|
||||
// Track which original vertices each super-vertex contains
|
||||
const groups = new Array(n);
|
||||
for (let i = 0; i < n; i++) groups[i] = [i];
|
||||
|
||||
let activeVertices = Array.from({length: n}, (_, i) => i);
|
||||
let bestCut = Infinity;
|
||||
let bestPartitionSide = null; // group of vertices on the "t" side of the best cut
|
||||
|
||||
while (activeVertices.length > 1) {
|
||||
const { s, t, cutOfPhase } = minimumCutPhase(n, adj, activeVertices);
|
||||
|
||||
if (s === -1 || t === -1) break;
|
||||
|
||||
if (cutOfPhase < bestCut) {
|
||||
bestCut = cutOfPhase;
|
||||
bestPartitionSide = [...groups[t]];
|
||||
}
|
||||
|
||||
// Merge t into s: move all edges from t to s
|
||||
if (adj[t]) {
|
||||
for (const [neighbor, weight] of adj[t]) {
|
||||
if (neighbor === s) continue;
|
||||
const existing = adj[s].get(neighbor) || 0;
|
||||
adj[s].set(neighbor, existing + weight);
|
||||
// Update neighbor's adjacency
|
||||
adj[neighbor].delete(t);
|
||||
adj[neighbor].set(s, existing + weight);
|
||||
}
|
||||
}
|
||||
adj[s].delete(t);
|
||||
|
||||
// Merge group membership
|
||||
groups[s] = groups[s].concat(groups[t]);
|
||||
groups[t] = [];
|
||||
|
||||
// Remove t from active vertices
|
||||
activeVertices = activeVertices.filter(v => v !== t);
|
||||
}
|
||||
|
||||
// Build full partition
|
||||
if (!bestPartitionSide || bestPartitionSide.length === 0) {
|
||||
return { minCutValue: Infinity, partition: [Array.from({length: n}, (_, i) => i), []] };
|
||||
}
|
||||
|
||||
const sideSet = new Set(bestPartitionSide);
|
||||
const sideA = [], sideB = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
if (sideSet.has(i)) sideA.push(i);
|
||||
else sideB.push(i);
|
||||
}
|
||||
|
||||
return { minCutValue: bestCut, partition: [sideA, sideB] };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Recursive min-cut person separator
|
||||
//
|
||||
// Recursively applies Stoer-Wagner to split the correlation graph into
|
||||
// independent clusters. Each cluster = one person's Fresnel zone group.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @param {WeightedGraph} graph
|
||||
* @param {number} cutThreshold - min-cut below this = real person boundary
|
||||
* @param {number} maxPersons - stop splitting after this many partitions
|
||||
* @returns {number[][]} - array of vertex groups (each = one person's subcarriers)
|
||||
*/
|
||||
function separatePersons(graph, cutThreshold, maxPersons) {
|
||||
// Start with connected components (disconnected groups are trivially separate)
|
||||
const components = graph.connectedComponents();
|
||||
const personGroups = [];
|
||||
|
||||
for (const comp of components) {
|
||||
if (comp.length < 2) {
|
||||
// Single vertex — not enough for a person
|
||||
continue;
|
||||
}
|
||||
_splitComponent(graph, comp, cutThreshold, maxPersons, personGroups);
|
||||
}
|
||||
|
||||
return personGroups;
|
||||
}
|
||||
|
||||
function _splitComponent(graph, vertices, cutThreshold, maxPersons, result) {
|
||||
if (vertices.length < 2 || result.length >= maxPersons) {
|
||||
if (vertices.length >= 2) result.push(vertices);
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract subgraph
|
||||
const { graph: sub, mapping } = graph.subgraph(vertices);
|
||||
|
||||
// Run Stoer-Wagner on the subgraph
|
||||
const { minCutValue, partition } = stoerWagner(sub);
|
||||
|
||||
// If the min-cut is above threshold, this is one coherent group (one person)
|
||||
if (minCutValue >= cutThreshold || partition[0].length === 0 || partition[1].length === 0) {
|
||||
result.push(vertices);
|
||||
return;
|
||||
}
|
||||
|
||||
// Map partition indices back to original vertex IDs
|
||||
const groupA = partition[0].map(i => mapping[i]);
|
||||
const groupB = partition[1].map(i => mapping[i]);
|
||||
|
||||
// Recurse on each side
|
||||
_splitComponent(graph, groupA, cutThreshold, maxPersons, result);
|
||||
_splitComponent(graph, groupB, cutThreshold, maxPersons, result);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CSI frame parsing (from JSONL recording or UDP)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Parse IQ hex string into amplitude array */
|
||||
function parseIqHex(iqHex, nSubcarriers) {
|
||||
const bytes = Buffer.from(iqHex, 'hex');
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
|
||||
// IQ data: pairs of signed int8 (I, Q) for each subcarrier
|
||||
// First 2 bytes are header/padding, then I/Q pairs
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = 2 + sc * 2; // skip 2-byte header
|
||||
if (offset + 1 >= bytes.length) break;
|
||||
|
||||
// Read as signed int8
|
||||
let I = bytes[offset];
|
||||
let Q = bytes[offset + 1];
|
||||
if (I > 127) I -= 256;
|
||||
if (Q > 127) Q -= 256;
|
||||
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
return amplitudes;
|
||||
}
|
||||
|
||||
/** Parse binary UDP CSI packet (ADR-018 format) */
|
||||
function parseUdpPacket(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const nAntennas = buf.readUInt8(5) || 1;
|
||||
const nSubcarriers = buf.readUInt16LE(6);
|
||||
const freqMhz = buf.readUInt32LE(8);
|
||||
const rssi = buf.readInt8(16);
|
||||
|
||||
const iqLen = nSubcarriers * nAntennas * 2;
|
||||
if (buf.length < HEADER_SIZE + iqLen) return null;
|
||||
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = HEADER_SIZE + sc * 2;
|
||||
const I = buf.readInt8(offset);
|
||||
const Q = buf.readInt8(offset + 1);
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
return { nodeId, nSubcarriers, freqMhz, rssi, amplitudes, timestamp: Date.now() / 1000 };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Analysis engine
|
||||
// ---------------------------------------------------------------------------
|
||||
class PersonCounter {
|
||||
constructor(opts) {
|
||||
this.windowMs = opts.windowMs;
|
||||
this.corrThreshold = opts.corrThreshold;
|
||||
this.cutThreshold = opts.cutThreshold;
|
||||
this.maxPersons = opts.maxPersons;
|
||||
|
||||
// Per-node sliding windows
|
||||
this.windows = new Map(); // nodeId -> SubcarrierWindow
|
||||
|
||||
// Latest result
|
||||
this.lastResult = null;
|
||||
this.analysisCount = 0;
|
||||
}
|
||||
|
||||
ingestFrame(nodeId, timestamp, amplitudes) {
|
||||
if (!this.windows.has(nodeId)) {
|
||||
this.windows.set(nodeId, new SubcarrierWindow(this.windowMs));
|
||||
}
|
||||
this.windows.get(nodeId).push(timestamp * 1000, amplitudes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the min-cut analysis on accumulated data.
|
||||
* Merges subcarrier data from all nodes into a single correlation graph.
|
||||
*
|
||||
* @returns {{ personCount, groups, graphStats, perNode }}
|
||||
*/
|
||||
analyze() {
|
||||
this.analysisCount++;
|
||||
const perNode = {};
|
||||
const allGroups = [];
|
||||
let totalPersons = 0;
|
||||
|
||||
for (const [nodeId, window] of this.windows) {
|
||||
const corr = window.correlationMatrix();
|
||||
if (!corr || !corr.matrix || corr.n < 2) {
|
||||
perNode[nodeId] = { personCount: 0, activeSubcarriers: corr ? corr.n : 0, groups: [], edges: 0 };
|
||||
continue;
|
||||
}
|
||||
|
||||
// Build correlation graph
|
||||
const graph = WeightedGraph.fromCorrelation(corr.matrix, corr.n, this.corrThreshold);
|
||||
|
||||
// Separate persons via recursive min-cut
|
||||
const groups = separatePersons(graph, this.cutThreshold, this.maxPersons);
|
||||
|
||||
// Map group indices back to original subcarrier indices
|
||||
const mappedGroups = groups.map(g => g.map(i => corr.activeIndices[i]));
|
||||
|
||||
const nodeResult = {
|
||||
personCount: groups.length,
|
||||
activeSubcarriers: corr.n,
|
||||
totalSubcarriers: window.nSubcarriers,
|
||||
groups: mappedGroups,
|
||||
edges: graph.edgeCount,
|
||||
frames: window.length,
|
||||
};
|
||||
|
||||
perNode[nodeId] = nodeResult;
|
||||
totalPersons = Math.max(totalPersons, groups.length);
|
||||
allGroups.push(...mappedGroups);
|
||||
}
|
||||
|
||||
this.lastResult = {
|
||||
personCount: totalPersons,
|
||||
groups: allGroups,
|
||||
perNode,
|
||||
timestamp: Date.now() / 1000,
|
||||
analysisIndex: this.analysisCount,
|
||||
};
|
||||
|
||||
return this.lastResult;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ASCII output
|
||||
// ---------------------------------------------------------------------------
|
||||
function formatResult(result) {
|
||||
const lines = [];
|
||||
const ts = new Date(result.timestamp * 1000).toISOString().slice(11, 19);
|
||||
|
||||
lines.push(`\x1b[1m[${ts}] Persons: ${result.personCount}\x1b[0m (analysis #${result.analysisIndex})`);
|
||||
|
||||
for (const [nodeId, nodeResult] of Object.entries(result.perNode)) {
|
||||
const { personCount, activeSubcarriers, totalSubcarriers, groups, edges, frames } = nodeResult;
|
||||
lines.push(` Node ${nodeId}: ${personCount} person(s) | ${activeSubcarriers}/${totalSubcarriers} active subcarriers | ${edges} edges | ${frames} frames`);
|
||||
|
||||
for (let i = 0; i < groups.length; i++) {
|
||||
const g = groups[i];
|
||||
const scList = g.length <= 12 ? g.join(',') : g.slice(0, 10).join(',') + `...+${g.length - 10}`;
|
||||
lines.push(` Person ${i + 1}: subcarriers [${scList}] (${g.length} sc)`);
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function formatJson(result) {
|
||||
return JSON.stringify(result);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// UDP forwarding (override person count in feature vector)
|
||||
// ---------------------------------------------------------------------------
|
||||
let forwardSocket = null;
|
||||
function forwardWithCorrectedCount(buf, personCount) {
|
||||
if (!FORWARD_PORT || !forwardSocket) return;
|
||||
// If it's a vitals packet (magic 0xC5110002), override byte 13 (nPersons)
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic === 0xC5110002 && buf.length >= 14) {
|
||||
const copy = Buffer.from(buf);
|
||||
copy.writeUInt8(Math.min(personCount, 255), 13);
|
||||
forwardSocket.send(copy, FORWARD_PORT, '127.0.0.1');
|
||||
} else {
|
||||
// Forward as-is
|
||||
forwardSocket.send(buf, FORWARD_PORT, '127.0.0.1');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main: live UDP mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const counter = new PersonCounter({
|
||||
windowMs: WINDOW_MS,
|
||||
corrThreshold: CORR_THRESHOLD,
|
||||
cutThreshold: CUT_THRESHOLD,
|
||||
maxPersons: MAX_PERSONS,
|
||||
});
|
||||
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
if (FORWARD_PORT) {
|
||||
forwardSocket = dgram.createSocket('udp4');
|
||||
}
|
||||
|
||||
server.on('message', (buf, rinfo) => {
|
||||
const frame = parseUdpPacket(buf);
|
||||
if (frame) {
|
||||
counter.ingestFrame(frame.nodeId, frame.timestamp, frame.amplitudes);
|
||||
}
|
||||
|
||||
// Forward all packets with corrected person count
|
||||
if (counter.lastResult) {
|
||||
forwardWithCorrectedCount(buf, counter.lastResult.personCount);
|
||||
}
|
||||
});
|
||||
|
||||
// Periodic analysis
|
||||
setInterval(() => {
|
||||
const result = counter.analyze();
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(formatJson(result));
|
||||
} else {
|
||||
process.stdout.write('\x1b[2J\x1b[H'); // clear screen
|
||||
console.log('ADR-075 Min-Cut Person Counter (live UDP)');
|
||||
console.log('─'.repeat(60));
|
||||
console.log(formatResult(result));
|
||||
console.log('─'.repeat(60));
|
||||
console.log(`Thresholds: corr=${CORR_THRESHOLD} cut=${CUT_THRESHOLD} var-floor=${VAR_FLOOR}`);
|
||||
}
|
||||
}, INTERVAL_MS);
|
||||
|
||||
server.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Listening on UDP port ${PORT} (analysis every ${INTERVAL_MS}ms, window ${WINDOW_MS}ms)`);
|
||||
if (FORWARD_PORT) console.log(`Forwarding corrected packets to UDP port ${FORWARD_PORT}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main: replay mode (from .csi.jsonl recording)
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
const counter = new PersonCounter({
|
||||
windowMs: WINDOW_MS,
|
||||
corrThreshold: CORR_THRESHOLD,
|
||||
cutThreshold: CUT_THRESHOLD,
|
||||
maxPersons: MAX_PERSONS,
|
||||
});
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
let analysisResults = [];
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
|
||||
let record;
|
||||
try {
|
||||
record = JSON.parse(line);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (record.type !== 'raw_csi' || !record.iq_hex) continue;
|
||||
|
||||
const amplitudes = parseIqHex(record.iq_hex, record.subcarriers || 64);
|
||||
counter.ingestFrame(record.node_id, record.timestamp, amplitudes);
|
||||
frameCount++;
|
||||
|
||||
// Run analysis every INTERVAL_MS worth of frames
|
||||
const tsMs = record.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
const result = counter.analyze();
|
||||
analysisResults.push(result);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(formatJson(result));
|
||||
} else {
|
||||
console.log(formatResult(result));
|
||||
console.log();
|
||||
}
|
||||
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Final analysis
|
||||
const result = counter.analyze();
|
||||
analysisResults.push(result);
|
||||
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log('─'.repeat(60));
|
||||
console.log('FINAL ANALYSIS');
|
||||
console.log('─'.repeat(60));
|
||||
console.log(formatResult(result));
|
||||
console.log();
|
||||
console.log(`Processed ${frameCount} frames, ${analysisResults.length} analysis windows`);
|
||||
|
||||
// Summary statistics
|
||||
const counts = analysisResults.map(r => r.personCount);
|
||||
const avg = counts.reduce((a, b) => a + b, 0) / counts.length;
|
||||
const max = Math.max(...counts);
|
||||
const min = Math.min(...counts);
|
||||
console.log(`Person count: min=${min} max=${max} avg=${avg.toFixed(1)}`);
|
||||
console.log(`Thresholds: corr=${CORR_THRESHOLD} cut=${CUT_THRESHOLD} var-floor=${VAR_FLOOR}`);
|
||||
} else {
|
||||
console.log(formatJson(result));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry point
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,677 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Passive Bistatic Radar — Multi-Frequency Mesh Application
|
||||
*
|
||||
* Uses neighbor WiFi APs as illuminators of opportunity to build range-Doppler
|
||||
* maps for moving target detection. Each neighbor AP is an uncontrolled
|
||||
* transmitter whose signals pass through the room and are modulated by people
|
||||
* and objects. The ESP32 nodes capture CSI from these transmissions across
|
||||
* 6 channels.
|
||||
*
|
||||
* This is the same principle used by military passive radar (Kolchuga, VERA-NG)
|
||||
* but with WiFi APs instead of broadcast towers.
|
||||
*
|
||||
* Requires multi-frequency mesh scanning (ADR-073): 2 ESP32 nodes hopping
|
||||
* across channels 1, 3, 5, 6, 9, 11.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/passive-radar.js
|
||||
* node scripts/passive-radar.js --port 5006 --duration 60
|
||||
* node scripts/passive-radar.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/passive-radar.js --node-distance 3.0
|
||||
*
|
||||
* ADR: docs/adr/ADR-078-multifreq-mesh-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
duration: { type: 'string', short: 'd' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
interval: { type: 'string', short: 'i', default: '3000' },
|
||||
json: { type: 'boolean', default: false },
|
||||
'node-distance': { type: 'string', default: '3.0' },
|
||||
'doppler-bins': { type: 'string', default: '16' },
|
||||
'range-bins': { type: 'string', default: '12' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const DURATION_MS = args.duration ? parseInt(args.duration, 10) * 1000 : null;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const NODE_DISTANCE = parseFloat(args['node-distance']);
|
||||
const DOPPLER_BINS = parseInt(args['doppler-bins'], 10);
|
||||
const RANGE_BINS = parseInt(args['range-bins'], 10);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
const SPEED_OF_LIGHT = 3e8; // m/s
|
||||
|
||||
const CHANNEL_FREQ = {};
|
||||
for (let ch = 1; ch <= 13; ch++) CHANNEL_FREQ[ch] = 2412 + (ch - 1) * 5;
|
||||
|
||||
const NODE1_CHANNELS = [1, 6, 11];
|
||||
const NODE2_CHANNELS = [3, 5, 9];
|
||||
|
||||
// Neighbor APs as illuminators with estimated positions
|
||||
const ILLUMINATORS = [
|
||||
{ ssid: 'ruv.net', channel: 5, signal: 100, pos: [1.5, 3.5], freq: 2432e6 },
|
||||
{ ssid: 'Cohen-Guest', channel: 5, signal: 100, pos: [2.0, 3.8], freq: 2432e6 },
|
||||
{ ssid: 'COGECO-21B20', channel: 11, signal: 100, pos: [4.0, 2.0], freq: 2462e6 },
|
||||
{ ssid: 'HP M255', channel: 5, signal: 94, pos: [0.5, 1.5], freq: 2432e6 },
|
||||
{ ssid: 'conclusion', channel: 3, signal: 44, pos: [3.5, 3.0], freq: 2422e6 },
|
||||
{ ssid: 'NETGEAR72', channel: 9, signal: 42, pos: [4.5, 1.0], freq: 2452e6 },
|
||||
{ ssid: 'COGECO-4321', channel: 11, signal: 30, pos: [4.0, 3.5], freq: 2462e6 },
|
||||
{ ssid: 'Innanen', channel: 6, signal: 19, pos: [1.0, 4.0], freq: 2437e6 },
|
||||
];
|
||||
|
||||
const NODE_POS = {
|
||||
1: [0, 2.0],
|
||||
2: [NODE_DISTANCE, 2.0],
|
||||
};
|
||||
|
||||
// Range-Doppler plot characters
|
||||
const RD_CHARS = [' ', '\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588'];
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per-illuminator CSI history for Doppler processing
|
||||
// ---------------------------------------------------------------------------
|
||||
class IlluminatorTracker {
|
||||
constructor(illuminator, nodeId) {
|
||||
this.illuminator = illuminator;
|
||||
this.nodeId = nodeId;
|
||||
this.ssid = illuminator.ssid;
|
||||
this.channel = illuminator.channel;
|
||||
this.freqHz = illuminator.freq;
|
||||
this.wavelength = SPEED_OF_LIGHT / this.freqHz;
|
||||
|
||||
// Phase history per subcarrier (ring buffer)
|
||||
this.maxHistory = 64;
|
||||
this.phaseHistory = []; // array of { timestamp, phases: Float64Array }
|
||||
this.amplitudeHistory = [];
|
||||
|
||||
// Range-Doppler map
|
||||
this.rangeDoppler = null;
|
||||
this.lastUpdateMs = 0;
|
||||
}
|
||||
|
||||
/** Ingest a new CSI frame */
|
||||
ingest(timestamp, amplitudes, phases) {
|
||||
this.phaseHistory.push({ timestamp, phases: new Float64Array(phases) });
|
||||
this.amplitudeHistory.push({ timestamp, amplitudes: new Float64Array(amplitudes) });
|
||||
|
||||
if (this.phaseHistory.length > this.maxHistory) {
|
||||
this.phaseHistory.shift();
|
||||
this.amplitudeHistory.shift();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute range-Doppler map from CSI phase history.
|
||||
*
|
||||
* Doppler: phase change rate across consecutive frames for each subcarrier.
|
||||
* fd = d(phase)/dt / (2*pi) -> velocity = fd * wavelength / 2
|
||||
*
|
||||
* Range: phase slope across subcarriers within each frame.
|
||||
* tau = d(phase)/d(subcarrier_freq) / (2*pi) -> range = c * tau
|
||||
*/
|
||||
computeRangeDoppler(dopplerBins, rangeBins) {
|
||||
const n = this.phaseHistory.length;
|
||||
if (n < 4) return null;
|
||||
|
||||
const nSub = this.phaseHistory[0].phases.length;
|
||||
if (nSub < 4) return null;
|
||||
|
||||
// Initialize range-Doppler map
|
||||
const rd = new Float64Array(rangeBins * dopplerBins);
|
||||
|
||||
// Doppler processing: compute phase change rate per subcarrier
|
||||
const dopplerPerSub = new Float64Array(nSub);
|
||||
const rangePerFrame = new Float64Array(n);
|
||||
|
||||
for (let sc = 0; sc < nSub; sc++) {
|
||||
// Linear regression of phase vs time for this subcarrier
|
||||
let sumT = 0, sumP = 0, sumTT = 0, sumTP = 0;
|
||||
let prevPhase = this.phaseHistory[0].phases[sc];
|
||||
|
||||
for (let f = 0; f < n; f++) {
|
||||
const t = this.phaseHistory[f].timestamp;
|
||||
// Unwrap phase
|
||||
let phase = this.phaseHistory[f].phases[sc];
|
||||
while (phase - prevPhase > Math.PI) phase -= 2 * Math.PI;
|
||||
while (phase - prevPhase < -Math.PI) phase += 2 * Math.PI;
|
||||
prevPhase = phase;
|
||||
|
||||
sumT += t;
|
||||
sumP += phase;
|
||||
sumTT += t * t;
|
||||
sumTP += t * phase;
|
||||
}
|
||||
|
||||
const meanT = sumT / n;
|
||||
const denom = sumTT - sumT * meanT;
|
||||
if (Math.abs(denom) > 1e-10) {
|
||||
const slope = (sumTP - sumT * (sumP / n)) / denom;
|
||||
// Doppler frequency (Hz) = slope / (2*pi)
|
||||
dopplerPerSub[sc] = slope / (2 * Math.PI);
|
||||
}
|
||||
}
|
||||
|
||||
// Range processing: phase slope across subcarriers per frame
|
||||
const subcarrierSpacing = 312.5e3; // OFDM subcarrier spacing: 312.5 kHz
|
||||
|
||||
for (let f = 0; f < n; f++) {
|
||||
const phases = this.phaseHistory[f].phases;
|
||||
// Linear regression of phase vs subcarrier index
|
||||
let sumI = 0, sumP = 0, sumII = 0, sumIP = 0;
|
||||
let prevPhase = phases[0];
|
||||
|
||||
for (let sc = 0; sc < nSub; sc++) {
|
||||
let phase = phases[sc];
|
||||
// Unwrap
|
||||
while (phase - prevPhase > Math.PI) phase -= 2 * Math.PI;
|
||||
while (phase - prevPhase < -Math.PI) phase += 2 * Math.PI;
|
||||
prevPhase = phase;
|
||||
|
||||
sumI += sc;
|
||||
sumP += phase;
|
||||
sumII += sc * sc;
|
||||
sumIP += sc * phase;
|
||||
}
|
||||
|
||||
const meanI = sumI / nSub;
|
||||
const denom = sumII - sumI * meanI;
|
||||
if (Math.abs(denom) > 1e-10) {
|
||||
const slope = (sumIP - sumI * (sumP / nSub)) / denom;
|
||||
// Time delay (seconds) = slope / (2*pi * subcarrier_spacing)
|
||||
const tau = Math.abs(slope) / (2 * Math.PI * subcarrierSpacing);
|
||||
rangePerFrame[f] = SPEED_OF_LIGHT * tau / 2; // bistatic range / 2
|
||||
}
|
||||
}
|
||||
|
||||
// Map to bins
|
||||
const maxDoppler = 5.0; // Hz (corresponds to ~0.3 m/s at 2.4 GHz)
|
||||
const maxRange = 10.0; // meters
|
||||
|
||||
for (let sc = 0; sc < nSub; sc++) {
|
||||
const doppler = dopplerPerSub[sc];
|
||||
const dBin = Math.floor(((doppler + maxDoppler) / (2 * maxDoppler)) * (dopplerBins - 1));
|
||||
if (dBin < 0 || dBin >= dopplerBins) continue;
|
||||
|
||||
// Use mean amplitude as intensity
|
||||
let meanAmp = 0;
|
||||
for (let f = 0; f < n; f++) {
|
||||
meanAmp += this.amplitudeHistory[f].amplitudes[sc];
|
||||
}
|
||||
meanAmp /= n;
|
||||
|
||||
// Average range across frames for this subcarrier's range bin
|
||||
let meanRange = 0;
|
||||
for (let f = 0; f < n; f++) meanRange += rangePerFrame[f];
|
||||
meanRange /= n;
|
||||
|
||||
const rBin = Math.floor((meanRange / maxRange) * (rangeBins - 1));
|
||||
if (rBin < 0 || rBin >= rangeBins) continue;
|
||||
|
||||
rd[rBin * dopplerBins + dBin] += meanAmp;
|
||||
}
|
||||
|
||||
this.rangeDoppler = {
|
||||
map: rd,
|
||||
dopplerBins,
|
||||
rangeBins,
|
||||
maxDoppler,
|
||||
maxRange,
|
||||
nFrames: n,
|
||||
};
|
||||
|
||||
return this.rangeDoppler;
|
||||
}
|
||||
|
||||
/** Get dominant Doppler (strongest moving target) */
|
||||
getDominantDoppler() {
|
||||
if (!this.rangeDoppler) return null;
|
||||
const { map, dopplerBins, rangeBins, maxDoppler } = this.rangeDoppler;
|
||||
|
||||
let maxVal = 0, maxD = 0, maxR = 0;
|
||||
for (let r = 0; r < rangeBins; r++) {
|
||||
for (let d = 0; d < dopplerBins; d++) {
|
||||
const val = map[r * dopplerBins + d];
|
||||
if (val > maxVal) {
|
||||
maxVal = val;
|
||||
maxD = d;
|
||||
maxR = r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (maxVal < 0.01) return null;
|
||||
|
||||
const doppler = (maxD / (dopplerBins - 1)) * 2 * maxDoppler - maxDoppler;
|
||||
const velocity = doppler * this.wavelength / 2;
|
||||
const range = (maxR / (rangeBins - 1)) * this.rangeDoppler.maxRange;
|
||||
|
||||
return { doppler: doppler.toFixed(2), velocity: velocity.toFixed(3), range: range.toFixed(1), intensity: maxVal.toFixed(1) };
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Multi-static fusion
|
||||
// ---------------------------------------------------------------------------
|
||||
class MultiStaticFusion {
|
||||
constructor() {
|
||||
this.trackers = new Map(); // key: `${ssid}-node${nodeId}` -> IlluminatorTracker
|
||||
}
|
||||
|
||||
getOrCreateTracker(illuminator, nodeId) {
|
||||
const key = `${illuminator.ssid}-node${nodeId}`;
|
||||
if (!this.trackers.has(key)) {
|
||||
this.trackers.set(key, new IlluminatorTracker(illuminator, nodeId));
|
||||
}
|
||||
return this.trackers.get(key);
|
||||
}
|
||||
|
||||
ingestFrame(nodeId, channel, timestamp, amplitudes, phases) {
|
||||
// Find illuminators on this channel
|
||||
for (const il of ILLUMINATORS) {
|
||||
if (il.channel === channel) {
|
||||
const tracker = this.getOrCreateTracker(il, nodeId);
|
||||
tracker.ingest(timestamp, amplitudes, phases);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Compute all range-Doppler maps */
|
||||
computeAll(dopplerBins, rangeBins) {
|
||||
const results = [];
|
||||
for (const [key, tracker] of this.trackers) {
|
||||
const rd = tracker.computeRangeDoppler(dopplerBins, rangeBins);
|
||||
if (rd) {
|
||||
results.push({ key, tracker, rd });
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fuse multi-static detections.
|
||||
* Each illuminator provides a range measurement to the target.
|
||||
* The target lies on an ellipse with foci at TX (illuminator) and RX (ESP32 node).
|
||||
* Intersection of multiple ellipses gives position.
|
||||
*/
|
||||
fuseDetections() {
|
||||
const detections = [];
|
||||
for (const [key, tracker] of this.trackers) {
|
||||
const dom = tracker.getDominantDoppler();
|
||||
if (dom && parseFloat(dom.intensity) > 1.0) {
|
||||
detections.push({
|
||||
key,
|
||||
ssid: tracker.ssid,
|
||||
channel: tracker.channel,
|
||||
nodeId: tracker.nodeId,
|
||||
txPos: tracker.illuminator.pos,
|
||||
rxPos: NODE_POS[tracker.nodeId],
|
||||
bistaticRange: parseFloat(dom.range),
|
||||
velocity: parseFloat(dom.velocity),
|
||||
intensity: parseFloat(dom.intensity),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (detections.length < 2) {
|
||||
return { detections, fusedPosition: null };
|
||||
}
|
||||
|
||||
// Simple centroid-based fusion:
|
||||
// For each detection, compute the midpoint of the TX-RX baseline
|
||||
// weighted by intensity. This is a rough approximation.
|
||||
// (Full ellipse intersection requires nonlinear optimization.)
|
||||
let sumX = 0, sumY = 0, sumW = 0;
|
||||
for (const det of detections) {
|
||||
// Midpoint between TX and RX, offset by bistatic range
|
||||
const mx = (det.txPos[0] + det.rxPos[0]) / 2;
|
||||
const my = (det.txPos[1] + det.rxPos[1]) / 2;
|
||||
const w = det.intensity;
|
||||
sumX += mx * w;
|
||||
sumY += my * w;
|
||||
sumW += w;
|
||||
}
|
||||
|
||||
const fusedPosition = sumW > 0
|
||||
? { x: (sumX / sumW).toFixed(2), y: (sumY / sumW).toFixed(2), confidence: Math.min(1, detections.length / 4).toFixed(2) }
|
||||
: null;
|
||||
|
||||
return { detections, fusedPosition };
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CSI parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseIqHex(iqHex, nSubcarriers) {
|
||||
const bytes = Buffer.from(iqHex, 'hex');
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
const phases = new Float64Array(nSubcarriers);
|
||||
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = 2 + sc * 2;
|
||||
if (offset + 1 >= bytes.length) break;
|
||||
let I = bytes[offset];
|
||||
let Q = bytes[offset + 1];
|
||||
if (I > 127) I -= 256;
|
||||
if (Q > 127) Q -= 256;
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
phases[sc] = Math.atan2(Q, I);
|
||||
}
|
||||
|
||||
return { amplitudes, phases };
|
||||
}
|
||||
|
||||
function parseCSIFrame(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const nSubcarriers = buf.readUInt16LE(6);
|
||||
const freqMhz = buf.readUInt32LE(8);
|
||||
const rssi = buf.readInt8(16);
|
||||
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
const phases = new Float64Array(nSubcarriers);
|
||||
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = HEADER_SIZE + sc * 2;
|
||||
if (offset + 1 >= buf.length) break;
|
||||
const I = buf.readInt8(offset);
|
||||
const Q = buf.readInt8(offset + 1);
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
phases[sc] = Math.atan2(Q, I);
|
||||
}
|
||||
|
||||
let channel = 0;
|
||||
if (freqMhz >= 2412 && freqMhz <= 2484) {
|
||||
channel = freqMhz === 2484 ? 14 : Math.round((freqMhz - 2412) / 5) + 1;
|
||||
}
|
||||
|
||||
return { nodeId, nSubcarriers, freqMhz, rssi, amplitudes, phases, channel };
|
||||
}
|
||||
|
||||
// Channel assignment for legacy JSONL
|
||||
const nodeChannelIdx = { 1: 0, 2: 0 };
|
||||
function assignChannel(nodeId) {
|
||||
const channels = nodeId === 1 ? NODE1_CHANNELS : NODE2_CHANNELS;
|
||||
const ch = channels[nodeChannelIdx[nodeId] % channels.length];
|
||||
nodeChannelIdx[nodeId]++;
|
||||
return ch;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Visualization
|
||||
// ---------------------------------------------------------------------------
|
||||
function renderRangeDoppler(tracker) {
|
||||
const rd = tracker.rangeDoppler;
|
||||
if (!rd) return ` ${tracker.ssid} (ch${tracker.channel}): insufficient data`;
|
||||
|
||||
const { map, dopplerBins, rangeBins, maxDoppler, maxRange, nFrames } = rd;
|
||||
const lines = [];
|
||||
|
||||
lines.push(` ${tracker.ssid} (ch${tracker.channel}, node${tracker.nodeId}) | ${nFrames} frames`);
|
||||
|
||||
// Find max for normalization
|
||||
let maxVal = 0;
|
||||
for (let i = 0; i < map.length; i++) {
|
||||
if (map[i] > maxVal) maxVal = map[i];
|
||||
}
|
||||
if (maxVal === 0) maxVal = 1;
|
||||
|
||||
// Render range (y-axis) vs Doppler (x-axis)
|
||||
for (let r = rangeBins - 1; r >= 0; r--) {
|
||||
const range = (r / (rangeBins - 1)) * maxRange;
|
||||
let row = ` ${range.toFixed(1).padStart(5)}m |`;
|
||||
for (let d = 0; d < dopplerBins; d++) {
|
||||
const val = map[r * dopplerBins + d] / maxVal;
|
||||
const level = Math.floor(val * 8.99);
|
||||
row += RD_CHARS[Math.max(0, Math.min(8, level))];
|
||||
}
|
||||
row += '|';
|
||||
lines.push(row);
|
||||
}
|
||||
|
||||
// X-axis (Doppler)
|
||||
lines.push(' ' + ' '.repeat(7) + '+' + '-'.repeat(dopplerBins) + '+');
|
||||
const dLabel = ` ${' '.repeat(7)}-${maxDoppler}Hz${' '.repeat(Math.max(0, dopplerBins - 10))}+${maxDoppler}Hz`;
|
||||
lines.push(dLabel);
|
||||
|
||||
// Dominant detection
|
||||
const dom = tracker.getDominantDoppler();
|
||||
if (dom) {
|
||||
lines.push(` Peak: range=${dom.range}m doppler=${dom.doppler}Hz vel=${dom.velocity}m/s`);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function renderFusion(fusion) {
|
||||
const { detections, fusedPosition } = fusion;
|
||||
const lines = [];
|
||||
|
||||
lines.push('');
|
||||
lines.push(' MULTI-STATIC FUSION');
|
||||
lines.push(' ' + '='.repeat(50));
|
||||
|
||||
if (detections.length === 0) {
|
||||
lines.push(' No detections above threshold');
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
lines.push(` Active bistatic pairs: ${detections.length}`);
|
||||
for (const det of detections) {
|
||||
lines.push(` ${det.ssid.padEnd(16)} ch${det.channel} -> node${det.nodeId} | ` +
|
||||
`range=${det.bistaticRange.toFixed(1)}m vel=${det.velocity.toFixed(3)}m/s`);
|
||||
}
|
||||
|
||||
if (fusedPosition) {
|
||||
lines.push(` Fused position: (${fusedPosition.x}, ${fusedPosition.y}) m confidence=${fusedPosition.confidence}`);
|
||||
} else {
|
||||
lines.push(' Insufficient detections for position fusion (need 2+)');
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Global state
|
||||
// ---------------------------------------------------------------------------
|
||||
const multiStatic = new MultiStaticFusion();
|
||||
let lastDisplayMs = 0;
|
||||
|
||||
function processFrame(nodeId, channel, timestamp, amplitudes, phases) {
|
||||
multiStatic.ingestFrame(nodeId, channel, timestamp, amplitudes, phases);
|
||||
}
|
||||
|
||||
function displayUpdate() {
|
||||
const results = multiStatic.computeAll(DOPPLER_BINS, RANGE_BINS);
|
||||
const fusion = multiStatic.fuseDetections();
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
const output = {
|
||||
timestamp: Date.now() / 1000,
|
||||
bistaticPairs: results.length,
|
||||
detections: fusion.detections.map(d => ({
|
||||
ssid: d.ssid, channel: d.channel, nodeId: d.nodeId,
|
||||
bistaticRange: d.bistaticRange, velocity: d.velocity,
|
||||
})),
|
||||
fusedPosition: fusion.fusedPosition,
|
||||
};
|
||||
console.log(JSON.stringify(output));
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
console.log(' PASSIVE BISTATIC RADAR');
|
||||
console.log(' Using neighbor WiFi APs as illuminators of opportunity');
|
||||
console.log(' ' + '-'.repeat(55));
|
||||
console.log('');
|
||||
|
||||
// Show top 3 trackers by signal strength
|
||||
const sorted = results.sort((a, b) => b.tracker.illuminator.signal - a.tracker.illuminator.signal);
|
||||
for (const r of sorted.slice(0, 3)) {
|
||||
console.log(renderRangeDoppler(r.tracker));
|
||||
console.log('');
|
||||
}
|
||||
|
||||
console.log(renderFusion(fusion));
|
||||
console.log('');
|
||||
console.log(` Total bistatic pairs: ${multiStatic.trackers.size}`);
|
||||
console.log(' Press Ctrl+C to exit');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const sock = dgram.createSocket('udp4');
|
||||
|
||||
sock.on('message', (buf, rinfo) => {
|
||||
if (buf.length < 4) return;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return;
|
||||
|
||||
const frame = parseCSIFrame(buf);
|
||||
if (!frame) return;
|
||||
|
||||
processFrame(frame.nodeId, frame.channel, Date.now() / 1000, frame.amplitudes, frame.phases);
|
||||
|
||||
const now = Date.now();
|
||||
if (now - lastDisplayMs >= INTERVAL_MS) {
|
||||
displayUpdate();
|
||||
lastDisplayMs = now;
|
||||
}
|
||||
});
|
||||
|
||||
sock.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Passive Bistatic Radar listening on UDP port ${PORT}`);
|
||||
console.log(`Illuminators: ${ILLUMINATORS.length} neighbor APs`);
|
||||
console.log(`Node distance: ${NODE_DISTANCE} m`);
|
||||
console.log('Waiting for CSI frames...');
|
||||
}
|
||||
});
|
||||
|
||||
if (DURATION_MS) {
|
||||
setTimeout(() => {
|
||||
displayUpdate();
|
||||
sock.close();
|
||||
process.exit(0);
|
||||
}, DURATION_MS);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
let windowCount = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
if (record.type !== 'raw_csi' || !record.iq_hex) continue;
|
||||
|
||||
const { amplitudes, phases } = parseIqHex(record.iq_hex, record.subcarriers || 64);
|
||||
const channel = record.channel || assignChannel(record.node_id);
|
||||
|
||||
processFrame(record.node_id, channel, record.timestamp, amplitudes, phases);
|
||||
frameCount++;
|
||||
|
||||
const tsMs = record.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
windowCount++;
|
||||
const results = multiStatic.computeAll(DOPPLER_BINS, RANGE_BINS);
|
||||
const fusion = multiStatic.fuseDetections();
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
window: windowCount,
|
||||
timestamp: record.timestamp,
|
||||
frames: frameCount,
|
||||
detections: fusion.detections.length,
|
||||
fusedPosition: fusion.fusedPosition,
|
||||
}));
|
||||
} else {
|
||||
console.log(`\n${'='.repeat(60)}`);
|
||||
console.log(`Window ${windowCount} | t=${record.timestamp.toFixed(1)}s | frames=${frameCount}`);
|
||||
console.log('='.repeat(60));
|
||||
|
||||
const sorted = results.sort((a, b) => b.tracker.illuminator.signal - a.tracker.illuminator.signal);
|
||||
for (const r of sorted.slice(0, 3)) {
|
||||
console.log(renderRangeDoppler(r.tracker));
|
||||
console.log('');
|
||||
}
|
||||
|
||||
console.log(renderFusion(fusion));
|
||||
}
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Final
|
||||
if (!JSON_OUTPUT) {
|
||||
const results = multiStatic.computeAll(DOPPLER_BINS, RANGE_BINS);
|
||||
const fusion = multiStatic.fuseDetections();
|
||||
|
||||
console.log(`\n${'='.repeat(60)}`);
|
||||
console.log('FINAL PASSIVE RADAR SUMMARY');
|
||||
console.log('='.repeat(60));
|
||||
|
||||
for (const [key, tracker] of multiStatic.trackers) {
|
||||
const dom = tracker.getDominantDoppler();
|
||||
const domStr = dom ? `range=${dom.range}m vel=${dom.velocity}m/s` : 'no detection';
|
||||
console.log(` ${key.padEnd(30)} ${domStr}`);
|
||||
}
|
||||
|
||||
console.log(renderFusion(fusion));
|
||||
console.log(`\nProcessed ${frameCount} frames in ${windowCount} windows`);
|
||||
console.log(`Bistatic pairs tracked: ${multiStatic.trackers.size}`);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry point
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
+4
-1
@@ -25,6 +25,7 @@ const { parseArgs } = require('util');
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
bind: { type: 'string', short: 'b', default: '0.0.0.0' },
|
||||
duration: { type: 'string', short: 'd' },
|
||||
json: { type: 'boolean', default: false },
|
||||
interval: { type: 'string', short: 'i', default: '2000' },
|
||||
@@ -573,7 +574,9 @@ function main() {
|
||||
}
|
||||
});
|
||||
|
||||
server.bind(PORT);
|
||||
// On Windows, binding to 0.0.0.0 may be blocked by firewall.
|
||||
// Use --bind <ip> to specify your WiFi IP (e.g., --bind 192.168.1.20)
|
||||
server.bind(PORT, args.bind);
|
||||
|
||||
// Periodic display update
|
||||
const displayTimer = setInterval(display, INTERVAL_MS);
|
||||
|
||||
@@ -0,0 +1,581 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* RF Tomographic Imaging — Multi-Frequency Mesh Application
|
||||
*
|
||||
* Back-projects CSI attenuation along each TX->RX path across 6 WiFi channels
|
||||
* to build a 2D heatmap of RF absorption in the room. Areas with high absorption
|
||||
* correspond to people, furniture, or walls.
|
||||
*
|
||||
* Requires multi-frequency mesh scanning (ADR-073): 2 ESP32 nodes hopping
|
||||
* across channels 1, 3, 5, 6, 9, 11.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/rf-tomography.js
|
||||
* node scripts/rf-tomography.js --port 5006 --duration 60
|
||||
* node scripts/rf-tomography.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/rf-tomography.js --grid 15 --node-distance 4.0
|
||||
*
|
||||
* ADR: docs/adr/ADR-078-multifreq-mesh-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
duration: { type: 'string', short: 'd' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
interval: { type: 'string', short: 'i', default: '2000' },
|
||||
grid: { type: 'string', short: 'g', default: '10' },
|
||||
json: { type: 'boolean', default: false },
|
||||
'node-distance': { type: 'string', default: '3.0' },
|
||||
'room-width': { type: 'string', default: '5.0' },
|
||||
'room-height': { type: 'string', default: '4.0' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const DURATION_MS = args.duration ? parseInt(args.duration, 10) * 1000 : null;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const GRID_SIZE = parseInt(args.grid, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const NODE_DISTANCE = parseFloat(args['node-distance']);
|
||||
const ROOM_WIDTH = parseFloat(args['room-width']);
|
||||
const ROOM_HEIGHT = parseFloat(args['room-height']);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
const CHANNEL_FREQ = {};
|
||||
for (let ch = 1; ch <= 13; ch++) CHANNEL_FREQ[ch] = 2412 + (ch - 1) * 5;
|
||||
CHANNEL_FREQ[14] = 2484;
|
||||
|
||||
const NODE1_CHANNELS = [1, 6, 11];
|
||||
const NODE2_CHANNELS = [3, 5, 9];
|
||||
|
||||
// Known neighbor APs as additional illuminators (TX positions estimated)
|
||||
const ILLUMINATORS = [
|
||||
{ ssid: 'ruv.net', channel: 5, signal: 100, pos: [1.5, 3.5] },
|
||||
{ ssid: 'Cohen-Guest', channel: 5, signal: 100, pos: [2.0, 3.8] },
|
||||
{ ssid: 'COGECO-21B20', channel: 11, signal: 100, pos: [4.0, 2.0] },
|
||||
{ ssid: 'HP M255', channel: 5, signal: 94, pos: [0.5, 1.5] },
|
||||
{ ssid: 'conclusion', channel: 3, signal: 44, pos: [3.5, 3.0] },
|
||||
{ ssid: 'NETGEAR72', channel: 9, signal: 42, pos: [4.5, 1.0] },
|
||||
{ ssid: 'COGECO-4321', channel: 11, signal: 30, pos: [4.0, 3.5] },
|
||||
{ ssid: 'Innanen', channel: 6, signal: 19, pos: [1.0, 4.0] },
|
||||
];
|
||||
|
||||
// Node positions (meters)
|
||||
const NODE_POS = {
|
||||
1: [0, ROOM_HEIGHT / 2],
|
||||
2: [NODE_DISTANCE, ROOM_HEIGHT / 2],
|
||||
};
|
||||
|
||||
// Heatmap characters (8 levels: transparent -> opaque)
|
||||
const HEAT = [' ', '\u2591', '\u2591', '\u2592', '\u2592', '\u2593', '\u2593', '\u2588'];
|
||||
const HEAT_LABELS = ['air', 'low', 'low', 'med', 'med', 'high', 'high', 'solid'];
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tomographic grid
|
||||
// ---------------------------------------------------------------------------
|
||||
class TomographyGrid {
|
||||
constructor(gridSize, roomWidth, roomHeight) {
|
||||
this.gridSize = gridSize;
|
||||
this.roomWidth = roomWidth;
|
||||
this.roomHeight = roomHeight;
|
||||
this.cellWidth = roomWidth / gridSize;
|
||||
this.cellHeight = roomHeight / gridSize;
|
||||
|
||||
// Accumulated attenuation per cell
|
||||
this.attenuation = new Float64Array(gridSize * gridSize);
|
||||
// Number of paths passing through each cell (for normalization)
|
||||
this.pathCount = new Float64Array(gridSize * gridSize);
|
||||
// Per-channel attenuation (for frequency analysis)
|
||||
this.channelAttenuation = new Map(); // channel -> Float64Array
|
||||
|
||||
this.frameCount = 0;
|
||||
this.channelFrames = new Map();
|
||||
}
|
||||
|
||||
/** Get center position of grid cell (row, col) in meters */
|
||||
cellCenter(row, col) {
|
||||
return [
|
||||
(col + 0.5) * this.cellWidth,
|
||||
(row + 0.5) * this.cellHeight,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Perpendicular distance from point P to line segment AB.
|
||||
* Returns minimum distance to the infinite line through A and B.
|
||||
*/
|
||||
pointToLineDistance(px, py, ax, ay, bx, by) {
|
||||
const dx = bx - ax;
|
||||
const dy = by - ay;
|
||||
const len = Math.sqrt(dx * dx + dy * dy);
|
||||
if (len < 1e-6) return Math.sqrt((px - ax) ** 2 + (py - ay) ** 2);
|
||||
// Signed distance using cross product
|
||||
return Math.abs((dy * px - dx * py + bx * ay - by * ax)) / len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Back-project attenuation along a TX->RX path.
|
||||
* Each cell near the path receives a weighted contribution.
|
||||
*
|
||||
* @param {number[]} txPos - Transmitter position [x, y]
|
||||
* @param {number[]} rxPos - Receiver position [x, y]
|
||||
* @param {number} atten - Measured attenuation (dB or normalized)
|
||||
* @param {number} channel - WiFi channel number
|
||||
*/
|
||||
backProject(txPos, rxPos, atten, channel) {
|
||||
const [ax, ay] = txPos;
|
||||
const [bx, by] = rxPos;
|
||||
const pathLen = Math.sqrt((bx - ax) ** 2 + (by - ay) ** 2);
|
||||
if (pathLen < 0.01) return;
|
||||
|
||||
// Kernel width: how far from the path the contribution extends
|
||||
// Approximately lambda/2 at 2.4 GHz = ~6 cm, but we use wider for stability
|
||||
const kernelWidth = Math.max(this.cellWidth, this.cellHeight) * 1.5;
|
||||
|
||||
if (!this.channelAttenuation.has(channel)) {
|
||||
this.channelAttenuation.set(channel, new Float64Array(this.gridSize * this.gridSize));
|
||||
}
|
||||
const chAtten = this.channelAttenuation.get(channel);
|
||||
|
||||
for (let r = 0; r < this.gridSize; r++) {
|
||||
for (let c = 0; c < this.gridSize; c++) {
|
||||
const [cx, cy] = this.cellCenter(r, c);
|
||||
const dist = this.pointToLineDistance(cx, cy, ax, ay, bx, by);
|
||||
|
||||
if (dist < kernelWidth) {
|
||||
// Weight by proximity to path (Gaussian-like)
|
||||
const weight = Math.exp(-0.5 * (dist / (kernelWidth * 0.4)) ** 2);
|
||||
const idx = r * this.gridSize + c;
|
||||
this.attenuation[idx] += atten * weight;
|
||||
this.pathCount[idx] += weight;
|
||||
chAtten[idx] += atten * weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.frameCount++;
|
||||
this.channelFrames.set(channel, (this.channelFrames.get(channel) || 0) + 1);
|
||||
}
|
||||
|
||||
/** Get normalized attenuation image */
|
||||
getImage() {
|
||||
const img = new Float64Array(this.gridSize * this.gridSize);
|
||||
let maxVal = 0;
|
||||
|
||||
for (let i = 0; i < img.length; i++) {
|
||||
img[i] = this.pathCount[i] > 0 ? this.attenuation[i] / this.pathCount[i] : 0;
|
||||
if (img[i] > maxVal) maxVal = img[i];
|
||||
}
|
||||
|
||||
// Normalize to 0-1
|
||||
if (maxVal > 0) {
|
||||
for (let i = 0; i < img.length; i++) img[i] /= maxVal;
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
/** Get per-channel images for frequency analysis */
|
||||
getChannelImages() {
|
||||
const images = {};
|
||||
for (const [ch, chAtten] of this.channelAttenuation) {
|
||||
const img = new Float64Array(this.gridSize * this.gridSize);
|
||||
let maxVal = 0;
|
||||
for (let i = 0; i < img.length; i++) {
|
||||
img[i] = this.pathCount[i] > 0 ? chAtten[i] / this.pathCount[i] : 0;
|
||||
if (img[i] > maxVal) maxVal = img[i];
|
||||
}
|
||||
if (maxVal > 0) for (let i = 0; i < img.length; i++) img[i] /= maxVal;
|
||||
images[ch] = img;
|
||||
}
|
||||
return images;
|
||||
}
|
||||
|
||||
/** Detect high-attenuation regions (potential person locations) */
|
||||
detectObjects(threshold = 0.6) {
|
||||
const img = this.getImage();
|
||||
const objects = [];
|
||||
|
||||
for (let r = 0; r < this.gridSize; r++) {
|
||||
for (let c = 0; c < this.gridSize; c++) {
|
||||
const val = img[r * this.gridSize + c];
|
||||
if (val >= threshold) {
|
||||
const [x, y] = this.cellCenter(r, c);
|
||||
objects.push({
|
||||
row: r, col: c,
|
||||
x: x.toFixed(2), y: y.toFixed(2),
|
||||
attenuation: val.toFixed(3),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return objects;
|
||||
}
|
||||
|
||||
/** Reset accumulator for next window */
|
||||
reset() {
|
||||
this.attenuation.fill(0);
|
||||
this.pathCount.fill(0);
|
||||
this.channelAttenuation.clear();
|
||||
this.frameCount = 0;
|
||||
this.channelFrames.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CSI parsing (shared with other scripts)
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseIqHex(iqHex, nSubcarriers) {
|
||||
const bytes = Buffer.from(iqHex, 'hex');
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
const phases = new Float64Array(nSubcarriers);
|
||||
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = 2 + sc * 2;
|
||||
if (offset + 1 >= bytes.length) break;
|
||||
let I = bytes[offset];
|
||||
let Q = bytes[offset + 1];
|
||||
if (I > 127) I -= 256;
|
||||
if (Q > 127) Q -= 256;
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
phases[sc] = Math.atan2(Q, I);
|
||||
}
|
||||
|
||||
return { amplitudes, phases };
|
||||
}
|
||||
|
||||
function parseCSIFrame(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const nSubcarriers = buf.readUInt16LE(6);
|
||||
const freqMhz = buf.readUInt32LE(8);
|
||||
const rssi = buf.readInt8(16);
|
||||
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
const phases = new Float64Array(nSubcarriers);
|
||||
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = HEADER_SIZE + sc * 2;
|
||||
if (offset + 1 >= buf.length) break;
|
||||
const I = buf.readInt8(offset);
|
||||
const Q = buf.readInt8(offset + 1);
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
phases[sc] = Math.atan2(Q, I);
|
||||
}
|
||||
|
||||
let channel = 0;
|
||||
if (freqMhz >= 2412 && freqMhz <= 2484) {
|
||||
channel = freqMhz === 2484 ? 14 : Math.round((freqMhz - 2412) / 5) + 1;
|
||||
}
|
||||
|
||||
return { nodeId, nSubcarriers, freqMhz, rssi, amplitudes, phases, channel };
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute mean amplitude as a proxy for path attenuation.
|
||||
* Higher amplitude = less attenuation. We invert for the tomography grid.
|
||||
*/
|
||||
function computeAttenuation(amplitudes) {
|
||||
let sum = 0;
|
||||
for (let i = 0; i < amplitudes.length; i++) sum += amplitudes[i];
|
||||
const mean = sum / amplitudes.length;
|
||||
// Free-space reference (approximate, empirically calibrated)
|
||||
const freeSpaceRef = 15.0;
|
||||
// Attenuation: how much below free-space reference
|
||||
return Math.max(0, freeSpaceRef - mean);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Channel assignment for legacy JSONL (no freq field)
|
||||
// ---------------------------------------------------------------------------
|
||||
const nodeChannelIdx = { 1: 0, 2: 0 };
|
||||
|
||||
function assignChannel(nodeId) {
|
||||
const channels = nodeId === 1 ? NODE1_CHANNELS : NODE2_CHANNELS;
|
||||
const ch = channels[nodeChannelIdx[nodeId] % channels.length];
|
||||
nodeChannelIdx[nodeId]++;
|
||||
return ch;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Visualization
|
||||
// ---------------------------------------------------------------------------
|
||||
function renderHeatmap(grid) {
|
||||
const img = grid.getImage();
|
||||
const gs = grid.gridSize;
|
||||
|
||||
const lines = [];
|
||||
lines.push('');
|
||||
lines.push(' RF Tomographic Image');
|
||||
lines.push(' ' + '='.repeat(gs * 2 + 2));
|
||||
|
||||
// Y-axis label
|
||||
for (let r = 0; r < gs; r++) {
|
||||
const y = ((gs - r - 0.5) / gs * grid.roomHeight).toFixed(1);
|
||||
let row = `${y.padStart(4)}m |`;
|
||||
for (let c = 0; c < gs; c++) {
|
||||
const val = img[r * gs + c];
|
||||
const level = Math.floor(val * 7.99);
|
||||
row += HEAT[Math.max(0, Math.min(7, level))] + ' ';
|
||||
}
|
||||
row += '|';
|
||||
lines.push(' ' + row);
|
||||
}
|
||||
|
||||
// X-axis
|
||||
lines.push(' ' + ' '.repeat(6) + '+' + '-'.repeat(gs * 2) + '+');
|
||||
let xLabels = ' '.repeat(7);
|
||||
for (let c = 0; c < gs; c += Math.max(1, Math.floor(gs / 5))) {
|
||||
const x = (c / gs * grid.roomWidth).toFixed(1);
|
||||
xLabels += x.padEnd(Math.floor(gs / 5) * 2 || 2);
|
||||
}
|
||||
lines.push(' ' + xLabels + ' (m)');
|
||||
|
||||
// Legend
|
||||
lines.push('');
|
||||
lines.push(' Legend: ' + HEAT.map((ch, i) =>
|
||||
`${ch}=${HEAT_LABELS[i]}`
|
||||
).join(' '));
|
||||
|
||||
// Node positions
|
||||
const n1c = Math.floor(NODE_POS[1][0] / grid.roomWidth * gs);
|
||||
const n1r = gs - 1 - Math.floor(NODE_POS[1][1] / grid.roomHeight * gs);
|
||||
const n2c = Math.floor(NODE_POS[2][0] / grid.roomWidth * gs);
|
||||
const n2r = gs - 1 - Math.floor(NODE_POS[2][1] / grid.roomHeight * gs);
|
||||
lines.push(` Node 1: (${NODE_POS[1][0]}, ${NODE_POS[1][1]}) m [grid ${n1r},${n1c}]`);
|
||||
lines.push(` Node 2: (${NODE_POS[2][0]}, ${NODE_POS[2][1]}) m [grid ${n2r},${n2c}]`);
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function renderStats(grid) {
|
||||
const lines = [];
|
||||
lines.push(` Frames: ${grid.frameCount}`);
|
||||
|
||||
const chFrames = [...grid.channelFrames.entries()].sort((a, b) => a[0] - b[0]);
|
||||
if (chFrames.length > 0) {
|
||||
lines.push(' Per-channel frames: ' + chFrames.map(([ch, n]) =>
|
||||
`ch${ch}=${n}`
|
||||
).join(' '));
|
||||
}
|
||||
|
||||
const objects = grid.detectObjects(0.6);
|
||||
if (objects.length > 0) {
|
||||
lines.push(` Detected ${objects.length} high-attenuation region(s):`);
|
||||
for (const obj of objects.slice(0, 5)) {
|
||||
lines.push(` (${obj.x}, ${obj.y}) m attenuation=${obj.attenuation}`);
|
||||
}
|
||||
} else {
|
||||
lines.push(' No high-attenuation regions detected');
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function renderChannelComparison(grid) {
|
||||
const images = grid.getChannelImages();
|
||||
const channels = Object.keys(images).map(Number).sort((a, b) => a - b);
|
||||
if (channels.length < 2) return '';
|
||||
|
||||
const gs = grid.gridSize;
|
||||
const lines = [];
|
||||
lines.push('');
|
||||
lines.push(' Per-Channel Attenuation (middle row):');
|
||||
|
||||
const midRow = Math.floor(gs / 2);
|
||||
for (const ch of channels) {
|
||||
const img = images[ch];
|
||||
let bar = ` ch${String(ch).padStart(2)}: `;
|
||||
for (let c = 0; c < gs; c++) {
|
||||
const val = img[midRow * gs + c];
|
||||
const level = Math.floor(val * 7.99);
|
||||
bar += HEAT[Math.max(0, Math.min(7, level))] + ' ';
|
||||
}
|
||||
lines.push(bar);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Process a single CSI record
|
||||
// ---------------------------------------------------------------------------
|
||||
const grid = new TomographyGrid(GRID_SIZE, ROOM_WIDTH, ROOM_HEIGHT);
|
||||
let lastDisplayMs = 0;
|
||||
|
||||
function processFrame(nodeId, amplitudes, channel, timestamp) {
|
||||
const atten = computeAttenuation(amplitudes);
|
||||
|
||||
// Back-project along node-to-node path
|
||||
const txPos = NODE_POS[nodeId] || [0, 0];
|
||||
const otherNode = nodeId === 1 ? 2 : 1;
|
||||
const rxPos = NODE_POS[otherNode] || [NODE_DISTANCE, ROOM_HEIGHT / 2];
|
||||
|
||||
grid.backProject(txPos, rxPos, atten, channel);
|
||||
|
||||
// Also back-project along paths to known illuminators on this channel
|
||||
for (const il of ILLUMINATORS) {
|
||||
if (il.channel === channel) {
|
||||
grid.backProject(il.pos, txPos, atten * (il.signal / 100), channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function displayUpdate() {
|
||||
if (JSON_OUTPUT) {
|
||||
const img = grid.getImage();
|
||||
const objects = grid.detectObjects(0.6);
|
||||
console.log(JSON.stringify({
|
||||
timestamp: Date.now() / 1000,
|
||||
frames: grid.frameCount,
|
||||
channels: [...grid.channelFrames.keys()].sort(),
|
||||
image: Array.from(img).map(v => +v.toFixed(3)),
|
||||
gridSize: GRID_SIZE,
|
||||
roomWidth: ROOM_WIDTH,
|
||||
roomHeight: ROOM_HEIGHT,
|
||||
objects,
|
||||
}));
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H'); // clear screen
|
||||
console.log(renderHeatmap(grid));
|
||||
console.log(renderStats(grid));
|
||||
console.log(renderChannelComparison(grid));
|
||||
console.log('');
|
||||
console.log(' Press Ctrl+C to exit');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live mode (UDP)
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const sock = dgram.createSocket('udp4');
|
||||
|
||||
sock.on('message', (buf, rinfo) => {
|
||||
if (buf.length < 4) return;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return;
|
||||
|
||||
const frame = parseCSIFrame(buf);
|
||||
if (!frame) return;
|
||||
|
||||
processFrame(frame.nodeId, frame.amplitudes, frame.channel, Date.now() / 1000);
|
||||
|
||||
const now = Date.now();
|
||||
if (now - lastDisplayMs >= INTERVAL_MS) {
|
||||
displayUpdate();
|
||||
lastDisplayMs = now;
|
||||
}
|
||||
});
|
||||
|
||||
sock.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`RF Tomography listening on UDP port ${PORT}`);
|
||||
console.log(`Grid: ${GRID_SIZE}x${GRID_SIZE}, Room: ${ROOM_WIDTH}x${ROOM_HEIGHT} m`);
|
||||
console.log(`Node distance: ${NODE_DISTANCE} m`);
|
||||
console.log('Waiting for CSI frames...');
|
||||
}
|
||||
});
|
||||
|
||||
if (DURATION_MS) {
|
||||
setTimeout(() => {
|
||||
displayUpdate();
|
||||
sock.close();
|
||||
process.exit(0);
|
||||
}, DURATION_MS);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode (JSONL)
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
let windowCount = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
if (record.type !== 'raw_csi' || !record.iq_hex) continue;
|
||||
|
||||
const { amplitudes, phases } = parseIqHex(record.iq_hex, record.subcarriers || 64);
|
||||
const channel = record.channel || assignChannel(record.node_id);
|
||||
|
||||
processFrame(record.node_id, amplitudes, channel, record.timestamp);
|
||||
frameCount++;
|
||||
|
||||
const tsMs = record.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
windowCount++;
|
||||
if (JSON_OUTPUT) {
|
||||
displayUpdate();
|
||||
} else {
|
||||
console.log(`\n${'='.repeat(60)}`);
|
||||
console.log(`Window ${windowCount} | t=${record.timestamp.toFixed(1)}s | frames=${frameCount}`);
|
||||
console.log('='.repeat(60));
|
||||
console.log(renderHeatmap(grid));
|
||||
console.log(renderStats(grid));
|
||||
console.log(renderChannelComparison(grid));
|
||||
}
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Final output
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`\n${'='.repeat(60)}`);
|
||||
console.log('FINAL RF TOMOGRAPHIC IMAGE');
|
||||
console.log('='.repeat(60));
|
||||
console.log(renderHeatmap(grid));
|
||||
console.log(renderStats(grid));
|
||||
console.log(renderChannelComparison(grid));
|
||||
console.log(`\nProcessed ${frameCount} frames in ${windowCount} windows`);
|
||||
} else {
|
||||
displayUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry point
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,480 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-077: Room Environment Fingerprinting
|
||||
*
|
||||
* Clusters CSI feature vectors to identify distinct room states (empty,
|
||||
* working, sleeping, etc.), tracks transitions, and detects anomalies.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/room-fingerprint.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/room-fingerprint.js --port 5006
|
||||
* node scripts/room-fingerprint.js --replay FILE --json
|
||||
*
|
||||
* ADR: docs/adr/ADR-077-novel-rf-sensing-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
json: { type: 'boolean', default: false },
|
||||
interval: { type: 'string', short: 'i', default: '10000' },
|
||||
'k': { type: 'string', default: '5' },
|
||||
'new-cluster-threshold': { type: 'string', default: '2.0' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const K = parseInt(args.k, 10);
|
||||
const NEW_CLUSTER_DIST = parseFloat(args['new-cluster-threshold']);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ADR-018 packet constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const VITALS_MAGIC = 0xC5110002;
|
||||
const FEATURE_MAGIC = 0xC5110003;
|
||||
const FUSED_MAGIC = 0xC5110004;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Online k-means clustering
|
||||
// ---------------------------------------------------------------------------
|
||||
class OnlineKMeans {
|
||||
constructor(maxK, featureDim, newClusterThreshold) {
|
||||
this.maxK = maxK;
|
||||
this.dim = featureDim;
|
||||
this.threshold = newClusterThreshold;
|
||||
|
||||
this.centroids = []; // { center: Float64Array, count: number, label: string }
|
||||
this.alpha = 0.01; // EMA update rate
|
||||
}
|
||||
|
||||
_distance(a, b) {
|
||||
let sum = 0;
|
||||
const len = Math.min(a.length, b.length);
|
||||
for (let i = 0; i < len; i++) {
|
||||
sum += (a[i] - b[i]) ** 2;
|
||||
}
|
||||
return Math.sqrt(sum);
|
||||
}
|
||||
|
||||
assign(features) {
|
||||
if (this.centroids.length === 0) {
|
||||
// First point creates first cluster
|
||||
this.centroids.push({
|
||||
center: Float64Array.from(features),
|
||||
count: 1,
|
||||
label: `State-0`,
|
||||
});
|
||||
return { clusterId: 0, distance: 0 };
|
||||
}
|
||||
|
||||
// Find nearest centroid
|
||||
let bestDist = Infinity;
|
||||
let bestIdx = 0;
|
||||
for (let i = 0; i < this.centroids.length; i++) {
|
||||
const d = this._distance(features, this.centroids[i].center);
|
||||
if (d < bestDist) {
|
||||
bestDist = d;
|
||||
bestIdx = i;
|
||||
}
|
||||
}
|
||||
|
||||
// If too far from any cluster, create new one (up to maxK)
|
||||
if (bestDist > this.threshold && this.centroids.length < this.maxK) {
|
||||
const newIdx = this.centroids.length;
|
||||
this.centroids.push({
|
||||
center: Float64Array.from(features),
|
||||
count: 1,
|
||||
label: `State-${newIdx}`,
|
||||
});
|
||||
return { clusterId: newIdx, distance: 0 };
|
||||
}
|
||||
|
||||
// Update centroid via EMA
|
||||
const c = this.centroids[bestIdx];
|
||||
c.count++;
|
||||
for (let i = 0; i < this.dim; i++) {
|
||||
c.center[i] = c.center[i] * (1 - this.alpha) + features[i] * this.alpha;
|
||||
}
|
||||
|
||||
return { clusterId: bestIdx, distance: bestDist };
|
||||
}
|
||||
|
||||
labelClusters(clusterMotion) {
|
||||
// Sort clusters by average motion to assign labels
|
||||
const sorted = Object.entries(clusterMotion)
|
||||
.sort((a, b) => a[1] - b[1]);
|
||||
|
||||
const labels = ['sleeping/empty', 'resting', 'working', 'active', 'highly active'];
|
||||
for (let i = 0; i < sorted.length; i++) {
|
||||
const clusterId = parseInt(sorted[i][0], 10);
|
||||
if (clusterId < this.centroids.length) {
|
||||
this.centroids[clusterId].label = labels[Math.min(i, labels.length - 1)];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Room state tracker
|
||||
// ---------------------------------------------------------------------------
|
||||
class RoomFingerprinter {
|
||||
constructor(maxK, featureDim, newClusterThreshold) {
|
||||
this.kmeans = new OnlineKMeans(maxK, featureDim, newClusterThreshold);
|
||||
this.featureDim = featureDim;
|
||||
|
||||
// State tracking
|
||||
this.currentState = null;
|
||||
this.stateHistory = []; // { timestamp, clusterId, label, distance }
|
||||
this.transitions = {}; // "from->to" -> count
|
||||
|
||||
// Vitals correlation
|
||||
this.clusterMotionSum = {}; // clusterId -> sum
|
||||
this.clusterMotionCount = {}; // clusterId -> count
|
||||
|
||||
// Feature buffer (latest per node)
|
||||
this.latestFeatures = new Map(); // nodeId -> { timestamp, features }
|
||||
this.latestVitals = new Map(); // nodeId -> { timestamp, motion, presence }
|
||||
|
||||
this.startTime = null;
|
||||
}
|
||||
|
||||
pushFeature(timestamp, nodeId, features) {
|
||||
if (!this.startTime) this.startTime = timestamp;
|
||||
this.latestFeatures.set(nodeId, { timestamp, features });
|
||||
}
|
||||
|
||||
pushVitals(timestamp, nodeId, motion, presence) {
|
||||
this.latestVitals.set(nodeId, { timestamp, motion, presence });
|
||||
}
|
||||
|
||||
analyze(timestamp) {
|
||||
// Find latest feature vector (prefer most recent node)
|
||||
let bestFeature = null;
|
||||
let bestTs = 0;
|
||||
for (const [, entry] of this.latestFeatures) {
|
||||
if (entry.timestamp > bestTs) {
|
||||
bestTs = entry.timestamp;
|
||||
bestFeature = entry.features;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bestFeature || bestFeature.length < this.featureDim) return null;
|
||||
|
||||
// Truncate or pad to featureDim
|
||||
const features = new Float64Array(this.featureDim);
|
||||
for (let i = 0; i < this.featureDim && i < bestFeature.length; i++) {
|
||||
features[i] = bestFeature[i];
|
||||
}
|
||||
|
||||
// Assign to cluster
|
||||
const { clusterId, distance } = this.kmeans.assign(features);
|
||||
|
||||
// Track motion per cluster for labeling
|
||||
let avgMotion = 0;
|
||||
let motionCount = 0;
|
||||
for (const [, v] of this.latestVitals) {
|
||||
avgMotion += v.motion;
|
||||
motionCount++;
|
||||
}
|
||||
avgMotion = motionCount > 0 ? avgMotion / motionCount : 0;
|
||||
|
||||
this.clusterMotionSum[clusterId] = (this.clusterMotionSum[clusterId] || 0) + avgMotion;
|
||||
this.clusterMotionCount[clusterId] = (this.clusterMotionCount[clusterId] || 0) + 1;
|
||||
|
||||
// Update labels periodically
|
||||
const clusterMotion = {};
|
||||
for (const id of Object.keys(this.clusterMotionCount)) {
|
||||
clusterMotion[id] = this.clusterMotionSum[id] / this.clusterMotionCount[id];
|
||||
}
|
||||
this.kmeans.labelClusters(clusterMotion);
|
||||
|
||||
const label = this.kmeans.centroids[clusterId]
|
||||
? this.kmeans.centroids[clusterId].label
|
||||
: `State-${clusterId}`;
|
||||
|
||||
// Track transitions
|
||||
if (this.currentState !== null && this.currentState !== clusterId) {
|
||||
const key = `${this.currentState}->${clusterId}`;
|
||||
this.transitions[key] = (this.transitions[key] || 0) + 1;
|
||||
}
|
||||
const prevState = this.currentState;
|
||||
this.currentState = clusterId;
|
||||
|
||||
const entry = {
|
||||
timestamp,
|
||||
clusterId,
|
||||
label,
|
||||
distance: +distance.toFixed(4),
|
||||
motion: +avgMotion.toFixed(3),
|
||||
transitioned: prevState !== null && prevState !== clusterId,
|
||||
prevState: prevState !== null ? prevState : undefined,
|
||||
totalClusters: this.kmeans.centroids.length,
|
||||
};
|
||||
|
||||
this.stateHistory.push(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
anomalyScore() {
|
||||
// Anomaly = current state is rarely seen at this time-of-day
|
||||
if (this.stateHistory.length < 10) return 0;
|
||||
|
||||
const currentCluster = this.currentState;
|
||||
const recentCount = this.stateHistory.slice(-20).filter(e => e.clusterId === currentCluster).length;
|
||||
return 1 - (recentCount / 20); // low count = high anomaly
|
||||
}
|
||||
|
||||
renderTimeline(width) {
|
||||
const w = width || 60;
|
||||
if (this.stateHistory.length === 0) return 'No data yet.';
|
||||
|
||||
const step = Math.max(1, Math.floor(this.stateHistory.length / w));
|
||||
const chars = '\u2581\u2582\u2583\u2584\u2585\u2586\u2587\u2588';
|
||||
|
||||
let line = '';
|
||||
for (let i = 0; i < this.stateHistory.length; i += step) {
|
||||
const cid = this.stateHistory[i].clusterId;
|
||||
line += chars[Math.min(cid, chars.length - 1)];
|
||||
}
|
||||
|
||||
return `State timeline: ${line}`;
|
||||
}
|
||||
|
||||
renderTransitionMatrix() {
|
||||
if (Object.keys(this.transitions).length === 0) return 'No transitions yet.';
|
||||
|
||||
const lines = ['Transition matrix:'];
|
||||
for (const [key, count] of Object.entries(this.transitions).sort((a, b) => b[1] - a[1])) {
|
||||
const [from, to] = key.split('->');
|
||||
const fromLabel = this.kmeans.centroids[parseInt(from, 10)]?.label || `State-${from}`;
|
||||
const toLabel = this.kmeans.centroids[parseInt(to, 10)]?.label || `State-${to}`;
|
||||
lines.push(` ${fromLabel} -> ${toLabel}: ${count}`);
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Packet parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseFeatureJsonl(record) {
|
||||
if (record.type !== 'feature' || !record.features) return null;
|
||||
return {
|
||||
timestamp: record.timestamp,
|
||||
nodeId: record.node_id,
|
||||
features: record.features,
|
||||
};
|
||||
}
|
||||
|
||||
function parseVitalsJsonl(record) {
|
||||
if (record.type !== 'vitals') return null;
|
||||
return {
|
||||
timestamp: record.timestamp,
|
||||
nodeId: record.node_id,
|
||||
motion: record.motion_energy || 0,
|
||||
presence: record.presence_score || 0,
|
||||
};
|
||||
}
|
||||
|
||||
function parseFeatureUdp(buf) {
|
||||
if (buf.length < 48) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== FEATURE_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const features = [];
|
||||
for (let i = 0; i < 8; i++) {
|
||||
features.push(buf.readFloatLE(12 + i * 4));
|
||||
}
|
||||
return { timestamp: Date.now() / 1000, nodeId, features };
|
||||
}
|
||||
|
||||
function parseVitalsUdp(buf) {
|
||||
if (buf.length < 32) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== VITALS_MAGIC && magic !== FUSED_MAGIC) return null;
|
||||
return {
|
||||
timestamp: Date.now() / 1000,
|
||||
nodeId: buf.readUInt8(4),
|
||||
motion: buf.readFloatLE(16),
|
||||
presence: buf.readFloatLE(20),
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const fingerprinter = new RoomFingerprinter(K, 8, NEW_CLUSTER_DIST);
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let featureCount = 0;
|
||||
let vitalsCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
|
||||
const feat = parseFeatureJsonl(record);
|
||||
if (feat) {
|
||||
fingerprinter.pushFeature(feat.timestamp, feat.nodeId, feat.features);
|
||||
featureCount++;
|
||||
}
|
||||
|
||||
const vit = parseVitalsJsonl(record);
|
||||
if (vit) {
|
||||
fingerprinter.pushVitals(vit.timestamp, vit.nodeId, vit.motion, vit.presence);
|
||||
vitalsCount++;
|
||||
}
|
||||
|
||||
const ts = feat || vit;
|
||||
if (!ts) continue;
|
||||
|
||||
const tsMs = ts.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
const result = fingerprinter.analyze(ts.timestamp);
|
||||
|
||||
if (result) {
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify(result));
|
||||
} else {
|
||||
const tsStr = new Date(ts.timestamp * 1000).toISOString().slice(11, 19);
|
||||
const transition = result.transitioned ? ` << TRANSITION from State-${result.prevState}` : '';
|
||||
console.log(`[${tsStr}] Cluster ${result.clusterId} (${result.label}) | dist ${result.distance} | motion ${result.motion} | ${result.totalClusters} clusters${transition}`);
|
||||
}
|
||||
}
|
||||
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Summary
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log('\n' + '='.repeat(60));
|
||||
console.log('ROOM FINGERPRINT SUMMARY');
|
||||
console.log('='.repeat(60));
|
||||
|
||||
console.log(`\nClusters discovered: ${fingerprinter.kmeans.centroids.length}`);
|
||||
for (let i = 0; i < fingerprinter.kmeans.centroids.length; i++) {
|
||||
const c = fingerprinter.kmeans.centroids[i];
|
||||
const stateCount = fingerprinter.stateHistory.filter(e => e.clusterId === i).length;
|
||||
const pct = fingerprinter.stateHistory.length > 0
|
||||
? ((stateCount / fingerprinter.stateHistory.length) * 100).toFixed(1)
|
||||
: '0';
|
||||
const avgMotion = fingerprinter.clusterMotionCount[i] > 0
|
||||
? (fingerprinter.clusterMotionSum[i] / fingerprinter.clusterMotionCount[i]).toFixed(2)
|
||||
: '?';
|
||||
console.log(` Cluster ${i} (${c.label}): ${stateCount} windows (${pct}%) | avg motion ${avgMotion} | ${c.count} assignments`);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log(fingerprinter.renderTimeline(60));
|
||||
console.log('');
|
||||
console.log(fingerprinter.renderTransitionMatrix());
|
||||
|
||||
const anomaly = fingerprinter.anomalyScore();
|
||||
console.log(`\nCurrent anomaly score: ${anomaly.toFixed(3)}`);
|
||||
console.log(`Processed: ${featureCount} feature packets, ${vitalsCount} vitals packets`);
|
||||
} else {
|
||||
console.log(JSON.stringify({
|
||||
type: 'summary',
|
||||
clusters: fingerprinter.kmeans.centroids.length,
|
||||
windows: fingerprinter.stateHistory.length,
|
||||
transitions: Object.keys(fingerprinter.transitions).length,
|
||||
anomaly: +fingerprinter.anomalyScore().toFixed(3),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live UDP mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const fingerprinter = new RoomFingerprinter(K, 8, NEW_CLUSTER_DIST);
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', (buf) => {
|
||||
if (buf.length < 4) return;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
|
||||
if (magic === FEATURE_MAGIC) {
|
||||
const feat = parseFeatureUdp(buf);
|
||||
if (feat) fingerprinter.pushFeature(feat.timestamp, feat.nodeId, feat.features);
|
||||
}
|
||||
if (magic === VITALS_MAGIC || magic === FUSED_MAGIC) {
|
||||
const vit = parseVitalsUdp(buf);
|
||||
if (vit) fingerprinter.pushVitals(vit.timestamp, vit.nodeId, vit.motion, vit.presence);
|
||||
}
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
const result = fingerprinter.analyze(Date.now() / 1000);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
if (result) console.log(JSON.stringify(result));
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
console.log('=== ROOM FINGERPRINT (ADR-077) ===\n');
|
||||
|
||||
if (result) {
|
||||
console.log(`Current state: Cluster ${result.clusterId} (${result.label})`);
|
||||
console.log(`Distance: ${result.distance} | Motion: ${result.motion}`);
|
||||
console.log(`Clusters: ${result.totalClusters}`);
|
||||
if (result.transitioned) {
|
||||
console.log(`** STATE TRANSITION from State-${result.prevState} **`);
|
||||
}
|
||||
} else {
|
||||
console.log('Collecting data...');
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log(fingerprinter.renderTimeline(50));
|
||||
console.log('');
|
||||
console.log(fingerprinter.renderTransitionMatrix());
|
||||
console.log(`\nAnomaly score: ${fingerprinter.anomalyScore().toFixed(3)}`);
|
||||
}
|
||||
}, INTERVAL_MS);
|
||||
|
||||
server.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Room Fingerprint listening on UDP :${PORT} (k=${K})`);
|
||||
}
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => { server.close(); process.exit(0); });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -391,7 +391,16 @@ def run_bridge(args):
|
||||
# Open UDP listener
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sock.bind(("0.0.0.0", args.udp_port))
|
||||
bind_addr = args.bind_addr
|
||||
if bind_addr == "auto":
|
||||
try:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.connect(("192.168.1.1", 80))
|
||||
bind_addr = s.getsockname()[0]
|
||||
s.close()
|
||||
except Exception:
|
||||
bind_addr = "0.0.0.0"
|
||||
sock.bind((bind_addr, args.udp_port))
|
||||
sock.settimeout(1.0) # 1s timeout for responsive time-based flushing
|
||||
log.info(
|
||||
"Listening on UDP port %d (batch size: %d, flush interval: %.0fs)",
|
||||
@@ -597,6 +606,11 @@ def main():
|
||||
default=5006,
|
||||
help="UDP port to listen on (default: 5006)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--bind-addr",
|
||||
default="auto",
|
||||
help="Bind address for UDP listener (default: auto-detect WiFi IP; use 0.0.0.0 for all interfaces)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--batch-size",
|
||||
type=int,
|
||||
|
||||
@@ -0,0 +1,447 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-077: Sleep Quality Monitor — CSI-based sleep staging
|
||||
*
|
||||
* Classifies sleep stages from breathing rate + heart rate + motion energy
|
||||
* using 5-minute sliding windows. Produces a hypnogram and summary stats.
|
||||
*
|
||||
* DISCLAIMER: This is a consumer-grade informational tool, NOT a medical device.
|
||||
* Do not use for clinical diagnosis. Consult a physician for sleep concerns.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/sleep-monitor.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/sleep-monitor.js --port 5006
|
||||
* node scripts/sleep-monitor.js --replay FILE --json
|
||||
*
|
||||
* ADR: docs/adr/ADR-077-novel-rf-sensing-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
json: { type: 'boolean', default: false },
|
||||
interval: { type: 'string', short: 'i', default: '5000' },
|
||||
window: { type: 'string', short: 'w', default: '300' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const WINDOW_SEC = parseInt(args.window, 10); // default 5 min = 300s
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ADR-018 packet constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const VITALS_MAGIC = 0xC5110002;
|
||||
const FUSED_MAGIC = 0xC5110004;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sleep stage thresholds
|
||||
// ---------------------------------------------------------------------------
|
||||
const STAGES = { AWAKE: 'Awake', LIGHT: 'Light', REM: 'REM', DEEP: 'Deep' };
|
||||
const STAGE_CHARS = { Awake: 'W', Light: 'L', REM: 'R', Deep: 'D' };
|
||||
const STAGE_BARS = { Awake: '\u2581', Light: '\u2583', REM: '\u2585', Deep: '\u2588' };
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Vitals buffer
|
||||
// ---------------------------------------------------------------------------
|
||||
class VitalsBuffer {
|
||||
constructor(maxAgeSec) {
|
||||
this.maxAgeSec = maxAgeSec;
|
||||
this.samples = []; // { timestamp, br, hr, motion }
|
||||
}
|
||||
|
||||
push(timestamp, br, hr, motion) {
|
||||
this.samples.push({ timestamp, br, hr, motion });
|
||||
this._prune(timestamp);
|
||||
}
|
||||
|
||||
_prune(now) {
|
||||
const cutoff = now - this.maxAgeSec;
|
||||
while (this.samples.length > 0 && this.samples[0].timestamp < cutoff) {
|
||||
this.samples.shift();
|
||||
}
|
||||
}
|
||||
|
||||
get length() { return this.samples.length; }
|
||||
|
||||
stats() {
|
||||
const n = this.samples.length;
|
||||
if (n < 3) return null;
|
||||
|
||||
let brSum = 0, hrSum = 0, motionSum = 0;
|
||||
for (const s of this.samples) {
|
||||
brSum += s.br;
|
||||
hrSum += s.hr;
|
||||
motionSum += s.motion;
|
||||
}
|
||||
const brMean = brSum / n;
|
||||
const hrMean = hrSum / n;
|
||||
const motionMean = motionSum / n;
|
||||
|
||||
// BR variance
|
||||
let brVar = 0;
|
||||
for (const s of this.samples) {
|
||||
brVar += (s.br - brMean) ** 2;
|
||||
}
|
||||
brVar /= (n - 1);
|
||||
|
||||
// HR coefficient of variation
|
||||
let hrVar = 0;
|
||||
for (const s of this.samples) {
|
||||
hrVar += (s.hr - hrMean) ** 2;
|
||||
}
|
||||
hrVar /= (n - 1);
|
||||
const hrCV = hrMean > 0 ? Math.sqrt(hrVar) / hrMean : 0;
|
||||
|
||||
return { brMean, brVar, hrMean, hrCV, motionMean, n };
|
||||
}
|
||||
|
||||
classify() {
|
||||
const s = this.stats();
|
||||
if (!s) return null;
|
||||
|
||||
// High motion => Awake
|
||||
if (s.motionMean > 5.0 || s.brMean > 25 || s.brMean < 3) {
|
||||
return { stage: STAGES.AWAKE, ...s };
|
||||
}
|
||||
|
||||
// REM: irregular breathing (high variance), HR elevated
|
||||
if (s.brVar > 8.0 && s.brMean >= 15 && s.brMean <= 25) {
|
||||
return { stage: STAGES.REM, ...s };
|
||||
}
|
||||
|
||||
// Deep: low BR, very regular
|
||||
if (s.brMean >= 6 && s.brMean <= 14 && s.brVar < 2.0 && s.motionMean < 2.0) {
|
||||
return { stage: STAGES.DEEP, ...s };
|
||||
}
|
||||
|
||||
// Light: moderate BR and variance
|
||||
if (s.brMean >= 10 && s.brMean <= 20 && s.motionMean < 4.0) {
|
||||
return { stage: STAGES.LIGHT, ...s };
|
||||
}
|
||||
|
||||
// Default to Awake
|
||||
return { stage: STAGES.AWAKE, ...s };
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sleep session tracker
|
||||
// ---------------------------------------------------------------------------
|
||||
class SleepSession {
|
||||
constructor(windowSec) {
|
||||
this.windowSec = windowSec;
|
||||
this.buffers = new Map(); // nodeId -> VitalsBuffer
|
||||
this.hypnogram = []; // { timestamp, stage, stats }
|
||||
this.startTime = null;
|
||||
this.lastTime = null;
|
||||
}
|
||||
|
||||
ingest(timestamp, nodeId, br, hr, motion) {
|
||||
if (!this.startTime) this.startTime = timestamp;
|
||||
this.lastTime = timestamp;
|
||||
|
||||
if (!this.buffers.has(nodeId)) {
|
||||
this.buffers.set(nodeId, new VitalsBuffer(this.windowSec));
|
||||
}
|
||||
this.buffers.get(nodeId).push(timestamp, br, hr, motion);
|
||||
}
|
||||
|
||||
analyze(timestamp) {
|
||||
// Merge stats from all nodes (take the one with most samples)
|
||||
let bestResult = null;
|
||||
let bestCount = 0;
|
||||
for (const [, buf] of this.buffers) {
|
||||
const result = buf.classify();
|
||||
if (result && result.n > bestCount) {
|
||||
bestResult = result;
|
||||
bestCount = result.n;
|
||||
}
|
||||
}
|
||||
|
||||
if (bestResult) {
|
||||
this.hypnogram.push({ timestamp, ...bestResult });
|
||||
}
|
||||
return bestResult;
|
||||
}
|
||||
|
||||
summary() {
|
||||
if (this.hypnogram.length === 0) return null;
|
||||
|
||||
const counts = { Awake: 0, Light: 0, REM: 0, Deep: 0 };
|
||||
for (const entry of this.hypnogram) {
|
||||
counts[entry.stage]++;
|
||||
}
|
||||
const total = this.hypnogram.length;
|
||||
const sleepEntries = total - counts.Awake;
|
||||
const durationSec = this.lastTime - this.startTime;
|
||||
const durationMin = durationSec / 60;
|
||||
|
||||
return {
|
||||
totalRecordedMin: durationMin,
|
||||
totalSleepMin: (sleepEntries / total) * durationMin,
|
||||
sleepEfficiency: total > 0 ? ((sleepEntries / total) * 100) : 0,
|
||||
stageMinutes: {
|
||||
Awake: (counts.Awake / total) * durationMin,
|
||||
Light: (counts.Light / total) * durationMin,
|
||||
REM: (counts.REM / total) * durationMin,
|
||||
Deep: (counts.Deep / total) * durationMin,
|
||||
},
|
||||
stagePercent: {
|
||||
Awake: total > 0 ? ((counts.Awake / total) * 100) : 0,
|
||||
Light: total > 0 ? ((counts.Light / total) * 100) : 0,
|
||||
REM: total > 0 ? ((counts.REM / total) * 100) : 0,
|
||||
Deep: total > 0 ? ((counts.Deep / total) * 100) : 0,
|
||||
},
|
||||
entries: total,
|
||||
};
|
||||
}
|
||||
|
||||
renderHypnogram(width) {
|
||||
if (this.hypnogram.length === 0) return 'No data yet.';
|
||||
|
||||
const w = width || 60;
|
||||
const step = Math.max(1, Math.floor(this.hypnogram.length / w));
|
||||
let bars = '';
|
||||
let labels = '';
|
||||
for (let i = 0; i < this.hypnogram.length; i += step) {
|
||||
const entry = this.hypnogram[i];
|
||||
bars += STAGE_BARS[entry.stage] || ' ';
|
||||
labels += STAGE_CHARS[entry.stage] || '?';
|
||||
}
|
||||
|
||||
const lines = [];
|
||||
lines.push('Hypnogram:');
|
||||
lines.push(` ${bars}`);
|
||||
lines.push(` ${labels}`);
|
||||
lines.push(' W=Awake L=Light R=REM D=Deep');
|
||||
return lines.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Packet parsing (from JSONL or UDP)
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseVitalsJsonl(record) {
|
||||
if (record.type !== 'vitals') return null;
|
||||
return {
|
||||
timestamp: record.timestamp,
|
||||
nodeId: record.node_id,
|
||||
br: record.breathing_bpm || 0,
|
||||
hr: record.heartrate_bpm || 0,
|
||||
motion: record.motion_energy || 0,
|
||||
};
|
||||
}
|
||||
|
||||
function parseVitalsUdp(buf) {
|
||||
if (buf.length < 32) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== VITALS_MAGIC && magic !== FUSED_MAGIC) return null;
|
||||
|
||||
return {
|
||||
timestamp: Date.now() / 1000,
|
||||
nodeId: buf.readUInt8(4),
|
||||
br: buf.readUInt16LE(6) / 100,
|
||||
hr: buf.readUInt32LE(8) / 10000,
|
||||
motion: buf.readFloatLE(16),
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Display
|
||||
// ---------------------------------------------------------------------------
|
||||
function renderLive(session, latest) {
|
||||
const lines = [];
|
||||
lines.push('=== SLEEP QUALITY MONITOR (ADR-077) ===');
|
||||
lines.push('DISCLAIMER: Informational only. Not a medical device.');
|
||||
lines.push('');
|
||||
|
||||
if (latest) {
|
||||
lines.push(`Current stage: ${latest.stage}`);
|
||||
lines.push(` BR: ${latest.brMean.toFixed(1)} BPM (var ${latest.brVar.toFixed(2)})`);
|
||||
lines.push(` HR: ${latest.hrMean.toFixed(1)} BPM (CV ${(latest.hrCV * 100).toFixed(1)}%)`);
|
||||
lines.push(` Motion: ${latest.motionMean.toFixed(2)}`);
|
||||
lines.push(` Window: ${latest.n} samples`);
|
||||
} else {
|
||||
lines.push('Collecting data...');
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
lines.push(session.renderHypnogram(60));
|
||||
|
||||
const sum = session.summary();
|
||||
if (sum) {
|
||||
lines.push('');
|
||||
lines.push(`Duration: ${sum.totalRecordedMin.toFixed(1)} min | Sleep: ${sum.totalSleepMin.toFixed(1)} min | Efficiency: ${sum.sleepEfficiency.toFixed(1)}%`);
|
||||
lines.push(` Deep: ${sum.stagePercent.Deep.toFixed(1)}% | Light: ${sum.stagePercent.Light.toFixed(1)}% | REM: ${sum.stagePercent.REM.toFixed(1)}% | Awake: ${sum.stagePercent.Awake.toFixed(1)}%`);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const session = new SleepSession(WINDOW_SEC);
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let vitalsCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
|
||||
const v = parseVitalsJsonl(record);
|
||||
if (!v) continue;
|
||||
|
||||
session.ingest(v.timestamp, v.nodeId, v.br, v.hr, v.motion);
|
||||
vitalsCount++;
|
||||
|
||||
// Analyze every INTERVAL_MS worth of time
|
||||
const tsMs = v.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
const result = session.analyze(v.timestamp);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
if (result) {
|
||||
console.log(JSON.stringify({
|
||||
timestamp: v.timestamp,
|
||||
stage: result.stage,
|
||||
br_mean: +result.brMean.toFixed(2),
|
||||
br_var: +result.brVar.toFixed(3),
|
||||
hr_mean: +result.hrMean.toFixed(2),
|
||||
hr_cv: +result.hrCV.toFixed(4),
|
||||
motion_mean: +result.motionMean.toFixed(3),
|
||||
}));
|
||||
}
|
||||
} else if (result) {
|
||||
const ts = new Date(v.timestamp * 1000).toISOString().slice(11, 19);
|
||||
console.log(`[${ts}] ${result.stage.padEnd(5)} | BR ${result.brMean.toFixed(1)} (var ${result.brVar.toFixed(2)}) | HR ${result.hrMean.toFixed(1)} | Motion ${result.motionMean.toFixed(2)}`);
|
||||
}
|
||||
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Final summary
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log('\n' + '='.repeat(60));
|
||||
console.log('SLEEP SESSION SUMMARY');
|
||||
console.log('='.repeat(60));
|
||||
console.log(session.renderHypnogram(60));
|
||||
|
||||
const sum = session.summary();
|
||||
if (sum) {
|
||||
console.log('');
|
||||
console.log(`Total recorded: ${sum.totalRecordedMin.toFixed(1)} min`);
|
||||
console.log(`Total sleep: ${sum.totalSleepMin.toFixed(1)} min`);
|
||||
console.log(`Efficiency: ${sum.sleepEfficiency.toFixed(1)}%`);
|
||||
console.log(`Entries: ${sum.entries} analysis windows`);
|
||||
console.log('');
|
||||
console.log('Stage breakdown:');
|
||||
for (const stage of ['Deep', 'Light', 'REM', 'Awake']) {
|
||||
const pct = sum.stagePercent[stage].toFixed(1);
|
||||
const min = sum.stageMinutes[stage].toFixed(1);
|
||||
const bar = '\u2588'.repeat(Math.round(sum.stagePercent[stage] / 2));
|
||||
console.log(` ${stage.padEnd(6)} ${bar.padEnd(50)} ${pct}% (${min} min)`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\nProcessed ${vitalsCount} vitals packets`);
|
||||
} else {
|
||||
const sum = session.summary();
|
||||
if (sum) {
|
||||
console.log(JSON.stringify({ type: 'summary', ...sum }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live UDP mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const session = new SleepSession(WINDOW_SEC);
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', (buf) => {
|
||||
const v = parseVitalsUdp(buf);
|
||||
if (v) {
|
||||
session.ingest(v.timestamp, v.nodeId, v.br, v.hr, v.motion);
|
||||
}
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
const result = session.analyze(Date.now() / 1000);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
if (result) {
|
||||
console.log(JSON.stringify({
|
||||
timestamp: Date.now() / 1000,
|
||||
stage: result.stage,
|
||||
br_mean: +result.brMean.toFixed(2),
|
||||
br_var: +result.brVar.toFixed(3),
|
||||
hr_mean: +result.hrMean.toFixed(2),
|
||||
motion_mean: +result.motionMean.toFixed(3),
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
process.stdout.write(renderLive(session, result) + '\n');
|
||||
}
|
||||
}, INTERVAL_MS);
|
||||
|
||||
server.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Sleep Monitor listening on UDP :${PORT} (window ${WINDOW_SEC}s)`);
|
||||
console.log('DISCLAIMER: Informational only. Not a medical device.\n');
|
||||
}
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log('\n' + '='.repeat(60));
|
||||
const sum = session.summary();
|
||||
if (sum) {
|
||||
console.log(`Session: ${sum.totalRecordedMin.toFixed(1)} min | Sleep: ${sum.totalSleepMin.toFixed(1)} min | Efficiency: ${sum.sleepEfficiency.toFixed(1)}%`);
|
||||
}
|
||||
}
|
||||
server.close();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,475 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* SNN-CSI Processor — Spiking Neural Network for WiFi CSI Sensing
|
||||
*
|
||||
* Receives live CSI frames via UDP (ADR-018 binary format), feeds subcarrier
|
||||
* amplitude deltas through a 128-64-8 SNN with STDP online learning.
|
||||
* Output neurons map to: presence, motion, breathing, HR, phase_var, persons, fall, RSSI.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/snn-csi-processor.js [options]
|
||||
*
|
||||
* Options:
|
||||
* --port <n> UDP listen port (default: 5006)
|
||||
* --max-rate <n> Max spike rate in Hz (default: 200)
|
||||
* --learning-rate <n> STDP a_plus/a_minus (default: 0.005)
|
||||
* --hidden <n> Hidden layer neurons (default: 64)
|
||||
* --no-learn Disable STDP (freeze weights)
|
||||
* --send-vectors Forward spike vectors to Cognitum Seed
|
||||
* --seed-host <host> Cognitum Seed host (default: localhost)
|
||||
* --seed-port <n> Cognitum Seed port (default: 5007)
|
||||
* --quiet Suppress visualization, print only JSON
|
||||
*
|
||||
* Requires: @ruvector/spiking-neural (vendored or npm)
|
||||
*
|
||||
* ADR-074: Spiking Neural Network for CSI Sensing
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const path = require('path');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Resolve spiking-neural: try npm, then vendor
|
||||
// ---------------------------------------------------------------------------
|
||||
let snn_lib;
|
||||
try {
|
||||
snn_lib = require('@ruvector/spiking-neural');
|
||||
} catch {
|
||||
try {
|
||||
snn_lib = require(path.resolve(
|
||||
__dirname, '..', 'vendor', 'ruvector', 'npm', 'packages', 'spiking-neural', 'src', 'index.js'
|
||||
));
|
||||
} catch {
|
||||
// If src/index.js doesn't exist locally, fall back to the CLI which re-exports
|
||||
snn_lib = require(path.resolve(
|
||||
__dirname, '..', 'vendor', 'ruvector', 'npm', 'packages', 'spiking-neural', 'bin', 'cli.js'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
const { createFeedforwardSNN, rateEncoding, SIMDOps, version: snnVersion } = snn_lib;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI argument parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseArgs() {
|
||||
const args = process.argv.slice(2);
|
||||
const opts = {
|
||||
port: 5006,
|
||||
maxRate: 200,
|
||||
learningRate: 0.005,
|
||||
hidden: 64,
|
||||
learn: true,
|
||||
sendVectors: false,
|
||||
seedHost: 'localhost',
|
||||
seedPort: 5007,
|
||||
quiet: false,
|
||||
};
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
switch (args[i]) {
|
||||
case '--port': opts.port = parseInt(args[++i], 10); break;
|
||||
case '--max-rate': opts.maxRate = parseInt(args[++i], 10); break;
|
||||
case '--learning-rate': opts.learningRate = parseFloat(args[++i]); break;
|
||||
case '--hidden': opts.hidden = parseInt(args[++i], 10); break;
|
||||
case '--no-learn': opts.learn = false; break;
|
||||
case '--send-vectors': opts.sendVectors = true; break;
|
||||
case '--seed-host': opts.seedHost = args[++i]; break;
|
||||
case '--seed-port': opts.seedPort = parseInt(args[++i], 10); break;
|
||||
case '--quiet': opts.quiet = true; break;
|
||||
case '--help': case '-h':
|
||||
console.log(`SNN-CSI Processor (spiking-neural v${snnVersion || '?'})`);
|
||||
console.log('Usage: node scripts/snn-csi-processor.js [options]');
|
||||
console.log(' --port <n> UDP listen port (default: 5006)');
|
||||
console.log(' --max-rate <n> Max spike rate Hz (default: 200)');
|
||||
console.log(' --learning-rate <n> STDP rate (default: 0.005)');
|
||||
console.log(' --hidden <n> Hidden neurons (default: 64)');
|
||||
console.log(' --no-learn Freeze STDP weights');
|
||||
console.log(' --send-vectors Forward to Cognitum Seed');
|
||||
console.log(' --seed-host <host> Seed host (default: localhost)');
|
||||
console.log(' --seed-port <n> Seed port (default: 5007)');
|
||||
console.log(' --quiet JSON-only output');
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
return opts;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ADR-018 binary frame parser
|
||||
// ---------------------------------------------------------------------------
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
function parseFrame(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
|
||||
const magic = buf.readUInt32LE(0);
|
||||
// ADR-018 magic: 0xC5110001 (raw CSI), 0xC5110002 (vitals), 0xC5110003 (features)
|
||||
if (magic !== 0xC5110001 && magic !== 0xC5110002 && magic !== 0xC5110003) return null;
|
||||
|
||||
const version = buf.readUInt8(2);
|
||||
const flags = buf.readUInt8(3);
|
||||
const timestamp = buf.readUInt32LE(4);
|
||||
const frequency = buf.readUInt32LE(8);
|
||||
const rssi = buf.readInt8(12);
|
||||
const noiseFloor = buf.readInt8(13);
|
||||
const numSubcarriers = buf.readUInt16LE(14);
|
||||
const nodeId = buf.readUInt16LE(16);
|
||||
const seqNum = buf.readUInt16LE(18);
|
||||
|
||||
const expectedPayload = numSubcarriers * 4; // 2 bytes I + 2 bytes Q per subcarrier
|
||||
if (buf.length < HEADER_SIZE + expectedPayload) {
|
||||
// Fallback: try 2 bytes per subcarrier (amplitude only)
|
||||
if (buf.length >= HEADER_SIZE + numSubcarriers * 2) {
|
||||
const amplitudes = new Float32Array(numSubcarriers);
|
||||
for (let i = 0; i < numSubcarriers; i++) {
|
||||
amplitudes[i] = buf.readInt16LE(HEADER_SIZE + i * 2);
|
||||
}
|
||||
return { timestamp, frequency, rssi, noiseFloor, numSubcarriers, nodeId, seqNum, amplitudes };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Parse I/Q and compute amplitudes
|
||||
const amplitudes = new Float32Array(numSubcarriers);
|
||||
for (let i = 0; i < numSubcarriers; i++) {
|
||||
const offset = HEADER_SIZE + i * 4;
|
||||
const real = buf.readInt16LE(offset);
|
||||
const imag = buf.readInt16LE(offset + 2);
|
||||
amplitudes[i] = Math.sqrt(real * real + imag * imag);
|
||||
}
|
||||
|
||||
return { timestamp, frequency, rssi, noiseFloor, numSubcarriers, nodeId, seqNum, amplitudes };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SNN setup
|
||||
// ---------------------------------------------------------------------------
|
||||
const INPUT_NEURONS = 128;
|
||||
const OUTPUT_NEURONS = 8;
|
||||
|
||||
const OUTPUT_LABELS = [
|
||||
'presence', 'motion', 'breathing', 'heart_rate',
|
||||
'phase_var', 'persons', 'fall', 'rssi'
|
||||
];
|
||||
|
||||
function createCSISnn(opts) {
|
||||
const snn = createFeedforwardSNN([INPUT_NEURONS, opts.hidden, OUTPUT_NEURONS], {
|
||||
dt: 1.0,
|
||||
tau: 20.0,
|
||||
v_rest: -70.0,
|
||||
v_reset: -75.0,
|
||||
v_thresh: -50.0,
|
||||
resistance: 10.0,
|
||||
a_plus: opts.learningRate,
|
||||
a_minus: opts.learningRate * 0.6, // Slight asymmetry: LTP > LTD for stability
|
||||
w_min: 0.0,
|
||||
w_max: 1.0,
|
||||
init_weight: 0.3,
|
||||
init_std: 0.05,
|
||||
lateral_inhibition: true,
|
||||
inhibition_strength: 15.0,
|
||||
});
|
||||
return snn;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Amplitude delta tracking + normalization
|
||||
// ---------------------------------------------------------------------------
|
||||
class DeltaTracker {
|
||||
constructor(size) {
|
||||
this.size = size;
|
||||
this.prev = null;
|
||||
this.maxDelta = 1.0; // Adaptive normalization ceiling
|
||||
this.ewmaMaxDelta = 1.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute normalized amplitude deltas from a new frame.
|
||||
* Returns Float32Array of length INPUT_NEURONS (zero-padded if fewer subcarriers).
|
||||
*/
|
||||
update(amplitudes) {
|
||||
const n = Math.min(amplitudes.length, this.size);
|
||||
const deltas = new Float32Array(this.size);
|
||||
|
||||
if (this.prev === null) {
|
||||
this.prev = new Float32Array(amplitudes);
|
||||
return deltas; // First frame: all zeros (no delta yet)
|
||||
}
|
||||
|
||||
let frameMax = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
const d = Math.abs(amplitudes[i] - this.prev[i]);
|
||||
deltas[i] = d;
|
||||
if (d > frameMax) frameMax = d;
|
||||
}
|
||||
|
||||
// Update adaptive normalization with EWMA
|
||||
if (frameMax > 0) {
|
||||
this.ewmaMaxDelta = 0.95 * this.ewmaMaxDelta + 0.05 * frameMax;
|
||||
this.maxDelta = Math.max(this.ewmaMaxDelta, 1.0);
|
||||
}
|
||||
|
||||
// Normalize to [0, 1]
|
||||
for (let i = 0; i < this.size; i++) {
|
||||
deltas[i] = Math.min(deltas[i] / this.maxDelta, 1.0);
|
||||
}
|
||||
|
||||
// Store current amplitudes for next delta
|
||||
this.prev = new Float32Array(amplitudes);
|
||||
|
||||
return deltas;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Spike rate smoother (exponentially-weighted moving average on output)
|
||||
// ---------------------------------------------------------------------------
|
||||
class OutputSmoother {
|
||||
constructor(size, alpha) {
|
||||
this.size = size;
|
||||
this.alpha = alpha; // Smoothing factor (0.1 = slow, 0.5 = fast)
|
||||
this.smoothed = new Float32Array(size);
|
||||
}
|
||||
|
||||
update(raw) {
|
||||
for (let i = 0; i < this.size; i++) {
|
||||
this.smoothed[i] = this.alpha * raw[i] + (1 - this.alpha) * this.smoothed[i];
|
||||
}
|
||||
return this.smoothed;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ASCII visualization
|
||||
// ---------------------------------------------------------------------------
|
||||
const BAR_CHARS = ' .:;+=xX#@';
|
||||
|
||||
function renderBar(value, maxWidth) {
|
||||
const clamped = Math.min(Math.max(value, 0), 1);
|
||||
const filled = Math.round(clamped * maxWidth);
|
||||
const charIdx = Math.min(Math.floor(clamped * (BAR_CHARS.length - 1)), BAR_CHARS.length - 1);
|
||||
return BAR_CHARS[charIdx].repeat(filled).padEnd(maxWidth);
|
||||
}
|
||||
|
||||
function renderVisualization(outputSmoothed, stats, frameCount, opts) {
|
||||
const lines = [];
|
||||
lines.push('');
|
||||
lines.push(`--- SNN-CSI Processor (frame #${frameCount}) ---`);
|
||||
lines.push(` Network: ${INPUT_NEURONS}-${opts.hidden}-${OUTPUT_NEURONS} | STDP: ${opts.learn ? 'ON' : 'OFF'} | Spikes: ${stats.totalSpikes}`);
|
||||
lines.push('');
|
||||
lines.push(' Output Activity:');
|
||||
|
||||
// Find max for relative scaling
|
||||
const maxVal = Math.max(...outputSmoothed, 0.001);
|
||||
|
||||
for (let i = 0; i < OUTPUT_NEURONS; i++) {
|
||||
const norm = outputSmoothed[i] / maxVal;
|
||||
const bar = renderBar(norm, 30);
|
||||
const raw = outputSmoothed[i].toFixed(2).padStart(6);
|
||||
lines.push(` ${OUTPUT_LABELS[i].padEnd(12)} |${bar}| ${raw}`);
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
|
||||
// Hidden layer activity heatmap (single row)
|
||||
const hiddenActivity = stats.hiddenSpikes || [];
|
||||
let heatmap = ' Hidden: ';
|
||||
for (let i = 0; i < Math.min(opts.hidden, 64); i++) {
|
||||
const val = hiddenActivity[i] || 0;
|
||||
const charIdx = Math.min(Math.floor(val * (BAR_CHARS.length - 1)), BAR_CHARS.length - 1);
|
||||
heatmap += BAR_CHARS[Math.max(charIdx, 0)];
|
||||
}
|
||||
lines.push(heatmap);
|
||||
|
||||
// Weight stats
|
||||
if (stats.weightMean !== undefined) {
|
||||
lines.push(` Weights: mean=${stats.weightMean.toFixed(3)} min=${stats.weightMin.toFixed(3)} max=${stats.weightMax.toFixed(3)}`);
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
|
||||
// Clear screen and print (ANSI escape)
|
||||
process.stdout.write('\x1b[2J\x1b[H');
|
||||
process.stdout.write(lines.join('\n'));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main processing loop
|
||||
// ---------------------------------------------------------------------------
|
||||
function main() {
|
||||
const opts = parseArgs();
|
||||
|
||||
console.log(`SNN-CSI Processor`);
|
||||
console.log(` spiking-neural version: ${snnVersion || 'unknown'}`);
|
||||
console.log(` Network: ${INPUT_NEURONS} -> ${opts.hidden} -> ${OUTPUT_NEURONS}`);
|
||||
console.log(` Synapses: ${INPUT_NEURONS * opts.hidden + opts.hidden * OUTPUT_NEURONS}`);
|
||||
console.log(` STDP: ${opts.learn ? `ON (lr=${opts.learningRate})` : 'OFF (frozen)'}`);
|
||||
console.log(` Lateral inhibition: ON (strength=15.0)`);
|
||||
console.log(` Listening on UDP port ${opts.port}...`);
|
||||
console.log('');
|
||||
|
||||
const snn = createCSISnn(opts);
|
||||
const deltaTracker = new DeltaTracker(INPUT_NEURONS);
|
||||
const smoother = new OutputSmoother(OUTPUT_NEURONS, 0.3);
|
||||
|
||||
let frameCount = 0;
|
||||
let totalSpikes = 0;
|
||||
const SIM_STEPS_PER_FRAME = 5; // Run 5ms of SNN simulation per CSI frame
|
||||
|
||||
// Optional: Cognitum Seed forwarding socket
|
||||
let seedSocket = null;
|
||||
if (opts.sendVectors) {
|
||||
seedSocket = dgram.createSocket('udp4');
|
||||
console.log(` Forwarding spike vectors to ${opts.seedHost}:${opts.seedPort}`);
|
||||
}
|
||||
|
||||
// UDP listener
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', (msg, rinfo) => {
|
||||
const frame = parseFrame(msg);
|
||||
if (!frame) return;
|
||||
|
||||
frameCount++;
|
||||
|
||||
// Compute amplitude deltas
|
||||
const deltas = deltaTracker.update(frame.amplitudes);
|
||||
|
||||
// Run SNN for multiple simulation steps per frame
|
||||
let frameSpikes = 0;
|
||||
const outputAccum = new Float32Array(OUTPUT_NEURONS);
|
||||
|
||||
for (let t = 0; t < SIM_STEPS_PER_FRAME; t++) {
|
||||
// Rate-encode deltas as Poisson spikes
|
||||
const inputSpikes = rateEncoding(deltas, 1.0, opts.maxRate);
|
||||
|
||||
// Step SNN (STDP learning happens inside if weights are not frozen)
|
||||
frameSpikes += snn.step(inputSpikes);
|
||||
|
||||
// Accumulate output
|
||||
const output = snn.getOutput();
|
||||
for (let i = 0; i < OUTPUT_NEURONS; i++) {
|
||||
outputAccum[i] += output[i];
|
||||
}
|
||||
}
|
||||
|
||||
totalSpikes += frameSpikes;
|
||||
|
||||
// Normalize accumulated output by simulation steps
|
||||
for (let i = 0; i < OUTPUT_NEURONS; i++) {
|
||||
outputAccum[i] /= SIM_STEPS_PER_FRAME;
|
||||
}
|
||||
|
||||
// Smooth output
|
||||
const smoothed = smoother.update(outputAccum);
|
||||
|
||||
// Get network stats
|
||||
const netStats = snn.getStats();
|
||||
const stats = {
|
||||
totalSpikes: frameSpikes,
|
||||
hiddenSpikes: [],
|
||||
weightMean: 0,
|
||||
weightMin: 0,
|
||||
weightMax: 0,
|
||||
};
|
||||
|
||||
// Extract hidden layer spike info if available
|
||||
if (netStats.layers && netStats.layers.length > 1) {
|
||||
const hiddenLayer = netStats.layers[1];
|
||||
if (hiddenLayer.neurons) {
|
||||
// Build a rough activity vector from spike counts
|
||||
// The API gives aggregate counts, not per-neuron; approximate with output
|
||||
stats.hiddenSpikes = new Array(opts.hidden).fill(0);
|
||||
stats.hiddenSpikes[0] = hiddenLayer.neurons.spike_count > 0 ? 1 : 0;
|
||||
}
|
||||
if (netStats.layers[0] && netStats.layers[0].synapses) {
|
||||
stats.weightMean = netStats.layers[0].synapses.mean;
|
||||
stats.weightMin = netStats.layers[0].synapses.min;
|
||||
stats.weightMax = netStats.layers[0].synapses.max;
|
||||
}
|
||||
}
|
||||
|
||||
// Visualization or JSON output
|
||||
if (opts.quiet) {
|
||||
const result = {
|
||||
frame: frameCount,
|
||||
timestamp: frame.timestamp,
|
||||
nodeId: frame.nodeId,
|
||||
channel: Math.round((frame.frequency - 2407) / 5),
|
||||
subcarriers: frame.numSubcarriers,
|
||||
rssi: frame.rssi,
|
||||
spikes: frameSpikes,
|
||||
output: {},
|
||||
};
|
||||
for (let i = 0; i < OUTPUT_NEURONS; i++) {
|
||||
result.output[OUTPUT_LABELS[i]] = parseFloat(smoothed[i].toFixed(3));
|
||||
}
|
||||
console.log(JSON.stringify(result));
|
||||
} else {
|
||||
renderVisualization(smoothed, stats, frameCount, opts);
|
||||
}
|
||||
|
||||
// Forward spike vector to Cognitum Seed
|
||||
if (seedSocket) {
|
||||
const vectorBuf = Buffer.alloc(4 + OUTPUT_NEURONS * 4); // 4-byte header + float32 array
|
||||
vectorBuf.writeUInt16LE(0x534E, 0); // 'SN' magic
|
||||
vectorBuf.writeUInt8(OUTPUT_NEURONS, 2);
|
||||
vectorBuf.writeUInt8(frame.nodeId & 0xFF, 3);
|
||||
for (let i = 0; i < OUTPUT_NEURONS; i++) {
|
||||
vectorBuf.writeFloatLE(smoothed[i], 4 + i * 4);
|
||||
}
|
||||
seedSocket.send(vectorBuf, opts.seedPort, opts.seedHost);
|
||||
}
|
||||
});
|
||||
|
||||
server.on('error', (err) => {
|
||||
console.error(`UDP error: ${err.message}`);
|
||||
server.close();
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
server.bind(opts.port, () => {
|
||||
console.log(`Listening for CSI frames on UDP port ${opts.port}`);
|
||||
});
|
||||
|
||||
// Periodic weight decay (prevent drift) — every 1 second
|
||||
if (opts.learn) {
|
||||
setInterval(() => {
|
||||
// Weight decay is applied implicitly by the SNN's w_min/w_max clamping
|
||||
// and the balanced LTP/LTD rates. No additional decay needed for now.
|
||||
// Future: iterate weights and multiply by 0.999 if drift is observed.
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
// Periodic stats dump (every 10 seconds)
|
||||
setInterval(() => {
|
||||
if (opts.quiet) return;
|
||||
const stats = snn.getStats();
|
||||
const uptimeSec = Math.floor(process.uptime());
|
||||
const fps = frameCount > 0 ? (frameCount / uptimeSec).toFixed(1) : '0.0';
|
||||
process.stderr.write(
|
||||
`[${uptimeSec}s] frames=${frameCount} fps=${fps} totalSpikes=${totalSpikes} ` +
|
||||
`mem=${Math.round(process.memoryUsage().heapUsed / 1024)}KB\n`
|
||||
);
|
||||
}, 10000);
|
||||
|
||||
// Graceful shutdown
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n\nShutting down SNN-CSI Processor...');
|
||||
const stats = snn.getStats();
|
||||
console.log(` Total frames processed: ${frameCount}`);
|
||||
console.log(` Total spikes: ${totalSpikes}`);
|
||||
if (stats.layers && stats.layers[0] && stats.layers[0].synapses) {
|
||||
const w = stats.layers[0].synapses;
|
||||
console.log(` Final weights: mean=${w.mean.toFixed(3)} min=${w.min.toFixed(3)} max=${w.max.toFixed(3)}`);
|
||||
}
|
||||
server.close();
|
||||
if (seedSocket) seedSocket.close();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -0,0 +1,414 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* ADR-077: Stress Monitor — HRV-based emotional state detection
|
||||
*
|
||||
* Computes RMSSD and LF/HF ratio from heart rate time series to produce
|
||||
* a stress score (0-100). Uses 5-minute sliding windows with FFT analysis.
|
||||
*
|
||||
* DISCLAIMER: This is an informational wellness tool, NOT a medical device.
|
||||
* Do not use for clinical diagnosis.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/stress-monitor.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/stress-monitor.js --port 5006
|
||||
* node scripts/stress-monitor.js --replay FILE --json
|
||||
*
|
||||
* ADR: docs/adr/ADR-077-novel-rf-sensing-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
json: { type: 'boolean', default: false },
|
||||
interval: { type: 'string', short: 'i', default: '5000' },
|
||||
window: { type: 'string', short: 'w', default: '300' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const WINDOW_SEC = parseInt(args.window, 10);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ADR-018 packet constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const VITALS_MAGIC = 0xC5110002;
|
||||
const FUSED_MAGIC = 0xC5110004;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Simple FFT (radix-2 DIT, power-of-2 only)
|
||||
// ---------------------------------------------------------------------------
|
||||
function fft(re, im) {
|
||||
const n = re.length;
|
||||
if (n <= 1) return;
|
||||
|
||||
// Bit-reversal permutation
|
||||
for (let i = 1, j = 0; i < n; i++) {
|
||||
let bit = n >> 1;
|
||||
for (; j & bit; bit >>= 1) {
|
||||
j ^= bit;
|
||||
}
|
||||
j ^= bit;
|
||||
if (i < j) {
|
||||
[re[i], re[j]] = [re[j], re[i]];
|
||||
[im[i], im[j]] = [im[j], im[i]];
|
||||
}
|
||||
}
|
||||
|
||||
// Cooley-Tukey
|
||||
for (let len = 2; len <= n; len *= 2) {
|
||||
const half = len / 2;
|
||||
const angle = -2 * Math.PI / len;
|
||||
const wRe = Math.cos(angle);
|
||||
const wIm = Math.sin(angle);
|
||||
|
||||
for (let i = 0; i < n; i += len) {
|
||||
let curRe = 1, curIm = 0;
|
||||
for (let j = 0; j < half; j++) {
|
||||
const tRe = curRe * re[i + j + half] - curIm * im[i + j + half];
|
||||
const tIm = curRe * im[i + j + half] + curIm * re[i + j + half];
|
||||
|
||||
re[i + j + half] = re[i + j] - tRe;
|
||||
im[i + j + half] = im[i + j] - tIm;
|
||||
re[i + j] += tRe;
|
||||
im[i + j] += tIm;
|
||||
|
||||
const newCurRe = curRe * wRe - curIm * wIm;
|
||||
curIm = curRe * wIm + curIm * wRe;
|
||||
curRe = newCurRe;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function nextPow2(n) {
|
||||
let p = 1;
|
||||
while (p < n) p *= 2;
|
||||
return p;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HRV analysis engine
|
||||
// ---------------------------------------------------------------------------
|
||||
class HRVAnalyzer {
|
||||
constructor(windowSec) {
|
||||
this.windowSec = windowSec;
|
||||
this.hrSamples = []; // { timestamp, hr }
|
||||
this.history = []; // { timestamp, rmssd, lfhf, stress, motionMean }
|
||||
this.maxHistory = 500;
|
||||
}
|
||||
|
||||
push(timestamp, hr, motion) {
|
||||
this.hrSamples.push({ timestamp, hr, motion: motion || 0 });
|
||||
// Prune old samples
|
||||
const cutoff = timestamp - this.windowSec;
|
||||
while (this.hrSamples.length > 0 && this.hrSamples[0].timestamp < cutoff) {
|
||||
this.hrSamples.shift();
|
||||
}
|
||||
}
|
||||
|
||||
analyze(timestamp) {
|
||||
const samples = this.hrSamples;
|
||||
const n = samples.length;
|
||||
if (n < 10) return null;
|
||||
|
||||
// Compute RR intervals (from HR in BPM -> interval in ms)
|
||||
// HR = 60000 / RR_ms, so RR_ms = 60000 / HR
|
||||
const rr = [];
|
||||
for (const s of samples) {
|
||||
if (s.hr > 20 && s.hr < 200) {
|
||||
rr.push(60000 / s.hr);
|
||||
}
|
||||
}
|
||||
if (rr.length < 5) return null;
|
||||
|
||||
// RMSSD: root mean square of successive differences
|
||||
let sumSqDiff = 0;
|
||||
let diffCount = 0;
|
||||
for (let i = 1; i < rr.length; i++) {
|
||||
const diff = rr[i] - rr[i - 1];
|
||||
sumSqDiff += diff * diff;
|
||||
diffCount++;
|
||||
}
|
||||
const rmssd = diffCount > 0 ? Math.sqrt(sumSqDiff / diffCount) : 0;
|
||||
|
||||
// FFT-based LF/HF ratio
|
||||
// Resample RR series to uniform ~1 Hz for FFT
|
||||
const fs = 1.0; // 1 Hz sampling (approximate, given ~1 Hz vitals)
|
||||
const nfft = nextPow2(Math.max(rr.length, 64));
|
||||
const re = new Float64Array(nfft);
|
||||
const im = new Float64Array(nfft);
|
||||
|
||||
// De-mean and window (Hann)
|
||||
const mean = rr.reduce((a, b) => a + b, 0) / rr.length;
|
||||
for (let i = 0; i < rr.length; i++) {
|
||||
const hann = 0.5 * (1 - Math.cos(2 * Math.PI * i / (rr.length - 1)));
|
||||
re[i] = (rr[i] - mean) * hann;
|
||||
}
|
||||
|
||||
fft(re, im);
|
||||
|
||||
// Compute power spectral density
|
||||
const freqRes = fs / nfft;
|
||||
let lfPower = 0, hfPower = 0;
|
||||
for (let k = 0; k < nfft / 2; k++) {
|
||||
const freq = k * freqRes;
|
||||
const power = re[k] * re[k] + im[k] * im[k];
|
||||
|
||||
if (freq >= 0.04 && freq <= 0.15) lfPower += power;
|
||||
if (freq >= 0.15 && freq <= 0.40) hfPower += power;
|
||||
}
|
||||
|
||||
const lfhf = hfPower > 0.001 ? lfPower / hfPower : 0;
|
||||
|
||||
// Stress score (0-100)
|
||||
// High RMSSD = relaxed (low stress), high LF/HF = stressed
|
||||
const maxRmssd = 100; // typical max RMSSD for WiFi-derived HR
|
||||
const rmssdNorm = Math.min(rmssd / maxRmssd, 1.0);
|
||||
const lfhfNorm = Math.min(lfhf / 4.0, 1.0);
|
||||
const stress = Math.round(50 * (1 - rmssdNorm) + 50 * lfhfNorm);
|
||||
|
||||
// Average motion in window
|
||||
let motionSum = 0;
|
||||
for (const s of samples) motionSum += s.motion;
|
||||
const motionMean = motionSum / n;
|
||||
|
||||
// HR stats
|
||||
const hrValues = samples.map(s => s.hr).filter(h => h > 20 && h < 200);
|
||||
const hrMean = hrValues.reduce((a, b) => a + b, 0) / hrValues.length;
|
||||
|
||||
const result = {
|
||||
timestamp,
|
||||
rmssd: +rmssd.toFixed(2),
|
||||
lfPower: +lfPower.toFixed(2),
|
||||
hfPower: +hfPower.toFixed(2),
|
||||
lfhf: +lfhf.toFixed(3),
|
||||
stress,
|
||||
hrMean: +hrMean.toFixed(1),
|
||||
motionMean: +motionMean.toFixed(3),
|
||||
samples: n,
|
||||
};
|
||||
|
||||
this.history.push(result);
|
||||
if (this.history.length > this.maxHistory) this.history.shift();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
stressLabel(score) {
|
||||
if (score < 20) return 'Very relaxed';
|
||||
if (score < 40) return 'Relaxed';
|
||||
if (score < 60) return 'Moderate';
|
||||
if (score < 80) return 'Stressed';
|
||||
return 'Very stressed';
|
||||
}
|
||||
|
||||
renderTrend(width) {
|
||||
const w = width || 50;
|
||||
if (this.history.length === 0) return 'No data yet.';
|
||||
|
||||
const step = Math.max(1, Math.floor(this.history.length / w));
|
||||
const bars = ['\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588'];
|
||||
|
||||
let line = '';
|
||||
for (let i = 0; i < this.history.length; i += step) {
|
||||
const s = this.history[i].stress;
|
||||
const idx = Math.min(7, Math.floor(s / 12.5));
|
||||
line += bars[idx];
|
||||
}
|
||||
return `Stress trend: ${line} (low)\u2581\u2582\u2583\u2584\u2585\u2586\u2587\u2588(high)`;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Packet parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseVitalsJsonl(record) {
|
||||
if (record.type !== 'vitals') return null;
|
||||
return {
|
||||
timestamp: record.timestamp,
|
||||
nodeId: record.node_id,
|
||||
hr: record.heartrate_bpm || 0,
|
||||
motion: record.motion_energy || 0,
|
||||
};
|
||||
}
|
||||
|
||||
function parseVitalsUdp(buf) {
|
||||
if (buf.length < 32) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== VITALS_MAGIC && magic !== FUSED_MAGIC) return null;
|
||||
return {
|
||||
timestamp: Date.now() / 1000,
|
||||
nodeId: buf.readUInt8(4),
|
||||
hr: buf.readUInt32LE(8) / 10000,
|
||||
motion: buf.readFloatLE(16),
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const analyzer = new HRVAnalyzer(WINDOW_SEC);
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let vitalsCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
|
||||
const v = parseVitalsJsonl(record);
|
||||
if (!v) continue;
|
||||
|
||||
analyzer.push(v.timestamp, v.hr, v.motion);
|
||||
vitalsCount++;
|
||||
|
||||
const tsMs = v.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
const result = analyzer.analyze(v.timestamp);
|
||||
|
||||
if (result) {
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify(result));
|
||||
} else {
|
||||
const ts = new Date(v.timestamp * 1000).toISOString().slice(11, 19);
|
||||
const label = analyzer.stressLabel(result.stress);
|
||||
const bar = '\u2588'.repeat(Math.round(result.stress / 5));
|
||||
console.log(`[${ts}] Stress: ${String(result.stress).padStart(3)}/100 ${bar.padEnd(20)} ${label} | RMSSD ${result.rmssd} | LF/HF ${result.lfhf} | HR ${result.hrMean} | Motion ${result.motionMean}`);
|
||||
}
|
||||
}
|
||||
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Final summary
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log('\n' + '='.repeat(70));
|
||||
console.log('STRESS ANALYSIS SUMMARY');
|
||||
console.log('DISCLAIMER: Informational only. Not a medical device.');
|
||||
console.log('='.repeat(70));
|
||||
|
||||
if (analyzer.history.length > 0) {
|
||||
const scores = analyzer.history.map(h => h.stress);
|
||||
const avg = scores.reduce((a, b) => a + b, 0) / scores.length;
|
||||
const min = Math.min(...scores);
|
||||
const max = Math.max(...scores);
|
||||
|
||||
console.log(`Average stress: ${avg.toFixed(0)}/100 (${analyzer.stressLabel(avg)})`);
|
||||
console.log(`Range: ${min} - ${max}`);
|
||||
console.log(`Windows: ${analyzer.history.length}`);
|
||||
console.log('');
|
||||
console.log(analyzer.renderTrend(60));
|
||||
|
||||
// Activity correlation
|
||||
const highMotion = analyzer.history.filter(h => h.motionMean > 3.0);
|
||||
const lowMotion = analyzer.history.filter(h => h.motionMean < 1.0);
|
||||
if (highMotion.length > 0 && lowMotion.length > 0) {
|
||||
const avgHigh = highMotion.reduce((s, h) => s + h.stress, 0) / highMotion.length;
|
||||
const avgLow = lowMotion.reduce((s, h) => s + h.stress, 0) / lowMotion.length;
|
||||
console.log('');
|
||||
console.log(`Activity correlation:`);
|
||||
console.log(` Active periods (motion > 3): avg stress ${avgHigh.toFixed(0)} (${highMotion.length} windows)`);
|
||||
console.log(` Rest periods (motion < 1): avg stress ${avgLow.toFixed(0)} (${lowMotion.length} windows)`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\nProcessed ${vitalsCount} vitals packets`);
|
||||
} else {
|
||||
if (analyzer.history.length > 0) {
|
||||
const scores = analyzer.history.map(h => h.stress);
|
||||
console.log(JSON.stringify({
|
||||
type: 'summary',
|
||||
avg_stress: +(scores.reduce((a, b) => a + b, 0) / scores.length).toFixed(1),
|
||||
min_stress: Math.min(...scores),
|
||||
max_stress: Math.max(...scores),
|
||||
windows: analyzer.history.length,
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live UDP mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const analyzer = new HRVAnalyzer(WINDOW_SEC);
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('message', (buf) => {
|
||||
const v = parseVitalsUdp(buf);
|
||||
if (v) {
|
||||
analyzer.push(v.timestamp, v.hr, v.motion);
|
||||
}
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
const result = analyzer.analyze(Date.now() / 1000);
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
if (result) console.log(JSON.stringify(result));
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
console.log('=== STRESS MONITOR (ADR-077) ===');
|
||||
console.log('DISCLAIMER: Informational only. Not a medical device.');
|
||||
console.log('');
|
||||
|
||||
if (result) {
|
||||
const label = analyzer.stressLabel(result.stress);
|
||||
const bar = '\u2588'.repeat(Math.round(result.stress / 5));
|
||||
console.log(`Stress: ${result.stress}/100 ${bar} ${label}`);
|
||||
console.log(`RMSSD: ${result.rmssd} ms | LF/HF: ${result.lfhf}`);
|
||||
console.log(`HR: ${result.hrMean} BPM | Motion: ${result.motionMean}`);
|
||||
console.log(`Window: ${result.samples} samples`);
|
||||
console.log('');
|
||||
console.log(analyzer.renderTrend(50));
|
||||
} else {
|
||||
console.log('Collecting data...');
|
||||
}
|
||||
}
|
||||
}, INTERVAL_MS);
|
||||
|
||||
server.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Stress Monitor listening on UDP :${PORT} (window ${WINDOW_SEC}s)`);
|
||||
}
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => { server.close(); process.exit(0); });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
@@ -0,0 +1,595 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Through-Wall Motion Detection — Multi-Frequency Mesh Application
|
||||
*
|
||||
* Detects motion behind walls by exploiting the fact that lower WiFi frequencies
|
||||
* penetrate walls better than higher frequencies. With 6 channels spanning
|
||||
* 2412-2462 MHz, we can:
|
||||
*
|
||||
* 1. Baseline each channel's attenuation through the wall (calibration phase)
|
||||
* 2. Detect changes above baseline = motion behind wall
|
||||
* 3. Weight lower channels more heavily (better through-wall SNR)
|
||||
* 4. Cross-validate across channels (real motion is coherent; noise is not)
|
||||
*
|
||||
* Requires multi-frequency mesh scanning (ADR-073): 2 ESP32 nodes hopping
|
||||
* across channels 1, 3, 5, 6, 9, 11.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/through-wall-detector.js --calibrate 60
|
||||
* node scripts/through-wall-detector.js --port 5006 --duration 300
|
||||
* node scripts/through-wall-detector.js --replay data/recordings/overnight-1775217646.csi.jsonl
|
||||
* node scripts/through-wall-detector.js --threshold 3.0
|
||||
*
|
||||
* ADR: docs/adr/ADR-078-multifreq-mesh-applications.md
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const dgram = require('dgram');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const { parseArgs } = require('util');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
const { values: args } = parseArgs({
|
||||
options: {
|
||||
port: { type: 'string', short: 'p', default: '5006' },
|
||||
duration: { type: 'string', short: 'd' },
|
||||
replay: { type: 'string', short: 'r' },
|
||||
interval: { type: 'string', short: 'i', default: '1000' },
|
||||
calibrate: { type: 'string', short: 'c', default: '30' },
|
||||
threshold: { type: 'string', short: 't', default: '2.5' },
|
||||
json: { type: 'boolean', default: false },
|
||||
'consecutive-frames': { type: 'string', default: '3' },
|
||||
},
|
||||
strict: true,
|
||||
});
|
||||
|
||||
const PORT = parseInt(args.port, 10);
|
||||
const DURATION_MS = args.duration ? parseInt(args.duration, 10) * 1000 : null;
|
||||
const INTERVAL_MS = parseInt(args.interval, 10);
|
||||
const CALIBRATE_S = parseInt(args.calibrate, 10);
|
||||
const ALERT_THRESHOLD = parseFloat(args.threshold);
|
||||
const CONSECUTIVE_FRAMES = parseInt(args['consecutive-frames'], 10);
|
||||
const JSON_OUTPUT = args.json;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Constants
|
||||
// ---------------------------------------------------------------------------
|
||||
const CSI_MAGIC = 0xC5110001;
|
||||
const HEADER_SIZE = 20;
|
||||
|
||||
const CHANNEL_FREQ = {};
|
||||
for (let ch = 1; ch <= 13; ch++) CHANNEL_FREQ[ch] = 2412 + (ch - 1) * 5;
|
||||
|
||||
const NODE1_CHANNELS = [1, 6, 11];
|
||||
const NODE2_CHANNELS = [3, 5, 9];
|
||||
|
||||
// Channel penetration weights: lower freq = better wall penetration
|
||||
// Approximate wall loss at each channel for drywall+stud:
|
||||
// ch1 (2412 MHz) = 2.5 dB, ch11 (2462 MHz) = 2.7 dB
|
||||
// Weight inversely proportional to loss
|
||||
const PENETRATION_WEIGHT = {
|
||||
1: 1.00, // 2412 MHz - best penetration
|
||||
3: 0.96,
|
||||
5: 0.92,
|
||||
6: 0.90,
|
||||
9: 0.85,
|
||||
11: 0.80, // 2462 MHz - worst penetration
|
||||
};
|
||||
|
||||
// Status display
|
||||
const STATUS = {
|
||||
CALIBRATING: 'CALIBRATING',
|
||||
MONITORING: 'MONITORING',
|
||||
ALERT: 'ALERT',
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per-channel baseline
|
||||
// ---------------------------------------------------------------------------
|
||||
class ChannelBaseline {
|
||||
constructor(channel) {
|
||||
this.channel = channel;
|
||||
this.freqMhz = CHANNEL_FREQ[channel] || 2432;
|
||||
this.weight = PENETRATION_WEIGHT[channel] || 0.9;
|
||||
|
||||
// Welford online mean/variance
|
||||
this.nSub = 0;
|
||||
this.count = 0;
|
||||
this.mean = null; // Float64Array
|
||||
this.m2 = null; // Float64Array
|
||||
this.calibrated = false;
|
||||
}
|
||||
|
||||
/** Ingest a frame during calibration */
|
||||
calibrate(amplitudes) {
|
||||
const n = amplitudes.length;
|
||||
if (!this.mean) {
|
||||
this.nSub = n;
|
||||
this.mean = new Float64Array(n);
|
||||
this.m2 = new Float64Array(n);
|
||||
}
|
||||
|
||||
this.count++;
|
||||
for (let i = 0; i < n && i < this.nSub; i++) {
|
||||
const delta = amplitudes[i] - this.mean[i];
|
||||
this.mean[i] += delta / this.count;
|
||||
const delta2 = amplitudes[i] - this.mean[i];
|
||||
this.m2[i] += delta * delta2;
|
||||
}
|
||||
}
|
||||
|
||||
/** Finalize calibration */
|
||||
finalize() {
|
||||
if (this.count < 5) return;
|
||||
this.calibrated = true;
|
||||
}
|
||||
|
||||
/** Get standard deviation per subcarrier */
|
||||
getStd() {
|
||||
if (!this.mean || this.count < 2) return null;
|
||||
const std = new Float64Array(this.nSub);
|
||||
for (let i = 0; i < this.nSub; i++) {
|
||||
std[i] = Math.sqrt(this.m2[i] / (this.count - 1));
|
||||
// Minimum std to avoid division by zero
|
||||
if (std[i] < 0.1) std[i] = 0.1;
|
||||
}
|
||||
return std;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute deviation score for a new frame.
|
||||
* Score = mean(|amplitude - baseline_mean| / baseline_std) across subcarriers
|
||||
*/
|
||||
computeDeviation(amplitudes) {
|
||||
if (!this.calibrated || !this.mean) return 0;
|
||||
|
||||
const std = this.getStd();
|
||||
if (!std) return 0;
|
||||
|
||||
let sumDeviation = 0;
|
||||
let count = 0;
|
||||
for (let i = 0; i < amplitudes.length && i < this.nSub; i++) {
|
||||
const z = Math.abs(amplitudes[i] - this.mean[i]) / std[i];
|
||||
sumDeviation += z;
|
||||
count++;
|
||||
}
|
||||
|
||||
return count > 0 ? sumDeviation / count : 0;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Through-wall detector
|
||||
// ---------------------------------------------------------------------------
|
||||
class ThroughWallDetector {
|
||||
constructor(calibrateDuration, alertThreshold, consecutiveFrames) {
|
||||
this.calibrateDuration = calibrateDuration;
|
||||
this.alertThreshold = alertThreshold;
|
||||
this.consecutiveFrames = consecutiveFrames;
|
||||
|
||||
this.baselines = new Map(); // channel -> ChannelBaseline
|
||||
this.status = STATUS.CALIBRATING;
|
||||
this.startTime = null;
|
||||
|
||||
// Detection state
|
||||
this.perChannelScores = new Map();
|
||||
this.fusedScore = 0;
|
||||
this.alertStreak = 0;
|
||||
this.alertActive = false;
|
||||
this.alerts = [];
|
||||
|
||||
// History for display
|
||||
this.scoreHistory = []; // { timestamp, fusedScore, perChannel }
|
||||
this.maxHistory = 60;
|
||||
|
||||
this.totalFrames = 0;
|
||||
}
|
||||
|
||||
ingestFrame(channel, amplitudes, timestamp) {
|
||||
this.totalFrames++;
|
||||
|
||||
if (!this.startTime) this.startTime = timestamp;
|
||||
|
||||
// Get or create baseline
|
||||
if (!this.baselines.has(channel)) {
|
||||
this.baselines.set(channel, new ChannelBaseline(channel));
|
||||
}
|
||||
const baseline = this.baselines.get(channel);
|
||||
|
||||
// Calibration phase
|
||||
if (this.status === STATUS.CALIBRATING) {
|
||||
baseline.calibrate(amplitudes);
|
||||
|
||||
if (timestamp - this.startTime >= this.calibrateDuration) {
|
||||
// Finalize all baselines
|
||||
for (const bl of this.baselines.values()) bl.finalize();
|
||||
this.status = STATUS.MONITORING;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Detection phase
|
||||
const deviation = baseline.computeDeviation(amplitudes);
|
||||
const weight = PENETRATION_WEIGHT[channel] || 0.9;
|
||||
const weightedScore = deviation * weight;
|
||||
|
||||
this.perChannelScores.set(channel, {
|
||||
deviation: deviation,
|
||||
weighted: weightedScore,
|
||||
channel,
|
||||
freqMhz: CHANNEL_FREQ[channel],
|
||||
});
|
||||
|
||||
// Fused score: weighted average across all channels
|
||||
let sumWeighted = 0, sumWeights = 0;
|
||||
for (const [ch, score] of this.perChannelScores) {
|
||||
sumWeighted += score.weighted;
|
||||
sumWeights += PENETRATION_WEIGHT[ch] || 0.9;
|
||||
}
|
||||
this.fusedScore = sumWeights > 0 ? sumWeighted / sumWeights : 0;
|
||||
|
||||
// Cross-channel coherence: how many channels agree on motion?
|
||||
let agreeCount = 0;
|
||||
for (const score of this.perChannelScores.values()) {
|
||||
if (score.deviation > this.alertThreshold * 0.5) agreeCount++;
|
||||
}
|
||||
const coherence = this.perChannelScores.size > 0
|
||||
? agreeCount / this.perChannelScores.size
|
||||
: 0;
|
||||
|
||||
// Alert logic
|
||||
if (this.fusedScore > this.alertThreshold && coherence > 0.4) {
|
||||
this.alertStreak++;
|
||||
} else {
|
||||
this.alertStreak = Math.max(0, this.alertStreak - 1);
|
||||
}
|
||||
|
||||
const wasAlert = this.alertActive;
|
||||
this.alertActive = this.alertStreak >= this.consecutiveFrames;
|
||||
|
||||
if (this.alertActive && !wasAlert) {
|
||||
this.status = STATUS.ALERT;
|
||||
this.alerts.push({
|
||||
timestamp,
|
||||
fusedScore: this.fusedScore,
|
||||
coherence,
|
||||
channels: [...this.perChannelScores.values()].map(s => ({
|
||||
ch: s.channel, dev: s.deviation.toFixed(2),
|
||||
})),
|
||||
});
|
||||
} else if (!this.alertActive && wasAlert) {
|
||||
this.status = STATUS.MONITORING;
|
||||
}
|
||||
|
||||
// Store history
|
||||
this.scoreHistory.push({
|
||||
timestamp,
|
||||
fusedScore: this.fusedScore,
|
||||
coherence,
|
||||
perChannel: [...this.perChannelScores.entries()].map(([ch, s]) => ({
|
||||
ch, dev: s.deviation.toFixed(2), weight: (PENETRATION_WEIGHT[ch] || 0.9).toFixed(2),
|
||||
})),
|
||||
});
|
||||
if (this.scoreHistory.length > this.maxHistory) this.scoreHistory.shift();
|
||||
}
|
||||
|
||||
getState() {
|
||||
return {
|
||||
status: this.status,
|
||||
fusedScore: this.fusedScore,
|
||||
alertActive: this.alertActive,
|
||||
alertStreak: this.alertStreak,
|
||||
totalFrames: this.totalFrames,
|
||||
calibratedChannels: [...this.baselines.values()]
|
||||
.filter(b => b.calibrated)
|
||||
.map(b => b.channel)
|
||||
.sort((a, b) => a - b),
|
||||
perChannelScores: [...this.perChannelScores.entries()]
|
||||
.sort((a, b) => a[0] - b[0])
|
||||
.map(([ch, s]) => ({ ch, deviation: s.deviation.toFixed(2), weighted: s.weighted.toFixed(2) })),
|
||||
alertCount: this.alerts.length,
|
||||
scoreHistory: this.scoreHistory,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CSI parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
function parseIqHex(iqHex, nSubcarriers) {
|
||||
const bytes = Buffer.from(iqHex, 'hex');
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = 2 + sc * 2;
|
||||
if (offset + 1 >= bytes.length) break;
|
||||
let I = bytes[offset];
|
||||
let Q = bytes[offset + 1];
|
||||
if (I > 127) I -= 256;
|
||||
if (Q > 127) Q -= 256;
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
return amplitudes;
|
||||
}
|
||||
|
||||
function parseCSIFrame(buf) {
|
||||
if (buf.length < HEADER_SIZE) return null;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return null;
|
||||
|
||||
const nodeId = buf.readUInt8(4);
|
||||
const nSubcarriers = buf.readUInt16LE(6);
|
||||
const freqMhz = buf.readUInt32LE(8);
|
||||
|
||||
const amplitudes = new Float64Array(nSubcarriers);
|
||||
for (let sc = 0; sc < nSubcarriers; sc++) {
|
||||
const offset = HEADER_SIZE + sc * 2;
|
||||
if (offset + 1 >= buf.length) break;
|
||||
const I = buf.readInt8(offset);
|
||||
const Q = buf.readInt8(offset + 1);
|
||||
amplitudes[sc] = Math.sqrt(I * I + Q * Q);
|
||||
}
|
||||
|
||||
let channel = 0;
|
||||
if (freqMhz >= 2412 && freqMhz <= 2484) {
|
||||
channel = freqMhz === 2484 ? 14 : Math.round((freqMhz - 2412) / 5) + 1;
|
||||
}
|
||||
|
||||
return { nodeId, nSubcarriers, freqMhz, amplitudes, channel };
|
||||
}
|
||||
|
||||
const nodeChannelIdx = { 1: 0, 2: 0 };
|
||||
function assignChannel(nodeId) {
|
||||
const channels = nodeId === 1 ? NODE1_CHANNELS : NODE2_CHANNELS;
|
||||
const ch = channels[nodeChannelIdx[nodeId] % channels.length];
|
||||
nodeChannelIdx[nodeId]++;
|
||||
return ch;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Visualization
|
||||
// ---------------------------------------------------------------------------
|
||||
function renderStatus(detector) {
|
||||
const state = detector.getState();
|
||||
const lines = [];
|
||||
|
||||
lines.push('');
|
||||
lines.push(' THROUGH-WALL MOTION DETECTOR');
|
||||
lines.push(' ' + '='.repeat(55));
|
||||
lines.push('');
|
||||
|
||||
// Status banner
|
||||
const statusBanner = {
|
||||
[STATUS.CALIBRATING]: ' [ CALIBRATING ] Establishing wall baseline...',
|
||||
[STATUS.MONITORING]: ' [ MONITORING ] Watching for through-wall motion',
|
||||
[STATUS.ALERT]: ' [ ** ALERT ** ] Motion detected behind wall!',
|
||||
};
|
||||
lines.push(statusBanner[state.status] || ` [ ${state.status} ]`);
|
||||
lines.push('');
|
||||
|
||||
if (state.status === STATUS.CALIBRATING) {
|
||||
const progress = Math.min(100, (state.totalFrames / (CALIBRATE_S * 12)) * 100);
|
||||
const barLen = Math.floor(progress / 2);
|
||||
const bar = '\u2588'.repeat(barLen) + '\u2591'.repeat(50 - barLen);
|
||||
lines.push(` Calibration progress: [${bar}] ${progress.toFixed(0)}%`);
|
||||
lines.push(` Frames collected: ${state.totalFrames}`);
|
||||
lines.push(` Channels: ${state.calibratedChannels.length > 0 ? state.calibratedChannels.join(', ') : 'accumulating...'}`);
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// Fused score meter
|
||||
const maxMeter = 40;
|
||||
const meterFill = Math.min(maxMeter, Math.floor((state.fusedScore / (ALERT_THRESHOLD * 2)) * maxMeter));
|
||||
const meterChar = state.alertActive ? '\u2588' : '\u2593';
|
||||
const meterEmpty = '\u2591';
|
||||
const meter = meterChar.repeat(meterFill) + meterEmpty.repeat(maxMeter - meterFill);
|
||||
const threshMark = Math.floor((ALERT_THRESHOLD / (ALERT_THRESHOLD * 2)) * maxMeter);
|
||||
lines.push(` Fused score: [${meter}] ${state.fusedScore.toFixed(2)}`);
|
||||
lines.push(` ${''.padStart(15 + threshMark)}^ threshold=${ALERT_THRESHOLD}`);
|
||||
|
||||
// Per-channel breakdown
|
||||
lines.push('');
|
||||
lines.push(' Per-Channel Deviation (weighted by penetration quality):');
|
||||
lines.push(' ' + '-'.repeat(55));
|
||||
lines.push(' Ch Freq(MHz) Weight Deviation Weighted Status');
|
||||
|
||||
for (const score of state.perChannelScores) {
|
||||
const ch = score.ch;
|
||||
const freq = CHANNEL_FREQ[ch] || 0;
|
||||
const wt = (PENETRATION_WEIGHT[ch] || 0.9).toFixed(2);
|
||||
const dev = score.deviation;
|
||||
const wtd = score.weighted;
|
||||
const above = parseFloat(dev) > ALERT_THRESHOLD * 0.5;
|
||||
const marker = above ? ' <--' : '';
|
||||
lines.push(` ${String(ch).padStart(2)} ${freq} ${wt} ${dev.padStart(6)} ${wtd.padStart(6)} ${marker}`);
|
||||
}
|
||||
|
||||
// Score timeline (last 30 readings)
|
||||
const history = state.scoreHistory.slice(-30);
|
||||
if (history.length > 0) {
|
||||
lines.push('');
|
||||
lines.push(' Score Timeline (last 30 readings):');
|
||||
const SPARK = '\u2581\u2582\u2583\u2584\u2585\u2586\u2587\u2588';
|
||||
let timeline = ' ';
|
||||
for (const h of history) {
|
||||
const level = Math.min(7, Math.floor((h.fusedScore / (ALERT_THRESHOLD * 2)) * 7.99));
|
||||
timeline += SPARK[level];
|
||||
}
|
||||
lines.push(timeline);
|
||||
lines.push(` ${''.padStart(2)}${'oldest'.padEnd(15)}${''.padEnd(Math.max(0, history.length - 21))}newest`);
|
||||
}
|
||||
|
||||
// Alert summary
|
||||
lines.push('');
|
||||
lines.push(` Alert history: ${state.alertCount} alert(s)`);
|
||||
lines.push(` Consecutive frames above threshold: ${state.alertStreak}/${CONSECUTIVE_FRAMES}`);
|
||||
lines.push(` Calibrated channels: ${state.calibratedChannels.join(', ')}`);
|
||||
lines.push(` Total frames: ${state.totalFrames}`);
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Global state
|
||||
// ---------------------------------------------------------------------------
|
||||
const detector = new ThroughWallDetector(CALIBRATE_S, ALERT_THRESHOLD, CONSECUTIVE_FRAMES);
|
||||
let lastDisplayMs = 0;
|
||||
|
||||
function displayUpdate() {
|
||||
const state = detector.getState();
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
timestamp: Date.now() / 1000,
|
||||
status: state.status,
|
||||
fusedScore: +state.fusedScore.toFixed(3),
|
||||
alertActive: state.alertActive,
|
||||
perChannel: state.perChannelScores,
|
||||
alertCount: state.alertCount,
|
||||
}));
|
||||
} else {
|
||||
process.stdout.write('\x1B[2J\x1B[H');
|
||||
console.log(renderStatus(detector));
|
||||
console.log('');
|
||||
console.log(' Press Ctrl+C to exit');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live mode
|
||||
// ---------------------------------------------------------------------------
|
||||
function startLive() {
|
||||
const sock = dgram.createSocket('udp4');
|
||||
|
||||
sock.on('message', (buf) => {
|
||||
if (buf.length < 4) return;
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic !== CSI_MAGIC) return;
|
||||
|
||||
const frame = parseCSIFrame(buf);
|
||||
if (!frame) return;
|
||||
|
||||
detector.ingestFrame(frame.channel, frame.amplitudes, Date.now() / 1000);
|
||||
|
||||
const now = Date.now();
|
||||
if (now - lastDisplayMs >= INTERVAL_MS) {
|
||||
displayUpdate();
|
||||
lastDisplayMs = now;
|
||||
}
|
||||
});
|
||||
|
||||
sock.bind(PORT, () => {
|
||||
if (!JSON_OUTPUT) {
|
||||
console.log(`Through-Wall Detector listening on UDP port ${PORT}`);
|
||||
console.log(`Calibration period: ${CALIBRATE_S}s`);
|
||||
console.log(`Alert threshold: ${ALERT_THRESHOLD}`);
|
||||
console.log('Waiting for CSI frames...');
|
||||
}
|
||||
});
|
||||
|
||||
if (DURATION_MS) {
|
||||
setTimeout(() => { displayUpdate(); sock.close(); process.exit(0); }, DURATION_MS);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Replay mode
|
||||
// ---------------------------------------------------------------------------
|
||||
async function startReplay(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File not found: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(filePath),
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let frameCount = 0;
|
||||
let lastAnalysisTs = 0;
|
||||
let windowCount = 0;
|
||||
let firstAlertTs = null;
|
||||
let totalAlertWindows = 0;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (!line.trim()) continue;
|
||||
|
||||
let record;
|
||||
try { record = JSON.parse(line); } catch { continue; }
|
||||
if (record.type !== 'raw_csi' || !record.iq_hex) continue;
|
||||
|
||||
const amplitudes = parseIqHex(record.iq_hex, record.subcarriers || 64);
|
||||
const channel = record.channel || assignChannel(record.node_id);
|
||||
|
||||
detector.ingestFrame(channel, amplitudes, record.timestamp);
|
||||
frameCount++;
|
||||
|
||||
const tsMs = record.timestamp * 1000;
|
||||
if (lastAnalysisTs === 0) lastAnalysisTs = tsMs;
|
||||
|
||||
if (tsMs - lastAnalysisTs >= INTERVAL_MS) {
|
||||
windowCount++;
|
||||
const state = detector.getState();
|
||||
|
||||
if (state.alertActive) {
|
||||
totalAlertWindows++;
|
||||
if (!firstAlertTs) firstAlertTs = record.timestamp;
|
||||
}
|
||||
|
||||
if (JSON_OUTPUT) {
|
||||
console.log(JSON.stringify({
|
||||
window: windowCount,
|
||||
timestamp: record.timestamp,
|
||||
status: state.status,
|
||||
fusedScore: +state.fusedScore.toFixed(3),
|
||||
alertActive: state.alertActive,
|
||||
}));
|
||||
} else {
|
||||
const statusTag = state.status === STATUS.ALERT ? ' ** ALERT **' :
|
||||
state.status === STATUS.CALIBRATING ? ' calibrating' : '';
|
||||
console.log(
|
||||
` [${windowCount.toString().padStart(4)}] t=${record.timestamp.toFixed(1)}s` +
|
||||
` score=${state.fusedScore.toFixed(2).padStart(5)}` +
|
||||
` channels=${state.calibratedChannels.length}` +
|
||||
` streak=${state.alertStreak}/${CONSECUTIVE_FRAMES}` +
|
||||
statusTag
|
||||
);
|
||||
}
|
||||
|
||||
lastAnalysisTs = tsMs;
|
||||
}
|
||||
}
|
||||
|
||||
// Final summary
|
||||
if (!JSON_OUTPUT) {
|
||||
const state = detector.getState();
|
||||
console.log('');
|
||||
console.log('='.repeat(60));
|
||||
console.log('THROUGH-WALL DETECTION SUMMARY');
|
||||
console.log('='.repeat(60));
|
||||
console.log(` Total frames: ${frameCount}`);
|
||||
console.log(` Analysis windows: ${windowCount}`);
|
||||
console.log(` Calibrated channels: ${state.calibratedChannels.join(', ')}`);
|
||||
console.log(` Alert windows: ${totalAlertWindows} / ${windowCount} (${windowCount > 0 ? (totalAlertWindows / windowCount * 100).toFixed(1) : 0}%)`);
|
||||
console.log(` Total alerts: ${state.alertCount}`);
|
||||
if (firstAlertTs) {
|
||||
console.log(` First alert at: t=${firstAlertTs.toFixed(1)}s`);
|
||||
}
|
||||
console.log(` Threshold: ${ALERT_THRESHOLD}, Consecutive frames: ${CONSECUTIVE_FRAMES}`);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entry point
|
||||
// ---------------------------------------------------------------------------
|
||||
if (args.replay) {
|
||||
startReplay(args.replay);
|
||||
} else {
|
||||
startLive();
|
||||
}
|
||||
Reference in New Issue
Block a user