"""Waveform base class. A Waveform maps per-user symbol streams S with shape (..., M, n_data) to baseband blocks U with shape (..., M, T). The map is linear in S plus a deterministic additive pilot component, so a single power-calibration scalar (estimated once with unit-variance inputs) makes the average transmit power per user per sample equal to 1 for every format. ``demodulate`` is the (pilot-stripping) inverse used by loopback tests and by Bob-side equalization; it operates only along the time axis, so it commutes with any memoryless per-sample MIMO channel. """ from __future__ import annotations import numpy as np from ..config import SystemConfig _CAL_SEED = 0xC0FFEE _CAL_BLOCKS = 512 class Waveform: name = "base" def __init__(self, cfg: SystemConfig): self.cfg = cfg self._scale = 1.0 self._scale = self._calibrate() if cfg.total_power is not None: # fixed-total-power mode: per-user power = total_power / M self._scale *= float(np.sqrt(cfg.total_power / cfg.n_tx)) # -- subclass interface (no power scaling) -------------------------------- def _modulate_raw(self, s: np.ndarray) -> np.ndarray: raise NotImplementedError def demodulate(self, u: np.ndarray) -> np.ndarray: """(..., M, T) -> (..., M, n_data) data-RE estimates (pilots stripped). Inverts the modulation up to the calibration scale: for the exact formats demodulate(modulate(S)) == S * scale. """ raise NotImplementedError # -- public API ----------------------------------------------------------- def modulate(self, s: np.ndarray) -> np.ndarray: """(..., M, n_data) symbol streams -> (..., M, T) calibrated blocks.""" return self._scale * self._modulate_raw(np.asarray(s, dtype=np.complex128)) @property def scale(self) -> float: return self._scale def _calibrate(self) -> float: rng = np.random.default_rng(_CAL_SEED) m, nd = self.cfg.n_tx, self.cfg.n_data s = (rng.standard_normal((_CAL_BLOCKS, m, nd)) + 1j * rng.standard_normal((_CAL_BLOCKS, m, nd))) / np.sqrt(2.0) u = self._modulate_raw(s) return float(1.0 / np.sqrt(np.mean(np.abs(u) ** 2))) # -- resource accounting for the fair-comparison table -------------------- @property def n_pilot_re(self) -> int: raise NotImplementedError @property def resources(self) -> dict: return { "format": self.name, "samples": self.cfg.n_samples, "data_re_per_user": self.cfg.n_data, "pilot_re_per_user": self.n_pilot_re, "design_occupancy": 0.75, }