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

📄 testturbo.c

📁 信道编解码中的turbo编解码程序
💻 C
字号:
/*
*******************************************************************************
* ansi c source code
* file name:
*	TestTurbo.c
* abstract:
*	to test the turbo code
* reference:
*	EV-DO specification
* author:
*	Wang Jiaheng	2005-02-04
* revision history:
*	Wang Jiaheng	2005-02-04	original version
*******************************************************************************
*/


/*
*******************************************************************************
*                               include files
*******************************************************************************
*/
#include <stdio.h>
#include <math.h>
#include "Random.h"
#include "FileSearch.h"
#include "ShowProgress.h"
#include "TurboEncoder.h"
#include "TurboDecoder.h"


/*
*******************************************************************************
*			           constants and define declarations
*******************************************************************************
*/
#define	MAX_SIM_POINT_NUM	20
#define FILE_NAME			"TurboLog.txt"
#define PROGRAM_NAME		"TURBO LOG"
#define SIM_MODE			"AWGN"

#define PACKET_SIZE			1024
#define ENCODE_RATE			2
#define ITERATE_TIME		4

#define ENCODE_BIT_LEN		(PACKET_SIZE * ENCODE_RATE)
#define SYMBOL_LEN			ENCODE_BIT_LEN


/*
*******************************************************************************
* 		                    local object definition
*******************************************************************************
*/
/* number of points to be simulated */
int		sim_point_num;
/* SNR of every point (db) */
double	sim_point_snr[MAX_SIM_POINT_NUM];
/* simulation times at every point */
int		sim_point_time[MAX_SIM_POINT_NUM];


/*
*******************************************************************************
*                             function definition
*******************************************************************************
*/
/*
******************************************************************************* 
* description:
*	set the configuration parameters in Tx side
* input: 
*	cfg_file_name: name of the configuration file
* output:
*	if ok, return 1; otherwise retrun 0.
* function reference:
*	FileSearch.c
* author:
*	Wang Jiaheng, 2004-09-22	created
*******************************************************************************
*/
int SetTx(char cfg_file_name[])
{
	/* array for storing the configuration parameters */
	double	para_cfg[2 * MAX_SIM_POINT_NUM + 1];
	/* number of parameters */
	int		num_para;
	int		i;

	/* read parameters from the configuration file */
	if ( !ReadFile(cfg_file_name, &num_para, para_cfg) )
	{
		return 0;
	}
	if ( num_para > 2 * MAX_SIM_POINT_NUM + 1 )
	{
		return 0;
	}

	/* set parameters of simulation points */
	sim_point_num = (int)para_cfg[0];
	if ( (sim_point_num < 0 ) || (sim_point_num > MAX_SIM_POINT_NUM) )
	{
		return 0;
	}
	
	/* save SNR and simulation times of every point */
	for (i = 0; i < sim_point_num; i++)
	{
		sim_point_snr[i] = para_cfg[2*i + 1];
		sim_point_time[i] = (int)para_cfg[2*i + 2];
		if ( sim_point_time[i] < 0 )
		{
			return 0;
		}
	}

	return 1;
}


/*
******************************************************************************* 
* description:
*	initialize all the global variables on Tx side
* input: 
*	none
* output:
*	if ok, return 1; otherwise retrun 0.
* function reference:
*	GlobalDef.h, GlobalVar.c
* author:
*	Wang Jiaheng, 2004-11-01	created
*******************************************************************************
*/
int InitTx(void)
{
	/* set turbo encoder */
	if ( !SetTurboEncoder(PACKET_SIZE) )
	{
		printf("SetTurboEncoder failed !\n");
		return 0;
	}

	/* set random bit */
	if ( !SetRandBit1(7) )
	{
		printf("SetRandBit1 failed !\n");
		return 0;
	}

	/* set noise */
	SetRandGas(-1);

	return 1;
}


/*
******************************************************************************* 
* description:
*	main function of the whole simulation platform
* input: 
*	none
* output:
*	if ok, return 1; otherwise retrun 0.
* function reference:
* author:
*	Wang Jiaheng, 2005-02-04	created
*******************************************************************************
*/
int main(void)
{
	int		k;
	int		sim_cnt;
	int		test_cnt;
	int		test_time;
	int		packet_send[PACKET_SIZE];
	int		packet_recv[PACKET_SIZE];
	int		bit_encode[ENCODE_BIT_LEN];
	double	sym_send[SYMBOL_LEN];
	double	sym_recv[SYMBOL_LEN];
	double	noise_var;
	double	ch_rely;
	int		bit_len;
	int		sym_len;
	int		packet_len;
	int		valid_len;
	int		bit_total_num;
	int		bit_error_num;
	int		packet_total_num;
	int		packet_error_num;
	char	packet_error_flag;
	FILE	*pf;

	
	/* set parameters for tranmission */
	if ( !SetTx("TxCfg.txt") )
	{
		printf("SetTx failed\n");
		return -1;
	}

	/* open the file */
	pf = fopen(FILE_NAME, "a");
	if ( pf == NULL )
	{
		printf("cannot open the file\n");
		return -1;
	}

	printf("--------------------------------------------------\n");
	printf("Program Name:		%s\n", PROGRAM_NAME);
	printf("Simulation Mode:	%s\n", SIM_MODE);
	printf("Packet Size:		%d\n", PACKET_SIZE);
	printf("Encode Rate:		1/%d\n", ENCODE_RATE);
	printf("Iterate Time:		%d\n", ITERATE_TIME);
	printf("--------------------------------------------------\n\n");
	

	fprintf(pf, "--------------------------------------------------\n");
	fprintf(pf, "Program Name:		%s\n", PROGRAM_NAME);
	fprintf(pf, "Simulation Mode:	%s\n", SIM_MODE);
	fprintf(pf, "Packet Size:		%d\n", PACKET_SIZE);
	fprintf(pf, "Encode Rate:		1/%d\n", ENCODE_RATE);
	fprintf(pf, "Iterate Time:		%d\n", ITERATE_TIME);
	
	fprintf(pf, "--------------------------------------------------\n\n");
	fprintf(pf, "#  | ");
	fprintf(pf, "Eb/Nt       | ");
	fprintf(pf, "FER         | ");
	fprintf(pf, "BER         | ");
	fprintf(pf, "Packet Recv     Packet Error    | ");
	fprintf(pf, "Bit Recv        Bit Error\n");
	fclose(pf);


	/* run simulation at all specified points */
	for (sim_cnt = 0; sim_cnt < sim_point_num; sim_cnt++)
	{
		/* initialize the global variables on Tx side */
		if ( !InitTx() )
		{
			printf("InitTx failed !\n");
			return 0;
		}
		
		/* set variance of the noise */
		noise_var = 0.5 * ENCODE_RATE * pow(10.0, -sim_point_snr[sim_cnt]/10.0);
		ch_rely = 2 / noise_var;
		
		/* set test time at this point */
		test_time = sim_point_time[sim_cnt];
		valid_len = PACKET_SIZE - 6;
		bit_total_num = 0;
		bit_error_num = 0;
		packet_total_num = 0;
		packet_error_num = 0;
		
		/* output point information */
		printf("--------------------------------------------------\n");
		printf("Point Index:		%d/%d\n", sim_cnt + 1, sim_point_num);
		printf("Point SNR (db):		%.2f\n", sim_point_snr[sim_cnt]);
		printf("Packets To Send:	%d\n", test_time);
		printf("Bits To Send:		%d\n", test_time * valid_len);
		
		
		/* start the Tx and Rx process at the ith point */
		for (test_cnt = 0; test_cnt < test_time; test_cnt++)
		{
			/* generate a packet to send */
			for (k = 0; k < PACKET_SIZE; k++)
			{
				packet_send[k] = RunRandBit1();
			}
			
			
			/* encode the packet */
			if ( !RunTurboEncoder(packet_send, PACKET_SIZE, ENCODE_RATE, bit_encode, &bit_len) )
			{
				printf("RunTurboEncoder failed\n");
				return -1;
			}
			if ( bit_len != ENCODE_BIT_LEN )
			{
				printf("bit_len wrong\n");
				return -1;
			}


			/* map encoded bits to symbols */
			sym_len = bit_len;
			for (k = 0; k < sym_len; k++)
			{
				sym_send[k] = 1 - 2 * bit_encode[k];
			}
			
			
			/* through a AWGN channel */
			for (k = 0; k < sym_len; k++)
			{
				sym_recv[k] = sym_send[k] + RunRandGasMd(0, noise_var);
			}
			
			
			/* decode received symbols */
			if ( !RunTurboDecoder(sym_recv, sym_len, ENCODE_RATE, ch_rely, ITERATE_TIME, packet_recv, &packet_len) )
			{
				printf("RunTurboDecoder failed\n");
				return -1;
			}
			if ( packet_len != PACKET_SIZE )
			{
				printf("packet_len wrong\n");
				return -1;
			}
			
			
			/* compare recieved packets with original packets */
			packet_error_flag = 0;
			for (k = 0; k < valid_len; k++)
			{
				if ( packet_recv[k] != packet_send[k] )
				{
					bit_error_num++;
					packet_error_flag = 1;
				}
			}
			if ( packet_error_flag )
			{
				packet_error_num++;
			}
			bit_total_num += valid_len;
			packet_total_num++;
			
			
			/* show the progress status of the whole process */
			ShowProgress(test_cnt + 1, test_time);
		}

		
		/* output statistics */
		printf("Packets Received:	%d\n", packet_total_num);
		printf("Packets Erroneous:	%d\n", packet_error_num);
		printf("Bits Received:		%d\n", bit_total_num);
		printf("Bits Erroneous:		%d\n", bit_error_num);
		printf("FER:			%.3e\n", (double)packet_error_num / (double)packet_total_num);
		printf("BER:			%.3e\n", (double)bit_error_num / (double)bit_total_num);
		printf("--------------------------------------------------\n\n");
		
		/* store statics to the file */
		pf = fopen(FILE_NAME, "a");
		if ( pf == NULL )
		{
			printf("cannot open the file\n");
			return 0;
		}
		fprintf(pf, "%-2d | ", sim_cnt + 1);
		fprintf(pf, "%-12.2f| ", sim_point_snr[sim_cnt]);
		fprintf(pf, "%-12.3e| ", (double)packet_error_num / (double)packet_total_num);
		fprintf(pf, "%-12.3e| ", (double)bit_error_num / (double)bit_total_num);
		fprintf(pf, "%-16d", packet_total_num);
		fprintf(pf, "%-16d| ", packet_error_num);
		fprintf(pf, "%-16d", bit_total_num);
		fprintf(pf, "%-16d\n", bit_error_num);
		fclose(pf);
	}


	/* changle line in file */
	pf = fopen(FILE_NAME, "a");
	fprintf(pf, "\n\n\n");
	fclose(pf);

	return 0;
}

⌨️ 快捷键说明

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