-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurbo_code_version1.cpp
More file actions
362 lines (299 loc) · 10.4 KB
/
Turbo_code_version1.cpp
File metadata and controls
362 lines (299 loc) · 10.4 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
/***************************************************
Channel Coding Course Work: Turbo Codes (Final Working Version)
Fixes:
1. Corrected De-interleaving mapping logic (CRITICAL FIX).
2. Removed strict termination assumption in decoder (Beta init).
3. Corrected data types and print formats.
***************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
// --- System Parameters ---
#define MSG_LEN 256 // Message length
#define STATE_NUM 4 // States for RSC(1, 5/7)
#define ITERATIONS 8 // Iterations
#define TAIL_BITS 2
#define message_length (MSG_LEN + TAIL_BITS)
#define codeword_length (message_length * 3)
float code_rate = 1.0f / 3.0f;
// --- Global Constants & Variables ---
#define pi 3.141592653589793
#define INF 1e9
double N0, sgm;
// Trellis Structures
int next_state[STATE_NUM][2];
int output_parity[STATE_NUM][2];
// Interleaver Map
int alpha_interleaver[message_length];
// int deinterleaver[message_length]; // REMOVED: Not needed and caused confusion
// Data Buffers
int message[message_length];
int codeword[codeword_length];
int re_codeword[codeword_length];
int de_message[message_length];
// Modulation Buffers
double tx_symbol[codeword_length][2];
double rx_symbol[codeword_length][2];
// Decoder Internal Buffers
double rx_sys[message_length];
double rx_par1[message_length];
double rx_par2[message_length];
// Extrinsic Information exchange
double L_ext1[message_length];
double L_ext2[message_length];
// Function Prototypes
void statetable();
void encoder();
void modulation();
void channel();
void decoder();
void rsc_encoder(int* input_bits, int* parity_out, int len);
void siso_decoder(double* L_in_sys, double* L_in_par, double* L_in_apriori, double* L_out_extrinsic, int len, int terminate);
double max_star(double a, double b);
int main()
{
int i;
float SNR, start, finish;
long int bit_error, seq, seq_num;
double BER;
double progress;
statetable();
srand((unsigned int)time(0));
printf("--- Turbo Code Simulation (Final Version) ---\n");
printf("Message Length: %d, Codeword Length: %d\n", message_length, codeword_length);
printf("\nEnter start SNR (dB) [suggest -2.0]: ");
scanf("%f", &start);
printf("\nEnter finish SNR (dB) [suggest 3.0]: ");
scanf("%f", &finish);
printf("\nPlease input number of frames [e.g., 50]: ");
scanf("%ld", &seq_num);
for (SNR = start; SNR <= finish; SNR += 0.5f)
{
N0 = (1.0 / code_rate) / pow(10.0, (float)(SNR) / 10.0);
sgm = sqrt(N0 / 2.0);
bit_error = 0;
for (seq = 1; seq <= seq_num; seq++)
{
// 1. Generate Message (Last 2 bits 0)
for (i = 0; i < message_length - TAIL_BITS; i++)
message[i] = rand() % 2;
for (i = message_length - TAIL_BITS; i < message_length; i++)
message[i] = 0;
// 2. Encode
encoder();
// 3. Modulate
modulation();
// 4. Channel
channel();
// 5. Decode
decoder();
// 6. Count Errors
for (i = 0; i < message_length - TAIL_BITS; i++)
{
if (message[i] != de_message[i])
bit_error++;
}
progress = (double)(seq * 100) / (double)seq_num;
if (seq % 10 == 0 || seq == seq_num) {
BER = (double)bit_error / (double)((message_length - TAIL_BITS) * seq);
printf("Progress=%3.0f%%, SNR=%4.1f, Errors=%6ld, BER=%E\r", progress, SNR, bit_error, BER);
}
}
BER = (double)bit_error / (double)((message_length - TAIL_BITS) * seq_num);
printf("Progress=%3.0f%%, SNR=%4.1f, Errors=%6ld, BER=%E\n", progress, SNR, bit_error, BER);
}
printf("\nSimulation finished. Press Enter to exit.");
getchar(); getchar();
return 0;
}
void statetable()
{
int s, u;
// RSC (1, 5/7) octal
for (s = 0; s < STATE_NUM; s++) {
for (u = 0; u < 2; u++) {
int m1 = (s >> 1) & 1;
int m0 = s & 1;
int a_k = (u + m1 + m0) % 2;
int out = (a_k + m0) % 2;
int next_s = (a_k << 1) | m1;
next_state[s][u] = next_s;
output_parity[s][u] = out;
}
}
// Random Interleaver
int i, j, temp;
for (i = 0; i < message_length; i++) alpha_interleaver[i] = i;
for (i = message_length - 1; i > 0; i--) {
j = rand() % (i + 1);
temp = alpha_interleaver[i];
alpha_interleaver[i] = alpha_interleaver[j];
alpha_interleaver[j] = temp;
}
}
void rsc_encoder(int* input_bits, int* parity_out, int len)
{
int s = 0;
int i;
for (i = 0; i < len; i++) {
int u = input_bits[i];
parity_out[i] = output_parity[s][u];
s = next_state[s][u];
}
}
void encoder()
{
int i;
int parity1[message_length];
int parity2[message_length];
int interleaved_msg[message_length];
for(i=0; i<message_length; i++) interleaved_msg[i] = message[alpha_interleaver[i]];
rsc_encoder(message, parity1, message_length);
rsc_encoder(interleaved_msg, parity2, message_length);
for (i = 0; i < message_length; i++) {
codeword[3 * i] = message[i];
codeword[3 * i + 1] = parity1[i];
codeword[3 * i + 2] = parity2[i];
}
}
void modulation()
{
// BPSK: 0->+1, 1->-1
int i;
for (i = 0; i < codeword_length; i++)
{
tx_symbol[i][0] = (codeword[i] == 0) ? 1.0 : -1.0;
tx_symbol[i][1] = 0;
}
}
void channel()
{
int i, j;
double u, r, g;
for (i = 0; i < codeword_length; i++) {
for (j = 0; j < 2; j++) {
u = (float)rand() / (float)RAND_MAX;
if (u == 1.0) u = 0.999999;
r = sgm * sqrt(2.0 * log(1.0 / (1.0 - u)));
u = (float)rand() / (float)RAND_MAX;
if (u == 1.0) u = 0.999999;
g = (float)r * cos(2 * pi * u);
rx_symbol[i][j] = tx_symbol[i][j] + g;
}
}
}
double max_star(double a, double b) {
return (a > b) ? a : b;
}
void siso_decoder(double* L_in_sys, double* L_in_par, double* L_in_apriori, double* L_out_extrinsic, int len, int terminate)
{
static double alpha[MSG_LEN + TAIL_BITS + 1][STATE_NUM];
static double beta[MSG_LEN + TAIL_BITS + 1][STATE_NUM];
static double gamma[MSG_LEN + TAIL_BITS][STATE_NUM][2];
int k, s, u;
// 1. Init Alpha
for (s = 0; s < STATE_NUM; s++) alpha[0][s] = (s == 0) ? 0.0 : -INF;
// 2. Init Beta
// Assuming unknown termination (equal probability)
for (s = 0; s < STATE_NUM; s++) {
if (terminate) beta[len][s] = (s == 0) ? 0.0 : -INF;
else beta[len][s] = 0.0;
}
// 3. Gamma Calculation
for (k = 0; k < len; k++) {
for (s = 0; s < STATE_NUM; s++) {
for (u = 0; u < 2; u++) {
int parity = output_parity[s][u];
double sign_u = (u == 0) ? 1.0 : -1.0;
double sign_p = (parity == 0) ? 1.0 : -1.0;
// LLR: Positive supports 0.
double metric = 0.5 * (sign_u * (L_in_sys[k] + L_in_apriori[k]) + sign_p * L_in_par[k]);
gamma[k][s][u] = metric;
}
}
}
// 4. Alpha Recursion
for (k = 1; k <= len; k++) {
for (s = 0; s < STATE_NUM; s++) {
alpha[k][s] = -INF;
int prev_s, input_bit;
for(prev_s = 0; prev_s < STATE_NUM; prev_s++) {
for(input_bit = 0; input_bit < 2; input_bit++) {
if (next_state[prev_s][input_bit] == s) {
alpha[k][s] = max_star(alpha[k][s], alpha[k-1][prev_s] + gamma[k-1][prev_s][input_bit]);
}
}
}
}
}
// 5. Beta Recursion
for (k = len - 1; k >= 0; k--) {
for (s = 0; s < STATE_NUM; s++) {
beta[k][s] = -INF;
for (u = 0; u < 2; u++) {
int n_s = next_state[s][u];
beta[k][s] = max_star(beta[k][s], beta[k+1][n_s] + gamma[k][s][u]);
}
}
}
// 6. Extrinsic Calculation
for (k = 0; k < len; k++) {
double L0 = -INF;
double L1 = -INF;
for (s = 0; s < STATE_NUM; s++) {
int ns0 = next_state[s][0];
L0 = max_star(L0, alpha[k][s] + gamma[k][s][0] + beta[k+1][ns0]);
int ns1 = next_state[s][1];
L1 = max_star(L1, alpha[k][s] + gamma[k][s][1] + beta[k+1][ns1]);
}
double L_total = L0 - L1;
double ext = L_total - L_in_sys[k] - L_in_apriori[k];
// Clamp to avoid overflow/instability
if (ext > 50.0) ext = 50.0;
if (ext < -50.0) ext = -50.0;
L_out_extrinsic[k] = ext;
}
}
void decoder()
{
int i, iter;
double Lc = 2.0 / (sgm * sgm);
// Initialize inputs
for (i = 0; i < message_length; i++) {
rx_sys[i] = Lc * rx_symbol[3 * i][0];
rx_par1[i] = Lc * rx_symbol[3 * i + 1][0];
rx_par2[i] = Lc * rx_symbol[3 * i + 2][0];
L_ext2[i] = 0.0;
}
for (iter = 0; iter < ITERATIONS; iter++) {
// --- Decoder 1 ---
siso_decoder(rx_sys, rx_par1, L_ext2, L_ext1, message_length, 0);
// Interleave Ext1 -> Priori2
double rx_sys_int[message_length];
double L_apriori2[message_length];
for (i = 0; i < message_length; i++) {
rx_sys_int[i] = rx_sys[alpha_interleaver[i]];
L_apriori2[i] = L_ext1[alpha_interleaver[i]];
}
// --- Decoder 2 ---
siso_decoder(rx_sys_int, rx_par2, L_apriori2, L_ext2, message_length, 0);
// De-interleave Ext2 -> Priori1
// FIX: Mapping back from interleaved domain to natural domain
// Interleaved position 'i' corresponds to Natural position 'alpha[i]'
double temp[message_length];
for (i = 0; i < message_length; i++) temp[i] = L_ext2[i]; // Store interleaved output
for (i = 0; i < message_length; i++) {
// Put the info back to where it belongs in the natural sequence
L_ext2[alpha_interleaver[i]] = temp[i];
}
}
// Hard Decision
for (i = 0; i < message_length; i++) {
double final_LLR = rx_sys[i] + L_ext1[i] + L_ext2[i];
if (final_LLR >= 0) de_message[i] = 0;
else de_message[i] = 1;
}
}
void demodulation() {}