summaryrefslogtreecommitdiff
path: root/gr-fec/lib/ldpc_bit_flip_decoder_impl.cc
blob: ed8eb07db41a75ea2ef98ba46fb8a59fa40e63f2 (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
/* -*- c++ -*- */
/*
 * Copyright 2015 Free Software Foundation, Inc.
 *
 * This 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.
 *
 * This software 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 this software; 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 <ldpc_bit_flip_decoder_impl.h>
#include <math.h>
#include <boost/assign/list_of.hpp>
#include <volk/volk.h>
#include <sstream>
#include <stdio.h>
#include <vector>

namespace gr {
  namespace fec {
    namespace code {

      generic_decoder::sptr
      ldpc_bit_flip_decoder::make(const fec_mtrx_sptr mtrx_obj,
                                  unsigned int max_iter)
      {
        return generic_decoder::sptr
          (new ldpc_bit_flip_decoder_impl(mtrx_obj, max_iter));
      }

      ldpc_bit_flip_decoder_impl::ldpc_bit_flip_decoder_impl(const fec_mtrx_sptr mtrx_obj,
                                                             unsigned int max_iter)
        : generic_decoder("ldpc_bit_flip_decoder")
      {
        // FEC matrix object to use for decoding
        d_mtrx = mtrx_obj;

        d_rate = static_cast<double>(d_mtrx->k())/static_cast<double>(d_mtrx->n());

        // Set frame size to k, the # of bits in the information word
        // All buffers and settings will be based on this value.
        set_frame_size(d_mtrx->k());
        // Maximum number of iterations in the decoding algorithm
        d_max_iterations = max_iter;
      }

      ldpc_bit_flip_decoder_impl::~ldpc_bit_flip_decoder_impl()
      {
      }

      int
      ldpc_bit_flip_decoder_impl::get_output_size()
      {
        return d_output_size;
      }

      int
      ldpc_bit_flip_decoder_impl::get_input_size()
      {
        return d_input_size;
      }

      bool
      ldpc_bit_flip_decoder_impl::set_frame_size(unsigned int frame_size)
      {
        if(frame_size % d_mtrx->k() != 0) {
          GR_LOG_ERROR(d_logger, boost::format("Frame size (%1% bits) must be a "
                                               "multiple of the information word "
                                               "size of the LDPC matrix, %2%") \
                       % frame_size % (d_mtrx->k()));
          throw std::runtime_error("ldpc_bit_flip_decoder: cannot use frame size.");
        }

        d_output_size = frame_size;
        d_input_size = static_cast<int>(round(frame_size / d_rate));

        return true;
      }

      double
      ldpc_bit_flip_decoder_impl::rate()
      {
        return d_rate;
      }


      void
      ldpc_bit_flip_decoder_impl::generic_work(void *inbuffer,
                                               void *outbuffer)
      {
        // Populate the information word
        const float *in = (const float*)inbuffer;
        unsigned char *out = (unsigned char*) outbuffer;

        int j = 0;
        for(int i = 0; i < d_input_size; i+=d_mtrx->n()) {
          d_mtrx->decode(&out[j], &in[i], d_mtrx->k(), d_max_iterations);
          j += d_mtrx->k();
        }

      } /* ldpc_bit_flip_decoder_impl::generic_work() */

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