-
Notifications
You must be signed in to change notification settings - Fork 0
/
FEC_codes.h
289 lines (243 loc) · 9.35 KB
/
FEC_codes.h
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <vector>
#include <complex>
#include <cmath>
#include <iostream>
#include<valarray>
#include <cstdint>
// CRC Lookup Table
uint32_t CRC_LUT[256];
class HammingDistance {
public:
virtual int distance() const = 0; // pure virtual function
virtual ~HammingDistance() {} // virtual destructor
};
class FEC_Codes {
// #32 bit CRC Verify and 32 bit CRC Compute function
struct CRC_Data {
uint32_t message_length; // The length of the input message in bits.
uint32_t crc_length; // The number of CRC bits.
uint32_t gen_poly; // The generator polynomial.
const uint32_t* in_data; // Pointer to the input data.
uint32_t extract_crc; // A mask for extracting CRC bits.
uint32_t crc_bits; // The resulting CRC bits.
};
// Forward declarations of helper functions.
void processBits(CRC_Data* pCRC, uint32_t& r0, uint32_t& r2, uint32_t bits_to_process);
uint32_t computeCRC(CRC_Data* pCRC);
uint32_t verifyCRC(CRC_Data* pCRC);
void generateCRCLUT(const CRC_Data* pCRC);
void processBits(CRC_Data* pCRC, uint32_t& r0, uint32_t& r2, uint32_t bits_to_process) {
_to_process) {
uint32_t r1, r3, r4 = pCRC->gen_poly, tb = 1 << (pCRC->crc_length - 1);
for (uint32_t i = 0; i < bits_to_process; i++) {
r1 = r0 >> 31; // Get the most significant bit of r0.
r3 = r2 & tb; // Check if the most significant bit of LFSR is set.
r2 <<= 1; // Left shift the LFSR.
r2 |= r1; // Append the MSB of r0 to the LFSR.
if (r3) r2 ^= r4; // If the MSB of LFSR was set, XOR with the generator polynomial.
r0 <<= 1; // Left shift the message bit.
}
}
// Compute the CRC.
uint32_t computeCRC(CRC_Data* pCRC) {
uint32_t r0 = *pCRC->in_data++, r2 = 0;
uint32_t k = pCRC->message_length, m = k >> 5;
// Process 32-bit chunks.
for (uint32_t j = 0; j < m; j++) {
processBits(pCRC, r0, r2, 32);
r0 = *pCRC->in_data++;
}
// Process any remaining bits.
processBits(pCRC, r0, r2, k - 32 * m);
// Pass n zero bits for CRC computation.
r0 = 0;
processBits(pCRC, r0, r2, pCRC->crc_length);
return r2 & pCRC->extract_crc;
}
// Function to verify the CRC.
uint32_t verifyCRC(CRC_Data* pCRC) {
uint32_t r0 = *pCRC->in_data++, r2 = 0, m = 0;
uint32_t k = pCRC->message_length, total_bits = k + pCRC->crc_length;
for (uint32_t i = 0; i < total_bits; i++) {
processBits(pCRC, r0, r2, 1);
m++;
// If processed 32 bits, fetch the next chunk.
if (m == 32) {
r0 = *pCRC->in_data++;
m = 0;
}
}
return r2 & pCRC->extract_crc;
// Function to generate the CRC Lookup Table
void generateCRCLUT(const CRC_Data * pCRC) {
uint32_t r0, r1, r2;
r2 = pCRC->gen_poly; // Load generator polynomial
// Generate CRC values for all 256 possible byte values
for (uint32_t i = 0; i < 256; i++) {
r0 = (i << 24); // Place the byte value in the MSB position
// Process each bit of the byte
for (uint32_t j = 0; j < 8; j++) {
r1 = r0 >> 31; // Extract the most significant bit
r0 = r0 << 1; // Shift left to process the next bit
// If the extracted MSB was 1, XOR with the generator polynomial
if (r1) r0 = r0 ^ r2;
}
// Store the resulting CRC in the lookup table
CRC_LUT[i] = r0;
}
}
};
///////////////////////////////////////////////////////////////////
//
// Get (72, 64) Hamming Distance
// Applications: Single bit error-correction and double binary digit error detection.
// Author: Enrique Peraza
//
///////////////////////////////////////////////////////////////////
class HammingDistance64 : public HammingDistance {
private:
uint32_t data[3];
static const uint32_t hm_error_table[72]; // Assuming you have this table
statics const uint32 hm_masks[];
uint32_t r7; // Declare r7
void computeR7(const uint32_t* hm_masks) {
r7 = 0;
uint32_t r0 = data[0];
uint32_t r1 = data[1];
uint32_t r2, r3;
for (uint32_t i = 0; i < 8; i++) {
r2 = hm_masks[2 * i];
r3 = hm_masks[2 * i + 1];
r2 = r0 & r2;
r3 = r1 & r3;
r2 = r2 ^ r3;
r3 = r2 >> 16;
r2 = r2 ^ r3;
r3 = r2 >> 8;
r2 = r2 ^ r3;
r3 = r2 >> 4;
r2 = r2 ^ r3;
r3 = r2 >> 2;
r2 = r2 ^ r3;
r3 = r2 >> 1;
r2 = r2 ^ r3;
r2 = r2 & 1;
r2 = r2 << i;
r7 = r7 | r2;
r6 = data[2];
r6 = r6 ˆ r7;
r6 = r6 >> 24;
r4 = r6 & 0x80;
r6 = r6 & 0x7f;
j = 0; // assume no errors
if (r6 != 0) {
if ((r4 == 0x80) & (r6 != 0)) { // correct single bit errors
if (r6 < 72) {
r5 = hm_error_table[r6];
if (r5 < 32) {
r5 = 31 - r5;
r4 = 1 << r5;
data[0] = data[0] ˆ r4;
}
else if (r5 < 64) {
r5 = r5 - 32;
r5 = 31 - r5;
r4 = 1 << r5;
data[1] = data[1] ˆ r4;
}
}
else
j = 2; // multiple errors
}
else
j = 1; // double bit error detected
}
}
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// BCH Coding. (Can detect and correct code.
// cs.)
//
// Author: Enrique Peraza
//
//////////////////////////////////////////////////////////////////////////////////////////////
class BCHDecoder {
private:
std::vector<int> Galois_Log;
std::vector<int> Galois_aLog;
int T, N;
public:
BCHDecoder(const std::vector<int>& gLog, const std::vector<int>& gaLog, int t, int n)
: Galois_Log(gLog), Galois_aLog(gaLog), T(t), N(n) {}
std::vector<int> computeErrorLocator(const std::vector<int>& Syndromes) {
int L = 0;
std::vector<int> Elp(2 * T, 0);
std::vector<int> Tx(2 * T, 0);
Elp[0] = 1; Tx[0] = 1;
int r0 = Syndromes[0];
for (int k = 0; k < 2 * T; k++) {
std::vector<int> Conn_poly = Elp;
if (r0 != 0) {
k = 0;
for (i = 127; i >= 1; i--) {
r0 = Elp[0];
for (j = 1; j < L + 1; j++) {
r1 = i * j;
r2 = r1 >> 7; r1 = r1 & 0x7f;
r3 = log_Elp[j]; r1 = r1 + r2;
r2 = r1 >> 7; r1 = r1 & 0x7f;
r1 = r1 + r2;
r1 = r1 + r3;
r2 = Galois_aLog[r1];
r0 = r0 ^ r2;
}
if (r0 == 0) {
Error_positions[k] = 127 - i;
k++;
}
}
}
}
return Elp; // return error-locator polynomial
}
std::vector<int> correctData(const std::vector<int>& Elp, const std::vector<int>& input_data) {
int k = 0;
std::vector<int> Error_positions(2 * T, 0);
std::vector<int> data = input_data; // Create a copy of the input data
for (int i = 127; i >= 1; i--) {
int r0 = Elp[0];
for (int j = 1; j < T + 1; j++) {
int r1 = i * j;
int r2 = r1 >> 7;
r1 = r1 & 0x7f;
int r3 = Galois_Log[Elp[j]];
r1 = r1 + r2;
r2 = r1 >> 7; r1 = r1 & 0x7f;
r1 = r1 + r2;
r1 = r1 + r3;
r2 = Galois_aLog[r1];
r0 = r0 ^ r2;
}
if (r0 == 0) {
Error_positions[k] = 127 - i;
k++;
}
}
for (int i = 0; i < k; i++) {
int m = Error_positions[i];
int index = N - 1 - m;
data[index] = data[index] ^ 1;
}
return data; // return corrected data-frame
}
};
public:
HammingDistance64(uint32_t r0, uint32_t r1, uint32_t r2, const uint32_t* hm_masks) {
data[0] = r0;
data[1] = r1;
data[2] = r2;
computeR7(hm_masks); // Compute r7 during object construction
}
// Define static member outside the class.
uint32_t FEC_codes::CRC_LUT[256] = { 0 };