-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbch_decoder_test.cc
130 lines (126 loc) · 5.33 KB
/
bch_decoder_test.cc
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
/*
Test for the Bose Chaudhuri Hocquenghem Decoder
Copyright 2018 Ahmet Inan <inan@aicodix.de>
*/
#include <cassert>
#include <random>
#include <iostream>
#include <functional>
#include "bitman.hh"
#include "galois_field.hh"
#include "bose_chaudhuri_hocquenghem_decoder.hh"
int main()
{
std::random_device rd;
std::default_random_engine generator(rd());
if (1) {
// NASA INTRO BCH(15, 5) T=3
typedef CODE::GaloisField<4, 0b10011, uint8_t> GF;
typedef CODE::BoseChaudhuriHocquenghemDecoder<6, 1, 5, GF> BCH;
const int D = (BCH::K + 7) / 8;
const int P = (BCH::NP + 7) / 8;
const int L = D + P;
GF instance;
BCH decode;
uint8_t target[L] = { 0b11001000, 0b00011110, 0b10000000 };
uint8_t code[L];
for (int i = 0; i < L; ++i)
code[i] = target[i];
typedef std::uniform_int_distribution<BCH::value_type> distribution;
auto noise = std::bind(distribution(0, BCH::N-1), generator);
for (int i = 0; i < BCH::NR/2; ++i) {
int n = noise();
if (n < BCH::K)
CODE::xor_be_bit(code, n, 1);
else
CODE::xor_be_bit(code+D, n-BCH::K, 1);
}
decode(code, code + D);
for (int i = 0; i < L; ++i)
assert(code[i] == target[i]);
}
if (1) {
// DVB-S2 FULL BCH(65535, 65343) T=12
typedef CODE::GaloisField<16, 0b10000000000101101, uint16_t> GF;
typedef CODE::BoseChaudhuriHocquenghemDecoder<24, 1, 65343, GF> BCH;
const int D = (BCH::K + 7) / 8;
const int P = (BCH::NP + 7) / 8;
const int L = D + P;
GF *instance = new GF();
BCH *decode = new BCH();
uint8_t *target = new uint8_t[L];
for (int i = 0; i < L; ++i)
target[i] = 0;
for (int i = 0, s = 0; i < BCH::K; ++i, s=(s*(s*s*51767+71287)+35149)&0xffffff)
CODE::set_be_bit(target, i, (s^=s>>7)&1);
bool parity[BCH::NP] = { 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0 };
for (int i = 0; i < BCH::NP; ++i)
CODE::set_be_bit(target + D, i, parity[i]);
uint8_t *code = new uint8_t[L];
for (int i = 0; i < L; ++i)
code[i] = target[i];
typedef std::uniform_int_distribution<BCH::value_type> distribution;
auto noise = std::bind(distribution(0, BCH::N-1), generator);
for (int i = 0; i < BCH::NR/2; ++i) {
int n = noise();
if (n < BCH::K)
CODE::xor_be_bit(code, n, 1);
else
CODE::xor_be_bit(code+D, n-BCH::K, 1);
}
(*decode)(code, code + D);
for (int i = 0; i < L; ++i)
assert(code[i] == target[i]);
delete[] target;
delete[] code;
delete decode;
delete instance;
}
if (1) {
// NASA INTRO BCH(15, 5) T=3
typedef CODE::GaloisField<4, 0b10011, uint8_t> GF;
typedef CODE::BoseChaudhuriHocquenghemDecoderReference<6, 1, 5, GF> BCH;
GF instance;
BCH decode;
BCH::value_type target[BCH::N] = { 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0 };
BCH::value_type code[BCH::N];
for (int i = 0; i < BCH::N; ++i)
code[i] = target[i];
typedef std::uniform_int_distribution<BCH::value_type> distribution;
auto noise = std::bind(distribution(0, BCH::N-1), generator);
for (int i = 0; i < BCH::NR/2; ++i)
code[noise()] ^= 1;
decode(reinterpret_cast<GF::ValueType *>(code), reinterpret_cast<GF::ValueType *>(code) + BCH::K);
for (int i = 0; i < BCH::N; ++i)
assert(code[i] == target[i]);
}
if (1) {
// DVB-S2 FULL BCH(65535, 65343) T=12
typedef CODE::GaloisField<16, 0b10000000000101101, uint16_t> GF;
typedef CODE::BoseChaudhuriHocquenghemDecoderReference<24, 1, 65343, GF> BCH;
GF *instance = new GF();
BCH *decode = new BCH();
BCH::value_type *target = new BCH::value_type[BCH::N];
for (int i = 0, s = 0; i < BCH::K; ++i, s=(s*(s*s*51767+71287)+35149)&0xffffff)
target[i] = (s^=s>>7)&1;
BCH::value_type parity[BCH::NP] = { 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0 };
for (int i = 0; i < BCH::NP; ++i)
target[BCH::K+i] = parity[i];
BCH::value_type *code = new BCH::value_type[BCH::N];
for (int i = 0; i < BCH::N; ++i)
code[i] = target[i];
typedef std::uniform_int_distribution<BCH::value_type> distribution;
auto noise = std::bind(distribution(0, BCH::N-1), generator);
for (int i = 0; i < BCH::NR/2; ++i)
code[noise()] ^= 1;
(*decode)(reinterpret_cast<GF::ValueType *>(code), reinterpret_cast<GF::ValueType *>(code) + BCH::K);
for (int i = 0; i < BCH::N; ++i)
assert(code[i] == target[i]);
delete[] target;
delete[] code;
delete decode;
delete instance;
}
std::cerr << "Bose Chaudhuri Hocquenghem Decoder test passed!" << std::endl;
return 0;
}