summaryrefslogtreecommitdiff
path: root/gr-fec/lib/polar_common.cc
blob: 19caedf00ca59daeb1007943f70a525dc8ff60a8 (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
/* -*- c++ -*- */
/* 
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include <gnuradio/fec/polar_common.h>

#include <gnuradio/blocks/pack_k_bits.h>
#include <gnuradio/blocks/unpack_k_bits.h>

#include <cmath>
#include <stdexcept>
#include <iostream>
#include <vector>

namespace gr
{
  namespace fec
  {

    polar_common::polar_common(int block_size, int num_info_bits,
                               std::vector<int> frozen_bit_positions,
                               std::vector<char> frozen_bit_values, bool is_packed) :
        d_block_size(block_size), d_block_power((int) log2(float(block_size))), d_num_info_bits(num_info_bits), d_is_packed(is_packed),
            d_frozen_bit_positions(frozen_bit_positions), d_frozen_bit_values(frozen_bit_values)
    {
      if(pow(2, d_block_power) != d_block_size) {
        throw std::runtime_error("block_size MUST be a power of 2!");
      }

      unsigned int num_frozen_bits = d_block_size - d_num_info_bits;
      if(num_frozen_bits != d_frozen_bit_positions.size()) {
        throw std::runtime_error(
            "number of frozen bit positions must equal block_size - num_info_bits");
      }

      // According to papers frozen bits default to '0'.
      while(d_frozen_bit_values.size() < num_frozen_bits) {
        d_frozen_bit_values.push_back(0);
      }
      initialize_info_bit_position_vector();

      d_packer = new gr::blocks::kernel::pack_k_bits(8);
      d_unpacker = new gr::blocks::kernel::unpack_k_bits(8);
    }


    std::vector<int>
    polar_common::info_bit_position_vector()
    {
      return d_info_bit_positions;
    }

    void
    polar_common::initialize_info_bit_position_vector()
    {
      int num_frozen_bit = 0;
      int frozen_pos = d_frozen_bit_positions.at(num_frozen_bit);
      for(int i = 0; i < block_size(); i++) {
        if(i != frozen_pos) {
          d_info_bit_positions.push_back((int) i);
        }
        else {
          num_frozen_bit++;
          num_frozen_bit = std::min(num_frozen_bit, (int) (d_frozen_bit_positions.size() - 1));
          frozen_pos = d_frozen_bit_positions.at(num_frozen_bit);
        }
      }

      if((int) d_info_bit_positions.size() != num_info_bits()) {
        throw std::runtime_error("polar_common: number of info bit positions MUST equal num_info_bits (K)!");
      }
    }

    polar_common::~polar_common()
    {
      delete d_packer;
      delete d_unpacker;
    }

    long
    polar_common::bit_reverse(long value, int active_bits) const
    {
      long r = 0;
      for(int i = 0; i < active_bits; i++) {
        r <<= 1;
        r |= value & 1;
        value >>= 1;
      }
      return r;
    }

    void
    polar_common::print_packed_bit_array(const unsigned char* printed_array,
                                          const int num_bytes) const
    {
      int num_bits = num_bytes << 3;
      unsigned char* temp = new unsigned char[num_bits];
      unpacker()->unpack(temp, printed_array, num_bytes);

      std::cout << "[";
      for(int i = 0; i < num_bits; i++) {
        std::cout << (int) *(temp + i) << " ";
      }
      std::cout << "]" << std::endl;

      delete [] temp;
    }

    void
    polar_common::print_unpacked_bit_array(const unsigned char* bits,
                                                    const unsigned int num_bytes) const
    {
      std::cout << "( ";
      for(unsigned int i = 0; i < num_bytes; i++){
        std::cout << (int) *bits++ << ", ";
      }
      std::cout << ")" << std::endl;
    }

  } /* namespace fec */
} /* namespace gr */