-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturbo_code_Log_MAP.c
More file actions
2544 lines (2177 loc) · 63.2 KB
/
turbo_code_Log_MAP.c
File metadata and controls
2544 lines (2177 loc) · 63.2 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------
* Copyright Nov 2003, Mobile Communication Lab of BUPT
* All rights reserved.
*
* turbo_code_Log_MAP.cpp
*
* This script implements the functions used in turbo coding and decoding.
*
* Version: 1.0
* Programmed By Peng Zhang
* Last updated date: Dec, 13, 2003
---------------------------------------------------------------*/
#include "turbo_code_Log_MAP.h"
/*==================================================*/
/* lookup table used in Log-MAP decoder */
const double lookup_index_Log_MAP[16] = {0.0, 0.08824, 0.19587, 0.31026, 0.43275, 0.56508,
0.70963, 0.86972, 1.0502, 1.2587, 1.5078, 1.8212,
2.2522, 2.9706, 3.6764, 4.3758};
const double lookup_table_Log_MAP[16] = {0.69315, 0.65, 0.6, 0.55, 0.5, 0.45, 0.4, 0.35,
0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.025, 0.0125};
/*CDMA2000中Turbo码所使用的交织器要使用该查找表。
各列分别对应于N=4,5,6,7,8,9,10。N>=log2(Nturbo)-5。*/
const int lookuptable_cdma2000[7][32] =
{{5,15,5,15,1,9,9,15,13,15,7,11,15,3,15,5,13,15,9,3,1,3,15,1,13,1,9,15,11,3,15,5},
{27,3,1,15,13,17,23,13,9,3,15,3,13,1,13,29,21,19,1,3,29,17,25,29,9,13,23,13,13,1,13,13},
{3,27,15,13,29,5,1,31,3,9,15,31,17,5,39,1,19,27,15,13,45,5,33,15,13,9,15,31,17,5,15,33},
{15,127,89,1,31,15,61,47,127,17,119,15,57,123,95,5,85,17,55,57,15,41,93,87,63,15,13,15,81,57,31,69},
{3,1,5,83,19,179,19,99,23,1,3,13,13,3,17,1,63,131,17,131,211,173,231,171,23,147,243,213,189,51,15,67},
{13,335,87,15,15,1,333,11,13,1,121,155,1,175,421,5,509,215,47,425,295,229,427,83,409,387,193,57,501,313,489,391},
{1,349,303,721,973,703,761,327,453,95,241,187,497,909,769,349,71,557,197,499,409,259,335,253,677,717,313,757,189,15,75,163}};
/*==================================================*/
/* parameters on g matrix */
const int M_num_reg = COLUMN_OF_G-1; /* number of rigisters */
const int n_states = 8; /* number of states : pow(2, M) */
/*==================================================*/
/*---------------------------------------------------------------
FUNCTION:
TurboCodingTraffic(int *trafficflow_source, int *coded_trafficflow_source,
int *traffic_source_length)编码程序
DESCRIPTION:
This function encodes the traffic flow bits.
PARAMETERS:
INPUT:
trafficflow_source - Contains pointer to the source bits sequence.输入数据流
traffic_source_length - The pointer that give out the length of the source bits.输入数据流长度
OUTPUT:
coded_trafficflow_source - Contains pointer to the coded bits sequence.输出数据流的指针,也就是输出
RETURN VALUE:
None.
---------------------------------------------------------------*/
void TurboCodingTraffic(int *trafficflow_source, float *coded_trafficflow_source,
int *traffic_source_length)
{
int i;
int *temp_send = NULL;
int *send = NULL;
int *send_punc = NULL;
int length_info = *traffic_source_length;
switch( length_info/320 )
{
case 1:
printf("1!\n");
case 2:
printf("23!\n");
case 4:
break;
default:
{
printf("Wrong frame length!!\n");
exit(1);
}
}
/*----------------------错误检查----------------------------------------*/
if ((send=(int *)malloc((3*length_info+4*M_num_reg)*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of send \n");
exit(1);
}
if ((send_punc=(int *)malloc(2*length_info*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of send_punc \n");
exit(1);
}
if (TYPE_INTERLEAVER == 2)
{
gen_rand_index(length_info, 1);
}
/*------------------------------------------------------------------------*/
encoderm_turbo(trafficflow_source, send, length_info, 1); /* encode and BPSK modulate */
temp_send = send;
if (TURBO_PUNCTURE) /* punture the coded bits */
{
puncture(send, 3*length_info+4*M_num_reg, send_punc, 3, length_info/4, 4);
temp_send = send_punc;
}
for (i=0; i<((3-TURBO_PUNCTURE)*length_info+4*M_num_reg*(1-TURBO_PUNCTURE)); i++)
{
*(coded_trafficflow_source+i) = (float) *(temp_send+i);
}
*traffic_source_length = (3-TURBO_PUNCTURE)*length_info+4*M_num_reg*(1-TURBO_PUNCTURE);
free(send);
free(send_punc);
}
/*---------------------------------------------------------------
FUNCTION:
TurboDecodingTraffic(float *trafficflow_for_decode, int *trafficflow_decoded,
int *trafficflow_length, float EbN0dB)
DESCRIPTION:
This function decodes the received traffic flow bits.
PARAMETERS:
INPUT:
trafficflow_for_decode - Contains pointer to the received bits sequence.
trafficflow_length - The pointer that give out the length of the received bits.
EbN0dB - Eb/N0 in dB.
OUTPUT:
trafficflow_decoded - Contains pointer to the decoded bits sequence.
RETURN VALUE:
None.
---------------------------------------------------------------*/
void TurboDecodingTraffic(float *trafficflow_for_decode, int *trafficflow_decoded,
int *trafficflow_length, float EbN0dB)
{
int i;
int length_info, length_total;
int iteration;
float *receive_punc = NULL; /* receiving data */
float *yk_turbo = NULL; /* include ys & yp */
float *La_turbo, *Le_turbo, *LLR_all_turbo; /* extrinsic information & LLR */
int *tempout;
float en_rate = (float)pow(10, EbN0dB*0.1);
float Lc_turbo = 4*en_rate*rate_coding; /* reliability value of the channel */
printf("trafficflow_length: %d\n", *trafficflow_length);
switch((*trafficflow_length)/640)
{
case 1:
case 2:
case 4:
break;
default:
{
printf("Wrong frame length!!!!\n");
exit(1);
break;
}
}
if (TURBO_PUNCTURE)
{
length_info = (*trafficflow_length)/2;
}
else
{
length_info = (*trafficflow_length-4*M_num_reg)/3;
}
length_total = length_info+M_num_reg;
if ((receive_punc=(float *)malloc((3*length_info+4*M_num_reg)*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of receive_punc \n");
exit(1);
}
if ((yk_turbo=(float *)malloc(4*length_total*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of yk_turbo \n");
exit(1);
}
if ((La_turbo=(float *)malloc(length_total*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of La_turbo \n");
exit(1);
}
if ((Le_turbo=(float *)malloc(length_total*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of Le_turbo \n");
exit(1);
}
if ((LLR_all_turbo=(float *)malloc(length_total*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of LLR_all_turbo \n");
exit(1);
}
if ((tempout=(int *)malloc(length_total*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of tempout \n");
exit(1);
}
if (TURBO_PUNCTURE) /* fill in the punctured bits and demultiplex */
{
depuncture(trafficflow_for_decode, 2*length_info, receive_punc, 3, length_info/4, 4);
demultiplex(receive_punc, length_info, yk_turbo, 1);
}
else
{
demultiplex(trafficflow_for_decode, length_info, yk_turbo, 1);
}
/* scale the data */
for (i=0; i<2*length_total*2; i++)
{
*(yk_turbo+i) = (float)( *(yk_turbo+i) * Lc_turbo *0.5 );
}
for (i=0; i<length_total; i++)
{
*(La_turbo+i) = *(Le_turbo+i) = *(LLR_all_turbo+i) = 0;
}
for (iteration=0; iteration<N_ITERATION; iteration++) /* start iteration */
{
/* decoder one: */
/* get extrinsic information from decoder two */
de_interleave_float(La_turbo, Le_turbo, TYPE_INTERLEAVER, length_info, 1);
for (i=length_info; i<length_total; i++)
{
*(La_turbo+i) = 0;
}
switch(TYPE_DECODER)
{
case 1:
Log_MAP_decoder(yk_turbo, La_turbo, 1, LLR_all_turbo, length_total);
break;
case 2:
MAX_Log_MAP_decoder(yk_turbo, La_turbo, 1, LLR_all_turbo, length_total);
break;
default:
break;
}
/* caculate the extrinsic information */
for (i=0; i<length_total; i++)
{
*(Le_turbo+i) = *(LLR_all_turbo+i) - *(La_turbo+i) - 2*(*(yk_turbo+2*i));
}
/* decoder two: */
/* get extrinsic information from decoder one */
interleave_float(Le_turbo, La_turbo, TYPE_INTERLEAVER, length_info, 1);
for (i=length_info; i<length_total; i++)
{
*(La_turbo+i) = 0;
}
switch(TYPE_DECODER)
{
case 1:
Log_MAP_decoder(yk_turbo+2*length_total, La_turbo, 1, LLR_all_turbo, length_total);
break;
case 2:
MAX_Log_MAP_decoder(yk_turbo+2*length_total, La_turbo, 1, LLR_all_turbo, length_total);
break;
default:
break;
}
/* caculate the extrinsic information */
for(i=0; i<length_total; i++)
{
*(Le_turbo+i) = *(LLR_all_turbo+i) - *(La_turbo+i) - 2*(*(yk_turbo+2*length_total+2*i));
}
/* end of decoder two */
}
/* get the decision bits from LLR gained from these decoder */
decision(LLR_all_turbo, length_total, tempout);
/* printf("%d", tempout);*/
de_interleave_int(trafficflow_decoded, tempout, TYPE_INTERLEAVER, length_info, 1);
*trafficflow_length = length_info;
free(receive_punc);
free(yk_turbo);
free(La_turbo);
free(Le_turbo);
free(LLR_all_turbo);
free(tempout);
}
void TurboCodingSupflow(int *supflow, float *coded_supflow, int *supflow_length)
{
int i;
int *temp_send = NULL;
int *send = NULL;
int *send_punc = NULL;
int length_info = *supflow_length;
switch( length_info/320 )
{
case 1:
case 2:
case 4:
break;
default:
{
printf("Wrong frame length!\n");
exit(1);
break;
}
}
if ((send=(int *)malloc((3*length_info+4*M_num_reg)*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of send \n");
exit(1);
}
if ((send_punc=(int *)malloc(2*length_info*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of send_punc \n");
exit(1);
}
if (TYPE_INTERLEAVER == 2)
{
gen_rand_index(length_info, 0);
}
encoderm_turbo(supflow, send, length_info, 0); /* encode and BPSK modulate */
temp_send = send;
if (TURBO_PUNCTURE) /* punture the coded bits */
{
puncture(send, 3*length_info+4*M_num_reg, send_punc, 3, length_info/4, 4);
temp_send = send_punc;
}
for (i=0; i<((3-TURBO_PUNCTURE)*length_info+4*M_num_reg*(1-TURBO_PUNCTURE)); i++)
{
*(coded_supflow+i) = (float) *(temp_send+i);
}
*supflow_length = (3-TURBO_PUNCTURE)*length_info+4*M_num_reg*(1-TURBO_PUNCTURE);
free(send);
free(send_punc);
}
void TurboDecodingSupflow(float *supflow_for_decode, int *supflow_decoded,
int *supflow_length, float EbN0dB)
{
int i;
int length_info, length_total;
int iteration;
float *receive_punc = NULL; /* receiving data */
float *yk_turbo = NULL; /* include ys & yp */
float *La_turbo, *Le_turbo, *LLR_all_turbo; /* extrinsic information & LLR */
int *tempout;
float en_rate = (float)pow(10, EbN0dB*0.1);
float Lc_turbo = 4*en_rate*rate_coding; /* reliability value of the channel */
switch((*supflow_length)/640)
{
case 1:
case 2:
case 4:
break;
default:
{
printf("Wrong frame length!\n");
exit(1);
break;
}
}
if (TURBO_PUNCTURE)
{
length_info = (*supflow_length)/2;
}
else
{
length_info = (*supflow_length-4*M_num_reg)/3;
}
length_total = length_info+M_num_reg;
if ((receive_punc=(float *)malloc((3*length_info+4*M_num_reg)*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of receive_punc \n");
exit(1);
}
if ((yk_turbo=(float *)malloc(4*length_total*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of yk_turbo \n");
exit(1);
}
if ((La_turbo=(float *)malloc(length_total*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of La_turbo \n");
exit(1);
}
if ((Le_turbo=(float *)malloc(length_total*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of Le_turbo \n");
exit(1);
}
if ((LLR_all_turbo=(float *)malloc(length_total*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of LLR_all_turbo \n");
exit(1);
}
if ((tempout=(int *)malloc(length_total*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of tempout \n");
exit(1);
}
if (TURBO_PUNCTURE) /* fill in the punctured bits and demultiplex */
{
depuncture(supflow_for_decode, 2*length_info, receive_punc, 3, length_info/4, 4);
demultiplex(receive_punc, length_info, yk_turbo, 0);
}
else
{
demultiplex(supflow_for_decode, length_info, yk_turbo, 0);
}
/* scale the data */
for (i=0; i<2*length_total*2; i++)
{
*(yk_turbo+i) = (float)( *(yk_turbo+i) * Lc_turbo *0.5 );
}
for (i=0; i<length_total; i++)
{
*(La_turbo+i) = *(Le_turbo+i) = *(LLR_all_turbo+i) = 0;
}
for (iteration=0; iteration<N_ITERATION; iteration++) /* start iteration */
{
/* decoder one: */
/* get extrinsic information from decoder two */
de_interleave_float(La_turbo, Le_turbo, TYPE_INTERLEAVER, length_info, 0);
for (i=length_info; i<length_total; i++)
{
*(La_turbo+i) = 0;
}
switch(TYPE_DECODER)
{
case 1:
Log_MAP_decoder(yk_turbo, La_turbo, 1, LLR_all_turbo, length_total);
break;
case 2:
MAX_Log_MAP_decoder(yk_turbo, La_turbo, 1, LLR_all_turbo, length_total);
break;
default:
break;
}
/* caculate the extrinsic information */
for (i=0; i<length_total; i++)
{
*(Le_turbo+i) = *(LLR_all_turbo+i) - *(La_turbo+i) - 2*(*(yk_turbo+2*i));
}
/* decoder two: */
/* get extrinsic information from decoder one */
interleave_float(Le_turbo, La_turbo, TYPE_INTERLEAVER, length_info, 0);
for (i=length_info; i<length_total; i++)
{
*(La_turbo+i) = 0;
}
switch(TYPE_DECODER)
{
case 1:
Log_MAP_decoder(yk_turbo+2*length_total, La_turbo, 1, LLR_all_turbo, length_total);
break;
case 2:
MAX_Log_MAP_decoder(yk_turbo+2*length_total, La_turbo, 1, LLR_all_turbo, length_total);
break;
default:
break;
}
/* caculate the extrinsic information */
for(i=0; i<length_total; i++)
{
*(Le_turbo+i) = *(LLR_all_turbo+i) - *(La_turbo+i) - 2*(*(yk_turbo+2*length_total+2*i));
}
/* end of decoder two */
}
/* get the decision bits from LLR gained from these decoder */
decision(LLR_all_turbo, length_total, tempout);
de_interleave_int(supflow_decoded, tempout, TYPE_INTERLEAVER, length_info, 0);
*supflow_length = length_info;
free(receive_punc);
free(yk_turbo);
free(La_turbo);
free(Le_turbo);
free(LLR_all_turbo);
free(tempout);
}
/*---------------------------------------------------------------
FUNCTION:
TurboCodingInit(int *traffic_source_length)Turbo编码初始化(int *traffic source length)
DESCRIPTION:
Initialize the parameters of coding.初始化编码的参数。
PARAMETERS:
INPUT:
traffic_source_length - The pointer that give out the length of the source bits.
输入:traffic source length-表示源比特流的指针。
RETURN VALUE:
None.
这段代码是一个用于初始化 Turbo 编码器的函数 `TurboCodingInit`。它包含了以下主要功能:
1. **初始化参数**:
- 设置 Turbo 编码的行数和列数。
- 根据是否使用了穿孔 (puncturing) 计算编码速率。
2. **分配内存**:
- 动态分配存储编码生成矩阵 (`g_matrix`) 的内存。
- 如果使用了穿孔,还会为不同长度的穿孔矩阵分配内存。
3. **生成生成矩阵**:
- 调用 `gen_g_matrix` 函数生成编码生成矩阵。
4. **生成穿孔矩阵**:
- 如果使用了穿孔,调用 `gen_mx_punc` 函数生成穿孔矩阵。
5. **初始化状态机(Trellis)结构**:
- 分配内存用于 Trellis 状态机的输出和状态。
6. **随机交织器索引的内存分配**:
- 如果选择了随机交织器类型,则为交织器索引分配内存。
这段代码展示了 Turbo 编码初始化的基本过程,包括内存管理和关键数据结构的设置。每个部分都有错误检查,以确保在内存分配失败时能及时退出。
---------------------------------------------------------------*/
void TurboCodingInit()
{
turbo_g.N_num_row = 2; /* initialize number of rows and columns of g */
turbo_g.K_num_col = COLUMN_OF_G;
/* caculate the coding rate */
rate_coding = TURBO_PUNCTURE? (float)(0.5) : (float)(0.333333);
if ((turbo_g.g_matrix=(int *)malloc(turbo_g.N_num_row*turbo_g.K_num_col*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of turbo_g\n");
exit(1);
}
/* generate the code generator matrix */
if (!gen_g_matrix(turbo_g.K_num_col, G_ROW_1, G_ROW_2, turbo_g.g_matrix))
{
printf("error number of G\n");
exit(1);
}
/* generate puncture matrix */
if (TURBO_PUNCTURE)
{
if ((mx_puncture_turbo_80=(int *)malloc(sizeof(int)*3*80))==NULL)
{
printf("\n fail to allocate memory of mx_puncture_turbo_80\n");
exit(1);
}
if ((mx_puncture_turbo_160=(int *)malloc(sizeof(int)*3*160))==NULL)
{
printf("\n fail to allocate memory of mx_puncture_turbo_160\n");
exit(1);
}
if ((mx_puncture_turbo_320=(int *)malloc(sizeof(int)*3*320))==NULL)
{
printf("\n fail to allocate memory of mx_puncture_turbo_320\n");
exit(1);
}
if (!gen_mx_punc()) /* generate the matrix for puncture */
{
printf("\nError! Can not generate the puncture matrix properly!\n");
exit(1);
}
}
/* initialize the trellis struct */
if ((turbo_trellis.mx_lastout=(int *)malloc(sizeof(int)*n_states*4))==NULL)
{
printf("\n fail to allocate memory of turbo_trellis.mx_lastout \n");
exit(1);
}
if ((turbo_trellis.mx_laststat=(int *)malloc(sizeof(int)*n_states*2))==NULL)
{
printf("\n fail to allocate memory of turbo_trellis.mx_laststat \n");
exit(1);
}
if ((turbo_trellis.mx_nextout=(int *)malloc(sizeof(int)*n_states*4))==NULL)
{
printf("\n fail to allocate memory of turbo_trellis.mx_nextout \n");
exit(1);
}
if ((turbo_trellis.mx_nextstat=(int *)malloc(sizeof(int)*n_states*2))==NULL)
{
printf("\n fail to allocate memory of turbo_trellis.mx_nextstat \n");
exit(1);
}
gen_trellis();
/* malloc memory for the index of random interleaver */
if (TYPE_INTERLEAVER==2)
{
if ((index_randomintlvr=(int *)malloc(MAX_FRAME_LENGTH*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of index_randomintlvr \n");
exit(1);
}
if ((index_randomintlvr_sup=(int *)malloc(SUP_FRAME_LENGTH*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of index_randomintlvr_sup \n");
exit(1);
}
}
}
/*---------------------------------------------------------------
FUNCTION:
TurboCodingFinish()
DESCRIPTION:
Free the memory of turbo coding.
PARAMETERS:
None.
RETURN VALUE:
None.
---------------------------------------------------------------*/
void TurboCodingRelease()
{
free(turbo_g.g_matrix);
if (TURBO_PUNCTURE)
{
free(mx_puncture_turbo_80);
free(mx_puncture_turbo_160);
free(mx_puncture_turbo_320);
}
free(turbo_trellis.mx_lastout);
free(turbo_trellis.mx_laststat);
free(turbo_trellis.mx_nextout);
free(turbo_trellis.mx_nextstat);
if (TYPE_INTERLEAVER==2)
{
free(index_randomintlvr);
free(index_randomintlvr_sup);
}
}
/*---------------------------------------------------------------
FUNCTION:
int gen_g_matrix(int k_column, int g_row1, int g_row2, int *mx_g_turbo)
DESCRIPTION:
This function generate the code generater g for the random interleaver.
PARAMETERS:
INPUT:
k - Number of columns of g matrix.
g_row1 - An octal number indicate the first row of turbo_g.
g_row2 - An octal number indicate the second row of turbo_g.
OUTPUT:
mx_g - Contains pointer to g matrix.
RETURN VALUE:
1 - If the matrix was successfully generated.
0 - Error occurred generating the g matrix.
---------------------------------------------------------------*/
int gen_g_matrix(int k_column, int g_row1, int g_row2, int *mx_g_turbo)
{
int i, position;
int high_num, low_num;
/* for the first row */
high_num = g_row1;
position = 1;
while (high_num>0)
{
low_num = high_num%10;
if (low_num>7)
{
return 0;
}
high_num = high_num/10;
for (i=k_column-(position-1)*3-1; i>=0 && i>=k_column-position*3; i--)
{
*(mx_g_turbo+i) = low_num%2;
low_num = low_num/2;
}
position++;
if (i<0)
{
break;
}
}
/*for the second row */
high_num = g_row2;
position = 1;
while (high_num>0)
{
low_num = high_num%10;
if (low_num>7)
{
return 0;
}
high_num = high_num/10;
for (i=k_column-(position-1)*3-1; i>=0 && i>=k_column-position*3; i--)
{
*(mx_g_turbo+k_column+i) = low_num%2;
low_num = low_num/2;
}
position++;
if (i<0)
{
break;
}
}
return 1;
}
/*---------------------------------------------------------------
FUNCTION:
void gen_trellis()
DESCRIPTION:
This function generate the turbo_trellis structure.
PARAMETERS:
None
RETURN VALUE:
None
---------------------------------------------------------------*/
void gen_trellis()
{
int i, j, k;
int dk_turbo, ak_turbo, outbit;
int *tempstat;
if ((tempstat=(int *)malloc(sizeof(int)*M_num_reg))==NULL)
{
printf("\n fail to allocate memory of tempstat \n");
exit(1);
}
/* generate next output and next state matrix */
for (i=0; i<n_states; i++)
{
for (j=0; j<2; j++)
{
int2bin(i, tempstat, M_num_reg);
dk_turbo = j;
ak_turbo = (*(turbo_g.g_matrix+0)) * dk_turbo;
for (k=1; k<turbo_g.K_num_col; k++)
{
ak_turbo = ak_turbo + (*(turbo_g.g_matrix+k)) * (*(tempstat+k-1));
}
ak_turbo = ak_turbo % 2;
outbit = encode_bit(ak_turbo, tempstat);
*(turbo_trellis.mx_nextout+i*4+2*j)=2*dk_turbo-1;
*(turbo_trellis.mx_nextout+i*4+2*j+1)=2*outbit-1;
*(turbo_trellis.mx_nextstat+i*2+j)=bin2int(tempstat, M_num_reg);
}
}
/* generate last output and last state matrix */
for (i=0; i<n_states; i++)
{
for (j=0; j<2; j++)
{
*(turbo_trellis.mx_laststat+(*(turbo_trellis.mx_nextstat+i*2+j))*2+j) = i;
*(turbo_trellis.mx_lastout+(*(turbo_trellis.mx_nextstat+i*2+j))*4+2*j)
= *(turbo_trellis.mx_nextout+i*4+2*j);
*(turbo_trellis.mx_lastout+(*(turbo_trellis.mx_nextstat+i*2+j))*4+2*j+1)
= *(turbo_trellis.mx_nextout+i*4+2*j+1);
}
}
free(tempstat);
}
/*---------------------------------------------------------------
FUNCTION:
void int2bin(int intstat, int *tempstat, int length)
DESCRIPTION:
This function turn a decimal integer to a binary sequence.
PARAMETERS:
INPUT:
intstat - Decimal integer needed to be changed.
length - Length of binary sequence.
OUTPUT:
tempstat - Contains pointer to the binary sequence.
RETURN VALUE:
None
---------------------------------------------------------------*/
void int2bin(int intstat, int *tempstat, int length)
{
int i, temp;
temp = intstat;
for (i=length-1; i>=0; i--)
{
*(tempstat+i) = temp%2;
temp = temp/2;
}
}
/*---------------------------------------------------------------
FUNCTION:
int bin2int(int *binseq, int length)
DESCRIPTION:
This function turn a binary sequence to a decimal integer.
PARAMETERS:
INPUT:
binseq - Contains pointer to the binary sequence.
length - Length of binary sequence.
RETURN VALUE:
The decimal integer corresponding to the binary sequence.
---------------------------------------------------------------*/
int bin2int(int *binseq, int length)
{
int i, j, temp;
int sum = 0;
for (i=0; i<length; i++)
{
temp = 1;
for (j=1; j<=i; j++)
{
temp = temp * 2;
}
sum = sum + temp * (*(binseq+length-1-i));
}
return sum;
}
/*---------------------------------------------------------------
FUNCTION:
random_turbo()
DESCRIPTION:
Generate a random number between 0 and 1.产生随机数(位于0和1之间)
RETURN VALUE:
The random number between 0 and 1.
---------------------------------------------------------------*/
float random_turbo()
{
long z,k;
static long s1 = 12345L;
static long s2 = 1234546346L;
k= s1 / 53668L;
s1 = 40014L * (s1 - k*53668L) - k*12211L;
if (s1<0)
s1 = s1 + 2147483563L;
k = s2 / 52774;
s2 = 40692L * (s2 - k*52774L) - k*3791L;
if (s2<0)
s2 = s2 + 2147483399L;
z=s1 - s2;
if (z<1)
z = z + 2147483562L;
return (float) z / (float) 2147483563.0;
}
/*---------------------------------------------------------------
FUNCTION:
void gen_rand_index(int length)
DESCRIPTION:
This function generate the index for the random interleaver.
PARAMETERS:
INPUT:
length - Length of interleaver.
type_flow - 1 for traffic flow, 0 for suplement flow.
RETURN VALUE:
None
---------------------------------------------------------------*/
void gen_rand_index(int length, int type_flow)
{
int *index_random;
float *tempindex;
float tempmax;
int selectedscr;
int i, j;
if ((tempindex=(float *)malloc((length)*sizeof(float)))==NULL)
{
printf("\n fail to allocate memory of tempindex \n");
exit(1);
}
index_random = type_flow? index_randomintlvr : index_randomintlvr_sup;
for (i=0; i<length; i++)
{
*(tempindex+i) = random_turbo();
}
for (i=0; i<length; i++)
{
tempmax = 0.0;
for (j=0; j<length; j++)
{
if (*(tempindex+j) >= tempmax)
{
tempmax = *(tempindex+j);
selectedscr = j;
}
}
*(index_random+i) = selectedscr;
*(tempindex+selectedscr) = 0.0;
}
free(tempindex);
}
/*---------------------------------------------------------------
FUNCTION:
void randominterleaver_int(int *data_unintlvr, int *interleaverddata,
int length, int type_flow)
DESCRIPTION:
Random interleaver of int.