"""Bob-side reference receiver: genie-CSI LMMSE combining and empirical SINR. Building block for the utility J_B (blueprint 1). Because every format's demodulator is linear along time and the block-fading channel is memoryless per sample, demodulate(H U) = H demodulate(U): the per-RE model after demodulation is z = B x + v, B = scale * H W, v ~ CN(0, scale^2 sigma_a^2 H H^H + sigma_B^2 I). The per-RE noise variance is exactly sigma_B^2 for the unitary formats (OFDM, DFT-s-OFDM, OTFS) and approximately so for SC (unit-energy matched filter) and AFDM (unitary DAFT after band-limited resampling). ``lmmse_sinr`` reports empirical per-message-component SINR from the MMSE identity SINR_i = 1/MSE_i - 1 (unit-power constellation symbols). """ from __future__ import annotations import numpy as np from .channel import receive from .config import SystemConfig from .formats.base import Waveform from .generate import collaborate def lmmse_estimate(fmt: Waveform, w: np.ndarray, h: np.ndarray, y: np.ndarray, sigma2: float, sigma_a2: float) -> np.ndarray: """Per-block LMMSE estimate of the message symbols. h: (b, R, M) genie CSI, y: (b, R, T) received blocks. Returns x_hat: (b, Kd, n_data). """ z = fmt.demodulate(y) # (b, R, n_data) b_eff = fmt.scale * np.einsum("brm,mk->brk", h, w) # (b, R, Kd) hh = np.einsum("brm,bsm->brs", h, np.conj(h)) # (b, R, R) r = h.shape[1] cov = ( np.einsum("brk,bsk->brs", b_eff, np.conj(b_eff)) + (fmt.scale**2) * sigma_a2 * hh + sigma2 * np.eye(r) ) gain = np.linalg.solve(cov, b_eff) # (b, R, Kd); x_hat = gain^H z return np.einsum("brk,brn->bkn", np.conj(gain), z) def empirical_sinr(x: np.ndarray, x_hat: np.ndarray) -> np.ndarray: """Per-component SINR from MSE (unit-power symbols): 1/MSE - 1.""" mse = np.mean(np.abs(x_hat - x) ** 2, axis=(0, 2)) return 1.0 / np.maximum(mse, 1e-12) - 1.0 def bob_sinr(cfg: SystemConfig, fmt: Waveform, w: np.ndarray, comp: dict, sigma2: float) -> np.ndarray: """End-to-end genie-CSI SINR per message component on a component set.""" u = fmt.modulate(collaborate(w, comp["X"], comp["A"])) y = receive(comp["H_bob"], u, np.sqrt(sigma2) * comp["N_bob"]) x_hat = lmmse_estimate(fmt, w, comp["H_bob"], y, sigma2, cfg.sigma_a2) return empirical_sinr(comp["X"], x_hat)