feat: Add commodity sensing, proof bundle, Three.js viz, mock isolation
Commodity Sensing Module (ADR-013): - sensing/rssi_collector.py: Real Linux WiFi RSSI collection from /proc/net/wireless and iw commands, with SimulatedCollector for testing - sensing/feature_extractor.py: FFT-based spectral analysis, CUSUM change-point detection, breathing/motion band power extraction - sensing/classifier.py: Rule-based presence/motion classification with confidence scoring and multi-receiver agreement - sensing/backend.py: Common SensingBackend protocol with honest capability reporting (PRESENCE + MOTION only for commodity) Proof of Reality Bundle (ADR-011): - data/proof/generate_reference_signal.py: Deterministic synthetic CSI with known breathing (0.3 Hz) and walking (1.2 Hz) signals - data/proof/sample_csi_data.json: Generated reference signal - data/proof/verify.py: One-command pipeline verification with SHA-256 - data/proof/expected_features.sha256: Expected output hash Three.js Visualization: - ui/components/scene.js: 3D scene setup with OrbitControls Mock Isolation: - testing/mock_pose_generator.py: Mock pose generation moved out of production pose_service.py - services/pose_service.py: Cleaned mock paths https://claude.ai/code/session_01Ki7pvEZtJDvqJkmyn6B714
This commit is contained in:
164
v1/src/sensing/backend.py
Normal file
164
v1/src/sensing/backend.py
Normal file
@@ -0,0 +1,164 @@
|
||||
"""
|
||||
Common sensing backend interface.
|
||||
|
||||
Defines the ``SensingBackend`` protocol and the ``CommodityBackend`` concrete
|
||||
implementation that wires together the RSSI collector, feature extractor, and
|
||||
classifier into a single coherent pipeline.
|
||||
|
||||
The ``Capability`` enum enumerates all possible sensing capabilities. The
|
||||
``CommodityBackend`` honestly reports that it supports only PRESENCE and MOTION.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from enum import Enum, auto
|
||||
from typing import List, Optional, Protocol, Set, runtime_checkable
|
||||
|
||||
from v1.src.sensing.classifier import MotionLevel, PresenceClassifier, SensingResult
|
||||
from v1.src.sensing.feature_extractor import RssiFeatureExtractor, RssiFeatures
|
||||
from v1.src.sensing.rssi_collector import (
|
||||
LinuxWifiCollector,
|
||||
SimulatedCollector,
|
||||
WifiCollector,
|
||||
WifiSample,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Capability enum
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class Capability(Enum):
|
||||
"""All possible sensing capabilities across backend tiers."""
|
||||
|
||||
PRESENCE = auto()
|
||||
MOTION = auto()
|
||||
RESPIRATION = auto()
|
||||
LOCATION = auto()
|
||||
POSE = auto()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Backend protocol
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@runtime_checkable
|
||||
class SensingBackend(Protocol):
|
||||
"""Protocol that all sensing backends must implement."""
|
||||
|
||||
def get_features(self) -> RssiFeatures:
|
||||
"""Extract current features from the sensing pipeline."""
|
||||
...
|
||||
|
||||
def get_capabilities(self) -> Set[Capability]:
|
||||
"""Return the set of capabilities this backend supports."""
|
||||
...
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Commodity backend
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class CommodityBackend:
|
||||
"""
|
||||
RSSI-based commodity sensing backend.
|
||||
|
||||
Wires together:
|
||||
- A WiFi collector (real or simulated)
|
||||
- An RSSI feature extractor
|
||||
- A presence/motion classifier
|
||||
|
||||
Capabilities: PRESENCE and MOTION only.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
collector : WifiCollector-compatible object
|
||||
The data source (LinuxWifiCollector or SimulatedCollector).
|
||||
extractor : RssiFeatureExtractor, optional
|
||||
Feature extractor (created with defaults if not provided).
|
||||
classifier : PresenceClassifier, optional
|
||||
Classifier (created with defaults if not provided).
|
||||
"""
|
||||
|
||||
SUPPORTED_CAPABILITIES: Set[Capability] = frozenset(
|
||||
{Capability.PRESENCE, Capability.MOTION}
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
collector: LinuxWifiCollector | SimulatedCollector,
|
||||
extractor: Optional[RssiFeatureExtractor] = None,
|
||||
classifier: Optional[PresenceClassifier] = None,
|
||||
) -> None:
|
||||
self._collector = collector
|
||||
self._extractor = extractor or RssiFeatureExtractor()
|
||||
self._classifier = classifier or PresenceClassifier()
|
||||
|
||||
@property
|
||||
def collector(self) -> LinuxWifiCollector | SimulatedCollector:
|
||||
return self._collector
|
||||
|
||||
@property
|
||||
def extractor(self) -> RssiFeatureExtractor:
|
||||
return self._extractor
|
||||
|
||||
@property
|
||||
def classifier(self) -> PresenceClassifier:
|
||||
return self._classifier
|
||||
|
||||
# -- SensingBackend protocol ---------------------------------------------
|
||||
|
||||
def get_features(self) -> RssiFeatures:
|
||||
"""
|
||||
Get current features from the latest collected samples.
|
||||
|
||||
Uses the extractor's window_seconds to determine how many samples
|
||||
to pull from the collector's ring buffer.
|
||||
"""
|
||||
window = self._extractor.window_seconds
|
||||
sample_rate = self._collector.sample_rate_hz
|
||||
n_needed = int(window * sample_rate)
|
||||
samples = self._collector.get_samples(n=n_needed)
|
||||
return self._extractor.extract(samples)
|
||||
|
||||
def get_capabilities(self) -> Set[Capability]:
|
||||
"""CommodityBackend supports PRESENCE and MOTION only."""
|
||||
return set(self.SUPPORTED_CAPABILITIES)
|
||||
|
||||
# -- convenience methods -------------------------------------------------
|
||||
|
||||
def get_result(self) -> SensingResult:
|
||||
"""
|
||||
Run the full pipeline: collect -> extract -> classify.
|
||||
|
||||
Returns
|
||||
-------
|
||||
SensingResult
|
||||
Classification result with motion level and confidence.
|
||||
"""
|
||||
features = self.get_features()
|
||||
return self._classifier.classify(features)
|
||||
|
||||
def start(self) -> None:
|
||||
"""Start the underlying collector."""
|
||||
self._collector.start()
|
||||
logger.info(
|
||||
"CommodityBackend started (capabilities: %s)",
|
||||
", ".join(c.name for c in self.SUPPORTED_CAPABILITIES),
|
||||
)
|
||||
|
||||
def stop(self) -> None:
|
||||
"""Stop the underlying collector."""
|
||||
self._collector.stop()
|
||||
logger.info("CommodityBackend stopped")
|
||||
|
||||
def is_capable(self, capability: Capability) -> bool:
|
||||
"""Check whether this backend supports a specific capability."""
|
||||
return capability in self.SUPPORTED_CAPABILITIES
|
||||
|
||||
def __repr__(self) -> str:
|
||||
caps = ", ".join(c.name for c in sorted(self.SUPPORTED_CAPABILITIES, key=lambda c: c.value))
|
||||
return f"CommodityBackend(capabilities=[{caps}])"
|
||||
201
v1/src/sensing/classifier.py
Normal file
201
v1/src/sensing/classifier.py
Normal file
@@ -0,0 +1,201 @@
|
||||
"""
|
||||
Presence and motion classification from RSSI features.
|
||||
|
||||
Uses rule-based logic with configurable thresholds to classify the current
|
||||
sensing state into one of three motion levels:
|
||||
ABSENT -- no person detected
|
||||
PRESENT_STILL -- person present but stationary
|
||||
ACTIVE -- person present and moving
|
||||
|
||||
Confidence is derived from spectral feature strength and optional
|
||||
cross-receiver agreement.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from typing import List, Optional
|
||||
|
||||
from v1.src.sensing.feature_extractor import RssiFeatures
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MotionLevel(Enum):
|
||||
"""Classified motion state."""
|
||||
|
||||
ABSENT = "absent"
|
||||
PRESENT_STILL = "present_still"
|
||||
ACTIVE = "active"
|
||||
|
||||
|
||||
@dataclass
|
||||
class SensingResult:
|
||||
"""Output of the presence/motion classifier."""
|
||||
|
||||
motion_level: MotionLevel
|
||||
confidence: float # 0.0 to 1.0
|
||||
presence_detected: bool
|
||||
rssi_variance: float
|
||||
motion_band_energy: float
|
||||
breathing_band_energy: float
|
||||
n_change_points: int
|
||||
details: str = ""
|
||||
|
||||
|
||||
class PresenceClassifier:
|
||||
"""
|
||||
Rule-based presence and motion classifier.
|
||||
|
||||
Classification rules
|
||||
--------------------
|
||||
1. **Presence**: RSSI variance exceeds ``presence_variance_threshold``.
|
||||
2. **Motion level**:
|
||||
- ABSENT if variance < presence threshold
|
||||
- ACTIVE if variance >= presence threshold AND motion band energy
|
||||
exceeds ``motion_energy_threshold``
|
||||
- PRESENT_STILL otherwise (variance above threshold but low motion energy)
|
||||
|
||||
Confidence model
|
||||
----------------
|
||||
Base confidence comes from how far the measured variance / energy exceeds
|
||||
the respective thresholds. Cross-receiver agreement (when multiple
|
||||
receivers report results) can boost confidence further.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
presence_variance_threshold : float
|
||||
Minimum RSSI variance (dBm^2) to declare presence (default 0.5).
|
||||
motion_energy_threshold : float
|
||||
Minimum motion-band spectral energy to classify as ACTIVE (default 0.1).
|
||||
max_receivers : int
|
||||
Maximum number of receivers for cross-receiver agreement (default 1).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
presence_variance_threshold: float = 0.5,
|
||||
motion_energy_threshold: float = 0.1,
|
||||
max_receivers: int = 1,
|
||||
) -> None:
|
||||
self._var_thresh = presence_variance_threshold
|
||||
self._motion_thresh = motion_energy_threshold
|
||||
self._max_receivers = max_receivers
|
||||
|
||||
@property
|
||||
def presence_variance_threshold(self) -> float:
|
||||
return self._var_thresh
|
||||
|
||||
@property
|
||||
def motion_energy_threshold(self) -> float:
|
||||
return self._motion_thresh
|
||||
|
||||
def classify(
|
||||
self,
|
||||
features: RssiFeatures,
|
||||
other_receiver_results: Optional[List[SensingResult]] = None,
|
||||
) -> SensingResult:
|
||||
"""
|
||||
Classify presence and motion from extracted RSSI features.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
features : RssiFeatures
|
||||
Features extracted from the RSSI time series of one receiver.
|
||||
other_receiver_results : list of SensingResult, optional
|
||||
Results from other receivers for cross-receiver agreement.
|
||||
|
||||
Returns
|
||||
-------
|
||||
SensingResult
|
||||
"""
|
||||
variance = features.variance
|
||||
motion_energy = features.motion_band_power
|
||||
breathing_energy = features.breathing_band_power
|
||||
|
||||
# -- presence decision ------------------------------------------------
|
||||
presence = variance >= self._var_thresh
|
||||
|
||||
# -- motion level -----------------------------------------------------
|
||||
if not presence:
|
||||
level = MotionLevel.ABSENT
|
||||
elif motion_energy >= self._motion_thresh:
|
||||
level = MotionLevel.ACTIVE
|
||||
else:
|
||||
level = MotionLevel.PRESENT_STILL
|
||||
|
||||
# -- confidence -------------------------------------------------------
|
||||
confidence = self._compute_confidence(
|
||||
variance, motion_energy, breathing_energy, level, other_receiver_results
|
||||
)
|
||||
|
||||
# -- detail string ----------------------------------------------------
|
||||
details = (
|
||||
f"var={variance:.4f} (thresh={self._var_thresh}), "
|
||||
f"motion_energy={motion_energy:.4f} (thresh={self._motion_thresh}), "
|
||||
f"breathing_energy={breathing_energy:.4f}, "
|
||||
f"change_points={features.n_change_points}"
|
||||
)
|
||||
|
||||
return SensingResult(
|
||||
motion_level=level,
|
||||
confidence=confidence,
|
||||
presence_detected=presence,
|
||||
rssi_variance=variance,
|
||||
motion_band_energy=motion_energy,
|
||||
breathing_band_energy=breathing_energy,
|
||||
n_change_points=features.n_change_points,
|
||||
details=details,
|
||||
)
|
||||
|
||||
def _compute_confidence(
|
||||
self,
|
||||
variance: float,
|
||||
motion_energy: float,
|
||||
breathing_energy: float,
|
||||
level: MotionLevel,
|
||||
other_results: Optional[List[SensingResult]],
|
||||
) -> float:
|
||||
"""
|
||||
Compute a confidence score in [0, 1].
|
||||
|
||||
The score is composed of:
|
||||
- Base (60%): how clearly the variance exceeds (or falls below) the
|
||||
presence threshold.
|
||||
- Spectral (20%): strength of the relevant spectral band.
|
||||
- Agreement (20%): cross-receiver consensus (if available).
|
||||
"""
|
||||
# -- base confidence (0..1) ------------------------------------------
|
||||
if level == MotionLevel.ABSENT:
|
||||
# Confidence in absence increases as variance shrinks relative to threshold
|
||||
if self._var_thresh > 0:
|
||||
base = max(0.0, 1.0 - variance / self._var_thresh)
|
||||
else:
|
||||
base = 1.0
|
||||
else:
|
||||
# Confidence in presence increases as variance exceeds threshold
|
||||
ratio = variance / self._var_thresh if self._var_thresh > 0 else 10.0
|
||||
base = min(1.0, ratio)
|
||||
|
||||
# -- spectral confidence (0..1) --------------------------------------
|
||||
if level == MotionLevel.ACTIVE:
|
||||
spectral = min(1.0, motion_energy / max(self._motion_thresh, 1e-12))
|
||||
elif level == MotionLevel.PRESENT_STILL:
|
||||
# For still, breathing band energy is more relevant
|
||||
spectral = min(1.0, breathing_energy / max(self._motion_thresh, 1e-12))
|
||||
else:
|
||||
spectral = 1.0 # No spectral requirement for absence
|
||||
|
||||
# -- cross-receiver agreement (0..1) ---------------------------------
|
||||
agreement = 1.0 # default: single receiver
|
||||
if other_results:
|
||||
same_level = sum(
|
||||
1 for r in other_results if r.motion_level == level
|
||||
)
|
||||
agreement = (same_level + 1) / (len(other_results) + 1)
|
||||
|
||||
# Weighted combination
|
||||
confidence = 0.6 * base + 0.2 * spectral + 0.2 * agreement
|
||||
return max(0.0, min(1.0, confidence))
|
||||
312
v1/src/sensing/feature_extractor.py
Normal file
312
v1/src/sensing/feature_extractor.py
Normal file
@@ -0,0 +1,312 @@
|
||||
"""
|
||||
Signal feature extraction from RSSI time series.
|
||||
|
||||
Extracts both time-domain statistical features and frequency-domain spectral
|
||||
features using real mathematics (scipy.fft, scipy.stats). Also implements
|
||||
CUSUM change-point detection for abrupt RSSI transitions.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
import numpy as np
|
||||
from numpy.typing import NDArray
|
||||
from scipy import fft as scipy_fft
|
||||
from scipy import stats as scipy_stats
|
||||
|
||||
from v1.src.sensing.rssi_collector import WifiSample
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Feature dataclass
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@dataclass
|
||||
class RssiFeatures:
|
||||
"""Container for all extracted RSSI features."""
|
||||
|
||||
# -- time-domain --------------------------------------------------------
|
||||
mean: float = 0.0
|
||||
variance: float = 0.0
|
||||
std: float = 0.0
|
||||
skewness: float = 0.0
|
||||
kurtosis: float = 0.0
|
||||
range: float = 0.0
|
||||
iqr: float = 0.0 # inter-quartile range
|
||||
|
||||
# -- frequency-domain ---------------------------------------------------
|
||||
dominant_freq_hz: float = 0.0
|
||||
breathing_band_power: float = 0.0 # 0.1 - 0.5 Hz
|
||||
motion_band_power: float = 0.0 # 0.5 - 3.0 Hz
|
||||
total_spectral_power: float = 0.0
|
||||
|
||||
# -- change-point -------------------------------------------------------
|
||||
change_points: List[int] = field(default_factory=list)
|
||||
n_change_points: int = 0
|
||||
|
||||
# -- metadata -----------------------------------------------------------
|
||||
n_samples: int = 0
|
||||
duration_seconds: float = 0.0
|
||||
sample_rate_hz: float = 0.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Feature extractor
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class RssiFeatureExtractor:
|
||||
"""
|
||||
Extract time-domain and frequency-domain features from an RSSI time series.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
window_seconds : float
|
||||
Length of the analysis window in seconds (default 30).
|
||||
cusum_threshold : float
|
||||
CUSUM threshold for change-point detection (default 3.0 standard deviations
|
||||
of the signal).
|
||||
cusum_drift : float
|
||||
CUSUM drift allowance (default 0.5 standard deviations).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
window_seconds: float = 30.0,
|
||||
cusum_threshold: float = 3.0,
|
||||
cusum_drift: float = 0.5,
|
||||
) -> None:
|
||||
self._window_seconds = window_seconds
|
||||
self._cusum_threshold = cusum_threshold
|
||||
self._cusum_drift = cusum_drift
|
||||
|
||||
@property
|
||||
def window_seconds(self) -> float:
|
||||
return self._window_seconds
|
||||
|
||||
def extract(self, samples: List[WifiSample]) -> RssiFeatures:
|
||||
"""
|
||||
Extract features from a list of WifiSample objects.
|
||||
|
||||
Only the most recent ``window_seconds`` of data are used.
|
||||
At least 4 samples are required for meaningful features.
|
||||
"""
|
||||
if len(samples) < 4:
|
||||
logger.warning(
|
||||
"Not enough samples for feature extraction (%d < 4)", len(samples)
|
||||
)
|
||||
return RssiFeatures(n_samples=len(samples))
|
||||
|
||||
# Trim to window
|
||||
samples = self._trim_to_window(samples)
|
||||
rssi = np.array([s.rssi_dbm for s in samples], dtype=np.float64)
|
||||
timestamps = np.array([s.timestamp for s in samples], dtype=np.float64)
|
||||
|
||||
# Estimate sample rate from actual timestamps
|
||||
dt = np.diff(timestamps)
|
||||
if len(dt) == 0 or np.mean(dt) <= 0:
|
||||
sample_rate = 10.0 # fallback
|
||||
else:
|
||||
sample_rate = 1.0 / np.mean(dt)
|
||||
|
||||
duration = timestamps[-1] - timestamps[0] if len(timestamps) > 1 else 0.0
|
||||
|
||||
# Build features
|
||||
features = RssiFeatures(
|
||||
n_samples=len(rssi),
|
||||
duration_seconds=float(duration),
|
||||
sample_rate_hz=float(sample_rate),
|
||||
)
|
||||
|
||||
self._compute_time_domain(rssi, features)
|
||||
self._compute_frequency_domain(rssi, sample_rate, features)
|
||||
self._compute_change_points(rssi, features)
|
||||
|
||||
return features
|
||||
|
||||
def extract_from_array(
|
||||
self, rssi: NDArray[np.float64], sample_rate_hz: float
|
||||
) -> RssiFeatures:
|
||||
"""
|
||||
Extract features directly from a numpy array (useful for testing).
|
||||
|
||||
Parameters
|
||||
----------
|
||||
rssi : ndarray
|
||||
1-D array of RSSI values in dBm.
|
||||
sample_rate_hz : float
|
||||
Sampling rate in Hz.
|
||||
"""
|
||||
if len(rssi) < 4:
|
||||
return RssiFeatures(n_samples=len(rssi))
|
||||
|
||||
duration = len(rssi) / sample_rate_hz
|
||||
|
||||
features = RssiFeatures(
|
||||
n_samples=len(rssi),
|
||||
duration_seconds=float(duration),
|
||||
sample_rate_hz=float(sample_rate_hz),
|
||||
)
|
||||
|
||||
self._compute_time_domain(rssi, features)
|
||||
self._compute_frequency_domain(rssi, sample_rate_hz, features)
|
||||
self._compute_change_points(rssi, features)
|
||||
|
||||
return features
|
||||
|
||||
# -- time-domain ---------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _compute_time_domain(rssi: NDArray[np.float64], features: RssiFeatures) -> None:
|
||||
features.mean = float(np.mean(rssi))
|
||||
features.variance = float(np.var(rssi, ddof=1)) if len(rssi) > 1 else 0.0
|
||||
features.std = float(np.std(rssi, ddof=1)) if len(rssi) > 1 else 0.0
|
||||
features.skewness = float(scipy_stats.skew(rssi, bias=False)) if len(rssi) > 2 else 0.0
|
||||
features.kurtosis = float(scipy_stats.kurtosis(rssi, bias=False)) if len(rssi) > 3 else 0.0
|
||||
features.range = float(np.ptp(rssi))
|
||||
|
||||
q75, q25 = np.percentile(rssi, [75, 25])
|
||||
features.iqr = float(q75 - q25)
|
||||
|
||||
# -- frequency-domain ----------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _compute_frequency_domain(
|
||||
rssi: NDArray[np.float64],
|
||||
sample_rate: float,
|
||||
features: RssiFeatures,
|
||||
) -> None:
|
||||
"""Compute one-sided FFT power spectrum and extract band powers."""
|
||||
n = len(rssi)
|
||||
if n < 4:
|
||||
return
|
||||
|
||||
# Remove DC (subtract mean)
|
||||
signal = rssi - np.mean(rssi)
|
||||
|
||||
# Apply Hann window to reduce spectral leakage
|
||||
window = np.hanning(n)
|
||||
windowed = signal * window
|
||||
|
||||
# Compute real FFT
|
||||
fft_vals = scipy_fft.rfft(windowed)
|
||||
freqs = scipy_fft.rfftfreq(n, d=1.0 / sample_rate)
|
||||
|
||||
# Power spectral density (magnitude squared, normalised by N)
|
||||
psd = (np.abs(fft_vals) ** 2) / n
|
||||
|
||||
# Skip DC component (index 0)
|
||||
if len(freqs) > 1:
|
||||
freqs_no_dc = freqs[1:]
|
||||
psd_no_dc = psd[1:]
|
||||
else:
|
||||
return
|
||||
|
||||
# Total spectral power
|
||||
features.total_spectral_power = float(np.sum(psd_no_dc))
|
||||
|
||||
# Dominant frequency
|
||||
if len(psd_no_dc) > 0:
|
||||
peak_idx = int(np.argmax(psd_no_dc))
|
||||
features.dominant_freq_hz = float(freqs_no_dc[peak_idx])
|
||||
|
||||
# Band powers
|
||||
features.breathing_band_power = float(
|
||||
_band_power(freqs_no_dc, psd_no_dc, 0.1, 0.5)
|
||||
)
|
||||
features.motion_band_power = float(
|
||||
_band_power(freqs_no_dc, psd_no_dc, 0.5, 3.0)
|
||||
)
|
||||
|
||||
# -- change-point detection (CUSUM) --------------------------------------
|
||||
|
||||
def _compute_change_points(
|
||||
self, rssi: NDArray[np.float64], features: RssiFeatures
|
||||
) -> None:
|
||||
"""
|
||||
Detect change points using the CUSUM algorithm.
|
||||
|
||||
The CUSUM statistic tracks cumulative deviations from the mean,
|
||||
flagging points where the signal mean shifts abruptly.
|
||||
"""
|
||||
if len(rssi) < 4:
|
||||
return
|
||||
|
||||
mean_val = np.mean(rssi)
|
||||
std_val = np.std(rssi, ddof=1)
|
||||
if std_val < 1e-12:
|
||||
features.change_points = []
|
||||
features.n_change_points = 0
|
||||
return
|
||||
|
||||
threshold = self._cusum_threshold * std_val
|
||||
drift = self._cusum_drift * std_val
|
||||
|
||||
change_points = cusum_detect(rssi, mean_val, threshold, drift)
|
||||
features.change_points = change_points
|
||||
features.n_change_points = len(change_points)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helper functions
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _band_power(
|
||||
freqs: NDArray[np.float64],
|
||||
psd: NDArray[np.float64],
|
||||
low_hz: float,
|
||||
high_hz: float,
|
||||
) -> float:
|
||||
"""Sum PSD within a frequency band [low_hz, high_hz]."""
|
||||
mask = (freqs >= low_hz) & (freqs <= high_hz)
|
||||
return float(np.sum(psd[mask]))
|
||||
|
||||
|
||||
def cusum_detect(
|
||||
signal: NDArray[np.float64],
|
||||
target: float,
|
||||
threshold: float,
|
||||
drift: float,
|
||||
) -> List[int]:
|
||||
"""
|
||||
CUSUM (cumulative sum) change-point detection.
|
||||
|
||||
Detects both upward and downward shifts in the signal mean.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
signal : ndarray
|
||||
The 1-D signal to analyse.
|
||||
target : float
|
||||
Expected mean of the signal.
|
||||
threshold : float
|
||||
Decision threshold for declaring a change point.
|
||||
drift : float
|
||||
Allowable drift before accumulating deviation.
|
||||
|
||||
Returns
|
||||
-------
|
||||
list of int
|
||||
Indices where change points were detected.
|
||||
"""
|
||||
n = len(signal)
|
||||
s_pos = 0.0
|
||||
s_neg = 0.0
|
||||
change_points: List[int] = []
|
||||
|
||||
for i in range(n):
|
||||
deviation = signal[i] - target
|
||||
s_pos = max(0.0, s_pos + deviation - drift)
|
||||
s_neg = max(0.0, s_neg - deviation - drift)
|
||||
|
||||
if s_pos > threshold or s_neg > threshold:
|
||||
change_points.append(i)
|
||||
# Reset after detection to find subsequent changes
|
||||
s_pos = 0.0
|
||||
s_neg = 0.0
|
||||
|
||||
return change_points
|
||||
446
v1/src/sensing/rssi_collector.py
Normal file
446
v1/src/sensing/rssi_collector.py
Normal file
@@ -0,0 +1,446 @@
|
||||
"""
|
||||
RSSI data collection from Linux WiFi interfaces.
|
||||
|
||||
Provides two concrete collectors:
|
||||
- LinuxWifiCollector: reads real RSSI from /proc/net/wireless and iw commands
|
||||
- SimulatedCollector: produces deterministic synthetic signals for testing
|
||||
|
||||
Both share the same WifiSample dataclass and thread-safe ring buffer.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from collections import deque
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Deque, List, Optional, Protocol
|
||||
|
||||
import numpy as np
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Data types
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WifiSample:
|
||||
"""A single WiFi measurement sample."""
|
||||
|
||||
timestamp: float # UNIX epoch seconds (time.time())
|
||||
rssi_dbm: float # Received signal strength in dBm
|
||||
noise_dbm: float # Noise floor in dBm
|
||||
link_quality: float # Link quality 0-1 (normalised)
|
||||
tx_bytes: int # Cumulative TX bytes
|
||||
rx_bytes: int # Cumulative RX bytes
|
||||
retry_count: int # Cumulative retry count
|
||||
interface: str # WiFi interface name
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Thread-safe ring buffer
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class RingBuffer:
|
||||
"""Thread-safe fixed-size ring buffer for WifiSample objects."""
|
||||
|
||||
def __init__(self, max_size: int) -> None:
|
||||
self._buf: Deque[WifiSample] = deque(maxlen=max_size)
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def append(self, sample: WifiSample) -> None:
|
||||
with self._lock:
|
||||
self._buf.append(sample)
|
||||
|
||||
def get_all(self) -> List[WifiSample]:
|
||||
"""Return a snapshot of all samples (oldest first)."""
|
||||
with self._lock:
|
||||
return list(self._buf)
|
||||
|
||||
def get_last_n(self, n: int) -> List[WifiSample]:
|
||||
"""Return the most recent *n* samples."""
|
||||
with self._lock:
|
||||
items = list(self._buf)
|
||||
return items[-n:] if n < len(items) else items
|
||||
|
||||
def __len__(self) -> int:
|
||||
with self._lock:
|
||||
return len(self._buf)
|
||||
|
||||
def clear(self) -> None:
|
||||
with self._lock:
|
||||
self._buf.clear()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Collector protocol
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class WifiCollector(Protocol):
|
||||
"""Protocol that all WiFi collectors must satisfy."""
|
||||
|
||||
def start(self) -> None: ...
|
||||
def stop(self) -> None: ...
|
||||
def get_samples(self, n: Optional[int] = None) -> List[WifiSample]: ...
|
||||
@property
|
||||
def sample_rate_hz(self) -> float: ...
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Linux WiFi collector (real hardware)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class LinuxWifiCollector:
|
||||
"""
|
||||
Collects real RSSI data from a Linux WiFi interface.
|
||||
|
||||
Data sources:
|
||||
- /proc/net/wireless (RSSI, noise, link quality)
|
||||
- iw dev <iface> station dump (TX/RX bytes, retry count)
|
||||
|
||||
Parameters
|
||||
----------
|
||||
interface : str
|
||||
WiFi interface name, e.g. ``"wlan0"``.
|
||||
sample_rate_hz : float
|
||||
Target sampling rate in Hz (default 10).
|
||||
buffer_seconds : int
|
||||
How many seconds of history to keep in the ring buffer (default 120).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
interface: str = "wlan0",
|
||||
sample_rate_hz: float = 10.0,
|
||||
buffer_seconds: int = 120,
|
||||
) -> None:
|
||||
self._interface = interface
|
||||
self._rate = sample_rate_hz
|
||||
self._buffer = RingBuffer(max_size=int(sample_rate_hz * buffer_seconds))
|
||||
self._running = False
|
||||
self._thread: Optional[threading.Thread] = None
|
||||
|
||||
# -- public API ----------------------------------------------------------
|
||||
|
||||
@property
|
||||
def sample_rate_hz(self) -> float:
|
||||
return self._rate
|
||||
|
||||
def start(self) -> None:
|
||||
"""Start the background sampling thread."""
|
||||
if self._running:
|
||||
return
|
||||
self._validate_interface()
|
||||
self._running = True
|
||||
self._thread = threading.Thread(
|
||||
target=self._sample_loop, daemon=True, name="wifi-rssi-collector"
|
||||
)
|
||||
self._thread.start()
|
||||
logger.info(
|
||||
"LinuxWifiCollector started on %s at %.1f Hz",
|
||||
self._interface,
|
||||
self._rate,
|
||||
)
|
||||
|
||||
def stop(self) -> None:
|
||||
"""Stop the background sampling thread."""
|
||||
self._running = False
|
||||
if self._thread is not None:
|
||||
self._thread.join(timeout=2.0)
|
||||
self._thread = None
|
||||
logger.info("LinuxWifiCollector stopped")
|
||||
|
||||
def get_samples(self, n: Optional[int] = None) -> List[WifiSample]:
|
||||
"""
|
||||
Return collected samples.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
n : int or None
|
||||
If given, return only the most recent *n* samples.
|
||||
"""
|
||||
if n is not None:
|
||||
return self._buffer.get_last_n(n)
|
||||
return self._buffer.get_all()
|
||||
|
||||
def collect_once(self) -> WifiSample:
|
||||
"""Collect a single sample right now (blocking)."""
|
||||
return self._read_sample()
|
||||
|
||||
# -- internals -----------------------------------------------------------
|
||||
|
||||
def _validate_interface(self) -> None:
|
||||
"""Check that the interface exists on this machine."""
|
||||
try:
|
||||
with open("/proc/net/wireless", "r") as f:
|
||||
content = f.read()
|
||||
if self._interface not in content:
|
||||
raise RuntimeError(
|
||||
f"WiFi interface '{self._interface}' not found in "
|
||||
f"/proc/net/wireless. Available interfaces may include: "
|
||||
f"{self._parse_interface_names(content)}. "
|
||||
f"Ensure the interface is up and associated with an AP."
|
||||
)
|
||||
except FileNotFoundError:
|
||||
raise RuntimeError(
|
||||
"Cannot read /proc/net/wireless. "
|
||||
"This collector requires a Linux system with wireless-extensions support. "
|
||||
"If running in a container or VM without WiFi hardware, use "
|
||||
"SimulatedCollector instead."
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _parse_interface_names(proc_content: str) -> List[str]:
|
||||
"""Extract interface names from /proc/net/wireless content."""
|
||||
names: List[str] = []
|
||||
for line in proc_content.splitlines()[2:]: # skip header lines
|
||||
parts = line.split(":")
|
||||
if len(parts) >= 2:
|
||||
names.append(parts[0].strip())
|
||||
return names
|
||||
|
||||
def _sample_loop(self) -> None:
|
||||
interval = 1.0 / self._rate
|
||||
while self._running:
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
sample = self._read_sample()
|
||||
self._buffer.append(sample)
|
||||
except Exception:
|
||||
logger.exception("Error reading WiFi sample")
|
||||
elapsed = time.monotonic() - t0
|
||||
sleep_time = max(0.0, interval - elapsed)
|
||||
if sleep_time > 0:
|
||||
time.sleep(sleep_time)
|
||||
|
||||
def _read_sample(self) -> WifiSample:
|
||||
"""Read one sample from the OS."""
|
||||
rssi, noise, quality = self._read_proc_wireless()
|
||||
tx_bytes, rx_bytes, retries = self._read_iw_station()
|
||||
return WifiSample(
|
||||
timestamp=time.time(),
|
||||
rssi_dbm=rssi,
|
||||
noise_dbm=noise,
|
||||
link_quality=quality,
|
||||
tx_bytes=tx_bytes,
|
||||
rx_bytes=rx_bytes,
|
||||
retry_count=retries,
|
||||
interface=self._interface,
|
||||
)
|
||||
|
||||
def _read_proc_wireless(self) -> tuple[float, float, float]:
|
||||
"""Parse /proc/net/wireless for the configured interface."""
|
||||
try:
|
||||
with open("/proc/net/wireless", "r") as f:
|
||||
for line in f:
|
||||
if self._interface in line:
|
||||
# Format: iface: status quality signal noise ...
|
||||
parts = line.split()
|
||||
# parts[0] = "wlan0:", parts[2]=quality, parts[3]=signal, parts[4]=noise
|
||||
quality_raw = float(parts[2].rstrip("."))
|
||||
signal_raw = float(parts[3].rstrip("."))
|
||||
noise_raw = float(parts[4].rstrip("."))
|
||||
# Normalise quality to 0..1 (max is typically 70)
|
||||
quality = min(1.0, max(0.0, quality_raw / 70.0))
|
||||
return signal_raw, noise_raw, quality
|
||||
except (FileNotFoundError, IndexError, ValueError) as exc:
|
||||
raise RuntimeError(
|
||||
f"Failed to read /proc/net/wireless for {self._interface}: {exc}"
|
||||
) from exc
|
||||
raise RuntimeError(
|
||||
f"Interface {self._interface} not found in /proc/net/wireless"
|
||||
)
|
||||
|
||||
def _read_iw_station(self) -> tuple[int, int, int]:
|
||||
"""Run ``iw dev <iface> station dump`` and parse TX/RX/retries."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["iw", "dev", self._interface, "station", "dump"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=2.0,
|
||||
)
|
||||
text = result.stdout
|
||||
|
||||
tx_bytes = self._extract_int(text, r"tx bytes:\s*(\d+)")
|
||||
rx_bytes = self._extract_int(text, r"rx bytes:\s*(\d+)")
|
||||
retries = self._extract_int(text, r"tx retries:\s*(\d+)")
|
||||
return tx_bytes, rx_bytes, retries
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired):
|
||||
# iw not installed or timed out -- degrade gracefully
|
||||
return 0, 0, 0
|
||||
|
||||
@staticmethod
|
||||
def _extract_int(text: str, pattern: str) -> int:
|
||||
m = re.search(pattern, text)
|
||||
return int(m.group(1)) if m else 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Simulated collector (deterministic, for testing)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class SimulatedCollector:
|
||||
"""
|
||||
Deterministic simulated WiFi collector for testing.
|
||||
|
||||
Generates a synthetic RSSI signal composed of:
|
||||
- A constant baseline (-50 dBm default)
|
||||
- An optional sinusoidal component (configurable frequency/amplitude)
|
||||
- Optional step-change injection (for change-point testing)
|
||||
- Deterministic noise from a seeded PRNG
|
||||
|
||||
This is explicitly a test/development tool and makes no attempt to
|
||||
appear as real hardware.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
seed : int
|
||||
Random seed for deterministic output.
|
||||
sample_rate_hz : float
|
||||
Target sampling rate in Hz (default 10).
|
||||
buffer_seconds : int
|
||||
Ring buffer capacity in seconds (default 120).
|
||||
baseline_dbm : float
|
||||
RSSI baseline in dBm (default -50).
|
||||
sine_freq_hz : float
|
||||
Frequency of the sinusoidal RSSI component (default 0.3 Hz, breathing band).
|
||||
sine_amplitude_dbm : float
|
||||
Amplitude of the sinusoidal component (default 2.0 dBm).
|
||||
noise_std_dbm : float
|
||||
Standard deviation of additive Gaussian noise (default 0.5 dBm).
|
||||
step_change_at : float or None
|
||||
If set, inject a step change of ``step_change_dbm`` at this time offset
|
||||
(seconds from start).
|
||||
step_change_dbm : float
|
||||
Magnitude of the step change (default -10 dBm).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
seed: int = 42,
|
||||
sample_rate_hz: float = 10.0,
|
||||
buffer_seconds: int = 120,
|
||||
baseline_dbm: float = -50.0,
|
||||
sine_freq_hz: float = 0.3,
|
||||
sine_amplitude_dbm: float = 2.0,
|
||||
noise_std_dbm: float = 0.5,
|
||||
step_change_at: Optional[float] = None,
|
||||
step_change_dbm: float = -10.0,
|
||||
) -> None:
|
||||
self._rate = sample_rate_hz
|
||||
self._buffer = RingBuffer(max_size=int(sample_rate_hz * buffer_seconds))
|
||||
self._rng = np.random.default_rng(seed)
|
||||
|
||||
self._baseline = baseline_dbm
|
||||
self._sine_freq = sine_freq_hz
|
||||
self._sine_amp = sine_amplitude_dbm
|
||||
self._noise_std = noise_std_dbm
|
||||
self._step_at = step_change_at
|
||||
self._step_dbm = step_change_dbm
|
||||
|
||||
self._running = False
|
||||
self._thread: Optional[threading.Thread] = None
|
||||
self._start_time: float = 0.0
|
||||
self._sample_index: int = 0
|
||||
|
||||
# -- public API ----------------------------------------------------------
|
||||
|
||||
@property
|
||||
def sample_rate_hz(self) -> float:
|
||||
return self._rate
|
||||
|
||||
def start(self) -> None:
|
||||
if self._running:
|
||||
return
|
||||
self._running = True
|
||||
self._start_time = time.time()
|
||||
self._sample_index = 0
|
||||
self._thread = threading.Thread(
|
||||
target=self._sample_loop, daemon=True, name="sim-rssi-collector"
|
||||
)
|
||||
self._thread.start()
|
||||
logger.info("SimulatedCollector started at %.1f Hz (seed reused from init)", self._rate)
|
||||
|
||||
def stop(self) -> None:
|
||||
self._running = False
|
||||
if self._thread is not None:
|
||||
self._thread.join(timeout=2.0)
|
||||
self._thread = None
|
||||
|
||||
def get_samples(self, n: Optional[int] = None) -> List[WifiSample]:
|
||||
if n is not None:
|
||||
return self._buffer.get_last_n(n)
|
||||
return self._buffer.get_all()
|
||||
|
||||
def generate_samples(self, duration_seconds: float) -> List[WifiSample]:
|
||||
"""
|
||||
Generate a batch of samples without the background thread.
|
||||
|
||||
Useful for unit tests that need a known signal without timing jitter.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
duration_seconds : float
|
||||
How many seconds of signal to produce.
|
||||
|
||||
Returns
|
||||
-------
|
||||
list of WifiSample
|
||||
"""
|
||||
n_samples = int(duration_seconds * self._rate)
|
||||
samples: List[WifiSample] = []
|
||||
base_time = time.time()
|
||||
for i in range(n_samples):
|
||||
t = i / self._rate
|
||||
sample = self._make_sample(base_time + t, t, i)
|
||||
samples.append(sample)
|
||||
return samples
|
||||
|
||||
# -- internals -----------------------------------------------------------
|
||||
|
||||
def _sample_loop(self) -> None:
|
||||
interval = 1.0 / self._rate
|
||||
while self._running:
|
||||
t0 = time.monotonic()
|
||||
now = time.time()
|
||||
t_offset = now - self._start_time
|
||||
sample = self._make_sample(now, t_offset, self._sample_index)
|
||||
self._buffer.append(sample)
|
||||
self._sample_index += 1
|
||||
elapsed = time.monotonic() - t0
|
||||
sleep_time = max(0.0, interval - elapsed)
|
||||
if sleep_time > 0:
|
||||
time.sleep(sleep_time)
|
||||
|
||||
def _make_sample(self, timestamp: float, t_offset: float, index: int) -> WifiSample:
|
||||
"""Build one deterministic sample."""
|
||||
# Sinusoidal component
|
||||
sine = self._sine_amp * math.sin(2.0 * math.pi * self._sine_freq * t_offset)
|
||||
|
||||
# Deterministic Gaussian noise (uses the seeded RNG)
|
||||
noise = self._rng.normal(0.0, self._noise_std)
|
||||
|
||||
# Step change
|
||||
step = 0.0
|
||||
if self._step_at is not None and t_offset >= self._step_at:
|
||||
step = self._step_dbm
|
||||
|
||||
rssi = self._baseline + sine + noise + step
|
||||
|
||||
return WifiSample(
|
||||
timestamp=timestamp,
|
||||
rssi_dbm=float(rssi),
|
||||
noise_dbm=-95.0,
|
||||
link_quality=max(0.0, min(1.0, (rssi + 100.0) / 60.0)),
|
||||
tx_bytes=index * 1500,
|
||||
rx_bytes=index * 3000,
|
||||
retry_count=max(0, index // 100),
|
||||
interface="sim0",
|
||||
)
|
||||
Reference in New Issue
Block a user