forked from NVlabs/sionna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_flat_fading_channel.py
202 lines (184 loc) · 7.64 KB
/
test_flat_fading_channel.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#
# SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
try:
import sionna
except ImportError as e:
import sys
sys.path.append("../")
from sionna.channel import GenerateFlatFadingChannel, ApplyFlatFadingChannel, FlatFadingChannel, exp_corr_mat, KroneckerModel
from sionna.utils import QAMSource
import pytest
import unittest
import warnings
import numpy as np
import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
print('Number of GPUs available :', len(gpus))
if gpus:
gpu_num = 0 # Number of the GPU to be used
try:
tf.config.set_visible_devices(gpus[gpu_num], 'GPU')
print('Only GPU number', gpu_num, 'used.')
tf.config.experimental.set_memory_growth(gpus[gpu_num], True)
except RuntimeError as e:
print(e)
class TestGenerateFlatFading(unittest.TestCase):
"""Unittest for GenerateFlatFading"""
def test_without_spatial_correlation(self):
num_tx_ant = 4
num_rx_ant = 16
batch_size = 128
gen_chn = GenerateFlatFadingChannel(num_tx_ant, num_rx_ant)
h = gen_chn(batch_size)
self.assertEqual(h.shape, [batch_size, num_rx_ant, num_tx_ant])
self.assertEqual(h.dtype, tf.complex64)
gen_chn = GenerateFlatFadingChannel(num_tx_ant, num_rx_ant, dtype=tf.complex128)
h = gen_chn(batch_size)
self.assertEqual(h.dtype, tf.complex128)
def test_with_spatial_correlation(self):
num_tx_ant = 4
num_rx_ant = 16
r_tx = exp_corr_mat(0.4, num_tx_ant)
r_rx = exp_corr_mat(0.99, num_rx_ant)
kron = KroneckerModel(r_tx, r_rx)
gen_chn = GenerateFlatFadingChannel(num_tx_ant, num_rx_ant, spatial_corr=kron)
@tf.function()
def func():
h = gen_chn(1000000)
r_tx_hat = tf.reduce_mean(tf.matmul(h, h, adjoint_a=True), 0)
r_rx_hat = tf.reduce_mean(tf.matmul(h, h, adjoint_b=True), 0)
return r_tx_hat, r_rx_hat
r_tx_hat = tf.zeros_like(r_tx)
r_rx_hat = tf.zeros_like(r_rx)
iterations = 10
for i in range(iterations):
tmp = func()
r_tx_hat += tmp[0]/iterations/num_rx_ant
r_rx_hat += tmp[1]/iterations/num_tx_ant
print(np.max(np.abs(r_rx-r_rx_hat)))
self.assertTrue(np.allclose(r_tx, r_tx_hat, atol=1e-3))
self.assertTrue(np.allclose(r_rx, r_rx_hat, atol=1e-3))
def test_property_setter(self):
num_tx_ant = 4
num_rx_ant = 16
r_tx = exp_corr_mat(0.4, num_tx_ant)
r_rx = exp_corr_mat(0.99, num_rx_ant)
kron = KroneckerModel(r_tx, r_rx)
gen_chn = GenerateFlatFadingChannel(num_tx_ant, num_rx_ant)
@tf.function()
def func():
gen_chn.spatial_corr = kron
h = gen_chn(1000000)
r_tx_hat = tf.reduce_mean(tf.matmul(h, h, adjoint_a=True), 0)
r_rx_hat = tf.reduce_mean(tf.matmul(h, h, adjoint_b=True), 0)
return r_tx_hat, r_rx_hat
r_tx_hat = tf.zeros_like(r_tx)
r_rx_hat = tf.zeros_like(r_rx)
iterations = 10
for i in range(iterations):
tmp = func()
r_tx_hat += tmp[0]/iterations/num_rx_ant
r_rx_hat += tmp[1]/iterations/num_tx_ant
print(np.max(np.abs(r_rx-r_rx_hat)))
self.assertTrue(np.allclose(r_tx, r_tx_hat, atol=1e-3))
self.assertTrue(np.allclose(r_rx, r_rx_hat, atol=1e-3))
class TestGenerateApplyFading(unittest.TestCase):
"""Unittest for ApplyFlatFading"""
def test_without_noise(self):
num_tx_ant = 4
num_rx_ant = 16
batch_size = 24
r_tx = exp_corr_mat(0.4, num_tx_ant)
r_rx = exp_corr_mat(0.99, num_rx_ant)
kron = KroneckerModel(r_tx, r_rx)
gen_chn = GenerateFlatFadingChannel(num_tx_ant, num_rx_ant, spatial_corr=kron)
app_chn = ApplyFlatFadingChannel(add_awgn=False)
h = gen_chn(batch_size)
x = QAMSource(4)([batch_size, num_tx_ant])
y = app_chn([x, h])
self.assertTrue(np.array_equal(y, tf.squeeze(tf.matmul(h, tf.expand_dims(x, -1)))))
def test_with_noise(self):
num_tx_ant = 4
num_rx_ant = 16
batch_size = 100000
r_tx = exp_corr_mat(0.4, num_tx_ant)
r_rx = exp_corr_mat(0.99, num_rx_ant)
kron = KroneckerModel(r_tx, r_rx)
gen_chn = GenerateFlatFadingChannel(num_tx_ant, num_rx_ant, spatial_corr=kron)
app_chn = ApplyFlatFadingChannel(add_awgn=True)
h = gen_chn(batch_size)
x = QAMSource(4)([batch_size, num_tx_ant])
no = 0.1
y = app_chn([x, h, no])
n = y - tf.squeeze(tf.matmul(h, tf.expand_dims(x, -1)))
noise_var = np.var(n)
self.assertAlmostEqual(no, noise_var, places=3)
class TestFlatFadingChannel(unittest.TestCase):
"""Unittest for FlatFading"""
def test_without_noise(self):
num_tx_ant = 4
num_rx_ant = 16
batch_size = 24
dtype=tf.complex128
r_tx = exp_corr_mat(0.4, num_tx_ant, dtype)
r_rx = exp_corr_mat(0.99, num_rx_ant, dtype)
kron = KroneckerModel(r_tx, r_rx)
chn = FlatFadingChannel(num_tx_ant, num_rx_ant, spatial_corr=kron, add_awgn=False, return_channel=True, dtype=dtype)
x = QAMSource(4, dtype=tf.complex128)([batch_size, num_tx_ant])
y, h = chn(x)
self.assertTrue(np.array_equal(y, tf.squeeze(tf.matmul(h, tf.expand_dims(x, -1)))))
def test_with_noise(self):
num_tx_ant = 4
num_rx_ant = 16
batch_size = 100000
dtype=tf.complex128
r_tx = exp_corr_mat(0.4, num_tx_ant, dtype)
r_rx = exp_corr_mat(0.99, num_rx_ant, dtype)
kron = KroneckerModel(r_tx, r_rx)
chn = FlatFadingChannel(num_tx_ant, num_rx_ant, spatial_corr=kron, add_awgn=True, return_channel=True, dtype=dtype)
x = QAMSource(4, dtype=dtype)([batch_size, num_tx_ant])
no = 0.2
y, h = chn([x, no])
n = y - tf.squeeze(tf.matmul(h, tf.expand_dims(x, -1)))
noise_var = np.var(n)
self.assertAlmostEqual(no, noise_var, places=3)
def test_no_return_channel(self):
num_tx_ant = 4
num_rx_ant = 16
batch_size = 1000000
dtype=tf.complex64
chn = FlatFadingChannel(num_tx_ant, num_rx_ant, add_awgn=True, return_channel=False, dtype=dtype)
x = QAMSource(4, dtype=dtype)([batch_size, num_tx_ant])
no = 0.2
y = chn([x, no])
y_var = np.var(y)
self.assertAlmostEqual(y_var , num_tx_ant + no, places=2)
def test_property_setter(self):
num_tx_ant = 4
num_rx_ant = 16
r_tx = exp_corr_mat(0.4, num_tx_ant)
r_rx = exp_corr_mat(0.99, num_rx_ant)
kron = KroneckerModel(r_tx, r_rx)
chn = FlatFadingChannel(num_tx_ant, num_rx_ant, add_awgn=True, return_channel=True)
qam_source = QAMSource(4)
@tf.function()
def func():
chn.spatial_corr = kron
x = qam_source([1000000, num_tx_ant])
no = 0.2
y, h = chn([x, no])
r_tx_hat = tf.reduce_mean(tf.matmul(h, h, adjoint_a=True), 0)
r_rx_hat = tf.reduce_mean(tf.matmul(h, h, adjoint_b=True), 0)
return r_tx_hat, r_rx_hat
r_tx_hat = tf.zeros_like(r_tx)
r_rx_hat = tf.zeros_like(r_rx)
iterations = 10
for i in range(iterations):
tmp = func()
r_tx_hat += tmp[0]/iterations/num_rx_ant
r_rx_hat += tmp[1]/iterations/num_tx_ant
print(np.max(np.abs(r_rx-r_rx_hat)))
self.assertTrue(np.allclose(r_tx, r_tx_hat, atol=1e-3))
self.assertTrue(np.allclose(r_rx, r_rx_hat, atol=1e-3))