-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlibturbocodes.c
200 lines (154 loc) · 5.55 KB
/
libturbocodes.c
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
//
// Created by gianluca on 22/02/17.
//
#include "libturbocodes.h"
#include "utilities.h"
#include <stdlib.h>
#include <math.h>
int *turbo_interleave(int *packet, t_turbocode *code)
{
int *interleaved_packet = malloc(code->packet_length * sizeof(int));// {{{
for (int j = 0; j < code->packet_length; ++j) {
interleaved_packet[j] = packet[code->interleaver[j]];
}
return interleaved_packet;// }}}
}
int *turbo_deinterleave(int *packet, t_turbocode *code)
{
int *local = malloc(code->packet_length*sizeof(int));// {{{
for (int i = 0; i < code->packet_length; ++i) {
local[code->interleaver[i]] = packet[i];
}
return local;// }}}
}
void message_interleave(double ***messages, t_turbocode *code)
{
// local array// {{{
double *local[2];
local[0] = malloc(code->packet_length * sizeof(double));
local[1] = malloc(code->packet_length * sizeof(double));
for (int i = 0; i < code->packet_length; ++i) {
local[0][i] = (*messages)[0][code->interleaver[i]];
local[1][i] = (*messages)[1][code->interleaver[i]];
}
for (int i = 0; i < code->packet_length; ++i) {
(*messages)[0][i] = local[0][i];
(*messages)[1][i] = local[1][i];
}
free(local[0]);
free(local[1]);
}
void message_deinterleave(double ***messages, t_turbocode *code)
{
// local array
double *local[2];
local[0] = malloc(code->packet_length * sizeof(double));
local[1] = malloc(code->packet_length * sizeof(double));
for (int i = 0; i < code->packet_length; ++i) {
local[0][code->interleaver[i]] = (*messages)[0][i];
local[1][code->interleaver[i]] = (*messages)[1][i];
}
for (int i = 0; i < code->packet_length; ++i) {
(*messages)[0][i] = local[0][i];
(*messages)[1][i] = local[1][i];
}
free(local[0]);
free(local[1]);
}
t_turbocode *turbo_initialize(t_convcode *upper, t_convcode *lower, int *interleaver, int packet_length)
{
t_turbocode *code = (t_turbocode *) malloc(sizeof *code);/*{{{*/
code->upper_code = upper;
code->lower_code = lower;
code->packet_length = packet_length;
code->interleaver = interleaver;
// compute encoded length
int turbo_length = 0;
turbo_length += upper->components * (code->packet_length + upper->memory);
turbo_length += lower->components * (code->packet_length + lower->memory);
code->encoded_length = turbo_length;
return code;/*}}}*/
}
int *turbo_encode(int *packet, t_turbocode *code)
{
int *interleaved_packet = turbo_interleave(packet, code);/*{{{*/
// reference to encoded messages
int **conv_encoded = malloc(2 * sizeof(int*));
int turbo_length = code->encoded_length;
conv_encoded[0] = convcode_encode(packet, code->packet_length, code->upper_code);
conv_encoded[1] = convcode_encode(interleaved_packet, code->packet_length, code->lower_code);
int *turbo_encoded = malloc(turbo_length * sizeof *turbo_encoded);
t_convcode *codes[2] = {code->upper_code, code->lower_code};
// parallel to serial
int k = 0, c = 0, cw = 0;/*{{{*/
while (k < turbo_length) {
t_convcode *cc = codes[c];
// number of components of cc
int comps = cc->components;
// copy bits from cc output to turbo_encoded
for (int i = 0; i < comps; i++) {
int bit = conv_encoded[c][cw*comps + i];
turbo_encoded[k++] = bit;
}
c = (c + 1) % 2;
// when c = 0 the first codeword is complete
cw = !c ? cw+1 : cw;
}/*}}}*/
free(conv_encoded[0]);
free(conv_encoded[1]);
free(conv_encoded);
free(interleaved_packet);
return turbo_encoded;/*}}}*/
}
int *turbo_decode(double *received, int iterations, double noise_variance, t_turbocode *code)
{
// serial to parallel/*{{{*/
int lengths[2]; // = malloc(2 * sizeof *lengths);/*{{{*/
double *streams[2];
t_convcode *codes[2] = {code->upper_code, code->lower_code};
for (int i = 0; i < 2; i++) {
t_convcode *cc = codes[i];
lengths[i] = cc->components * (code->packet_length + cc->memory);
streams[i] = malloc(lengths[i] * sizeof(double));
}
int k = 0, c = 0, cw = 0;
while (k < code->encoded_length) {
t_convcode *cc = codes[c];
for (int i = 0; i < cc->components; i++)
streams[c][cw*cc->components + i] = received[k++];
c = (c + 1) % 2;
cw = !c ? cw + 1 : cw;
}/*}}}*/
// initial messages
double **messages = malloc(2 * sizeof *messages);
for (int i = 0; i < 2; i++) {
messages[i] = malloc(code->packet_length * sizeof(double));
for (int j = 0; j < code->packet_length; j++) {
messages[i][j] = log(0.5);
}
}
int *turbo_decoded = NULL;
for (int i = 0; i < iterations; i++) {
// run BCJR on upper code
convcode_extrinsic(streams[0], lengths[0], &messages, code->upper_code, noise_variance, 0);
// apply interleaver
message_interleave(&messages, code);
// run BCJR on lower code
turbo_decoded = convcode_extrinsic(streams[1], lengths[1], &messages, code->lower_code, noise_variance, i == (iterations - 1));
// deinterleave
message_deinterleave(&messages, code);
}
int *decoded_deinterleaved = turbo_deinterleave(turbo_decoded, code);
free(turbo_decoded);
free(messages[0]);
free(messages[1]);
free(messages);
free(streams[0]);
free(streams[1]);
//length of the
return decoded_deinterleaved; /*}}}*/
}
void *turbocode_clear(t_turbocode *code)
{
free(code->interleaver);
}