-
Notifications
You must be signed in to change notification settings - Fork 1
/
NBLDPC.cpp
163 lines (141 loc) · 4.32 KB
/
NBLDPC.cpp
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
#include "LDPC/NBLDPC.hpp"
#include <iostream>
#include "LDPC/Tanner.hpp"
#include "MatrixMath/MatrixMath.hpp"
NBLDPC::NBLDPC() {
H_mat = Eigen::SparseMatrix<int>();
G_mat = Eigen::SparseMatrix<int>();
K = 0;
N = 0;
GF = 0;
}
NBLDPC::NBLDPC(Eigen::SparseMatrix<int> H) {
// G.nRow = N - M, G.nCol = N
// N = n, M = n - k
// K = A.getnCol() - A.getnRow();
H_mat = H;
// G_mat = Eigen::SparseMatrix<int>(H_mat.cols() - H_mat.rows(),
// H_mat.cols()); G_mat.setZero();
G_mat =
Eigen::SparseMatrix<int>(NBtransform_H_to_G(H_mat, GF).sparseView());
assert(
!(NBproduct(G_mat, H_mat.transpose(), GF).any())); // G*H should be 0
K = G_mat.rows();
N = G_mat.cols();
Eigen::MatrixXi diff =
G_mat.block(0, N - K, K, K).unaryExpr([](const int x) {
return x == 0 ? 0 : 1;
}) -
Eigen::MatrixXi::Identity(K, K);
if (diff.any()) {
isSystematic = false;
} else {
isSystematic = true;
}
}
NBLDPC::NBLDPC(Alist<nbalist_matrix> A) {
GF = A.getGF();
memset(num_mlist, 0, A.getnRow() * sizeof(int));
memcpy(num_mlist, A.getData().num_mlist, A.getnRow() * sizeof(int));
memset(num_nlist, 0, A.getnCol() * sizeof(int));
memcpy(num_nlist, A.getData().num_nlist, A.getnCol() * sizeof(int));
new (this) NBLDPC(A.getMat()); // no need to delete, cause memory is reused
}
NBLDPC::NBLDPC(const char* filename) {
Alist<nbalist_matrix> A = Alist<nbalist_matrix>(filename);
GF = A.getGF();
num_mlist = (int*)malloc(A.getnRow() * sizeof(int));
memcpy(num_mlist, A.getData().num_mlist, A.getnRow() * sizeof(int));
num_nlist = (int*)malloc(A.getnCol() * sizeof(int));
memcpy(num_nlist, A.getData().num_nlist, A.getnCol() * sizeof(int));
new (this) NBLDPC(A.getMat()); // no need to delete, cause memory is reused
}
NBLDPC::~NBLDPC() {
free(num_mlist);
free(num_nlist);
}
Eigen::RowVectorXi NBLDPC::encode(Eigen::RowVectorXi& m) const {
return NBproduct(m, G_mat, GF);
}
/**
* @brief decode the received LLR
*
* @param LLR received LLR, <GF, N>
* @param iter_max stop early criterion
* @param factor normalize factor, should not larger than 1
* @param snr Channel snr
* @param mode decode mode, 0: NMS, 1: SPA
* @param n_max
* @return Eigen::RowVectorXi
*/
Eigen::RowVectorXi NBLDPC::decode(Eigen::MatrixXd& LLR, int iter_max,
double factor, double snr, int mode,
int n_max) const {
std::vector<NBVNode*> VNodes_; // size: N
std::vector<NBCNode*> CNodes_; // size: M
int M = H_mat.rows();
Eigen::MatrixXi Hdense = H_mat.toDense();
assert(LLR.cols() == N && LLR.rows() == GF);
// init Nodes
for (int i = 0; i < M; i++) {
NBCNode* c = new NBCNode(num_mlist[i], factor, GF, n_max);
CNodes_.push_back(c);
}
// TODO: use feature of SparseMatrix
for (int i = 0; i < N; i++) {
// init LLR directly by r
NBVNode* v = new NBVNode(num_nlist[i], LLR.col(i), GF, n_max);
VNodes_.push_back(v);
for (int j = 0; j < M; j++) {
if (Hdense(j, i)) {
VNodes_[i]->Link(CNodes_[j], Hdense(j, i));
}
}
assert(VNodes_[i]->isReady());
}
Eigen::RowVectorXi ret(N);
int count = 0;
do {
for (NBVNode* v : VNodes_) {
v->Update(3);
}
for (NBCNode* c : CNodes_) {
c->Update(mode);
}
// update ret
for (int i = 0; i < N; i++) {
// printf("%d: ", i);
ret[i] = VNodes_[i]->getValue();
// printf("\n");
}
// std::cout << ret << std::endl;
count++;
} while (NBproduct(H_mat.toDense(), ret.transpose(), GF).any() &&
count < iter_max); // stop criterion
// std::cout << "迭代次数: " << count << std::endl;
for (NBCNode* c : CNodes_) {
delete c;
}
for (NBVNode* d : VNodes_) {
delete d;
}
return ret;
}
Eigen::SparseMatrix<int> NBLDPC::getG() const {
return G_mat;
}
Eigen::SparseMatrix<int> NBLDPC::getH() const {
return H_mat;
}
int NBLDPC::getK() const {
return K;
}
int NBLDPC::getN() const {
return N;
}
int NBLDPC::getGF() const {
return GF;
}
bool NBLDPC::getIsSys() const {
return isSystematic;
}