-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhamenc.cpp
204 lines (163 loc) · 4.25 KB
/
hamenc.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#define NDEBUG
#define BOOST_UBLAS_NDEBUG
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#define DEBUG 0
using namespace boost::numeric::ublas ;
// Devuelve la cantidad de errores de paridad del vector cw segun la matriz de paridad H
int checkParity(vector<int> cw, matrix<int> H) {
unsigned int errors = 0,q;
vector<int> vc;
vc = prod(H,cw);
for (q=0;q<vc.size();q++) vc(q)=vc(q) % 2;
for (q=0;q<vc.size();q++) errors+=vc(q);
return errors;
}
void usage(void) {
std::cerr<<"Usage: hamenc <matrix> (takes input from stdin and outputs to stdout)"<<std::endl;
exit(1);
}
// LDPC simulator
matrix<int> H; //parity check
matrix<int> G;
void readMatrixes(char *file) {
std::ifstream thematrix;
thematrix.open (file);
// Cargamos H (Parity Check Matrix)
thematrix>>H;
// Cargamos G (Generator Matrix)
thematrix>>G;
thematrix.close();
if (DEBUG)
std::cerr<<"Loaded "<<file<<std::endl;
}
// Returns the position of the error
int HammingFindError(vector<int> cw) {
unsigned int q,x,y;
vector<int> vR(10);
vR = prod(H,cw);
for (q=0;q<vR.size();q++)
vR(q)=vR(q) % 2;
int count=0;
for (q=0;q<vR.size();q++)
count+=vR(q);
if (!count) return -1; // no errors
// segun el algoritmo de hamming, ahora en vR tenemos los errores de
// paridad, que equivalen a la columna de H donde esta el error,
// si es que solamente hay un bit de error
// Buscamos la columna de H:
for (x=0;x<H.size2();x++) {
q=0;
for(y=0;y<H.size1();y++)
if (vR(y)!=H(y,x))
q++;
if (!q) // la columna es igual a vR
return x;
}
return -2; // can't find the error
}
void test(void)
{
unsigned int dataLenght = G.size1();
unsigned int codewordLenght = G.size2();
vector<int> v(dataLenght),vR(dataLenght),T;
vector<int> cw(codewordLenght);
unsigned int i,q,epos,ecnt = 0;
int error,parity,eparity;
for (i=0;i<100000;i++) {
for (q=0;q<dataLenght;q++)
v(q)= rand() % 2;
cw = prod(v,G);
//paridad
parity=0;
for (q=0;q<codewordLenght;q++)
parity+=cw(q);
epos = rand() % codewordLenght;
cw(epos) = 1 - cw(epos);
epos = rand() % codewordLenght;
cw(epos) = 1 - cw(epos);
error = HammingFindError(cw);
if (error==-1)
{
//cw(error)=1-cw(error);
//eparity=0;
//for (q=0;q<codewordLenght;q++)
// eparity+=cw(q);
//if (eparity!=parity) //error detected
ecnt++;
}
}
printf("error-detected: %d\n",ecnt);
exit(0);
}
int main(int argc, char **argv)
{
int SEC_DED=0;
srand(0);
if (argc<2) usage();
readMatrixes(argv[1]);
if (argc==3) SEC_DED=1;
unsigned int dataLenght = G.size1();
unsigned int codewordLenght = G.size2();
vector<int> v(dataLenght),vR(dataLenght),T;
vector<int> cw(codewordLenght);
unsigned int q;
fprintf(stderr,"\nHamming %d\n",H.size1());
while(true) {
if (strstr(argv[0],"hamenc"))
{ // codifica hamming
// lee vector v()
char *rbuf = (char *)malloc(dataLenght+1);
q=fread(rbuf,1,dataLenght,stdin);
if (q!=dataLenght)
exit(0); //no more data
for (q=0;q<dataLenght;q++)
v(q)= (rbuf[q]-0x30);
free(rbuf);
// CodeWord = v*G
cw = prod(v,G);
int parity=0;
for (q=0;q<cw.size();q++) {
cw(q)=cw(q) % 2;
parity+=cw(q);
std::cout<<(cw(q)?'1':'0');
}
if (SEC_DED) // output additional parity bit
std::cout<<((parity % 2)?'1':'0');
}
else {// decodifica hamming
// lee codeword cw()
char *rbuf = (char *)malloc(codewordLenght+1);
int parity;
char parbit;
int error;
q=fread(rbuf,1,codewordLenght,stdin);
if (q!=codewordLenght)
exit(0); //no more data
for (q=0;q<codewordLenght;q++) {
cw(q)= (rbuf[q]-0x30);
}
free(rbuf);
parbit=0;
if (SEC_DED) // read additional parity bit
fread(&parbit,1,1,stdin);
error = HammingFindError(cw);
if (error>=0) { // error detected
int bkp = cw(error);
cw(error)=1-cw(error); // flip bit
}
if (error==-1) fprintf(stderr,".");
int parityBits = H.size1(); //number of parity bits
for (q=0;q<dataLenght;q++) {
cw(q+parityBits)=cw(q+parityBits) % 2;
std::cout<<(cw(q+parityBits)?'1':'0');
}
}
}
}