diff options
author | Johannes Demel <ufcsy@student.kit.edu> | 2015-06-15 12:04:34 +0200 |
---|---|---|
committer | Johannes Demel <ufcsy@student.kit.edu> | 2015-09-21 10:45:12 +0200 |
commit | 87670ad6f14e61bf4bde6c6e5b18d19b262fe33f (patch) | |
tree | 5f9b0b61f6c0395e7f9f4f8760326e6b4621c5e2 /gr-fec/python | |
parent | 8ac5a86f98d5a545cf65dda77c960cf273ee7aca (diff) |
polar: encoder implemented in C++
Diffstat (limited to 'gr-fec/python')
-rw-r--r-- | gr-fec/python/fec/polar/__init__.py | 22 | ||||
-rw-r--r-- | gr-fec/python/fec/polar/helper_functions.py | 28 | ||||
-rwxr-xr-x | gr-fec/python/fec/polar/testbed.py | 4 | ||||
-rw-r--r-- | gr-fec/python/fec/qa_polar_encoder.py | 133 |
4 files changed, 186 insertions, 1 deletions
diff --git a/gr-fec/python/fec/polar/__init__.py b/gr-fec/python/fec/polar/__init__.py new file mode 100644 index 0000000000..497c064995 --- /dev/null +++ b/gr-fec/python/fec/polar/__init__.py @@ -0,0 +1,22 @@ +#!/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. + +# turn this folder into a Python module diff --git a/gr-fec/python/fec/polar/helper_functions.py b/gr-fec/python/fec/polar/helper_functions.py index 1d82457f17..ffa4fc17dc 100644 --- a/gr-fec/python/fec/polar/helper_functions.py +++ b/gr-fec/python/fec/polar/helper_functions.py @@ -70,6 +70,29 @@ def pack_byte(bits): return res +def get_frozen_bit_positions(directory, n, k, p): + import glob, os + os.chdir(directory) + prefix = 'frozen_bit_positions_' + prefix_len = len(prefix) + for file in glob.glob("*.npy"): + if file.find(prefix) < 0: + continue + filename = file + file = file[file.find(prefix) + prefix_len:] + file = file[:-4] + file = file.split('_') + nstr = [x for x in file if x.startswith('n')] + kstr = [x for x in file if x.startswith('k')] + pstr = [x for x in file if x.startswith('p')] + nstr = int(nstr[0][1:]) + kstr = int(kstr[0][1:]) + pstr = float(pstr[0][1:]) + if n == nstr and k == kstr: + return np.load(filename) + return np.arange(k) + + def main(): print 'helper functions' @@ -79,6 +102,11 @@ def main(): k = n // 2 eta = 0.3 + # frozen_bit_positions = get_frozen_bit_positions('.', 256, 128, 0.11) + # print(frozen_bit_positions) + + print(np.arange(16)) + print bit_reverse_vector(np.arange(16), 4) if __name__ == '__main__': main() diff --git a/gr-fec/python/fec/polar/testbed.py b/gr-fec/python/fec/polar/testbed.py index f34f8a6b96..4ace91ee47 100755 --- a/gr-fec/python/fec/polar/testbed.py +++ b/gr-fec/python/fec/polar/testbed.py @@ -114,7 +114,9 @@ def channel_analysis(): print(np.min(channel_counter), np.max(channel_counter)) channel_counter[0] = np.min(channel_counter) good_indices = find_good_indices(channel_counter, channel_counter.size // 2) - frozen_bit_positions = np.where(good_indices > 0) + info_bit_positions = np.where(good_indices > 0) + print(info_bit_positions) + frozen_bit_positions = np.delete(np.arange(channel_counter.size), info_bit_positions) print(frozen_bit_positions) np.save('frozen_bit_positions_n256_k128_p0.11.npy', frozen_bit_positions) good_indices *= 2000 diff --git a/gr-fec/python/fec/qa_polar_encoder.py b/gr-fec/python/fec/qa_polar_encoder.py new file mode 100644 index 0000000000..03629354f0 --- /dev/null +++ b/gr-fec/python/fec/qa_polar_encoder.py @@ -0,0 +1,133 @@ +#!/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 +from _qa_helper import _qa_helper +import numpy as np + +from extended_encoder import extended_encoder +from extended_decoder import extended_decoder +from polar.encoder import PolarEncoder +from polar.helper_functions import get_frozen_bit_positions +from polar.helper_functions import bit_reverse_vector + + +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 = np.arange(block_size - num_info_bits) + 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(self): + block_size = 256 + num_info_bits = 128 + num_frozen_bits = block_size - num_info_bits + frozen_bit_positions = get_frozen_bit_positions('polar', block_size, num_frozen_bits, 0.11) + frozen_bit_values = np.array([0] * num_frozen_bits,) + python_encoder = PolarEncoder(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values) + + is_packed = False + polar_encoder = fec.polar_encoder.make(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values, is_packed) + + data = np.ones(num_info_bits, dtype=int) + src = blocks.vector_source_b(data, False) + packer = blocks.pack_k_bits_bb(8) + enc_block = extended_encoder(polar_encoder, None, '11') + unpacker = blocks.unpack_k_bits_bb(8) + snk = blocks.vector_sink_b(1) + + if is_packed: + self.tb.connect(src, packer, enc_block, unpacker, snk) + else: + self.tb.connect(src, enc_block, snk) + self.tb.run() + + res = np.array(snk.data()).astype(dtype=int) + penc = python_encoder.encode(data) + + print(res) + print(penc) + self.assertTupleEqual(tuple(res), tuple(penc)) + + def test_003_big_input(self): + is_packed = False + num_blocks = 30 + block_size = 256 + num_info_bits = 128 + num_frozen_bits = block_size - num_info_bits + frozen_bit_positions = get_frozen_bit_positions('/home/johannes/src/gnuradio-polar/gr-fec/python/fec/polar', block_size, num_frozen_bits, 0.11) + frozen_bit_values = np.array([0] * num_frozen_bits,) + python_encoder = PolarEncoder(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values) + + polar_encoder = fec.polar_encoder.make(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values, is_packed) + + 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)) + + + src = blocks.vector_source_b(data, False) + packer = blocks.pack_k_bits_bb(8) + enc_block = extended_encoder(polar_encoder, None, '11') + unpacker = blocks.unpack_k_bits_bb(8) + snk = blocks.vector_sink_b(1) + + if is_packed: + self.tb.connect(src, packer, enc_block, unpacker, snk) + else: + self.tb.connect(src, enc_block, snk) + self.tb.run() + + res = np.array(snk.data()).astype(dtype=int) + # penc = python_encoder.encode(data) + + print(res) + print(ref) + self.assertTupleEqual(tuple(res), tuple(ref)) + + + + + +if __name__ == '__main__': + gr_unittest.run(test_polar_encoder) + |