"""AFDM (Affine Frequency Division Multiplexing). Discrete affine Fourier transform (DAFT) modulation per symbol: s = Lambda_c1^H F_N^H Lambda_c2^H x, Lambda_c = diag(exp(-j 2 pi c n^2)), with N = 48 chirp subcarriers, chirp-periodic prefix (CPP) of 12 native samples, 4 symbols -> 240 native samples. Because every chirp subcarrier sweeps the full band, bandwidth cannot be limited by nulling chirp bins; instead AFDM runs at native rate 0.75 Fs and the block is FFT-resampled 240 -> 320 to the common rate, which band-limits it to |f| <= 0.375 Fs like the other formats. c1 = (2 a_max + 1) / (2N) with a_max = 1; c2 is an irrational-valued free parameter. Frame: symbol 0 = 48 user-orthogonal pilot chirps, symbols 1..3 = 144 data REs (time-major, like OFDM). The FFT up/down-sampling pair is exactly inverse on the native band, so loopback demodulation is exact. """ from __future__ import annotations import numpy as np from ..pilots import pilot_matrix from .base import Waveform def resample_fft(x: np.ndarray, t_out: int) -> np.ndarray: """Band-limited FFT resampling along the last axis (complex, batched).""" t_in = x.shape[-1] xf = np.fft.fft(x, axis=-1) yf = np.zeros(x.shape[:-1] + (t_out,), dtype=np.complex128) h = min(t_in, t_out) // 2 yf[..., :h] = xf[..., :h] yf[..., -(h - 1) :] = xf[..., -(h - 1) :] if t_out > t_in: # upsample: split the input Nyquist bin yf[..., h] = 0.5 * xf[..., h] yf[..., t_out - h] = 0.5 * xf[..., h] else: # downsample: merge the two bins that carried the native Nyquist yf[..., h] = xf[..., h] + xf[..., t_in - h] return np.fft.ifft(yf, axis=-1) * (t_out / t_in) class AFDM(Waveform): name = "afdm" n_chirp = 48 n_cpp = 12 n_sym = 4 alpha_max = 1 def __init__(self, cfg): n = self.n_chirp assert cfg.n_data == (self.n_sym - 1) * n self.n_native = self.n_sym * (n + self.n_cpp) # 240 assert cfg.n_samples > self.n_native, "AFDM native rate must be below the common rate" self.c1 = (2 * self.alpha_max + 1) / (2.0 * n) self.c2 = (np.sqrt(5.0) - 1.0) / 2.0 / (2.0 * n) idx = np.arange(n) self.lam1 = np.exp(-2j * np.pi * self.c1 * idx**2) # diag of Lambda_c1 self.lam2 = np.exp(-2j * np.pi * self.c2 * idx**2) i = np.arange(self.n_cpp) self.cpp_phase = np.exp(-2j * np.pi * self.c1 * (n**2 + 2.0 * n * (i - self.n_cpp))) self.pilots = pilot_matrix(cfg.pilot_scheme, cfg.n_tx, n) super().__init__(cfg) @property def n_pilot_re(self) -> int: return self.n_chirp def _idaft(self, x): """Chirp-domain symbols -> native time domain: Lambda1^H F^H Lambda2^H x.""" return np.conj(self.lam1) * np.fft.ifft(np.conj(self.lam2) * x, axis=-1, norm="ortho") def _daft(self, s): """Native time domain -> chirp domain: Lambda2 F Lambda1 s.""" return self.lam2 * np.fft.fft(self.lam1 * s, axis=-1, norm="ortho") def _modulate_raw(self, s): data = s.reshape(s.shape[:-1] + (self.n_sym - 1, self.n_chirp)) pil = np.broadcast_to(self.pilots[:, None, :], s.shape[:-1] + (1, self.n_chirp)) grid = np.concatenate([pil, data], axis=-2) # (..., M, 4, 48) body = self._idaft(grid) cpp = body[..., self.n_chirp - self.n_cpp :] * self.cpp_phase sym = np.concatenate([cpp, body], axis=-1) # (..., M, 4, 60) native = sym.reshape(sym.shape[:-2] + (self.n_native,)) return resample_fft(native, self.cfg.n_samples) def demodulate(self, u): native = resample_fft(np.asarray(u, dtype=np.complex128), self.n_native) sym = native.reshape(native.shape[:-1] + (self.n_sym, self.n_chirp + self.n_cpp)) grid = self._daft(sym[..., self.n_cpp :]) d = grid[..., 1:, :] return d.reshape(d.shape[:-2] + (self.cfg.n_data,))