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

📄 main.c

📁 myturbo 简单使用的turbo系统方针程序 对于理解turbo码的编解码很有帮助
💻 C
字号:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

#include"turbo_code_Log_MAP.h"

extern float rate_coding;
extern TURBO_G turbo_g;

#define MAX_TIMES 1000

void main()
{
    int *turbocoding_source = NULL;
	float *coded_source = NULL;
	int source_length;

    float *received_for_decode = NULL;
	int *turbo_decoded = NULL;
	int received_length;

	float EbN0dB = 0;

	float en, sgma;

	//double Eb_N0_dB[8] = {-1.0,-0.75,-0.5,-0.25,0.0, 0.25, 0.5, 0.75};
    double Eb_N0_dB[8] = {0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75};


	int err_bit_num[8];
	float err_bit_rate[8][MAX_TIMES];
	float err_rate[8];

	int i, j, times, ien;

	FILE *stream;	/* point to "simu_report.txt" which is used to record the results */



if ((turbocoding_source=(int *)malloc(FRAME_LENGTH*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of turbocoding_source \n");
	  exit(1);  
	}
if ((coded_source=(float *)malloc(2*(FRAME_LENGTH+COLUMN_OF_G-1)*sizeof(float)))==NULL)
	{
	  printf("\n fail to allocate memory of coded_source \n");
	  exit(1);  
	}

if ((received_for_decode=(float *)malloc(2*(FRAME_LENGTH+COLUMN_OF_G-1)*sizeof(float)))==NULL)
	{
	  printf("\n fail to allocate memory of received_for_decode \n");
	  exit(1);  
	}
if ((turbo_decoded=(int *)malloc((FRAME_LENGTH+COLUMN_OF_G-1)*sizeof(int)))==NULL)
	{
	  printf("\n fail to allocate memory of turbo_decoded\n");
	  exit(1);  
	}

if ((stream=fopen( "simu_report.txt", "w" )) == NULL)
    {
	  printf("\nError! Can not open file simu_report.txt\n");
	  exit(1);
    }

source_length = FRAME_LENGTH;


TurboCodingInit();
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/	

for (ien=0; ien<8; ien++)
	{
		EbN0dB = (float)Eb_N0_dB[ien];
		
		en = (float)pow(10,(EbN0dB)/10);
		sgma = (float)(1.0/sqrt(2*rate_coding*en));
		
		
		err_rate[ien]= 0.0 ;
		
		for(times=0;times<MAX_TIMES;times++)
		err_bit_rate[ien][times] = 0.0;
		
		
		
		/*====   output the simulation parameters	=====*/
		/*	to screen	*/
		printf("/*=================================*/\n");
		printf("Turbo code simulation:\n");
		printf("/*=================================*/\n");
		printf("====log map decoder====\n");
		printf("frame length : %d \n", source_length);
		printf("g = \n");
		for (i=0; i<turbo_g.N_num_row; i++)
		{
			for (j=0; j<turbo_g.K_num_col; j++)
			{
				printf("%d ", *(turbo_g.g_matrix+i*turbo_g.K_num_col+j));
			}
			printf("\n");
		}
		printf("Eb/N0 = %f \n", EbN0dB);

		
		printf("iteration number : %d \n", N_ITERATION);
		printf("/*=================================*/\n");

              /*===============================================================*/
		/*	to "simu_report.txt"	*/
		fprintf(stream, "/*=================================*/\n");
		fprintf(stream, "Turbo code simulation:\n");
		fprintf(stream, "/*=================================*/\n");
		fprintf(stream, "====log map decoder====\n");
		fprintf(stream, "frame length : %d \n", source_length);
		fprintf(stream, "g = \n");
		for (i=0; i<turbo_g.N_num_row; i++)
		{
			for (j=0; j<turbo_g.K_num_col; j++)
			{
				fprintf(stream, "%d ", *(turbo_g.g_matrix+i*turbo_g.K_num_col+j));
			}
			fprintf(stream, "\n");
		}
		fprintf(stream, "Eb/N0 = %f \n", EbN0dB);
		
		fprintf(stream, "iteration number : %d \n", N_ITERATION);
		fprintf(stream, "/*=================================*/\n");
		/*=================================================================*/
for(times=0;times<MAX_TIMES;times++)
{			
	        err_bit_num[ien] = 0;

			source_length = FRAME_LENGTH;
			
			gen_source(turbocoding_source, source_length);
			
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
			TurboCoding(turbocoding_source, coded_source, &source_length);


/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/

			AWGN(coded_source, received_for_decode, sgma, 2*(FRAME_LENGTH+COLUMN_OF_G-1));
			
			received_length = 2*(FRAME_LENGTH+COLUMN_OF_G-1);
	/*		for(i=0;i<received_length;i++)
			{	
			fprintf(stream," %f",*(received_for_decode+i));
			if((i+1)%5==0) fprintf(stream,"\n");
			}       
	*/			
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
		TurboDecoding(received_for_decode, turbo_decoded,&received_length, EbN0dB);
			
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
	/*		for(i=0;i<(FRAME_LENGTH+COLUMN_OF_G-1);i++)
			{	
			fprintf(stream," %d",*(turbo_decoded+i));
			if((i+1)%5==0) fprintf(stream,"\n");
			}    
	*/				

      		for (i=0; i<FRAME_LENGTH; i++)
			{
				if (*(turbocoding_source+i) != *(turbo_decoded+i))
				{
					err_bit_num[ien] = err_bit_num[ien]+1;
				}
			}
		    fprintf(stream,"%d ",err_bit_num[ien]);	
		  
		
		err_bit_rate[ien][times] = (float)err_bit_num[ien]/(FRAME_LENGTH);
			

	}
          fprintf(stream,"\n");
           
                 for(times=0;times<MAX_TIMES;times++)
			 {
				 fprintf(stream,"%f ",err_bit_rate[ien][times]);	
			     err_rate[ien]=err_rate[ien]+err_bit_rate[ien][times];
			 }
			 

}	


    fprintf(stream,"\n");
	for (i=0; i<8; i++)
	{
		fprintf(stream,"%f ",err_rate[i]/MAX_TIMES);
	}
	fprintf(stream,"\n");

	
/*-----------------------------------------------------------------*/	
/*-----------------------------------------------------------------*/	
	TurboCodingRelease();
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/	

	fclose(stream);

	free(turbocoding_source);
	free(coded_source);

    free(received_for_decode);
	free(turbo_decoded);

	
}

⌨️ 快捷键说明

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