GNSS-SDR  0.0.19
An Open Source GNSS Software Defined Receiver
reed_solomon.h
Go to the documentation of this file.
1 /*!
2  * \file reed_solomon.h
3  * \brief Class implementing a Reed-Solomon encoder/decoder for blocks of 255
4  * symbols and symbols of 8 bits.
5  * \author Carles Fernandez, 2021. cfernandez(at)cttc.es
6  *
7  * -----------------------------------------------------------------------------
8  *
9  * GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
10  * This file is part of GNSS-SDR.
11  *
12  * Copyright (C) 2010-2021 (see AUTHORS file for a list of contributors)
13  * SPDX-License-Identifier: GPL-3.0-or-later
14  *
15  * -----------------------------------------------------------------------------
16  */
17 
18 
19 #ifndef GNSS_SDR_REED_SOLOMON_H
20 #define GNSS_SDR_REED_SOLOMON_H
21 
22 #include <array>
23 #include <cstddef>
24 #include <cstdint>
25 #include <string>
26 #include <vector>
27 
28 
29 /** \addtogroup Core
30  * \{ */
31 /** \addtogroup System_Parameters
32  * \{ */
33 
34 /*!
35  * \brief
36  * Class implementing a Reed-Solomon encoder and decoder RS(255,K,d) where
37  * k=255-nroots is the information vector length and d=nroots+1 is the minimum
38  * Hamming distance, with symbols of 8 bits. It allows shortened RS codes.
39  */
41 {
42 public:
43  /*!
44  * \brief Default constructor.
45  * Constructs a Reed Solomon object. The encode_with_generator_poly
46  * and encode_with_generator_matrix methods are available for testing
47  * purposes.
48  *
49  * gnss_signal: empty or "E6B" sets the Galileo E6B RS parameters.
50  * "E1B" sets the Galileo E1B (INAV) RS parameters.
51  */
52  explicit ReedSolomon(const std::string& gnss_signal = std::string());
53 
54  /*!
55  * \brief Custom constructor for RS(255, 255-nroots, nroots+1). Parameters:
56  *
57  * nroots - the number of roots in the RS code generator polynomial,
58  * which is the same as the number of parity symbols in a block.
59  *
60  * minpoly - primitive polynomial.
61  *
62  * prim - the primitive root of the generator polynomial.
63  *
64  * fcr - first consecutive root of the Reed-Solomon generator polynomial.
65  *
66  * pad - the number of pad symbols in a block. If not defined, it defaults
67  * to 0.
68  *
69  * shortening - value of the shortening parameter. Defaults to 0.
70  * If different to 0, it defines a shortened RS encoder/decoder.
71  *
72  * genpoly_coeff - a vector of (nroots+1) elements containing the generator
73  * polynomial coefficients. Only used for encoding. Defaults to empty.
74  * If defined, the encode_with_generator_poly method can be used.
75  *
76  * gen_matrix - a (255-shortening)x(255-nroots-shortening) matrix containing
77  * the elements of the generator matrix. Only used for encoding. Defaults
78  * to empty. If defined, the encode_with_generator_matrix method can be
79  * used.
80  */
81  ReedSolomon(int nroots,
82  int minpoly,
83  int prim,
84  int fcr,
85  int pad = 0,
86  int shortening = 0,
87  const std::vector<uint8_t>& genpoly_coeff = std::vector<uint8_t>{},
88  const std::vector<std::vector<uint8_t>>& gen_matrix = std::vector<std::vector<uint8_t>>{});
89 
90  /*!
91  * \brief Decode an encoded block.
92  *
93  * The decoded symbols are at the first 255-nroots-shortening elements
94  * of the data_to_decode vector.
95  *
96  * The second parameter is optional, and contains a vector of erasure
97  * positions to be passed to the decoding algorithm. Defaults to empty.
98  *
99  * Returns the number of corrected errors, or -1 if decoding failed.
100  */
101  int decode(std::vector<uint8_t>& data_to_decode,
102  const std::vector<int>& erasure_positions = std::vector<int>{}) const;
103 
104  /*!
105  * \brief Encode data with the generator matrix (for testing purposes)
106  *
107  * Returns the encoded vector. It is set to all zeros if the generator
108  * matrix is not defined.
109  */
110  std::vector<uint8_t> encode_with_generator_matrix(const std::vector<uint8_t>& data_to_encode) const;
111 
112  /*!
113  * \brief Encode data with the generator polynomial (for testing purposes)
114  *
115  * Returns the encoded vector. It is set to all zeros if the generator
116  * polynomial is not defined.
117  */
118  std::vector<uint8_t> encode_with_generator_poly(const std::vector<uint8_t>& data_to_encode) const;
119 
120 private:
121  static const int d_symbols_per_block = 255; // the total number of symbols in a RS block.
122  static const int d_symsize = 8; // symbol size, in bits.
123 
124  int mod255(int x) const;
125  int rs_min(int a, int b) const;
126  int decode_rs_8(uint8_t* data, const int* eras_pos, int no_eras) const;
127 
128  uint8_t galois_mul(uint8_t a, uint8_t b) const;
129  uint8_t galois_add(uint8_t a, uint8_t b) const;
130  uint8_t galois_mul_table(uint8_t a, uint8_t b) const;
131 
132  void encode_rs_8(const uint8_t* data, uint8_t* parity) const;
133  void init_log_tables(); // initialize d_log_table and d_antilog
134  void init_alpha_tables(); // initialize d_alpha_to, d_index_of
135 
136  std::array<uint8_t, 256> d_alpha_to{}; // used for decoding
137  std::array<uint8_t, 256> d_index_of{}; // used for decoding
138  std::array<uint8_t, 256> d_log_table{}; // used for encoding
139  std::array<uint8_t, 255> d_antilog{}; // used for encoding
140 
141  std::vector<std::vector<uint8_t>> d_genmatrix; // used for encoding
142  std::vector<uint8_t> d_genpoly_coeff; // used for encoding
143  std::vector<uint8_t> d_genpoly_index; // used for encoding
144 
145  size_t d_data_in_block{}; // number of information symbols in a block
146  size_t d_rows_G{}; // number of rows of the generator matrix
147  size_t d_columns_G{}; // number of rows of the generator matrix
148  size_t d_info_symbols_shortened{}; // number of info symbols in the shortened code
149  size_t d_data_symbols_shortened{}; // number of data symbols in the shortened code
150 
151  int d_nroots{}; // number of parity symbols in a block
152  int d_prim{}; // The primitive root of the generator poly
153  int d_pad{}; // the number of pad symbols in a block
154  int d_iprim{}; // prim-th root of 1, index form
155  int d_fcr{}; // first consecutive root
156  int d_shortening{}; // shortening parameter
157 
158  uint8_t d_min_poly{}; // primitive polynomial
159  uint8_t d_a0{}; // auxiliar variable
160 };
161 
162 /** \} */
163 /** \} */
164 #endif // GNSS_SDR_REED_SOLOMON_H
std::vector< uint8_t > encode_with_generator_poly(const std::vector< uint8_t > &data_to_encode) const
Encode data with the generator polynomial (for testing purposes)
std::vector< uint8_t > encode_with_generator_matrix(const std::vector< uint8_t > &data_to_encode) const
Encode data with the generator matrix (for testing purposes)
Class implementing a Reed-Solomon encoder and decoder RS(255,K,d) where k=255-nroots is the informati...
Definition: reed_solomon.h:40
int decode(std::vector< uint8_t > &data_to_decode, const std::vector< int > &erasure_positions=std::vector< int >{}) const
Decode an encoded block.
ReedSolomon(const std::string &gnss_signal=std::string())
Default constructor. Constructs a Reed Solomon object. The encode_with_generator_poly and encode_with...