diff options
Diffstat (limited to 'gr-fec/python/fec/polar/common.py')
-rw-r--r-- | gr-fec/python/fec/polar/common.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/gr-fec/python/fec/polar/common.py b/gr-fec/python/fec/polar/common.py new file mode 100644 index 0000000000..b4b152de61 --- /dev/null +++ b/gr-fec/python/fec/polar/common.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# +# Copyright 2015 Free Software Foundation, Inc. +# +# 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. +# + + +import numpy as np +from helper_functions import * + +''' +PolarCommon holds value checks and common initializer code for both Encoder and Decoder. +''' + + +class PolarCommon: + def __init__(self, n, k, frozen_bit_position, frozenbits=None): + if not is_power_of_two(n): + raise ValueError("n={0} is not a power of 2!".format(n)) + if frozenbits is None: + frozenbits = np.zeros(n - k, dtype=np.int) + if not len(frozenbits) == n - k: + raise ValueError("len(frozenbits)={0} is not equal to n-k={1}!".format(len(frozenbits), n - k)) + if not frozenbits.dtype == np.int: + frozenbits = frozenbits.astype(dtype=int) + if not len(frozen_bit_position) == (n - k): + raise ValueError("len(frozen_bit_position)={0} is not equal to n-k={1}!".format(len(frozen_bit_position), n - k)) + if not frozen_bit_position.dtype == np.int: + frozen_bit_position = frozen_bit_position.astype(dtype=int) + + self.bit_reverse_positions = self._vector_bit_reversed(np.arange(n, dtype=int), int(np.log2(n))) + self.N = n + self.power = int(np.log2(self.N)) + self.K = k + self.frozenbits = frozenbits + self.frozen_bit_position = frozen_bit_position + self.info_bit_position = np.delete(np.arange(self.N), self.frozen_bit_position) + + def _insert_frozen_bits(self, u): + prototype = np.empty(self.N, dtype=int) + prototype[self.frozen_bit_position] = self.frozenbits + prototype[self.info_bit_position] = u + return prototype + + def _extract_info_bits(self, y): + return y[self.info_bit_position] + + def _reverse_bits(self, vec): + return vec[self.bit_reverse_positions] + + def _vector_bit_reversed(self, vec, n): + return bit_reverse_vector(vec, n) + + def info_print(self): + print "POLAR code ({0}, {1})".format(self.N, self.K) |