1#!/usr/bin/env python 2# 3# Copyright 2015 Free Software Foundation, Inc. 4# 5# This file is part of GNU Radio 6# 7# GNU Radio is free software; you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; either version 3, or (at your option) 10# any later version. 11# 12# GNU Radio is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with GNU Radio; see the file COPYING. If not, write to 19# the Free Software Foundation, Inc., 51 Franklin Street, 20# Boston, MA 02110-1301, USA. 21# 22 23from __future__ import absolute_import 24from __future__ import division 25 26import numpy as np 27 28from gnuradio import gr, gr_unittest, blocks, fec 29from gnuradio.fec.extended_encoder import extended_encoder 30from gnuradio.fec.polar.encoder import PolarEncoder 31from gnuradio.fec.polar import channel_construction as cc 32 33# import os 34# print('PID:', os.getpid()) 35# raw_input('tell me smth') 36 37 38class test_polar_encoder_systematic(gr_unittest.TestCase): 39 40 def setUp(self): 41 self.tb = gr.top_block() 42 43 def tearDown(self): 44 self.tb = None 45 46 def test_001_setup(self): 47 block_size = 16 48 num_info_bits = 8 49 frozen_bit_positions = cc.frozen_bit_positions(block_size, num_info_bits, 0.0) 50 51 polar_encoder = fec.polar_encoder_systematic.make(block_size, num_info_bits, frozen_bit_positions) 52 53 self.assertEqual(block_size, polar_encoder.get_output_size()) 54 self.assertEqual(num_info_bits, polar_encoder.get_input_size()) 55 self.assertFloatTuplesAlmostEqual((float(num_info_bits) / block_size, ), (polar_encoder.rate(), )) 56 self.assertFalse(polar_encoder.set_frame_size(10)) 57 58 def test_002_work_function_packed(self): 59 block_size = 256 60 num_info_bits = block_size // 2 61 62 data, ref, polar_encoder = self.get_test_data(block_size, num_info_bits, 1) 63 src = blocks.vector_source_b(data, False) 64 enc_block = extended_encoder(polar_encoder, None, '11') 65 snk = blocks.vector_sink_b(1) 66 67 self.tb.connect(src, enc_block, snk) 68 self.tb.run() 69 70 res = np.array(snk.data()).astype(dtype=int) 71 self.assertTupleEqual(tuple(res), tuple(ref)) 72 73 def test_004_big_input(self): 74 num_blocks = 30 75 block_size = 1024 76 num_info_bits = block_size // 8 77 78 data, ref, polar_encoder = self.get_test_data(block_size, num_info_bits, num_blocks) 79 src = blocks.vector_source_b(data, False) 80 enc_block = extended_encoder(polar_encoder, None, '11') 81 snk = blocks.vector_sink_b(1) 82 83 self.tb.connect(src, enc_block, snk) 84 self.tb.run() 85 86 res = np.array(snk.data()).astype(dtype=int) 87 self.assertTupleEqual(tuple(res), tuple(ref)) 88 89 def get_test_data(self, block_size, num_info_bits, num_blocks): 90 # helper function to set up test data and together with encoder object. 91 num_frozen_bits = block_size - num_info_bits 92 frozen_bit_positions = cc.frozen_bit_positions(block_size, num_info_bits, 0.0) 93 frozen_bit_values = np.array([0] * num_frozen_bits,) 94 python_encoder = PolarEncoder(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values) 95 96 data = np.array([], dtype=int) 97 ref = np.array([], dtype=int) 98 for i in range(num_blocks): 99 d = np.random.randint(2, size=num_info_bits) 100 data = np.append(data, d) 101 ref = np.append(ref, python_encoder.encode_systematic(d)) 102 polar_encoder = fec.polar_encoder_systematic.make(block_size, num_info_bits, frozen_bit_positions) 103 return data, ref, polar_encoder 104 105 106if __name__ == '__main__': 107 gr_unittest.run(test_polar_encoder_systematic) 108