forked from radfordneal/LDPC-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.c
154 lines (116 loc) · 3.44 KB
/
enc.c
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
/* ENC.C - Encoding procedures. */
/* Copyright (c) 1995-2012 by Radford M. Neal.
*
* Permission is granted for anyone to copy, use, modify, and distribute
* these programs and accompanying documents for any purpose, provided
* this copyright notice is retained and prominently displayed, and note
* is made of any changes made to these programs. These programs and
* documents are distributed without any warranty, express or implied.
* As the programs were written for research purposes only, they have not
* been tested to the degree that would be advisable in any important
* application. All use of these programs is entirely at the user's own
* risk.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "rand.h"
#include "alloc.h"
#include "mod2sparse.h"
#include "mod2dense.h"
#include "mod2convert.h"
#include "rcode.h"
#include "enc.h"
/* The procedures in this module obtain the generator matrix to use for
encoding from the global variables declared in rcode.h */
/* ENCODE A BLOCK USING A SPARSE REPRESENTATION OF THE GENERATOR MATRIX. */
void sparse_encode
( char *sblk,
char *cblk
)
{
int i, j;
mod2entry *e;
char *x, *y;
x = chk_alloc (M, sizeof *x);
y = chk_alloc (M, sizeof *y);
/* Multiply the vector of source bits by the systematic columns of the
parity check matrix, giving x. Also copy these bits to the coded block. */
for (i = 0; i<M; i++) x[i] = 0;
for (j = M; j<N; j++)
{
cblk[cols[j]] = sblk[j-M];
if (sblk[j-M]==1)
{ for (e = mod2sparse_first_in_col(H,cols[j]);
!mod2sparse_at_end(e);
e = mod2sparse_next_in_col(e))
{ x[mod2sparse_row(e)] ^= 1;
}
}
}
/* Solve Ly=x for y by forward substitution, then U(cblk)=y by backward
substitution. */
if (!mod2sparse_forward_sub(L,rows,x,y)
|| !mod2sparse_backward_sub(U,cols,y,cblk))
{
abort(); /* Shouldn't occur, even if the parity check matrix has
redundant rows */
}
free(x);
free(y);
}
/* ENCODE A BLOCK USING DENSE REPRESENTATION OF GENERATOR MATRIX. */
void dense_encode
( char *sblk,
char *cblk,
mod2dense *u,
mod2dense *v
)
{
int j;
/* Copy source bits to the systematic part of the coded block. */
for (j = M; j<N; j++)
{ cblk[cols[j]] = sblk[j-M];
}
/* Multiply by Inv(A) X B to produce check bits. */
for (j = M; j<N; j++)
{ mod2dense_set(u,j-M,0,sblk[j-M]);
}
mod2dense_multiply(G,u,v);
/* Copy check bits to the right places in the coded block. */
for (j = 0; j<M; j++)
{ cblk[cols[j]] = mod2dense_get(v,j,0);
}
}
/* ENCODE A BLOCK USING MIXED REPRESENTATION OF GENERATOR MATRIX. */
void mixed_encode
( char *sblk,
char *cblk,
mod2dense *u,
mod2dense *v
)
{
mod2entry *e;
int j;
/* Multiply the vector of source bits by the message bit columns of the
parity check matrix. Also copy these bits to the coded block. Take
account of how columns have been reordered. */
mod2dense_clear(u);
for (j = M; j<N; j++)
{
cblk[cols[j]] = sblk[j-M];
if (sblk[j-M]==1)
{ for (e = mod2sparse_first_in_col(H,cols[j]);
!mod2sparse_at_end(e);
e = mod2sparse_next_in_col(e))
{ (void) mod2dense_flip(u,mod2sparse_row(e),0);
}
}
}
/* Multiply by Inv(A) to produce check bits. */
mod2dense_multiply(G,u,v);
/* Copy check bits to the right places in the coded block. */
for (j = 0; j<M; j++)
{ cblk[cols[j]] = mod2dense_get(v,j,0);
}
}