File size: 4,729 Bytes
b96185c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""CP-OFDM and configurable variants.

Default grid: 64-point FFT, 16-sample cyclic prefix, 4 OFDM symbols -> 4*80 =
320 samples.  48 occupied subcarriers (logical frequencies -24..-1, 1..24; DC
and band edges null) -> 0.75 fractional bandwidth.

Pilot patterns (both give 48 pilot REs + 144 data REs, matched resources, but a
different pilot signature for Eve):
  * "block": symbol 0 is a full pilot symbol; symbols 1..n_sym-1 carry data
    (48 pilot REs, (n_sym-1)*n_occ data REs).
  * "comb":  every ``comb_spacing``-th subcarrier is a pilot in every symbol;
    the rest carry data (n_sym*n_pilot_sub pilot REs, n_sym*n_data_sub data).

Variants are subclasses that set the grid/pilot class attributes.  DFT-s-OFDM
and OTFS subclass this and override the grid-filling.
"""

from __future__ import annotations

import numpy as np

from ..pilots import pilot_matrix
from .base import Waveform


class OFDM(Waveform):
    name = "ofdm"
    n_fft = 64
    n_cp = 16
    n_occ = 48
    n_sym = 4
    pilot_pattern = "block"
    comb_spacing = 4

    def __init__(self, cfg):
        assert cfg.n_samples == self.n_sym * (self.n_fft + self.n_cp), "grid must fill T samples"
        logical = np.concatenate([np.arange(-self.n_occ // 2, 0), np.arange(1, self.n_occ // 2 + 1)])
        self.occ = logical % self.n_fft  # ascending logical frequency
        if self.pilot_pattern == "comb":
            self.pilot_sub = np.arange(0, self.n_occ, self.comb_spacing)
            self.data_sub = np.array([k for k in range(self.n_occ) if k not in set(self.pilot_sub)])
            self._n_pilot_re = len(self.pilot_sub) * self.n_sym
            assert cfg.n_data == len(self.data_sub) * self.n_sym
        else:  # block
            self._n_pilot_re = self.n_occ
            assert cfg.n_data == (self.n_sym - 1) * self.n_occ
        self.pilots = pilot_matrix(cfg.pilot_scheme, cfg.n_tx, self._n_pilot_re)
        super().__init__(cfg)

    @property
    def n_pilot_re(self) -> int:
        return self._n_pilot_re

    # -- grid <-> time ---------------------------------------------------------
    def _grid_to_time(self, grid):
        """(..., M, n_sym, n_occ) -> (..., M, T)"""
        f = np.zeros(grid.shape[:-1] + (self.n_fft,), dtype=np.complex128)
        f[..., self.occ] = grid
        x = np.fft.ifft(f, axis=-1, norm="ortho")
        x = np.concatenate([x[..., -self.n_cp :], x], axis=-1)  # (..., n_sym, n_fft+n_cp)
        return x.reshape(x.shape[:-2] + (self.cfg.n_samples,))

    def _time_to_grid(self, u):
        """(..., M, T) -> (..., M, n_sym, n_occ)"""
        x = u.reshape(u.shape[:-1] + (self.n_sym, self.n_fft + self.n_cp))[..., self.n_cp :]
        return np.fft.fft(x, axis=-1, norm="ortho")[..., self.occ]

    # -- grid filling (overridden by DFT-s-OFDM / OTFS) ------------------------
    def _fill_grid(self, s):
        if self.pilot_pattern == "comb":
            return self._fill_comb(s)
        data = s.reshape(s.shape[:-1] + (self.n_sym - 1, self.n_occ))
        pil = np.broadcast_to(self.pilots[:, None, :], s.shape[:-1] + (1, self.n_occ))
        return np.concatenate([pil, data], axis=-2)

    def _extract_data(self, grid):
        if self.pilot_pattern == "comb":
            return self._extract_comb(grid)
        d = grid[..., 1:, :]
        return d.reshape(d.shape[:-2] + (self.cfg.n_data,))

    def _fill_comb(self, s):
        lead = s.shape[:-1]
        grid = np.zeros(lead + (self.n_sym, self.n_occ), dtype=np.complex128)
        data = s.reshape(lead + (self.n_sym, len(self.data_sub)))
        pil = np.broadcast_to(
            self.pilots.reshape(self.cfg.n_tx, self.n_sym, len(self.pilot_sub)),
            lead + (self.n_sym, len(self.pilot_sub)),
        )
        grid[..., self.data_sub] = data
        grid[..., self.pilot_sub] = pil
        return grid

    def _extract_comb(self, grid):
        d = grid[..., self.data_sub]
        return d.reshape(d.shape[:-2] + (self.cfg.n_data,))

    def _modulate_raw(self, s):
        return self._grid_to_time(self._fill_grid(s))

    def demodulate(self, u):
        return self._extract_data(self._time_to_grid(np.asarray(u, dtype=np.complex128)))


class OFDMComb(OFDM):
    name = "ofdm_comb"
    pilot_pattern = "comb"
    comb_spacing = 4  # 12 pilot + 36 data subcarriers per symbol (matched: 48 pilot, 144 data)


class OFDM48(OFDM):
    """Smaller-FFT, more-symbol variant (T=320, 144 data, 0.75 BW).

    Block pilots need n_occ (=36) divisible by M, so this variant is valid only
    for M in {4, 6, 9, 12, 18, 36} -- not the default M=8.  Not in the default
    registry; instantiate directly when using a compatible M.
    """
    name = "ofdm48"
    n_fft = 48
    n_cp = 16
    n_occ = 36
    n_sym = 5