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
203
204
205
206
207
208
209
210
211
212
213
214
|
#!/usr/bin/env python
#
# Copyright 2012 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
from gnuradio import gr, gr_unittest
import blocks_swig as blocks
import fft_swig as fft
import analog_swig as analog
import digital_swig as digital
try: import pmt
except: from gruel import pmt
import numpy
class qa_ofdm_serializer_vcc (gr_unittest.TestCase):
def setUp (self):
self.tb = gr.top_block ()
def tearDown (self):
self.tb = None
def test_001_simple (self):
""" Standard test """
fft_len = 16
tx_symbols = range(1, 16);
tx_symbols = (0, 1, 1j, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 2j, 6, 0,
0, 7, 8, 3j, 9, 0, 0, 0, 0, 0, 0, 10, 4j, 11, 12, 0,
0, 13, 1j, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 2j, 0, 0)
expected_result = tuple(range(1, 16)) + (0, 0, 0)
occupied_carriers = ((1, 3, 4, 11, 12, 14), (1, 2, 4, 11, 13, 14),)
n_syms = len(tx_symbols)/fft_len
tag_name = "len"
tag = gr.gr_tag_t()
tag.offset = 0
tag.key = pmt.string_to_symbol(tag_name)
tag.value = pmt.from_long(n_syms)
src = gr.vector_source_c(tx_symbols, False, fft_len, (tag,))
serializer = digital.ofdm_serializer_vcc(fft_len, occupied_carriers, tag_name, "", 0, False)
sink = gr.vector_sink_c()
self.tb.connect(src, serializer, sink)
self.tb.run ()
self.assertEqual(sink.data(), expected_result)
self.assertEqual(len(sink.tags()), 1)
result_tag = sink.tags()[0]
self.assertEqual(pmt.symbol_to_string(result_tag.key), tag_name)
self.assertEqual(pmt.to_long(result_tag.value), n_syms * len(occupied_carriers[0]))
def test_002_with_offset (self):
""" Standard test, carrier offset """
fft_len = 16
tx_symbols = range(1, 16);
tx_symbols = (0, 0, 1, 1j, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 2j, 6,
0, 0, 7, 8, 3j, 9, 0, 0, 0, 0, 0, 0, 10, 4j, 11, 12,
0, 0, 13, 1j, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 2j, 0)
carr_offset = 1 # Compare this with tx_symbols from the previous test
expected_result = tuple(range(1, 16)) + (0, 0, 0)
occupied_carriers = ((1, 3, 4, 11, 12, 14), (1, 2, 4, 11, 13, 14),)
n_syms = len(tx_symbols)/fft_len
tag_name = "len"
tag = gr.gr_tag_t()
tag.offset = 0
tag.key = pmt.string_to_symbol(tag_name)
tag.value = pmt.from_long(n_syms)
offsettag = gr.gr_tag_t()
offsettag.offset = 0
offsettag.key = pmt.string_to_symbol("ofdm_sync_carr_offset")
offsettag.value = pmt.from_long(carr_offset)
src = gr.vector_source_c(tx_symbols, False, fft_len, (tag, offsettag))
serializer = digital.ofdm_serializer_vcc(fft_len, occupied_carriers, tag_name, "", 0, False)
sink = gr.vector_sink_c()
self.tb.connect(src, serializer, sink)
self.tb.run ()
self.assertEqual(sink.data(), expected_result)
self.assertEqual(len(sink.tags()), 2)
for tag in sink.tags():
if pmt.symbol_to_string(tag.key) == tag_name:
self.assertEqual(pmt.to_long(tag.value), n_syms * len(occupied_carriers[0]))
def test_003_connect (self):
""" Connect carrier_allocator to ofdm_serializer,
make sure output==input """
fft_len = 8
n_syms = 10
occupied_carriers = ((1, 2, 6, 7),)
pilot_carriers = ((3,),(5,))
pilot_symbols = ((1j,),(-1j,))
tx_data = tuple([numpy.random.randint(0, 10) for x in range(4 * n_syms)])
tag_name = "len"
tag = gr.gr_tag_t()
tag.offset = 0
tag.key = pmt.string_to_symbol(tag_name)
tag.value = pmt.from_long(len(tx_data))
src = gr.vector_source_c(tx_data, False, 1, (tag,))
alloc = digital.ofdm_carrier_allocator_cvc(fft_len,
occupied_carriers,
pilot_carriers,
pilot_symbols,
tag_name)
serializer = digital.ofdm_serializer_vcc(alloc)
sink = gr.vector_sink_c()
self.tb.connect(src, alloc, serializer, sink)
self.tb.run ()
self.assertEqual(sink.data(), tx_data)
def test_004_connect (self):
"""
Advanced test:
- Allocator -> IFFT -> Frequency offset -> FFT -> Serializer
- FFT does shift (moves DC to middle)
- Make sure input == output
- Frequency offset is -2 carriers
"""
fft_len = 8
n_syms = 2
carr_offset = -2
freq_offset = 2 * numpy.pi * carr_offset / fft_len # If the sampling rate == 1
occupied_carriers = ((1, 2, -2, -1),)
pilot_carriers = ((3,),(5,))
pilot_symbols = ((1j,),(-1j,))
tx_data = tuple([numpy.random.randint(0, 10) for x in range(4 * n_syms)])
#tx_data = (1,) * occupied_carriers[0] * n_syms
tag_name = "len"
tag = gr.gr_tag_t()
tag.offset = 0
tag.key = pmt.string_to_symbol(tag_name)
tag.value = pmt.from_long(len(tx_data))
offsettag = gr.gr_tag_t()
offsettag.offset = 0
offsettag.key = pmt.string_to_symbol("ofdm_sync_carr_offset")
offsettag.value = pmt.from_long(carr_offset)
src = gr.vector_source_c(tx_data, False, 1, (tag, offsettag))
alloc = digital.ofdm_carrier_allocator_cvc(fft_len,
occupied_carriers,
pilot_carriers,
pilot_symbols,
tag_name)
tx_ifft = fft.fft_vcc(fft_len, False, ())
offset_sig = analog.sig_source_c(1.0, analog.GR_COS_WAVE, freq_offset, 1.0)
mixer = blocks.multiply_cc()
rx_fft = fft.fft_vcc(fft_len, True, (), True)
serializer = digital.ofdm_serializer_vcc(alloc)
sink = gr.vector_sink_c()
self.tb.connect(
src, alloc, tx_ifft,
blocks.vector_to_stream(gr.sizeof_gr_complex, fft_len),
(mixer, 0),
blocks.stream_to_vector(gr.sizeof_gr_complex, fft_len),
rx_fft, serializer, sink
)
self.tb.connect(offset_sig, (mixer, 1))
self.tb.run ()
# FIXME check this
#self.assertEqual(sink.data(), tx_data)
def test_005_packet_len_tag (self):
""" Standard test """
fft_len = 16
tx_symbols = range(1, 16);
tx_symbols = (0, 1, 1j, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 2j, 6, 0,
0, 7, 8, 3j, 9, 0, 0, 0, 0, 0, 0, 10, 4j, 11, 12, 0,
0, 13, 1j, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 2j, 0, 0)
expected_result = tuple(range(1, 16))
occupied_carriers = ((1, 3, 4, 11, 12, 14), (1, 2, 4, 11, 13, 14),)
n_syms = len(tx_symbols)/fft_len
tag_name = "len"
tag = gr.gr_tag_t()
tag.offset = 0
tag.key = pmt.string_to_symbol(tag_name)
tag.value = pmt.from_long(n_syms)
tag2 = gr.gr_tag_t()
tag2.offset = 0
tag2.key = pmt.string_to_symbol("packet_len")
tag2.value = pmt.from_long(len(expected_result))
src = gr.vector_source_c(tx_symbols, False, fft_len, (tag, tag2))
serializer = digital.ofdm_serializer_vcc(fft_len, occupied_carriers, tag_name, "packet_len", 0, False)
sink = gr.vector_sink_c()
self.tb.connect(src, serializer, sink)
self.tb.run ()
self.assertEqual(sink.data(), expected_result)
self.assertEqual(len(sink.tags()), 1)
result_tag = sink.tags()[0]
self.assertEqual(pmt.symbol_to_string(result_tag.key), "packet_len")
self.assertEqual(pmt.to_long(result_tag.value), len(expected_result))
def test_099 (self):
""" Make sure it fails if it should """
fft_len = 16
occupied_carriers = ((1, 3, 4, 11, 12, 17),)
tag_name = "len"
self.assertRaises(RuntimeError, digital.ofdm_serializer_vcc, fft_len, occupied_carriers, tag_name)
if __name__ == '__main__':
#gr_unittest.run(qa_ofdm_serializer_vcc, "qa_ofdm_serializer_vcc.xml")
gr_unittest.run(qa_ofdm_serializer_vcc)
|