forked from NVlabs/sionna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ofdm.py
197 lines (176 loc) · 7.91 KB
/
test_ofdm.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
#
# 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.ofdm import OFDMModulator, OFDMDemodulator, ResourceGrid, ResourceGridMapper, ResourceGridDemapper
from sionna.mimo import StreamManagement
from sionna.utils import QAMSource
from tensorflow.keras import Model
import pytest
import unittest
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 TestOFDMModulator(unittest.TestCase):
def test_cyclic_prefixes(self):
batch_size = 64
fft_size = 72
num_ofdm_symbols = 14
qam_source = QAMSource(4)
for cp_length in range(1,fft_size+1):
modulator = OFDMModulator(cp_length)
x = qam_source([batch_size, num_ofdm_symbols, fft_size])
x_time = modulator(x)
x_time = tf.reshape(x_time, [batch_size, num_ofdm_symbols, -1])
self.assertTrue(np.array_equal(x_time[...,:cp_length], x_time[...,-cp_length:]))
cp_length = fft_size+1
modulator = OFDMModulator(cp_length)
x = qam_source([batch_size, num_ofdm_symbols, fft_size])
with self.assertRaises(AssertionError):
x_time = modulator(x)
def test_higher_dimensions(self):
batch_size = [64, 12, 6]
fft_size = 72
num_ofdm_symbols = 14
qam_source = QAMSource(4)
for cp_length in range(1,fft_size+1):
modulator = OFDMModulator(cp_length)
x = qam_source(batch_size + [num_ofdm_symbols, fft_size])
x_time = modulator(x)
x_time = tf.reshape(x_time, batch_size + [num_ofdm_symbols, -1])
self.assertTrue(np.array_equal(x_time[...,:cp_length], x_time[...,-cp_length:]))
class TestOFDMDemodulator(unittest.TestCase):
def test_cyclic_prefixes(self):
batch_size = 64
fft_size = 72
num_ofdm_symbols = 14
qam_source = QAMSource(4)
for cp_length in range(0,fft_size+1):
modulator = OFDMModulator(cp_length)
demodulator = OFDMDemodulator(fft_size, 0, cp_length)
x = qam_source([batch_size, num_ofdm_symbols, fft_size])
x_time = modulator(x)
x_hat = demodulator(x_time)
self.assertTrue(np.max(np.abs(x-x_hat))<1e-6)
def test_higher_dimensions(self):
batch_size = [64, 12, 6]
fft_size = 72
num_ofdm_symbols = 14
qam_source = QAMSource(4)
for cp_length in range(1,fft_size+1):
modulator = OFDMModulator(cp_length)
demodulator = OFDMDemodulator(fft_size, 0, cp_length)
x = qam_source(batch_size + [num_ofdm_symbols, fft_size])
x_time = modulator(x)
x_hat = demodulator(x_time)
self.assertTrue(np.max(np.abs(x-x_hat))<1e-6)
def test_overlapping_input(self):
batch_size = 64
fft_size = 72
num_ofdm_symbols = 14
qam_source = QAMSource(4)
for cp_length in [0, 12]:
modulator = OFDMModulator(cp_length)
demodulator = OFDMDemodulator(fft_size, 0, cp_length)
x = qam_source([batch_size, num_ofdm_symbols, fft_size])
x_time = modulator(x)
x_time = tf.concat([x_time, x_time[...,:10]], axis=-1)
x_hat = demodulator(x_time)
self.assertTrue(np.max(np.abs(x-x_hat))<1e-6)
class TestOFDMModDemod(unittest.TestCase):
def test_end_to_end(self):
"""E2E test verying that all shapes can be properly inferred (see Issue #7)"""
class E2ESystem(Model):
def __init__(self, cp_length, padding):
super().__init__()
self.cp_length = cp_length
self.padding = padding
self.fft_size = 72
self.num_ofdm_symbols = 14
self.qam = QAMSource(4)
self.mod = OFDMModulator(self.cp_length)
self.demod = OFDMDemodulator(self.fft_size, 0, self.cp_length)
@tf.function(jit_compile=True)
def call(self, batch_size):
x_rg = self.qam([batch_size, 1, 1, self.num_ofdm_symbols, self.fft_size])
x_time = self.mod(x_rg)
pad = tf.zeros_like(x_time)[...,:self.padding]
x_time = tf.concat([x_time, pad], axis=-1)
x_f = self.demod(x_time)
return x_f
for cp_length in [0,1,5,12]:
for padding in [0,1,5,71]:
e2e = E2ESystem(cp_length, padding)
self.assertEqual(e2e(128).shape, [128,1,1,e2e.num_ofdm_symbols,e2e.fft_size])
class TestResourceGridDemapper(unittest.TestCase):
def test_various_params(self):
"""data_dim dimension is omitted"""
fft_size = 72
def func(cp_length, num_tx, num_streams_per_tx):
rg = ResourceGrid(num_ofdm_symbols=14,
fft_size=fft_size,
subcarrier_spacing=30e3,
num_tx=num_tx,
num_streams_per_tx=num_streams_per_tx,
cyclic_prefix_length=cp_length)
sm = StreamManagement(np.ones([1, rg.num_tx]), rg.num_streams_per_tx)
rg_mapper = ResourceGridMapper(rg)
rg_demapper = ResourceGridDemapper(rg, sm)
modulator = OFDMModulator(rg.cyclic_prefix_length)
demodulator = OFDMDemodulator(rg.fft_size, 0, rg.cyclic_prefix_length)
qam_source = QAMSource(4)
x = qam_source([128, rg.num_tx, rg.num_streams_per_tx, rg.num_data_symbols])
x_rg = rg_mapper(x)
x_time = modulator(x_rg)
y = demodulator(x_time)
x_hat = rg_demapper(y)
return np.max(np.abs(x-x_hat))
for cp_length in [0,1,12,fft_size]:
for num_tx in [1,2,3,8]:
for num_streams_per_tx in [1,2,3]:
err = func(cp_length, num_tx, num_streams_per_tx)
self.assertTrue(err<1e-6)
def test_data_dim(self):
"""data_dim dimension is provided"""
fft_size = 72
def func(cp_length, num_tx, num_streams_per_tx):
rg = ResourceGrid(num_ofdm_symbols=14,
fft_size=fft_size,
subcarrier_spacing=30e3,
num_tx=num_tx,
num_streams_per_tx=num_streams_per_tx,
cyclic_prefix_length=cp_length)
sm = StreamManagement(np.ones([1, rg.num_tx]), rg.num_streams_per_tx)
rg_mapper = ResourceGridMapper(rg)
rg_demapper = ResourceGridDemapper(rg, sm)
modulator = OFDMModulator(rg.cyclic_prefix_length)
demodulator = OFDMDemodulator(rg.fft_size, 0, rg.cyclic_prefix_length)
qam_source = QAMSource(4)
x = qam_source([128, rg.num_tx, rg.num_streams_per_tx, rg.num_data_symbols])
x_rg = rg_mapper(x)
x_time = modulator(x_rg)
y = demodulator(x_time)
# Stack inputs to ResourceGridDemppaer to simulate the data_dim dimension
y = tf.stack([y,y,y], axis=-1)
x_hat = rg_demapper(y)
x = tf.stack([x,x,x], axis=-1)
return np.max(np.abs(x-x_hat))
for cp_length in [0,1,12,fft_size]:
for num_tx in [1,2,3,8]:
for num_streams_per_tx in [1,2,3]:
err = func(cp_length, num_tx, num_streams_per_tx)
self.assertTrue(err<1e-6)