-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
125 lines (107 loc) · 2.81 KB
/
main.cpp
File metadata and controls
125 lines (107 loc) · 2.81 KB
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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
#include <string>
#include <iostream>
#include <map>
#include "BCH_codes/Coder.h"
#include "BCH_codes/GfField.h"
#include "BCH_codes/Config.h"
int i;
int data[1048576];
int numerr, errpos[1024];
void int_rand_errors(Coder *c, int err = 0);
void int_rand_errors_position(Coder *c);
void int_print_polynomial(std::string title, std::string polynomial_symbol, int *data, int start, int length);
void get_random_data(int k, int *data_destination)
{
int seed = 131073;
srandom(seed);
for (int i = 0; i < k; i++)
{
data_destination[i] = ( random() & 65536 ) >> 16;
}
}
int main()
{
Config cnf;
cnf.load("config.xml");
std::map<std::string, std::string> codes_features = cnf.getDatabaseInfo();
int* recd = new int [1048576];
int bb[548576];
int poly_degree = std::stoi(codes_features["polynomial_degree"]);
int correction_cap = std::stoi(codes_features["correction_capabilities"]);
int error_num = std::stoi(codes_features["number_of_errors"]);
Coder* c = new Coder(poly_degree, correction_cap);
get_random_data(c->get_k(), data);
c->encode_bch(data, bb);
c->code_polynomial(data, bb, recd);
int_print_polynomial("Code polynomial:", "c(x)", recd, 0, c->get_code_length());
int_rand_errors(c, error_num);
if(error_num)
{
for (i = 0; i < numerr; i++)
recd[errpos[i]] ^= 1;
int_print_polynomial("r(x)", "r(x)", recd, 0, c->get_code_length());
}
c->decode_bch(recd);
std::cout<<"Results:\n";
int_print_polynomial("original data:", "send(x)", data, 0, c->get_k());
int_print_polynomial("recovered data:", "recovered(x)", recd, c->get_code_length() - c->get_k(), c->get_code_length());
c->get_decoding_error_number(data, recd);
delete c;
return 0;
}
void int_rand_errors(Coder *c, int err)
{
numerr = err;
if(err==0)
{
printf("Enter the number of errors:\n");
scanf("%d", &numerr); /* CHANNEL errors */
printf("Enter error locations (integers between");
printf(" 0 and %d): ", c->get_code_length() - 1);
for (i = 0; i < numerr; i++)
{
scanf("%d", &errpos[i]);
}
}
else
{
int_rand_errors_position(c);
}
}
void int_rand_errors_position(Coder *c)
{
srand( time(NULL));
int tmp = 0;
std::cout<<"ERRORS at: ";
for (i = 0; i < numerr; )
{
int flag = 0;
tmp = rand() % c->get_code_length();
for (int j = 0; j < numerr; ++j)
if(errpos[j] == tmp)
flag = 1;
if(!flag)
{
std::cout << tmp << ", ";
errpos[i] = tmp;
++i;
}
}
std::cout<<std::endl;
}
void int_print_polynomial(std::string title, std::string polynomial_symbol, int *data, int start, int length)
{
std::cout<<title<<std::endl;
std::cout<<polynomial_symbol<<" = ";
for (i = start; i < length; i++) {
printf("%1d", data[i]);
if (i && ((i % 50) == 0))
std::cout<<std::endl;
}
std::cout<<std::endl;
}