-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathldpc.c
103 lines (79 loc) · 2.66 KB
/
ldpc.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
/*****************************************************************
Copyright (C) 2014 Stefan Grönroos
Authors: Stefan Grönroos <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
********************************************************************/
#include "ldpc.h"
#include <stdlib.h>
#include <string.h>
void ldpc_ll_matrix_destroy(ldpc_ll_matrix_t *H) {
int i;
ldpc_ll_edge_t *cur;
ldpc_ll_edge_t *next;
if (!H)
return;
for(i=0;i<H->M;i++)
{
cur = H->rows[i];
while(cur)
{
next = cur->right;
free(cur);
cur = next;
}
}
free(H->rows);
free(H->cols);
free(H);
return;
}
void ldpc_param_init(ldpc_param_t *param)
{
param->h_matrix = NULL;
param->max_iter = 30; /* Default to 30 iterations */
return;
}
void ldpc_param_destroy(ldpc_param_t *param)
{
ldpc_ll_matrix_destroy(param->h_matrix);
}
/* For now, encodes a K*128 byte array (one bit per byte)
* Warning! This function is not created for speed, but only for testing.
*
*/
int ldpc_encode_c(ldpc_param_t *p, int len, char *input, char *output)
{
int bit;
ldpc_ll_edge_t *node;
if (p->h_matrix && len == p->h_matrix->K)
{
/* The first K bits in the encoded codeword are identical to the input */
memcpy(output, input, p->h_matrix->K*sizeof(char));
/* Zero out the rest of the output data */
memset( (void *)(output+p->h_matrix->K), 0, p->h_matrix->M*sizeof(char) );
/* Calculate the parity bits */
for (bit=0;bit<p->h_matrix->M;bit++)
{
output[p->h_matrix->K + bit] = 0;
node = p->h_matrix->rows[bit];
do {
output[p->h_matrix->K + bit] ^= output[node->col];
node = node->right;
} while(node->right);
}
}
else
return -1;
return 1;
}
int (*ldpc_encode)(ldpc_param_t *p, int len, char *input, char *output) = ldpc_encode_c;