-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibEDM_rate_matcher.cpp
More file actions
377 lines (309 loc) · 10.1 KB
/
libEDM_rate_matcher.cpp
File metadata and controls
377 lines (309 loc) · 10.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <libEDM_rate_matcher.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <libEDM_library.h>
#include <boost/math/common_factor.hpp>
using std::min;
using boost::math::gcd;
RateMatcher::RateMatchingBlock::ActionType RateMatcher::RateMatchingBlock::action()
{
_e -= _a * abs(_delta_N);
if (_e <= 0)
{
_e += _a * _N;
if (_delta_N < 0)
return Puncture;
else
return Repeat;
}
else
return NoChange;
}
dVector RateMatcher::dematch (const dVector &input)
{
return process(input, &RateMatcher::dematcher);
}
void RateMatcher::dematcher (dVector &output, const RateMatcher::RateMatchingBlock::ActionType action, const dMatrix &frames, const size_t frame, size_t &bit)
{
switch (action)
{
case RateMatchingBlock::Puncture :
// bit is missing, so output 0.0
output.push_back(0.0);
bit--;
break;
case RateMatchingBlock::NoChange :
// copy bit to output
output.push_back(frames[frame][bit]);
break;
case RateMatchingBlock::Repeat :
// bit is repeated, so add next two bits together and output
double value = frames[frame][bit];
bit++;
value += frames[frame][bit];
output.push_back(value);
break;
}
}
size_t RateMatcher::interleave (const size_t column_id)
{
if ((column_id < (_interleaver_depth / 2)) && (column_id % 2 == 1))
{
// odd column in first half of list
return column_id + _interleaver_depth / 2;
}
else
{
if ((column_id >= (_interleaver_depth / 2)) && (column_id % 2 == 0))
// even column in second half of list
return column_id - _interleaver_depth / 2;
else
// column position is not changed
return column_id;
}
}
vector<RateMatcher::SequenceType> RateMatcher::initialise_initial_parity_table()
{
vector<SequenceType> initial_parity_table(_interleaver_depth);
switch (_interleaver_depth)
{
case 1:
initial_parity_table[0] = X;
break;
case 2:
initial_parity_table[0] = X;
initial_parity_table[1] = Y;
break;
case 4:
initial_parity_table[0] = X;
initial_parity_table[1] = Y_dash;
initial_parity_table[2] = Y;
initial_parity_table[3] = X;
break;
case 8:
initial_parity_table[0] = X;
initial_parity_table[1] = Y;
initial_parity_table[2] = Y_dash;
initial_parity_table[3] = X;
initial_parity_table[4] = Y;
initial_parity_table[5] = Y_dash;
initial_parity_table[6] = X;
initial_parity_table[7] = Y;
break;
}
return initial_parity_table;
}
map<RateMatcher::SequenceType, RateMatcher::SequenceType> RateMatcher::initialise_next_sequence_table()
{
map<SequenceType, SequenceType> next_sequence_table;
switch (_interleaver_depth)
{
case 1:
case 4:
next_sequence_table[X] = Y;
next_sequence_table[Y] = Y_dash;
next_sequence_table[Y_dash] = X;
case 2:
case 8:
next_sequence_table[X] = Y_dash;
next_sequence_table[Y] = X;
next_sequence_table[Y_dash] = Y;
}
return next_sequence_table;
}
map<RateMatcher::SequenceType, vector<RateMatcher::RateMatchingBlock*> > RateMatcher::initialise_rate_matching_blocks()
{
map<SequenceType, vector<RateMatchingBlock*> > rate_matching_blocks;
if (_coder_type == Convolutional || _matched_block_size >= _dematched_block_size)
{
// one rate matching block per frame required
// compute a
const size_t a = 2;
// compute e_ini
const int N = divide(_dematched_block_size, _interleaver_depth);
const int delta_N = divide(_matched_block_size, _interleaver_depth) - N;
int q;
if (delta_N == 0)
q = -1;
else
q = N / abs(delta_N); // rounding down here is intentional
double q_dash = q;
if (q % 2 == 0)
// q is even
q_dash -= gcd(static_cast<size_t>(q), _interleaver_depth) / _interleaver_depth;
uVector S(_interleaver_depth, 0);
for (size_t x = 0; x < _interleaver_depth; x++)
{
ldiv_t temp = div(static_cast<long>(ceil(x * q_dash)), _interleaver_depth);
S[interleave(temp.rem)] = temp.quot;
}
vector<RateMatchingBlock*> frame_rate_matching_blocks;
for (size_t frame = 0; frame < _interleaver_depth; frame++)
{
size_t e_ini = (a * S[frame] * abs(delta_N) + N) % (a * N);
if (e_ini == 0)
e_ini = a * N;
// initialise rate matching block and add to frame_rate_matching_blocks
frame_rate_matching_blocks.push_back(new RateMatchingBlock(N, delta_N, a, e_ini));
}
rate_matching_blocks[X] = frame_rate_matching_blocks;
}
else
{
// Turbo code requiring puncturing
// compute number of bits per frame per sequence
map<SequenceType, uVector > N;
for (size_t frame = 0; frame < _interleaver_depth; frame++)
{
SequenceType sequence = _initial_parity_table[frame];
// compute and store N for initial parity type
const size_t sequence1_bits_per_frame = static_cast<size_t>(ceil(dematched_frame_size() / 3.0));
N[sequence].push_back(sequence1_bits_per_frame);
// compute and store N for next parity type
sequence = _next_sequence_table[sequence];
const size_t sequence2_bits_per_frame = static_cast<size_t>(ceil((dematched_frame_size() - sequence1_bits_per_frame) / 2.0));
N[sequence].push_back(sequence2_bits_per_frame);
// compute and store N for final parity type
sequence = _next_sequence_table[sequence];
const size_t sequence3_bits_per_frame = dematched_frame_size() - sequence1_bits_per_frame - sequence2_bits_per_frame;
N[sequence].push_back(sequence3_bits_per_frame);
}
const int sequence_frame_size = _matched_block_size / (3 * _interleaver_depth); // rounding down is intentional
const double unrounded_delta_N = (static_cast<int>(_matched_block_size) - static_cast<int>(_dematched_block_size)) / (2.0 * _interleaver_depth);
for (size_t sequence_index = 0; sequence_index < 2; sequence_index++)
{
// sequence 0 = Y; sequence 1 = Y dash
SequenceType sequence;
if (sequence_index == 0)
sequence = Y;
else
sequence = Y_dash;
size_t a;
int delta_N;
if (sequence == Y)
{
a = 2;
delta_N = static_cast<int>(floor(unrounded_delta_N));
}
else
{
a = 1;
delta_N = static_cast<int>(ceil(unrounded_delta_N));
}
vector<RateMatchingBlock*> frame_rate_matching_blocks;
for (size_t frame = 0; frame < _interleaver_depth; frame++)
{
const size_t q = N[sequence][frame] / abs(delta_N);
uVector S(_interleaver_depth, 0);
if (q <= 2)
{
for (size_t x = 0; x < _interleaver_depth; x++)
if (sequence == 0)
S[interleave((3*x + 1) % _interleaver_depth)] = x % 2;
else
S[interleave((3*x + 2) % _interleaver_depth)] = x % 2;
}
else
{
double q_dash = q;
if (q % 2 == 0)
// q is even
q_dash -= gcd(q, _interleaver_depth) / _interleaver_depth;
for (size_t x = 0; x < _interleaver_depth; x++)
{
const size_t r = static_cast<size_t>(ceil(x * q_dash)) % _interleaver_depth;
if (sequence == 0)
S[interleave((3*r + 1) % _interleaver_depth)] = ceil(x * q_dash) / _interleaver_depth;
else
S[interleave((3*r + 2) % _interleaver_depth)] = ceil(x * q_dash) / _interleaver_depth;
}
}
size_t e_ini = (a * S[frame] * abs(delta_N) + N[sequence][frame]) % (a * N[sequence][frame]);
if (e_ini == 0)
e_ini = a * N[sequence][frame];
// initialise rate matching block and add to frame_rate_matching_blocks
frame_rate_matching_blocks.push_back(new RateMatchingBlock(N[sequence][frame], delta_N, a, e_ini));
}
rate_matching_blocks[sequence] = frame_rate_matching_blocks;
}
}
return rate_matching_blocks;
}
dVector RateMatcher::match (const dVector &input)
{
return process(input, &RateMatcher::matcher);
}
void RateMatcher::matcher (dVector &output, const RateMatcher::RateMatchingBlock::ActionType action, const dMatrix &frames, const size_t frame, size_t &bit)
{
switch (action)
{
case RateMatchingBlock::Puncture :
// do nothing
break;
case RateMatchingBlock::NoChange :
// copy bit to output
output.push_back(frames[frame][bit]);
break;
case RateMatchingBlock::Repeat :
// copy bit twice to output
output.push_back(frames[frame][bit]);
output.push_back(frames[frame][bit]);
break;
}
}
dVector RateMatcher::process (const dVector &input, RateMatcher::ProcessFunction process_function)
{
reset();
const dMatrix frames = sort_into_frames(input);
dVector output;
for (size_t frame = 0; frame < _interleaver_depth; frame++)
{
SequenceType sequence = _initial_parity_table[frame];
for (size_t bit = 0; bit < frames[frame].size(); bit++)
{
RateMatchingBlock::ActionType action;
if (_coder_type == Convolutional || _matched_block_size >= _dematched_block_size)
{
action = _rate_matching_blocks[X][frame]->action();
}
else
{
// turbo coding requiring puncturing
switch (sequence)
{
case X:
// no puncturing of X sequence
action = RateMatchingBlock::NoChange;
break;
case Y:
case Y_dash:
action = _rate_matching_blocks[sequence][frame]->action();
assert(action != RateMatchingBlock::Repeat);
break;
}
// find sequence of next bit
sequence = _next_sequence_table[sequence];
}
(this->*process_function)(output, action, frames, frame, bit);
}
assert(output.size() == (frame + 1) * matched_frame_size() || output.size() == (frame + 1) * dematched_frame_size());
}
return output;
}
void RateMatcher::reset()
{
for (map<SequenceType, vector<RateMatchingBlock*> >::iterator rmb = _rate_matching_blocks.begin(); rmb != _rate_matching_blocks.end(); rmb++)
for (size_t frame = 0; frame < _interleaver_depth; frame++)
rmb->second[frame]->reset();
}
dMatrix RateMatcher::sort_into_frames (const dVector &input)
{
dMatrix output(_interleaver_depth);
const size_t bits_per_frame = input.size() / _interleaver_depth;
for (size_t frame = 0, input_index = 0; frame < _interleaver_depth; frame++)
for (size_t bit = 0; bit < bits_per_frame; bit++, input_index++)
output[frame].push_back(input[input_index]);
return output;
}