Repository navigation
Expand file tree
/
Copy pathquantization.py
More file actions
89 lines (73 loc) · 3.48 KB
/
Copy pathquantization.py
File metadata and controls
89 lines (73 loc) · 3.48 KB
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
"""b-bit RIS phase quantization on top of the diagonalization scheme.
Two ways to satisfy a finite phase codebook:
- quantize_phases: round an already-optimized p onto the codebook. The
rounded p leaves the diagonalization null space, so the effective
channels grow off-diagonal leakage (BER error floors).
- quantize_and_repair: the "p = U q" / quantized-subspace construction.
Keep the rounded phase pattern FIXED and re-solve the per-element
amplitudes so that p returns exactly to the null space. Requires
per-element amplitude control and N > 2*J*K*(K-1).
"""
import numpy as np
from typing import List, Optional
from diagonalization import calculate_A_total_power_multiple
def quantize_phases(p: np.ndarray, bits: Optional[int]) -> np.ndarray:
"""Nearest-neighbour rounding of the phases of p onto the uniform
2^bits codebook {0, 2pi/2^bits, ...}; amplitudes untouched."""
if bits is None:
return p.copy()
step = 2.0 * np.pi / 2 ** bits
phases = np.round(np.angle(p) / step) * step
return np.abs(p) * np.exp(1j * phases)
def constraint_rows(K: int, Gs: List[np.ndarray], H: np.ndarray) -> np.ndarray:
"""Rows v^T with v = g_k (row of G) Hadamard h_i (column of H) for all
i != k and all receivers: the linear forms whose zeroing diagonalizes
the effective channels (twc2025 eq. 8). Shape (J*K*(K-1), N)."""
rows = []
for G in Gs:
for i in range(K):
for k in range(K):
if i != k:
rows.append(G[k, :] * H[:, i])
return np.array(rows)
def quantize_and_repair(
p_cont: np.ndarray,
bits: int,
K: int,
Gs: List[np.ndarray],
H: np.ndarray,
eta: float,
) -> np.ndarray:
"""Fix the quantized phase pattern phi = Q_b(angle(p_cont)) and re-solve
the per-element amplitudes a (real) so that p = diag(e^{j phi}) a lies
exactly in the diagonalization null space:
C diag(e^{j phi}) a = 0, C = constraint_rows, a real
i.e. 2*J*K*(K-1) real linear equations in N real unknowns. Within the
surviving real subspace the amplitude vector maximizing the total
receiver gain is chosen (eigenproblem). A negative amplitude folds into
a +pi phase shift, which stays on the codebook grid for any bits >= 1.
Returns p with exactly-quantized phases AND exact diagonalization.
Raises ValueError when N <= 2*J*K*(K-1) (no amplitude freedom left).
"""
N = p_cont.shape[0]
phi = np.angle(quantize_phases(p_cont, bits))
E = np.exp(1j * phi)
C = constraint_rows(K, Gs, H) # (m, N) complex
D = C * E[np.newaxis, :] # constraints on real a
M = np.vstack([D.real, D.imag]) # (2m, N) real
rank = M.shape[0] # nominal: 2*J*K*(K-1)
if rank >= N:
raise ValueError(
f"Quantization repair infeasible: {rank} real constraints "
f"(2*J*K*(K-1) with J={len(Gs)}, K={K}) require N > {rank} "
f"RIS elements, got N = {N}")
_, _, Vh = np.linalg.svd(M, full_matrices=True)
B = Vh[rank:, :].T # (N, N - rank) real basis
A = calculate_A_total_power_multiple(K, N, Gs, H)
A_t = np.einsum("i,ij,j->ij", E.conj(), A, E).real
A_t = (A_t + A_t.T) / 2
M_gain = B.T @ A_t @ B
_, vecs = np.linalg.eigh((M_gain + M_gain.T) / 2)
a = B @ vecs[:, -1]
p = E * a # negative a == +pi shift, still on grid
return eta * p / np.max(np.abs(p))