GNU Radio's CCSDS Package
ldpc_encoder.h
Go to the documentation of this file.
1 #ifndef LDPC_ENCODER
2 #define LDPC_ENCODER
3 #include <vector>
4 #include <algorithm>
5 //#include <itpp/itcomm.h>
6 
7 
8 #define N 8176
9 #define K 7154
10 #define P (N - K)
11 //using namespace itpp;
12 void
13 leftrotate(string &s, int d)
14 {
15  reverse(s.begin(), s.begin()+d);
16  reverse(s.begin()+d, s.end());
17  reverse(s.begin(), s.end());
18 }
19 
20 // In-place rotates s towards right by d
21 void
22 rightrotate(string &s, int d)
23 {
24  leftrotate(s, s.length()-d);
25 }
26 
27 std::vector<std::string> splitString(const std::string& str, std::size_t len)
28 {
29  if (len >= str.size())
30  return { str };
31  auto it = str.begin();
32  auto end = it + len;
33  std::vector<std::string> strings;
34  while (end != str.end())
35  {
36  strings.emplace_back(it, end);
37  ++end;
38  ++it;
39  }
40  // have to do this to get the last string since end == str.end()
41  strings.emplace_back(it, end);
42  return strings;
43 }
44 
45 std::vector < std::vector<unsigned char> >
47 {
48  //Reading first circulants rows of generator matrix
49  std::ifstream infile;
50  infile.open("/home/mbkitine/Dropbox/Lulea/GRC/DeepSpace/gr-ccsds/lib/fec/ldpc/gmini/C2.txt");
51  if (!infile) {
52  cerr << "Unable to open file circulants.txt";
53  exit(1); // call system to stop
54  }
55  std::string line;
56  std::vector<std::string> circulants;
57  while (std::getline(infile, line))
58  {
59  circulants.push_back(line);
60  }
61  //std::cout << "Size of circulant before circulation : " << circulants.size() << std::endl;
62  const int numCirculants = 14;
63  const int numCirculations = 511;
64  const int lenCirculants = 511*2;
65 
66  std::vector<std::string> genMtx;
67  std::vector < std::vector<unsigned char> > G;
68  for(int i = 0; i < numCirculants; i++)
69  {
70  std::string curCirculant = circulants.at(i);
71  std::string b1 = curCirculant.substr(0,511);
72  std::string b2 = curCirculant.substr(511,511);
73 // std::cout << "Length of b : " << curCirculant.length() << std::endl;
74 // std::cout << "Length of b1 : " << b1.length() << std::endl;
75 // std::cout << "Length of b2 : " << b2.length() << std::endl;
76  //std::cout << b1 << b2 << std::endl;
77  //std::string b = b1 + b2;
78  //std::cout << b << std::endl;
79  for(int j = 0; j < numCirculations; j++)
80  {
81  std::string b = b1 + b2;
82  //genMtx.push_back(curCirculant);
83  genMtx.push_back(b);
84  rightrotate(b1,1);
85  rightrotate(b2,1);
86 
87 // if((j < 3) && (i < 1))
88 // std::cout << b1 << std::endl;
89  }
90  }
91  //std::cout << "Size of circulant after circulation : " << genMtx.size() << std::endl;
92 
93  std::vector<unsigned char> row;
94  for(int k = 0; k < numCirculants*numCirculations; k++)
95  {
96 
97  for(int i = 0; i < lenCirculants; i++)
98  {
99 
100  row.push_back((genMtx.at(k)[i] == '1') ? 0x01 : 0x00);
101 
102  }
103  G.push_back(row);
104  row.clear();
105  }
106 
107  std::cout << "Generator matrix initialized" << std::endl;
108  std::cout << "N : " << N << std::endl;
109  std::cout << "K : " << K << std::endl;
110  std::cout << "P : " << P << std::endl;
111  return G;
112 }
113 std::vector<unsigned char> ldpc_encode(std::vector<unsigned char> input, std::vector < std::vector<unsigned char> > G)
114 {
115 //Initializing parity bits to 0x00
116  std::vector<unsigned char> parity(P, 0x00);
117 
118  //Generating parity bits
119  for(int i = 0; i < K; i++)
120  {
121  if(input.at(i))//input.at(i)
122  {
123  std::transform(parity.begin(), parity.end(), G.at(i).begin(),
124  parity.begin(), std::bit_xor<unsigned char>());
125  }
126  }
127 
128  //Appending parity bits to the original input
129  input.insert(input.end(), parity.begin(), parity.end());
130  return input;
131 }
132 
133 
134 void C2_Parity()
135 {
136  std::vector<std::string> A1;
137  std::vector<std::string> A2;
138  std::vector< std::vector<int> > A1_indicies{{0, 176}, {12, 239}, {0, 352}, {24, 431}, {0, 392}, {151, 409}, {0, 351}, {9, 359}, {0, 307}, {53, 329}, {0, 207}, {18, 281}, {0, 399}, {202, 457}, {0, 247}, {36, 261}};
139  std::vector< std::vector<int> > A2_indicies{{99, 471}, {130, 473}, {198, 435}, {260, 478}, {215, 420}, {282, 481}, {48, 396}, {193, 445}, {273, 430}, {302, 451}, {96, 379}, {191, 386}, {244, 467}, {364, 470}, {51, 382}, {192, 414}};
140 
141  std::cout <<"size of A1 Indicies: " << A1_indicies.size() << std::endl;
142  std::cout <<"size of A2 Indicies: " << A2_indicies.size() << std::endl;
143 
144  //Initialize strings
145  const int size = 511;
146  std::string zeros = std::string(size,'0');
147  //std::cout << zeros;
148  for(int i = 0; i < 16; i++)
149  {
150  A1.push_back(zeros);A1[i][A1_indicies[i][0]] = '1';A1[i][A1_indicies[i][1]] = '1';
151  A2.push_back(zeros);A2[i][A2_indicies[i][0]] = '1';A2[i][A2_indicies[i][1]] = '1';
152  }
153  std::cout << "Size of A1 : " << A1.size() << std::endl;
154  std::cout << "Size of A2 : " << A1.size() << std::endl;
155 
156  std::vector<std::string> hString;
157 
158 
159  //Upper matrices
160  for(int j = 0; j < 511;j++)
161  {
162 
163  std::string a;
164  for(int i = 0; i < 16; i++)
165  a += A1[i];
166  std::cout << a ;
167  hString.push_back(a);
168  for(int r = 0; r < 16; r++)
169  rightrotate(A1[r],1);
170  }
171 
172  //Lower matrices
173  for(int j = 0; j < 511;j++)
174  {
175 
176  std::string a;
177  for(int i = 0; i < 16; i++)
178  a += A2[i];
179  std::cout << a ;
180  hString.push_back(a);
181  for(int r = 0; r < 16; r++)
182  rightrotate(A2[r],1);
183  }
184 
185 
186  //LDPC_Parity itppParity;
187  /*
188  int R = hString.size();
189  int C = hString[0].length();
190  LDPC_Parity itppParity = LDPC_Parity(R,C);
191  for(int r = 0; r < R; r++)
192  {
193  for(int c = 0; c < C; c++)
194  itppParity.set(r,c,(hString[r][c] == '1') ? 1 : 0);
195  }*/
196 
197  std::cout << "Size of hString : " << hString.size() << " x " << hString[0].length() << std::endl;
198 
199  //Generating parity matrix
200 
201  abort();
202 }
203 
204 #endif // LDPC_ENCODER
205 
#define P
Definition: ldpc_encoder.h:10
void leftrotate(string &s, int d)
Definition: ldpc_encoder.h:13
#define K
Definition: ldpc_encoder.h:9
std::vector< std::vector< unsigned char > > generatorMatrix()
Definition: ldpc_encoder.h:46
#define N
Definition: ldpc_encoder.h:8
void C2_Parity()
Definition: ldpc_encoder.h:134
void rightrotate(string &s, int d)
Definition: ldpc_encoder.h:22
std::vector< unsigned char > ldpc_encode(std::vector< unsigned char > input, std::vector< std::vector< unsigned char > > G)
Definition: ldpc_encoder.h:113
std::vector< std::string > splitString(const std::string &str, std::size_t len)
Definition: ldpc_encoder.h:27