summaryrefslogtreecommitdiff
path: root/gr-digital/python/digital/qa_constellation_soft_decoder_cf.py
blob: ca162f4adeb1f9a3eac81b521f05d27ac2c595b0 (plain)
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 2013-2014 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#


from gnuradio import gr, gr_unittest, digital, blocks
from math import sqrt
from numpy import random, vectorize


class test_constellation_soft_decoder(gr_unittest.TestCase):

    def setUp(self):
        random.seed(0)
        self.tb = gr.top_block()

    def tearDown(self):
        self.tb = None

    def helper_with_lut(self, prec, src_data, const_gen, const_sd_gen):
        cnst_pts, code = const_gen()
        Es = max([abs(c) for c in cnst_pts])
        lut = digital.soft_dec_table_generator(const_sd_gen, prec, Es)
        expected_result = list()
        for s in src_data:
            res = digital.calc_soft_dec_from_table(s, lut, prec, sqrt(2.0))
            expected_result += res

        cnst = digital.constellation_calcdist(cnst_pts, code, 2, 1)
        cnst.set_soft_dec_lut(lut, int(prec))
        src = blocks.vector_source_c(src_data)
        op = digital.constellation_soft_decoder_cf(cnst.base())
        dst = blocks.vector_sink_f()

        self.tb.connect(src, op)
        self.tb.connect(op, dst)
        self.tb.run()

        actual_result = dst.data()  # fetch the contents of the sink
        # print "actual result", actual_result
        # print "expected result", expected_result
        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 5)

    def helper_no_lut(self, prec, src_data, const_gen, const_sd_gen):
        cnst_pts, code = const_gen()
        cnst = digital.constellation_calcdist(cnst_pts, code, 2, 1)
        expected_result = list()
        for s in src_data:
            res = digital.calc_soft_dec(s, cnst.points(), code)
            expected_result += res

        src = blocks.vector_source_c(src_data)
        op = digital.constellation_soft_decoder_cf(cnst.base())
        dst = blocks.vector_sink_f()

        self.tb.connect(src, op)
        self.tb.connect(op, dst)
        self.tb.run()

        actual_result = dst.data()  # fetch the contents of the sink
        # print "actual result", actual_result
        # print "expected result", expected_result

        # Double vs. float precision issues between Python and C++, so
        # use only 4 decimals in comparisons.
        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 4)

    def test_constellation_soft_decoder_cf_bpsk_3(self):
        prec = 3
        src_data = (-1.0 - 1.0j, 1.0 - 1.0j, -1.0 + 1.0j, 1.0 + 1.0j,
                    -2.0 - 2.0j, 2.0 - 2.0j, -2.0 + 2.0j, 2.0 + 2.0j,
                    -0.2 - 0.2j, 0.2 - 0.2j, -0.2 + 0.2j, 0.2 + 0.2j,
                    0.3 + 0.4j, 0.1 - 1.2j, -0.8 - 0.1j, -0.4 + 0.8j,
                    0.8 + 1.0j, -0.5 + 0.1j, 0.1 + 1.2j, -1.7 - 0.9j)
        self.helper_with_lut(
            prec,
            src_data,
            digital.psk_2_0x0,
            digital.sd_psk_2_0x0)

    def test_constellation_soft_decoder_cf_bpsk_8(self):
        prec = 8
        src_data = (-1.0 - 1.0j, 1.0 - 1.0j, -1.0 + 1.0j, 1.0 + 1.0j,
                    -2.0 - 2.0j, 2.0 - 2.0j, -2.0 + 2.0j, 2.0 + 2.0j,
                    -0.2 - 0.2j, 0.2 - 0.2j, -0.2 + 0.2j, 0.2 + 0.2j,
                    0.3 + 0.4j, 0.1 - 1.2j, -0.8 - 0.1j, -0.4 + 0.8j,
                    0.8 + 1.0j, -0.5 + 0.1j, 0.1 + 1.2j, -1.7 - 0.9j)
        self.helper_with_lut(
            prec,
            src_data,
            digital.psk_2_0x0,
            digital.sd_psk_2_0x0)

    def test_constellation_soft_decoder_cf_bpsk_8_rand(self):
        prec = 8
        src_data = vectorize(complex)(
            2 * random.randn(100), 2 * random.randn(100))
        self.helper_with_lut(
            prec,
            src_data,
            digital.psk_2_0x0,
            digital.sd_psk_2_0x0)

    def test_constellation_soft_decoder_cf_bpsk_8_rand2(self):
        prec = 8
        src_data = vectorize(complex)(
            2 * random.randn(100), 2 * random.randn(100))
        self.helper_no_lut(
            prec,
            src_data,
            digital.psk_2_0x0,
            digital.sd_psk_2_0x0)

    def test_constellation_soft_decoder_cf_qpsk_3(self):
        prec = 3
        src_data = (-1.0 - 1.0j, 1.0 - 1.0j, -1.0 + 1.0j, 1.0 + 1.0j,
                    -2.0 - 2.0j, 2.0 - 2.0j, -2.0 + 2.0j, 2.0 + 2.0j,
                    -0.2 - 0.2j, 0.2 - 0.2j, -0.2 + 0.2j, 0.2 + 0.2j,
                    0.3 + 0.4j, 0.1 - 1.2j, -0.8 - 0.1j, -0.4 + 0.8j,
                    0.8 + 1.0j, -0.5 + 0.1j, 0.1 + 1.2j, -1.7 - 0.9j)
        self.helper_with_lut(
            prec,
            src_data,
            digital.psk_4_0x0_0_1,
            digital.sd_psk_4_0x0_0_1)

    def test_constellation_soft_decoder_cf_qpsk_8(self):
        prec = 8
        src_data = (-1.0 - 1.0j, 1.0 - 1.0j, -1.0 + 1.0j, 1.0 + 1.0j,
                    -2.0 - 2.0j, 2.0 - 2.0j, -2.0 + 2.0j, 2.0 + 2.0j,
                    -0.2 - 0.2j, 0.2 - 0.2j, -0.2 + 0.2j, 0.2 + 0.2j,
                    0.3 + 0.4j, 0.1 - 1.2j, -0.8 - 0.1j, -0.4 + 0.8j,
                    0.8 + 1.0j, -0.5 + 0.1j, 0.1 + 1.2j, -1.7 - 0.9j)
        self.helper_with_lut(
            prec,
            src_data,
            digital.psk_4_0x0_0_1,
            digital.sd_psk_4_0x0_0_1)

    def test_constellation_soft_decoder_cf_qpsk_8_rand(self):
        prec = 8
        src_data = vectorize(complex)(
            2 * random.randn(100), 2 * random.randn(100))
        self.helper_with_lut(
            prec,
            src_data,
            digital.psk_4_0x0_0_1,
            digital.sd_psk_4_0x0_0_1)

    def test_constellation_soft_decoder_cf_qpsk_8_rand2(self):
        prec = 8
        src_data = vectorize(complex)(
            2 * random.randn(100), 2 * random.randn(100))
        self.helper_no_lut(
            prec,
            src_data,
            digital.psk_4_0x0_0_1,
            digital.sd_psk_4_0x0_0_1)

    def test_constellation_soft_decoder_cf_qam16_3(self):
        prec = 3
        src_data = (-1.0 - 1.0j, 1.0 - 1.0j, -1.0 + 1.0j, 1.0 + 1.0j,
                    -2.0 - 2.0j, 2.0 - 2.0j, -2.0 + 2.0j, 2.0 + 2.0j,
                    -0.2 - 0.2j, 0.2 - 0.2j, -0.2 + 0.2j, 0.2 + 0.2j,
                    0.3 + 0.4j, 0.1 - 1.2j, -0.8 - 0.1j, -0.4 + 0.8j,
                    0.8 + 1.0j, -0.5 + 0.1j, 0.1 + 1.2j, -1.7 - 0.9j)
        self.helper_with_lut(
            prec,
            src_data,
            digital.qam_16_0x0_0_1_2_3,
            digital.sd_qam_16_0x0_0_1_2_3)

    def test_constellation_soft_decoder_cf_qam16_8(self):
        prec = 8
        src_data = (-1.0 - 1.0j, 1.0 - 1.0j, -1.0 + 1.0j, 1.0 + 1.0j,
                    -2.0 - 2.0j, 2.0 - 2.0j, -2.0 + 2.0j, 2.0 + 2.0j,
                    -0.2 - 0.2j, 0.2 - 0.2j, -0.2 + 0.2j, 0.2 + 0.2j,
                    0.3 + 0.4j, 0.1 - 1.2j, -0.8 - 0.1j, -0.4 + 0.8j,
                    0.8 + 1.0j, -0.5 + 0.1j, 0.1 + 1.2j, -1.7 - 0.9j)
        self.helper_with_lut(
            prec,
            src_data,
            digital.qam_16_0x0_0_1_2_3,
            digital.sd_qam_16_0x0_0_1_2_3)

    def test_constellation_soft_decoder_cf_qam16_8_rand(self):
        prec = 8
        src_data = vectorize(complex)(
            2 * random.randn(100), 2 * random.randn(100))
        self.helper_with_lut(
            prec,
            src_data,
            digital.qam_16_0x0_0_1_2_3,
            digital.sd_qam_16_0x0_0_1_2_3)

    def test_constellation_soft_decoder_cf_qam16_8_rand2(self):
        prec = 8
        #src_data = vectorize(complex)(2*random.randn(100), 2*random.randn(100))
        src_data = vectorize(complex)(2 * random.randn(2), 2 * random.randn(2))
        self.helper_no_lut(
            prec,
            src_data,
            digital.qam_16_0x0_0_1_2_3,
            digital.sd_qam_16_0x0_0_1_2_3)


if __name__ == '__main__':
    gr_unittest.run(test_constellation_soft_decoder)