summaryrefslogtreecommitdiff
path: root/gr-fec/python/fec/qa_polar_encoder.py
blob: 90190cd719be47356ac8132b78cf59014e6a83c2 (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
#!/usr/bin/env python
#
# Copyright 2015 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, blocks
import fec_swig as fec
import numpy as np

from extended_encoder import extended_encoder
from polar.encoder import PolarEncoder
import polar.channel_construction as cc

# import os
# print('PID:', os.getpid())
# raw_input('tell me smth')


class test_polar_encoder(gr_unittest.TestCase):

    def setUp(self):
        self.tb = gr.top_block()

    def tearDown(self):
        self.tb = None

    def test_001_setup(self):
        block_size = 16
        num_info_bits = 8
        frozen_bit_positions = cc.frozen_bit_positions(block_size, num_info_bits, 0.0)
        frozen_bit_values = np.array([],)

        polar_encoder = fec.polar_encoder.make(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values)

        self.assertEqual(block_size, polar_encoder.get_output_size())
        self.assertEqual(num_info_bits, polar_encoder.get_input_size())
        self.assertFloatTuplesAlmostEqual((float(num_info_bits) / block_size, ), (polar_encoder.rate(), ))
        self.assertFalse(polar_encoder.set_frame_size(10))

    def test_002_work_function_packed(self):
        is_packed = True
        block_size = 256
        num_info_bits = block_size // 2

        data, ref, polar_encoder = self.get_test_data(block_size, num_info_bits, 1, is_packed)
        src = blocks.vector_source_b(data, False)
        enc_block = extended_encoder(polar_encoder, None, '11')
        snk = blocks.vector_sink_b(1)

        self.tb.connect(src, enc_block, snk)
        self.tb.run()

        res = np.array(snk.data()).astype(dtype=int)
        self.assertTupleEqual(tuple(res), tuple(ref))

    def test_003_work_function_unpacked(self):
        is_packed = False
        block_size = 256
        num_info_bits = block_size // 2

        data, ref, polar_encoder = self.get_test_data(block_size, num_info_bits, 1, is_packed)
        src = blocks.vector_source_b(data, False)
        enc_block = extended_encoder(polar_encoder, None, '11')
        snk = blocks.vector_sink_b(1)

        self.tb.connect(src, enc_block, snk)
        self.tb.run()

        res = np.array(snk.data()).astype(dtype=int)
        self.assertTupleEqual(tuple(res), tuple(ref))

    def test_004_big_input(self):
        is_packed = False
        num_blocks = 30
        block_size = 1024
        num_info_bits = block_size // 8

        data, ref, polar_encoder = self.get_test_data(block_size, num_info_bits, num_blocks, is_packed)
        src = blocks.vector_source_b(data, False)
        enc_block = extended_encoder(polar_encoder, None, '11')
        snk = blocks.vector_sink_b(1)

        self.tb.connect(src, enc_block, snk)
        self.tb.run()

        res = np.array(snk.data()).astype(dtype=int)

        print(res)
        print(ref)
        self.assertTupleEqual(tuple(res), tuple(ref))

    def get_test_data(self, block_size, num_info_bits, num_blocks, is_packed):
        num_frozen_bits = block_size - num_info_bits
        frozen_bit_positions = cc.frozen_bit_positions(block_size, num_info_bits, 0.0)
        frozen_bit_values = np.array([0] * num_frozen_bits,)
        python_encoder = PolarEncoder(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values)

        data = np.array([], dtype=int)
        ref = np.array([], dtype=int)
        for i in range(num_blocks):
            d = np.random.randint(2, size=num_info_bits)
            data = np.append(data, d)
            ref = np.append(ref, python_encoder.encode(d))
        polar_encoder = fec.polar_encoder.make(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values, is_packed)
        return data, ref, polar_encoder






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