-
Notifications
You must be signed in to change notification settings - Fork 23
/
encoder.hh
65 lines (59 loc) · 1.05 KB
/
encoder.hh
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
/*
LDPC SISO encoder
Copyright 2018 Ahmet Inan <[email protected]>
*/
#ifndef ENCODER_HH
#define ENCODER_HH
#include "ldpc.hh"
template <typename TYPE>
class LDPCEncoder
{
LDPCInterface *ldpc;
int N, K, R;
bool initialized;
TYPE one()
{
return 1;
}
TYPE sign(TYPE a, TYPE b)
{
return b < TYPE(0) ? -a : b > TYPE(0) ? a : TYPE(0);
}
public:
LDPCEncoder() : initialized(false)
{
}
void init(LDPCInterface *it)
{
if (initialized)
delete ldpc;
initialized = true;
ldpc = it->clone();
N = ldpc->code_len();
K = ldpc->data_len();
R = N - K;
}
void operator()(TYPE *data, TYPE *parity)
{
for (int i = 0; i < R; ++i)
parity[i] = one();
ldpc->first_bit();
for (int j = 0; j < K; ++j) {
int *acc_pos = ldpc->acc_pos();
int bit_deg = ldpc->bit_deg();
for (int n = 0; n < bit_deg; ++n) {
int i = acc_pos[n];
parity[i] = sign(parity[i], data[j]);
}
ldpc->next_bit();
}
for (int i = 1; i < R; ++i)
parity[i] = sign(parity[i], parity[i-1]);
}
~LDPCEncoder()
{
if (initialized)
delete ldpc;
}
};
#endif