1 /* -*- c++ -*- */
2 /*
3  * Copyright 2015 Free Software Foundation, Inc.
4  *
5  * This is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 3, or (at your
8  * option) any later version.
9  *
10  * This software is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this software; see the file COPYING.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "ldpc_bit_flip_decoder_impl.h"
26 #include <math.h>
27 #include <stdio.h>
28 #include <volk/volk.h>
29 #include <boost/assign/list_of.hpp>
30 #include <sstream>
31 #include <vector>
32 
33 namespace gr {
34 namespace fec {
35 namespace code {
36 
make(const fec_mtrx_sptr mtrx_obj,unsigned int max_iter)37 generic_decoder::sptr ldpc_bit_flip_decoder::make(const fec_mtrx_sptr mtrx_obj,
38                                                   unsigned int max_iter)
39 {
40     return generic_decoder::sptr(new ldpc_bit_flip_decoder_impl(mtrx_obj, max_iter));
41 }
42 
ldpc_bit_flip_decoder_impl(const fec_mtrx_sptr mtrx_obj,unsigned int max_iter)43 ldpc_bit_flip_decoder_impl::ldpc_bit_flip_decoder_impl(const fec_mtrx_sptr mtrx_obj,
44                                                        unsigned int max_iter)
45     : generic_decoder("ldpc_bit_flip_decoder")
46 {
47     // FEC matrix object to use for decoding
48     d_mtrx = mtrx_obj;
49 
50     d_rate = static_cast<double>(d_mtrx->k()) / static_cast<double>(d_mtrx->n());
51 
52     // Set frame size to k, the # of bits in the information word
53     // All buffers and settings will be based on this value.
54     set_frame_size(d_mtrx->k());
55     // Maximum number of iterations in the decoding algorithm
56     d_max_iterations = max_iter;
57 }
58 
~ldpc_bit_flip_decoder_impl()59 ldpc_bit_flip_decoder_impl::~ldpc_bit_flip_decoder_impl() {}
60 
get_output_size()61 int ldpc_bit_flip_decoder_impl::get_output_size() { return d_output_size; }
62 
get_input_size()63 int ldpc_bit_flip_decoder_impl::get_input_size() { return d_input_size; }
64 
set_frame_size(unsigned int frame_size)65 bool ldpc_bit_flip_decoder_impl::set_frame_size(unsigned int frame_size)
66 {
67     if (frame_size % d_mtrx->k() != 0) {
68         GR_LOG_ERROR(d_logger,
69                      boost::format("Frame size (%1% bits) must be a "
70                                    "multiple of the information word "
71                                    "size of the LDPC matrix, %2%") %
72                          frame_size % (d_mtrx->k()));
73         throw std::runtime_error("ldpc_bit_flip_decoder: cannot use frame size.");
74     }
75 
76     d_output_size = frame_size;
77     d_input_size = static_cast<int>(round(frame_size / d_rate));
78 
79     return true;
80 }
81 
rate()82 double ldpc_bit_flip_decoder_impl::rate() { return d_rate; }
83 
84 
generic_work(void * inbuffer,void * outbuffer)85 void ldpc_bit_flip_decoder_impl::generic_work(void* inbuffer, void* outbuffer)
86 {
87     // Populate the information word
88     const float* in = (const float*)inbuffer;
89     unsigned char* out = (unsigned char*)outbuffer;
90 
91     int j = 0;
92     for (int i = 0; i < d_input_size; i += d_mtrx->n()) {
93         d_mtrx->decode(&out[j], &in[i], d_mtrx->k(), d_max_iterations);
94         j += d_mtrx->k();
95     }
96 
97 } /* ldpc_bit_flip_decoder_impl::generic_work() */
98 
99 } /* namespace code */
100 } /* namespace fec */
101 } /* namespace gr */
102