-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQAMDecoder.cpp
More file actions
178 lines (157 loc) · 3.55 KB
/
Copy pathQAMDecoder.cpp
File metadata and controls
178 lines (157 loc) · 3.55 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
/*
* QAMDecoder.cpp
*
* Created on: Dec 8, 2014
* Author: fede
*/
#include "QAMDecoder.h"
QAMDecoder::QAMDecoder(int k, int n, boost::numeric::ublas::matrix<int> matrix,
int symbols, int passes) {
this->k = k;
this->n = n;
this->passes = passes;
this->symbols = symbols;
this->matrix = matrix;
p = n - k;
//Symbol graph
s = n / log2(symbols);
initializeGraph();
}
bool QAMDecoder::parityCheck() {
for (int i = 0; i < p; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
sum += matrix(i, j)
* encoded.at(j)->llr2val(encoded.at(j)->getValue());
}
if (sum % 2 != 0) {
return false;
}
}
return true;
}
std::vector<int> QAMDecoder::decode(std::vector<double> phase,
std::vector<double> quad, double n0) {
if (phase.size() != quad.size() || phase.size() != s) {
std::stringstream str;
str << "Invalid input size";
throw std::invalid_argument(str.str());
}
//LLR symbol node initialization
for (int i = 0; i < s; i++) {
//TODO fix initialization
received.at(i)->setValue(phase.at(i), quad.at(i));
if (DEBUG) {
std::cout << phase.at(i) << " " << quad.at(i) << " ";
}
}
if (DEBUG) {
std::cout << " received\n";
}
//Message passing (stops when Hc=0 or after a max number of cycles)
int pass = 0;
bool check = parityCheck();
while (!check && pass < passes) {
forwardPassing();
if (DEBUG) {
for (int i = 0; i < k; i++) {
std::cout << parity.at(i)->getValue() << " ";
}
std::cout << "LLR (check)\n";
}
check = parityCheck();
if (check) {
break;
}
backwardPassing();
if (DEBUG) {
for (int i = 0; i < n; i++) {
std::cout << encoded.at(i)->getValue() << " ";
}
std::cout << "LLR (var)\n";
}
check = parityCheck();
pass += 2;
}
//Decoded vector
std::vector<int> decoded;
for (int i = 0; i < n; i++) {
double in = encoded.at(i)->getValue();
decoded.push_back(encoded.at(i)->llr2val(in));
}
resetGraph();
return decoded;
}
void QAMDecoder::forwardPassing() {
for (int i = 0; i < s; i++) {
received.at(i)->sendMessages();
}
for (int i = 0; i < n; i++) {
encoded.at(i)->sendMessages();
}
}
void QAMDecoder::backwardPassing() {
for (int i = 0; i < p; i++) {
parity.at(i)->sendMessages();
}
for (int i = 0; i < s; i++) {
received.at(i)->updateValue();
}
}
void QAMDecoder::initializeGraph() {
//Node initialization
for (int i = 0; i < s; i++) {
//Node type BP_SYMBOL
received.push_back(new SymbolNode(0, 0, log2(symbols)));
}
for (int i = 0; i < n; i++) {
encoded.push_back(new BpNode(0, BpNode::BP_VAR));
}
for (int i = 0; i < p; i++) {
parity.push_back(new BpNode(0, BpNode::BP_CHECK));
}
//Right-to-center neighbors
for (int i = 0; i < p; i++) {
for (int j = 0; j < n; j++) {
if (matrix(i, j) != 0) {
parity.at(i)->addNeighbor(encoded.at(j));
}
}
}
//Center-to-right neighbors
for (int j = 0; j < n; j++) {
for (int i = 0; i < p; i++) {
if (matrix(i, j) != 0) {
encoded.at(j)->addNeighbor(parity.at(i));
}
}
}
//TODO interleaver!
int modSize = log2(symbols);
//Left-to-center neighbors
for (int i = 0; i < s; i++) {
for (int j = 0; j < modSize; j++) {
int b = i * modSize + j;
received.at(i)->addNeighbor(encoded.at(b));
}
}
}
void QAMDecoder::resetGraph() {
//Reset left side nodes
for (int i = 0; i < n; i++) {
received.at(i)->resetNode();
}
//Reset center nodes
for (int i = 0; i < n; i++) {
encoded.at(i)->resetNode();
}
//Reset right side nodes
for (int i = 0; i < p; i++) {
parity.at(i)->resetNode();
}
}
QAMDecoder::~QAMDecoder() {
received.clear();
encoded.clear();
parity.clear();
}