-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_C_H.pas
More file actions
454 lines (353 loc) · 11.3 KB
/
Copy pathB_C_H.pas
File metadata and controls
454 lines (353 loc) · 11.3 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
unit B_C_H;
interface
uses Math, G_F, GF_x, SysUtils, Windows;
type
bvec= array of byte;
bbvec=array of bvec;
arGFX=array of ÏÀ׿
BCH=class
private
g: GFX;
zeroes: array of GF;
public
n,k,t,chain_begin:integer;
Constructor Create (in_n, in_k, in_t: integer;const genpolynom : ivec ); overload ;
constructor Create(const genpolynom : string ); overload ;
// Constructor Create (var genpolynom : ivec ); overload ;
// Constructor Create (var zeroes:GFX); overload ;
constructor Create(const inbch: BCH); overload ;
function Showg: string;
function Encode(const inuncodedbits : bvec) : bvec;
function Decode(const incodedbits : bvec) : bvec;
function Sindrom(const validatepol : GFX) : GFX;
function GetCheckPolinom : GFX;
function DecodeMeggit(const incodedbits : bvec) : bvec;
function ProduceTableError(const t,n,size:integer) : bbvec;
end;
implementation
function BCH.ProduceTableError(const n,size:integer) : bbvec;
var err:bbvec;
i,j,k:integer;
begin
SetLength(err,size);
for i := 0 to size-1 do
SetLength(err[i],n);
err[0][0]:=1;
for i := 1 to size-1 do
begin
err[i][0]:=1;
err[i][i]:=1;
end;
Result:=err;
end;
function BCH.DecodeMeggit(const incodedbits:bvec): bvec;
var codedbits, outvec, rbin, rmin ,mbin: bvec;
r,c,m,S,Lambda,OldLambda, Tpoly, Ohmega, One, x, x2:GFX;
i,j,itterations,size:integer;
cisvalid:boolean;
table:bbvec;
sindr_table_err:arGFX;
begin
if t=1 then size:=1 else
if t=2 then size:=round((n-1)/2)+1;
table:=ProduceTableError(n,size);
r:=GFX.Create(n+1,n-1);//input polinom
c:=GFX.Create(n+1,n-1);//code polinom
m:=GFX.Create(n+1,k-1);//inf polinom
S:=GFX.Create(n+1,n-1) ;//sindrom polinom
SetLength(codedbits,Length(incodedbits));
for i:=0 to Length(incodedbits) - 1 do
codedbits[i]:= incodedbits[i];
// set rest elements of codedbits to zeroes
itterations := ( length(codedbits) div n );
if( length(codedbits) mod n <>0) then
inc(itterations);
setlength(codedbits,itterations*n) ;
for i:=length(codedbits) to itterations*n-1 do
codedbits[i]:=0;
SetLength(outvec, itterations*k);
SetLength(rbin, n);
SetLength(mbin, k);
for i:=0 to itterations-1 do
begin //Fix the received polynomial r(x)
//copy current part(it's length is n) of codebits to rbin
for j:=0 to n - 1 do
rbin[j]:=codedbits[i*n+j];
// form input polinom by vector rbit
for j:=0 to n-1 do
begin
degree := -1 + rbin[j]; //transform 0 or 1 to it`s degree in field
r.coeffs[j] := GF.Create(n+1,degree);
end;
S:=Sindrom(r);
if (S.get_true_degree<>-1) then
begin
end
else
c:=GFX.Create(r);
cisvalid:=true;
end;
end;
end;
constructor BCH.Create(in_n, in_k, in_t: integer;const genpolynom : ivec );
var exponents:ivec;
temp: bvec;
i,j:integer;
begin
n := in_n;
k := in_k;
t := in_t;
//fix the generator polynomial g(x).
Setlength(exponents,3*Length(genpolynom));
SetLength(temp,3*Length(genpolynom));
for i := 0 to Length(genpolynom)-1 do
begin
t:=genpolynom[i];
for j := 2 downto 0 do
temp[i*3+2-j]:= (t and (1 shl j))shr(j) ; //oct2bin
end;
for i:=0 to Length(temp)-1 do
exponents[i] := -1 + temp[Length(temp)-i-1];
g:=GFX.Create(n+1,Length(temp));
g.set_field_values(n+1,exponents);
end;
function BCH.Showg: string;
Begin
Result:=g.tostring;
End;
constructor BCH.Create(const genpolynom : string );
var counter,i, max_counter, local_begin :integer;
factor, g0:GFX;
begin
//fix the generator polynomil g(x).
g0:=GFX.Create(2,genpolynom);
//find n
i:=g0.get_true_degree;
if(i>0) then
repeat
inc(i);
factor:=GFX.Create(2,i);
factor.coeffs[0].set_field_value(2,0);
factor.coeffs[i].set_field_value(2,0);
until ( GF_x.modd(factor,g0).get_true_degree=-1 )
else i:=1;
n := i;
//find k
k :=n-g0.get_true_degree ;
// set polinom
g:=GFX.Create(n+1,genpolynom);
SetLength(zeroes,n);
for i := 0 to n-1 do
zeroes[i]:=g.Count(GF.Create(n+1,i));
// find t
counter:=0;
max_counter:=0; // length of max chain
chain_begin:=0;
for I := 0 to n - 1 do
if(Equal (zeroes[i],(GF.Create(n+1,-1)) ) ) then
begin
if(counter=0) then local_begin:=i;
inc(counter);
if(counter>max_counter) then
begin
max_counter:=counter;
chain_begin:=local_begin;
end;
end
else
counter:=0;
t := max_counter div 2;
end;
// Copy constructor
constructor BCH.Create(const inbch: BCH);
begin
n := inbch.n;
k := inbch.k;
t := inbch.t;
g:=GFX.Create(inbch.g);
end;
function BCH.Encode(const inuncodedbits : bvec) : bvec;
var i,j,itterations,degree :integer;
m,c: GFX;
outbit,mbit,cbit, uncodedbits: bvec;
one:GF;
begin
setlength(uncodedbits,length(inuncodedbits));
for i:=0 to Length(inuncodedbits) - 1 do
uncodedbits[i]:= inuncodedbits[i];
itterations := ( length(uncodedbits) div k );
if( length(uncodedbits) mod k <>0) then
inc(itterations);
setlength(uncodedbits,itterations*k) ;
for i:=length(uncodedbits) to itterations*k-1 do
uncodedbits[i]:=0;
m:= GFX.Create(n+1,k);
c:= GFX.Create(n+1,n);
SetLength(Result,itterations*n);
SetLength(mbit,k);
SetLength(cbit,n);
One:=GF.Create(n+1,0);
for i:=0 to itterations-1 do
begin
//Fix the message polynom m(x). mbit = uncodedbits.mid(i*k,k);
for j:=0 to k - 1 do
mbit[j]:=uncodedbits[i*k+j];
for j:=0 to k-1 do
begin
degree:= -1 + mbit[j];
m.coeffs[j].set_field_value(n+1,degree);
end;
//Fix the outputbits cbit.
c:=GF_x.Mul(g,m);
for j:=0 to n-1 do
if ( Equal(c.coeffs[j],One) ) then cbit[j]:= 1
else cbit[j]:= 0;
for j:=0 to n-1 do
Result[i*n+j]:=cbit[j];
end;
end;
function BCH.Decode(const incodedbits : bvec) : bvec ;
var j, i, degree, kk, foundzeros, itterations, ii:integer;
r,c,m,S,Lambda,OldLambda, Tpoly, Ohmega, One, x, x2:GFX;
delta, temp, Zero:GF;
codedbits, outvec, rbin, rmin ,mbin: bvec ;
errorpos : ivec;
cisvalid: boolean;
begin
// copy incodedbits to codedbits
setlength(codedbits,length(incodedbits));
for i:=0 to Length(incodedbits) - 1 do
codedbits[i]:= incodedbits[i];
// set rest elements of codedbits to zeroes
itterations := ( length(codedbits) div n );
if( length(codedbits) mod n <>0) then
inc(itterations);
setlength(codedbits,itterations*n) ;
for i:=length(codedbits) to itterations*n-1 do
codedbits[i]:=0;
SetLength(outvec, itterations*k);
SetLength(rbin, n);
SetLength(mbin, k);
r:=GFX.Create(n+1,n-1);//input polinom
c:=GFX.Create(n+1,n-1);//code polinom
m:=GFX.Create(n+1,k-1);//inf polinom
S:=GFX.Create(n+1,2*t) ;//sindrom polinom
Lambda:= GFX.Create(n+1);//error polinom
OldLambda:=GFX.Create(n+1,n);
Tpoly:=GFX.Create(n+1);
Ohmega:=GFX.Create(n+1); //mul(Lambda,S)
One :=GFX.Create(n+1,'1');
x :=GFX.Create(n+1,'01'); //x^1
x2 :=GFX.Create(n+1,'001'); //x^2
Zero:=GF.Create(n+1,-1) ;//zero element
delta:=GF.Create(n+1);//disctintion on itteration
temp:= GF.Create(n+1);//means of Lambda in point
for i:=0 to itterations-1 do
begin //Fix the received polynomial r(x)
//copy current part(it's length is n) of codebits to rbin
for j:=0 to n - 1 do
rbin[j]:=codedbits[i*n+j];
// form input polinom by vector rbit
for j:=0 to n-1 do
begin
degree := -1 + rbin[j]; //transform 0 or 1 to it`s degree in field
r.coeffs[j] := GF.Create(n+1,degree);
end;
//Fix the syndrome polynomial S(x).
S.coeffs[0] := GF.Create(n+1,-1);
for j:=chain_begin to (chain_begin+2*t-1) do
S.coeffs[j-chain_begin+1] := r.count(GF.Create(n+1,j));
if (S.get_true_degree >= 1) then //Errors in the received word
begin //Itterate to find Lambda(x).
kk:=0;
Lambda:=GFX.Create(n+1,'1');
Tpoly := GFX.Create(n+1,'1');
while (kk<t) do
begin
Ohmega := GF_x.Mul(Lambda , GF_x.Add(S , One));
delta := Ohmega.coeffs[2*kk+1];
OldLambda.Copy(Lambda);
Lambda := GF_x.Add(OldLambda , GF_x.Mul( GF_x.Mul(x,Tpoly), delta ));
if ( Equal(delta,Zero) or (OldLambda.get_degree > kk)) then
Tpoly := GF_x.Mul(x2 , Tpoly)
else
Tpoly := GF_x.Divv(GF_x.Mul(x , OldLambda ) , delta);
inc(kk );
end;
//Find the zeros to Lambda(x).
Setlength(errorpos,Lambda.get_true_degree);
foundzeros := 0;
for j := 0 to n - 1 do
begin
temp.Copy(Lambda.count(GF.Create(n+1,j)) );
if(Equal(temp,Zero)) then
begin
errorpos[foundzeros]:= (n-j) mod n;
inc(foundzeros);
if (foundzeros >= Lambda.get_true_degree )then
break;
end;
end;
//Correct the codeword.
for j:=0 to foundzeros - 1 do
rbin[errorpos[j]]:= (rbin[errorpos[j]]+1) mod 2;
//Reconstruct the corrected codeword.
for j := 0 to n - 1 do
begin
degree := -1 + rbin[j];
c.coeffs[j] := GF.Create(n+1,degree);
end;
//Code word validation.
S.coeffs[0]:=GF.Create(n+1,-1);
for j:=1 to 2*t do
S.coeffs[j]:=C.Count(GF.Create(n+1,j));
if(S.get_true_degree<=0) then //c(x) is a valid codeword.
cisvalid:=true
else
cisvalid:=false;(**)
end // if (S.get_true_degree >= 1)
else
begin
c:=GFX.Create(r);
cisvalid:=true;
end;
for j := 0 to Length(mbin)-1 do
mbin[j]:=0;
//Construct the message bit vector.
if (cisvalid) then //c(x) is a valid codeword.
begin
if (c.get_true_degree> 1) then
begin
m:=GF_x.divv(c, g);
for j:=0 to m.get_true_degree do
mbin[j]:=m.coeffs[j].get_value+1;
end
else
begin //The zero word was transmitted
m := GFX.Create(n+1,0); // feel zeroes
end ;
end
else
begin //Decoder failure.
m := GFX.Create(n+1,0);
end;
for ii:=0 to Length(mbin)-1 do
outvec[i*k+ii]:= mbin[ii];
end; // end of main loop ( by i:=0 to itterations-1 )
Result:=outvec;
end;
function BCH.GetCheckPolinom : GFX;
var mn:GFX;
begin
mn:=GFX.Create(n+1,n);
mn.coeffs[0].set_field_value(n+1,0);
mn.coeffs[n].set_field_value(n+1,0);
Result:=GFX.Create(n+1);
Result:=Divv(mn,g);
end;
function BCH.Sindrom(const validatepol : GFX) : GFX;
begin
Result:=GFX.Create(n+1);
Result:=Modd(validatepol,g);
end;
end.