-
Notifications
You must be signed in to change notification settings - Fork 2
/
encode.c
75 lines (59 loc) · 1.82 KB
/
encode.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
/* Based on Code by Radford M. Neal, see LICENSE_LDPC.
This file is an adaption of encode.c */
#include "encode.h"
void encode(
char* message, // each char represents one bit
int message_length, // number of message bits (K)
int parity_length, // number of parity bits (M = N-K)
// parameters for parity matrix creation:
int parity_matrix_creation_method,
int checks_per_col,
int seed,
int avoid4cycle,
// parameters for generator matrix creation:
int generator_matrix_sparse_lu_strategy,
char* out_encoded_message // each char represents one bit. Length N.
) {
char *check;
int i;
/* Set global M and N */
N = message_length + parity_length;
M = parity_length;
if (N<=M)
{ printf(
"Can't encode if number of bits (%d) not greater than number of checks (%d)\n",
N,M);
free_and_exit(1);
}
printf("Message to encode:\n");
for (i=0; i<message_length; ++i) {
printf("%d ", message[i]);
}
printf("\n");
/* Create parity check matrix. This will be written to global variable H */
create_parity_matrix(parity_matrix_creation_method, checks_per_col,
seed, avoid4cycle);
/* Create generator matrix. */
create_generator_matrix(generator_matrix_sparse_lu_strategy);
/* Allocate needed space. */
check = chk_alloc (M, sizeof *check);
sparse_encode (message, out_encoded_message);
/* Multiply encoded message with H to check that encoded block is a code word. */
mod2sparse_mulvec (H, out_encoded_message, check);
for (i = 0; i<M; i++)
{ if (check[i]==1)
{ printf("LDPC: Encoded message is not a code word! (Fails check %d)\n",i);
free(check);
free_and_exit(1);
}
}
#ifndef RELEASE
printf("Encoded message:\n");
for (i=0; i<N; ++i) {
printf("%d ", out_encoded_message[i]);
}
printf("\n");
#endif
free(check);
free_globals();
}