📄 turbo_code_log_map.c
字号:
#include "turbo_code_Log_MAP.h"
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};
const int M_num_reg = COLUMN_OF_G-1; /* number of rigisters */
const int n_states = 8;
void TurboCoding(int *turbocoding_source, float *coded_source,
int *source_length)
{
int i;
int *temp_send = NULL;
int *send = NULL;
int length_info = *source_length;
if ((send=(int *)malloc((2*length_info+2*M_num_reg)*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of send \n");
exit(1);
}
gen_rand_index(length_info+M_num_reg);
encoderm_turbo(turbocoding_source, send, length_info); /* encode and BPSK modulate */
temp_send = send;
for (i=0; i<(2*length_info+2*M_num_reg); i++)
{
*(coded_source+i) = (float) *(temp_send+i);
}
free(send);
}
void TurboDecoding(float *received_for_decode, int *turbo_decoded,
int *received_length, float EbN0dB)
{
int i;
int length_info, length_total;
int iteration;
float *yk_turbo = NULL; /* include ys & yp */
float *La_turbo, *Le_turbo, *LLR_all_turbo; /* extrinsic information & LLR */
int *tempout;
/*==============================*/
//FILE *test;
/*========================*/
float en_rate = (float)pow(10, EbN0dB*0.1);
float Lc_turbo = 4*en_rate*rate_coding; /* reliability value of the channel */
length_info = (*received_length-2*M_num_reg)/2;
length_total = length_info+M_num_reg;
/*==================================================================*/
/*if ((test=fopen( "test.txt", "w" )) == NULL)
{
printf("\nError! Can not open file test.txt\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);
}
demultiplex(received_for_decode, length_info, yk_turbo);
/* scale the data */
for (i=0; i<4*length_total; 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 */
random_deinterlvr_float(La_turbo, Le_turbo, length_total);
for (i=length_info; i<length_total; i++)
{
*(La_turbo+i) = 0;
}
Log_MAP_decoder(yk_turbo, La_turbo, 1, LLR_all_turbo, length_total);
/* 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 */
randominterleaver_float(Le_turbo, La_turbo, length_total);
for (i=length_info; i<length_total; i++)
{
*(La_turbo+i) = 0;
}
Log_MAP_decoder(yk_turbo+2*length_total, La_turbo, 1, LLR_all_turbo, length_total);
/* 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);
/*==============================================*/
/* for(i=0;i<length_total;i++)
{fprintf(test,"% d",*(tempout+i));
if(i==200) fprintf(test,"\n");
}
/*=====================================================*/
random_deinterlvr_int(turbo_decoded, tempout, length_total);
/* for(i=0;i<length_total;i++)
{fprintf(test,"% d",*(turbo_decoded+i));
if(i==200) fprintf(test,"\n");
}
fclose(test);
*/
free(yk_turbo);
free(La_turbo);
free(Le_turbo);
free(LLR_all_turbo);
free(tempout);
}
void decision(float *LLR_seq, int length, int *output)
{
int i;
for (i=0; i<length; i++)
{
if (*(LLR_seq+i) < 0)
{
*(output+i) = 0;
}
else
{
*(output+i) = 1;
}
}
}
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 = (float)(0.5);
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);
}
/* 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();
if ((index_randomintlvr=(int *)malloc((FRAME_LENGTH+M_num_reg)*sizeof(int)))==NULL)
{
printf("\n fail to allocate memory of index_randomintlvr \n");
exit(1);
}
}
void TurboCodingRelease()
{
free(turbo_g.g_matrix);
free(turbo_trellis.mx_lastout);
free(turbo_trellis.mx_laststat);
free(turbo_trellis.mx_nextout);
free(turbo_trellis.mx_nextstat);
}
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;
}
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);
}
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;
}
}
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;
}
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;
}
void gen_rand_index(int length)
{
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 = index_randomintlvr ;
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -