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

📄 main.cpp

📁 本程序仿真了随机变量的产生和多径信道情况
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "channel.h"
#include "Jakes.h"

/*
*******************************************************************************
*			           constants and define declarations
*******************************************************************************
*/
/*衰落信道仿真常量定义*/
#define TEST_TIME	100
#define SLOT_LEN	1024
#define	SAMPLE_NUM	(TEST_TIME*SLOT_LEN)
#define PI			3.141592653589793

/*
********************************************************************************
	测试主程序

*
********************************************************************************
*/

void main(void)
{
	int i = 0,j = 0,k = 0;							/*循环变量*/
	/****************************************************************
						高斯随机变量产生用到的变量
	****************************************************************/
	FILE *fp_random_u = NULL;
	FILE *fp_gauss = NULL;
	struct awgn gauss;
	struct seed awgn_seed = {30323,173,13};			/*白高斯噪声的随机数种子*/
	struct seed random_u_seed = {100,100,100};		/*均匀分布随机变量的随机数种子*/
	double ru;										/*生成的均匀随机变量*/
	double rg;										/*生成的高斯随机变量*/
	double ave;										/*高斯随机变量的均值*/
	double sigma;									/*高斯随机变量的标准差*/

	/****************************************************************
						衰落信道产生用到的变量
	****************************************************************/
	struct awgn s_gauss;
	struct seed s_awgn_seed = {30323,173,13};		/*白高斯信源的随机数种子*/
	int		test_cnt;
	double	tx_slot_i[SLOT_LEN];					/*I路发送信号*/
	double	tx_slot_q[SLOT_LEN];					/*Q路发送信号*/
	double	rx_slot_i[SLOT_LEN];					/*I路接收信号*/
	double	rx_slot_q[SLOT_LEN];					/*Q路接收信号*/
	double	tx_energy;								/*平均发送能量*/
	double	rx_energy;								/*平均接收能量*/

	double  envelope_pdf[40];						/*统计接收信号幅度的概率密度函数*/
	double  phase_pdf[40];							/*统计接收信号相位的概率密度函数*/
	double  envelope; 
	double  phase;

	FILE *fp_multipath;								/*输出文件指针,幅度和相位频率统计*/
	FILE *fp_i;										/*输出文件指针,I路输出*/
	FILE *fp_q;										/*输出文件指针,Q路输出*/

	/************************************************************************/
	/*                测试AWGN:1.1  均匀分布随机变量                       */
	/************************************************************************/
	if ((fp_random_u = fopen("output\\random_u.txt","w")) == NULL)
	{
		printf("can't open file random_u.txt\n");
		exit(0);
	}
	for (i=0; i<1000; i++)
	{
		ru = random_u(&random_u_seed);
		fprintf(fp_random_u,"%f\n",ru);
	}
	fclose(fp_random_u);


	/************************************************************************/
	/*                测试AWGN:1.1  高斯随机变量                           */
	/************************************************************************/
	if ((fp_gauss = fopen("output\\gauss.txt","w")) == NULL)
	{
		printf("can't open file gauss.txt\n");
		exit(0);
	}

	ave = 0;
	sigma = 1.0;//生成零均值,标准差为1的高斯随机变量
	initial_gauss(&gauss, &awgn_seed, ave, sigma);
	for (i=0; i<1000; i++)
	{
		rg = gauss_g(&gauss);
		fprintf(fp_gauss,"%f\n",rg);
	}
	fclose(fp_gauss);

	/************************************************************************/
	/*             测试衰落信道:2 Jakes模型衰落信道                        */
	/************************************************************************/
	if ((fp_multipath = fopen("output\\multipath.txt","w")) == NULL)
	{
		printf("can't open file multipath.txt\n");
		exit(0);
	}
	if ((fp_i = fopen("output\\multipath_i.txt","w")) == NULL)
	{
		printf("can't open file multipath.txt\n");
		exit(0);
	}
	if ((fp_q = fopen("output\\multipath_q.txt","w")) == NULL)
	{
		printf("can't open file multipath.txt\n");
		exit(0);
	}

	/*用高斯随机变量做信源,这里参数为均值0,方差0.5*/
	initial_gauss(&s_gauss, &s_awgn_seed, 0, 0.71);

	/* 设置多径信道 */
	SetMultiPath();

	/*变量初始化*/
	for (i=0; i<40; i++)
	{
		envelope_pdf[i] = 0;
		phase_pdf[i] = 0;
	}
	tx_energy = 0;
	rx_energy = 0;

	for (test_cnt=0; test_cnt<TEST_TIME; test_cnt++ )
	{
		for (i=0; i<SLOT_LEN; i++)
		{
			tx_slot_i[i] = gauss_g(&s_gauss);
			tx_slot_q[i] = gauss_g(&s_gauss);
		}
		
		
		for (i=0; i<SLOT_LEN; i++)
		{
			RunMultiPath(tx_slot_i[i], tx_slot_q[i], &rx_slot_i[i], &rx_slot_q[i]);
			fprintf(fp_i,"%f\n",rx_slot_i[i]);
			fprintf(fp_q,"%f\n",rx_slot_q[i]);
		}
		
		
		for (i=0; i<SLOT_LEN; i++)
		{
			tx_energy += tx_slot_i[i] * tx_slot_i[i] + tx_slot_q[i] * tx_slot_q[i];
			rx_energy += rx_slot_i[i] * rx_slot_i[i] + rx_slot_q[i] * rx_slot_q[i];

			envelope = sqrt(rx_slot_i[i] * rx_slot_i[i] + rx_slot_q[i] * rx_slot_q[i]);
			phase = atan2(rx_slot_q[i], rx_slot_i[i]) / PI;


			/*统计包络的概率密度函数*/
			if (envelope < 0)
			{
				envelope_pdf[0] += 1;
			}
			else if (envelope > 4)
			{
				envelope_pdf[39] += 1;
			}
			else
			{
				j = (int)(10*envelope);
				envelope_pdf[j] += 1;
			}

			/*统计相位的概率密度函数*/
			if (phase < -1)
			{
				phase_pdf[0] += 1;
			}
			else if (phase > 1)
			{
				phase_pdf[39] += 1;
			}
			else
			{
				k = (int)(20*(phase+1));
				phase_pdf[k] += 1;
			}
		}
		
		printf("test progress : current test count is %d\r",test_cnt);
	}
	
	fclose(fp_i);
	fclose(fp_q);

	/* 输出到屏幕 */
	printf("\n\n");
	printf("Sample Sent:		%d\n", SAMPLE_NUM);
	printf("Average Tx Energy:	%f\n", tx_energy / SAMPLE_NUM);
	printf("Average Rx Energy:	%f\n\n", rx_energy / SAMPLE_NUM);

	/*输出到文件供画曲线用*/
	fprintf(fp_multipath,"包络频率统计\n");
	for (i=0; i<40; i++)
	{
		fprintf(fp_multipath,"%f\n",10*envelope_pdf[i]/SAMPLE_NUM);
	}
	fprintf(fp_multipath,"\n相位频率统计\n");
	for (i=0; i<40; i++)
	{
		fprintf(fp_multipath,"%f\n",20*phase_pdf[i]/SAMPLE_NUM/PI);
	}
	fclose(fp_multipath);
	

}


⌨️ 快捷键说明

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