-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopt_ce_vs_tau.py
147 lines (109 loc) · 4.51 KB
/
opt_ce_vs_tau.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import numpy as np
import os
import matplotlib.pyplot as plt
import scenario.common as cmn
from environment import RisProtocolEnv, command_parser, NOISE_POWER_dBm, T, TAU, TX_POW_dBm, NUM_PILOTS
from mpl_toolkits.axes_grid1 import make_axes_locatable
##################################################
# Parameters
##################################################
# Seed random generator
np.random.seed(42)
# Define transmit power [mW]
tx_power = cmn.dbm2watt(TX_POW_dBm)
# Get noise power
noise_power = cmn.dbm2watt(NOISE_POWER_dBm)
# Total tau
total_tau = TAU
# Define CHEST algorithmic cost
chest_time_cost = 5
# Parameter for saving datas
prefix = 'opt_ce_vs_tau'
# Setup option
setups = ['ob-cc', 'ib-no', 'ib-wf']
# For grid mesh
num_users = int(1e3)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# The following parser is used to impose some data without the need of changing the script (run with -h flag for
# help) Render bool needs to be True to save the data If no arguments are given the standard value are loaded (
# see environment) datasavedir should be used to save numpy arrays
render, side, name, datasavedir = command_parser()
prefix = os.path.join(datasavedir, prefix + name)
# Build environment
env = RisProtocolEnv(num_users=num_users, side=side)
# env.plot_scenario()
# Generate noise realizations
noise_ = (np.random.randn(num_users, env.ris.num_els) + 1j * np.random.randn(num_users, env.ris.num_els)) / np.sqrt(2)
##############################
# Generate DFT codebook of configurations
##############################
# Define fundamental frequency
fundamental_freq = np.exp(-1j * 2 * np.pi / env.ris.num_els)
# Compute DFT matrix
J, K = np.meshgrid(np.arange(env.ris.num_els), np.arange(env.ris.num_els))
DFT = np.power(fundamental_freq, J * K)
# Compute normalized DFT matrix
DFT_norm = DFT / np.sqrt(env.ris.num_els)
if not render:
# Plot DFT codebook matrix
fig, ax = plt.subplots()
im = ax.imshow(DFT_norm.real)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
ax.set_title('DFT configuration matrix: $' + str(env.ris.num_els) + r'\times' + str(env.ris.num_els) + '$')
plt.tight_layout()
plt.show()
# Get the channels:
# ur - user-ris
# rb - ris-bs
h_ur, g_rb, _ = env.return_separate_ris_channel()
# Squeeze out
g_rb = np.squeeze(g_rb)
##############################
# OPT-CE
##############################
# Generate estimation noise
est_var = noise_power / env.ris.num_els / tx_power / NUM_PILOTS
est_noise_ = np.sqrt(est_var) * noise_
# Get estimated channel coefficients for the equivalent channel
z_eq = h_ur * g_rb[np.newaxis, :]
z_hat = z_eq + est_noise_
# Get estimated/best configuration (the one that maximizes the SNR)
Phi_true = np.exp(-1j * np.angle(z_eq))
Phi_hat = np.exp(-1j * np.angle(z_hat))
# Compute equivalent channel
h_eq_chest = (Phi_true * z_eq).sum(axis=-1)
h_eq_chest_hat = (Phi_hat * z_hat).sum(axis=-1)
# Compute the SNR of each user when using OPT-CE
snr_oc = tx_power / noise_power * np.abs(h_eq_chest) ** 2
snr_oc_hat = tx_power / noise_power * np.abs(h_eq_chest_hat) ** 2
snr_oc_db = 10 * np.log10(snr_oc)
snr_oc_hat_db = 10 * np.log10(snr_oc_hat)
# Compute rate
se_oc = np.log2(1 + snr_oc_hat)
se_oc_real = np.log2(1 + snr_oc_hat)
se_oc_real[se_oc > se_oc] = 0
# Pre-log term
tau_alg = (env.ris.num_els + chest_time_cost) * T
for setup in setups:
if setup == 'ob-cc':
tau_setup = T
elif setup == 'ib-no':
tau_setup = 2 * T
else:
tau_setup = 3 * T
prelog_term = 1 - (tau_setup + tau_setup + tau_alg)/total_tau
prelog_term[prelog_term < 0] = 0
rate_opt_ce = prelog_term[np.newaxis].T * np.repeat(se_oc[np.newaxis], len(total_tau), axis=0)
rate_opt_ce_real = prelog_term[np.newaxis].T * np.repeat(se_oc_real[np.newaxis], len(total_tau), axis=0)
##################################################
# Save data
##################################################
if render:
np.savez(prefix + '_' + setup + str('.npz'),
snr_true=snr_oc,
snr_esti=snr_oc_hat,
rate=rate_opt_ce,
rate_real=rate_opt_ce_real)