GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
cc_encoder.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2013-2014 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_FEC_CC_ENCODER_H
12 #define INCLUDED_FEC_CC_ENCODER_H
13 
14 #include <gnuradio/fec/api.h>
15 #include <gnuradio/fec/cc_common.h>
16 #include <gnuradio/fec/encoder.h>
17 #include <map>
18 #include <string>
19 
20 namespace gr {
21 namespace fec {
22 namespace code {
23 
24 /*!
25  * \brief Convolutional Code Encoding class.
26  * \ingroup error_coding_blk
27  *
28  * \details
29  * This class performs convolutional encoding for unpacked bits
30  * for frames of a constant length. This class is general in its
31  * application of the convolutional encoding and allows us to
32  * specify the constraint length, the coding rate, and the
33  * polynomials used in the coding process.
34  *
35  * The parameter \p k sets the constraint length directly. We
36  * set the coding rate by setting \p rate to R given a desired
37  * rate of 1/R. That is, for a rate 1/2 coder, we would set \p
38  * rate to 2. And the polynomial is specified as a vector of
39  * integers, where each integer represents the coding polynomial
40  * for a different arm of the code. The number of polynomials
41  * given must be the same as the value \p rate.
42  *
43  * The encoding object holds a shift register that takes in each
44  * bit from the input stream and then ANDs the shift register
45  * with each polynomial, and places the parity of the result
46  * into the output stream. The output stream is therefore also
47  * unpacked bits.
48  *
49  * The encoder is set up with a number of bits per frame in the
50  * constructor. When not being used in a tagged stream mode,
51  * this encoder will only process frames of the length provided
52  * here. If used in a tagged stream block, this setting becomes
53  * the maximum allowable frame size that the block may process.
54  *
55  * The \p mode is a cc_mode_t that specifies how the convolutional
56  * encoder will behave and under what conditions.
57  *
58  * \li 'CC_STREAMING': mode expects an uninterrupted flow of
59  * samples into the encoder, and the output stream is
60  * continually encoded.
61  *
62  * \li 'CC_TERMINATED': is a mode designed for packet-based systems. This mode
63  * flushes the encoder with K-1 bits which adds rate*(K-1) bits to the output.
64  * This improves the protection of the last bits of a block and helps the
65  * decoder.
66  *
67  * \li 'CC_TAILBITING': is another packet-based method. Instead of adding bits
68  * onto the end of a packet (as with 'CC_TERMINATED'), this mode will
69  * pre-initialize the state of the encoder with a packet’s last (k-1) bits.
70  *
71  * \li 'CC_TRUNCATED': a truncated code always resets the registers
72  * to the \p start_state between frames.
73  *
74  * A common convolutional encoder uses K=7, Rate=1/2, and the polynomials
75  * \li 1 + x^2 + x^3 + x^5 + x^6
76  * \li 1 + x + x^2 + x^3 + x^6
77  * This is the Voyager code from NASA.
78  *
79  * Another encoder class is provided with gr-fec called the
80  * gr::fec::code::ccsds_encoder, which implements the above code
81  * that is more highly optimized for just those specific
82  * settings.
83  */
84 class FEC_API cc_encoder : virtual public generic_encoder
85 {
86 public:
87  /*!
88  * Build a convolutional code encoding FEC API object.
89  *
90  * \param frame_size Number of bits per frame; must be > 1. If using in the
91  * tagged stream style, this is the maximum allowable number of bits
92  * per frame.
93  * \param k Constraint length (K) of the encoder; must be in the range [2, 31].
94  * K = 1 implies a code without memory which does not make sense;
95  * upper limit is due the way the polynomials of the code are passed
96  * in \p polys.
97  * \param rate Inverse of the coder's rate; must be > 1.
98  * (rate=2 means 2 output bits per 1 input).
99  * \param polys Vector of polynomials as integers. The least significant bit
100  * (LSB) denotes the coefficient of exponent zero of the coding
101  * polynomial. The position of the most significant set bit
102  * (zero based counting) is \p K-1. Note: this representation
103  * is reversed compared to the common representation as found
104  * in most books and references. The common representation puts
105  * the coefficient of the highest exponent into the LSB and the
106  * coefficient of exponent zero is the highest set bit.
107  * Example: The common binary representation of the Voyager
108  * code polynomials (see above) is 1011011 and 1111001; the
109  * octal representation is 133 and 171. For this block, the
110  * binary representation must be reversed: 1101101 and 1001111;
111  * octal this is 155 and 117; decimal this is 109 and 79. Some
112  * standards (e.g. CCSDS 131.0-B-3) require the inversion of
113  * some outputs. This is supported by providing the negative
114  * value of the polynomial, e.g. -109.
115  * \param start_state Initialization state of the shift register; must be in
116  * range [0, 2^(K-1)-1] where K is the constraint length.
117  * The bits in \p start_state are also used to flush the
118  * encoder in mode 'CC_TERMINATED'.
119  * Note: Most books and references use a shift register
120  * shifting from left to right. This implementation,
121  * however, shifts from right to left. This means that
122  * the start state must be reversed. (The different shift
123  * direction is also the reason why the polynomials must
124  * be reversed as described above.)
125  * \param mode cc_mode_t mode of the encoding.
126  * \param padded true if the encoded frame should be padded
127  * to the nearest byte.
128  */
129  static generic_encoder::sptr make(int frame_size,
130  int k,
131  int rate,
132  std::vector<int> polys,
133  int start_state = 0,
134  cc_mode_t mode = CC_STREAMING,
135  bool padded = false);
136 
137  /*!
138  * Sets the uncoded frame size to \p frame_size. If \p
139  * frame_size is greater than the value given to the
140  * constructor, the frame size will be capped by that initial
141  * value and this function will return false. Otherwise, it
142  * returns true.
143  */
144  bool set_frame_size(unsigned int frame_size) override = 0;
145 
146  /*!
147  * Returns the coding rate of this encoder.
148  */
149  double rate() override = 0;
150 };
151 
152 } /* namespace code */
153 } /* namespace fec */
154 } /* namespace gr */
155 
156 #endif /* INCLUDED_FEC_CC_ENCODER_H */
enum _cc_mode_t cc_mode_t
@ CC_STREAMING
Definition: cc_common.h:17
Convolutional Code Encoding class.
Definition: cc_encoder.h:85
double rate() override=0
bool set_frame_size(unsigned int frame_size) override=0
static generic_encoder::sptr make(int frame_size, int k, int rate, std::vector< int > polys, int start_state=0, cc_mode_t mode=CC_STREAMING, bool padded=false)
Definition: generic_encoder.h:23
std::shared_ptr< generic_encoder > sptr
Definition: generic_encoder.h:37
#define FEC_API
Definition: gr-fec/include/gnuradio/fec/api.h:18
GNU Radio logging wrapper.
Definition: basic_block.h:29