⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 turbo_code_log_map.c

📁 禁忌搜索算法的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/*---------------------------------------------------------------
* 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:
	case 2:
	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 */

	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);

	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++)
		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -