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

📄 main.c

📁 一些关于Turbo编码译码实现所需的函数和原程序。
💻 C
字号:
/*----------------------------------------------------------
* Copyright (c) 2003, 北京邮电大学移动通信实验室
* All rights reserved.
*
* 文件名称:main.c
* 文件标识:
* 摘    要:Turbo仿真主程序.
*
* 当前版本:1.0
* 作    者:张鹏
* 完成日期:2003年12月1日
----------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

/*==================================================*/
#include "turbo_code_Log_MAP.h"
#include "turbo_other_functions.h"
/*==================================================*/



/*==================================================*/
extern double rate_coding;
extern TURBO_G turbo_g;
extern double *alpha_channel;
extern int M_num_reg;
extern double time_jarks;
/*==================================================*/

void main()
{
	int *trafficflow_sourceA = NULL;
	int *trafficflow_decodedA =NULL;
	int *trafficflow_sourceAB=NULL;
	
	int *trafficflow_decodedAB=NULL;

	float *coded_trafficflow_source1A =NULL;
	float *coded_trafficflow_sourceAB = NULL;

	float *trafficflow_for_decodeA = NULL;
	int *rsc1A = NULL;
	int *rscAB = NULL;
	int *interleaved_trafficflowAB=NULL;
	
	int *trafficflow_sourceB = NULL;
	int *trafficflow_decodedB =NULL;
	float *coded_trafficflow_source1B =NULL;

	float *trafficflow_for_decodeB = NULL;
	float *trafficflow_for_decodeAB = NULL;
	
	int *rsc1B = NULL;
	
	int traffic_source_length,traffic_source_lengthAB;

	int err_bit_num_traffic[8];
	int frame_limit = 100;
	int i,ien,nf;
	double en, sgma;
	double err_bit_rate_traffic[8];
	double fd_rayleigh = 10;
	double fs_rayleigh = FRAME_LENGTH*100;
	float EbN0dB=0;
//	double Eb_N0_dB[8] = {0.0, 1, 2, 3, 4, 5, 6, 7};
//	double Eb_N0_dB[8] = {0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3, 3.5};
	double Eb_N0_dB[8] = {0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1};
	int error_limit[8] = {1000, 1000, 500, 300, 100, 50, 20, 10};
//	double err_bit_rate_traffic[8];
	if ((trafficflow_sourceA=(int *)malloc(FRAME_LENGTH*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of trafficflow_source1 \n");
	  exit(1);  
	}
	if ((trafficflow_sourceAB=(int *)malloc(2*FRAME_LENGTH*sizeof(int)))==NULL)
	{
		printf("\n fail to allocate memory of trafficflow_source1 \n");
		exit(1);  
	}
	if ((trafficflow_decodedA=(int *)malloc(FRAME_LENGTH*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of trafficflow_source1 \n");
	  exit(1);  
	}
	if ((trafficflow_decodedAB=(int *)malloc(2*FRAME_LENGTH*sizeof(int)))==NULL)
	{
		printf("\n fail to allocate memory of trafficflow_source1 \n");
		exit(1);  
	}
	if ((coded_trafficflow_source1A=(float *)malloc(2*(FRAME_LENGTH + M_num_reg)*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of rsc2 \n");
	  exit(1);  
	}

	if ((coded_trafficflow_sourceAB=(float *)malloc((2*FRAME_LENGTH + 2*M_num_reg)*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of rsc2 \n");
	  exit(1);  
	}
	if ((trafficflow_for_decodeA=(float *)malloc((2*FRAME_LENGTH+2*M_num_reg)*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of rsc2 \n");
	  exit(1);  
	}

	if ((trafficflow_for_decodeAB=(float *)malloc((2*FRAME_LENGTH+2*M_num_reg)*sizeof(int)))==NULL)
	{
		printf("\n fail to allocate memory of rsc2 \n");
		exit(1);  
	}

	if ((rsc1A=(int *)malloc(2*(FRAME_LENGTH + M_num_reg)*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of rsc2 \n");
	  exit(1);  
	}

	if ((rscAB=(int *)malloc(2*(2*FRAME_LENGTH + M_num_reg)*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of rsc2 \n");
	  exit(1);  
	}

	if ((interleaved_trafficflowAB=(int *)malloc(2*FRAME_LENGTH*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of input2 \n");
	  exit(1);  
	}
	if ((trafficflow_sourceB=(int *)malloc(FRAME_LENGTH*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of trafficflow_source1 \n");
	  exit(1);  
	}
	if ((trafficflow_decodedB=(int *)malloc(FRAME_LENGTH*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of trafficflow_source1 \n");
	  exit(1);  
	}
	if ((coded_trafficflow_source1B=(float *)malloc(2*(FRAME_LENGTH + M_num_reg)*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of rsc2 \n");
	  exit(1);  
	}
	if ((trafficflow_for_decodeB=(float *)malloc((2*FRAME_LENGTH+2*M_num_reg)*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of rsc2 \n");
	  exit(1);  
	}

	if ((rsc1B=(int *)malloc(2*(FRAME_LENGTH + M_num_reg)*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of rsc2 \n");
	  exit(1);  
	}




	if ((alpha_channel=(double *)malloc((2*FRAME_LENGTH+2*M_num_reg)*sizeof(double)))==NULL)
	{
	  printf("\n fail to allocate memory of supflow_decoded\n");
	  exit(1);  
	}
srand((unsigned)time(NULL));
	
/*-----------------------------------------------------------------*/	
/*-----------------------------------------------------------------*/
	TurboCodingInit();
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/	
	for (ien=0; ien<8; ien++)
	{
	EbN0dB=(float)Eb_N0_dB[ien];
//	EbN0dB=0;
			en = (float)pow(10,EbN0dB/10);
			sgma = (float)(1.0/sqrt(2*en/3));
		err_bit_num_traffic[ien]=0;

		time_jarks=(double)(rand()*100000%RAND_MAX)*500*(1/fs_rayleigh);

		traffic_source_length = FRAME_LENGTH;
		traffic_source_lengthAB = 2*FRAME_LENGTH;
		for (nf=0; err_bit_num_traffic[ien]<error_limit[ien]; nf++)
//		for (nf=0; nf<200; nf++)
	{	
		//生成信源序列	
		gen_source(trafficflow_sourceA, traffic_source_length);
		gen_source(trafficflow_sourceB, traffic_source_length);
		
		//RSC编码
		rsc_encode(trafficflow_sourceA,rsc1A,1,traffic_source_length);
		rsc_encode(trafficflow_sourceB,rsc1B,1,traffic_source_length);

		//生成随机交织器
		gen_rand_index(traffic_source_lengthAB, 1);

		combo(trafficflow_sourceA,trafficflow_sourceB,trafficflow_sourceAB);
	

		//交织
		interleave_int(trafficflow_sourceAB, interleaved_trafficflowAB, TYPE_INTERLEAVER, traffic_source_lengthAB, 1);

		rsc_encode(interleaved_trafficflowAB, rscAB, 1, traffic_source_lengthAB);
		//合并发送的序列
		for (i=0; i<2*(FRAME_LENGTH+M_num_reg); i++)
		{
			*(coded_trafficflow_source1A+i) = (float) *(rsc1A+i);
			
			*(coded_trafficflow_source1B+i) = (float) *(rsc1B+i);	
		}	
		
		for (i=0; i<2*FRAME_LENGTH; i++)
		{
			*(coded_trafficflow_sourceAB+ i) = (float) *(rscAB+2*i+1);
		}
		for (i=0; i<2*M_num_reg; i++)
		{
			*(coded_trafficflow_sourceAB+ 2*FRAME_LENGTH+i) = (float) *(rscAB+4*FRAME_LENGTH+i);
		}



		//bpsk调制
		for (i=0; i<2*(FRAME_LENGTH+M_num_reg); i++)
		{	
			*(coded_trafficflow_source1A+i) = *(coded_trafficflow_source1A+i)*2-1;
			*(coded_trafficflow_source1B+i) = *(coded_trafficflow_source1B+i)*2-1;
			*(coded_trafficflow_sourceAB+i) = *(coded_trafficflow_sourceAB+ i)*2-1;
		}
		//生成信道
		if(TYPE_CHANNEL)
		{
			chan_jakes(alpha_channel, fd_rayleigh, fs_rayleigh,2*FRAME_LENGTH+2*M_num_reg);
		}
		else
		{
			for (i=0; i<(2*FRAME_LENGTH+2*M_num_reg); i++)
			{
				*(alpha_channel+i) = 1;
			}
		}

		rayleigh_channel_1226(coded_trafficflow_source1A, trafficflow_for_decodeA, sgma, 2*FRAME_LENGTH+2*M_num_reg);
		rayleigh_channel_1226(coded_trafficflow_source1B, trafficflow_for_decodeB, sgma, 2*FRAME_LENGTH+2*M_num_reg);
		rayleigh_channel_1226(coded_trafficflow_sourceAB, trafficflow_for_decodeAB, sgma, 2*FRAME_LENGTH+2*M_num_reg);

 		TurboDecodingTraffic(trafficflow_for_decodeA,trafficflow_for_decodeB,trafficflow_for_decodeAB, trafficflow_decodedAB,&traffic_source_length, EbN0dB);
		
		for (i=0;i<traffic_source_lengthAB;i++)
		{		
			if ((*(trafficflow_decodedAB+i))!=(*(trafficflow_sourceAB+i)))
			{
				
				err_bit_num_traffic[ien]++;
			}
			
		}
		
		
		}

		



err_bit_rate_traffic[ien] = (double)err_bit_num_traffic[ien]/(2*nf*FRAME_LENGTH);
printf("Eb/N0= %f db\n",EbN0dB);
printf("BER= %f\n",err_bit_rate_traffic[ien]);
printf("%d\n",err_bit_num_traffic[ien]);


	}
	TurboCodingRelease();
	free(trafficflow_sourceA);
	free(trafficflow_decodedA);
	free(coded_trafficflow_source1A);
	free(coded_trafficflow_sourceAB);

	free(trafficflow_for_decodeA);
	free(interleaved_trafficflowAB);
	free(rsc1A);
	free(rscAB);	

	free(trafficflow_sourceAB);
	free(trafficflow_sourceB);
	free(trafficflow_decodedB);
	free(coded_trafficflow_source1B);
	free(trafficflow_for_decodeAB);
	free(trafficflow_for_decodeB);

	free(rsc1B);
	free(alpha_channel);
}

⌨️ 快捷键说明

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