-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLDPCEncoder.cpp
More file actions
112 lines (102 loc) · 2.51 KB
/
Copy pathLDPCEncoder.cpp
File metadata and controls
112 lines (102 loc) · 2.51 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
#include "LDPCEncoder.h"
#include "QAMDecoder.h"
LDPCEncoder::LDPCEncoder(int k, int n,
boost::numeric::ublas::matrix<int> matrix, int symbols) {
this->k = k;
this->n = n;
this->matrix = matrix;
this->symbols = symbols;
energy = (symbols - 1) * 2 / 3;
if (symbols > 2) {
initModulator();
}
}
std::vector<int> LDPCEncoder::encode(std::vector<int> uncoded) {
if (uncoded.size() != k) {
throw std::invalid_argument("Invalid input size");
}
std::vector<int> coded;
for (int i = 0; i < k; i++) {
coded.push_back(uncoded.at(i));
}
for (int i = k; i < n; i++) {
int element = 0;
for (int j = 0; j < k; j++) {
element += matrix(i, j) * uncoded.at(j);
}
coded.push_back(element % 2);
}
return coded;
}
std::vector<LDPCEncoder::symbol> LDPCEncoder::modulate(std::vector<int> coded) {
coded = interleave(coded);
std::vector<LDPCEncoder::symbol> qam;
int modSize = log2(symbols);
for (int i = 0; i < coded.size(); i += modSize) {
std::vector<int> next;
for (int j = 0; j < modSize; j++) {
if (i + j >= coded.size()) {
break;
}
next.push_back(coded.at(i + j));
}
qam.push_back(modulateSymbol(next));
}
return qam;
}
std::vector<int> LDPCEncoder::interleave(std::vector<int> coded) {
std::vector<int> interleaved;
for (int i = 0; i < n / QAMDecoder::PI_ROW; i++) {
for (int j = 0; j < QAMDecoder::PI_ROW; j++) {
interleaved.push_back(coded.at(i * QAMDecoder::PI_ROW + j));
}
}
return interleaved;
}
LDPCEncoder::symbol LDPCEncoder::modulateSymbol(std::vector<int> coded) {
int modSize = log2(symbols);
//Input check
if (modSize < coded.size()) {
throw std::invalid_argument("Invalid input size");
}
//Zero padding for incomplete symbols
while (coded.size() < modSize) {
coded.push_back(0);
}
std::stringstream bits;
for (int i = 0; i < modSize; i++) {
bits << coded.at(i);
}
return grayMap.at(bits.str());
}
void LDPCEncoder::initModulator() {
std::string mapFile;
bool mapped = false;
if (symbols == 4) {
mapFile = "./maps/gray4.csv";
mapped = true;
}
if (symbols == 16) {
mapFile = "./maps/gray16.csv";
mapped = true;
}
if (symbols == 64) {
mapFile = "./maps/gray64.csv";
mapped = true;
}
if (!mapped) {
return;
}
std::ifstream mapReader;
mapReader.open(mapFile.c_str());
for (int i = 0; i < symbols; i++) {
std::string sequence;
symbol symb;
mapReader >> sequence >> symb.phase >> symb.quad;
symb.phase /= sqrt(energy);
symb.quad /= sqrt(energy);
grayMap.insert(std::pair<std::string, symbol>(sequence, symb));
}
}
LDPCEncoder::~LDPCEncoder() {
}