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

📄 awgn.cpp

📁 在vc上做的802.16d ofdm phy的仿真
💻 CPP
字号:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <fstream.h>
#include <iomanip.h>

#include "awgn.h"
#include "typedef.h"
// 静态局部变量//
static unsigned long xinit_low48 = 1, xinit_mid48 = 0, xinit_high48 = 0;


/********************************************************************
 * Function:    awgn
 *
 * Parameters:  none
 *  
 * Returns:     u.d - 1.0     
 *
 * Description: Generate a uniform distribute between 0 and 1 random value.
 *******************************************************************/

double drand_48(void)
{
	unsigned long base0, base16, base32;
	union double_long {struct two_longs {unsigned long high, low;}s; double d;} u;

	base0  = xinit_low48*A0+C;
	base16 = xinit_low48*A1+xinit_mid48*A0+(base0>>16);
	base32 = xinit_low48*A2+xinit_mid48*A1+xinit_high48*A0+(base16>>16);
	xinit_low48 = base0 & 0xffff;
	xinit_mid48 = base16 & 0xffff;
	xinit_high48 = base32 & 0xffff;
	u.s.high = (xinit_low48<<4)+(xinit_mid48<<20);
	u.s.low = (xinit_mid48>>12)+(xinit_high48<<4)+0x3ff00000;
	return u.d - 1.0;
}

void time_seed(void)
{
  static time_t t_sec48;

  t_sec48      =  time(NULL);

  xinit_low48  = (unsigned long)((t_sec48>>16) & 0xffff);
  
  xinit_high48 = (unsigned long)(t_sec48 & 0xffff);

  xinit_mid48  =  xinit_low48 ^ xinit_high48;
}

/********************************************************************
 * Function:    awgn
 *
 * Parameters:  noiseDeviation
 *              noiseLength
 *              *pAwgnOut
 *  
 * Returns:     none
 *
 * Description: Generate additive white Gaussian noise signal with 0 mean.
 *******************************************************************/

void awgn(double noiseDeviation,SizeType noiseLength,double *pAwgnOut)
{
	int i;
	double unif_0,unif_1,radial,circle;

	time_seed();
	for (i = 0; i < noiseLength; i++)
	{
		unif_0 = drand_48();                  
		unif_1 = drand_48();
		radial = sqrt(2 * noiseDeviation * (log(1 / (1 - unif_0))));
		circle = TWOPI * unif_1;
		pAwgnOut[2*i] = radial * cos(circle);
		pAwgnOut[2*i+1] = radial * sin(circle);
	} 
	
/*

#ifdef DEBUG
	ofstream out("awgn.dat");
	for (i=0; i<2*noiseLength; i++)
	{
		out << pAwgnOut[i] << endl;
	}
out.close();
#endif //DEBUG
*/
}

/********************************************************************
 * Function:    propagationChannel
 *
 * Parameters:  propagationMode
 *              *pPropagationChIn
 *              propagationChInLength
 *              *pChannelParam
 *              *pPropagationChOut
 *              propagationChOutLength
 *  
 * Returns:    none
 *
 * Description: Simulate the practical channel effect the transmission signal.
 *******************************************************************/
 
 void propagationChannel(Uint8 propagationMode,
 						 Int16 *pPropagationChIn, 
 						 SizeType propagationChInLength,
						 Int16 *pPropagationChOut)

{
	// define all bit in the propagationMode
	
	SizeType i;
	ofstream out1;
	const double BIT_ENERGY_TO_NOISE = ENERGY_TO_NOISE;
	double *Gauss;
	double bitEnergy = 8386816 / BIT_SYMBOL_RATIO ; //* 200 / 256;
	double noiseDeviation = bitEnergy  / (2 * pow(10, BIT_ENERGY_TO_NOISE / 10));
	
	double nosie = 0;
	double power = 0;

	Gauss = (double *)calloc(2 * propagationChInLength,sizeof(double));

	// generate awgn or not
	if (propagationMode == 1)
	{
		awgn(noiseDeviation, propagationChInLength,Gauss);
	}
  	else 
  	{
  		for (i = 0; i < propagationChInLength; i++) 
  		{
  			Gauss[i] = 0;
  		}
  	}
	for (i = 0; i < propagationChInLength; i++)
	{
		nosie = nosie + (Gauss[2*i] * Gauss[2*i] + Gauss[2*i+1] * Gauss[2*i+1])/ propagationChInLength;
		power = power + (((Int32)pPropagationChIn[2*i] * pPropagationChIn[2*i] 
				+ (Int32)pPropagationChIn[2*i+1] * pPropagationChIn[2*i+1]) / propagationChInLength);
	}

	for (i = 0; i < propagationChInLength; i++)
	{
		pPropagationChOut[2 * i] =  (Int16)(pPropagationChIn[2 * i] + Gauss[2 * i]);
		pPropagationChOut[2 * i + 1] = (Int16)(pPropagationChIn[2 * i + 1] + Gauss[2 * i + 1]) ; 
	}

    free(Gauss);

    out1.open("temp\\channelout0.txt",ios::app);

	for (i = 0; i < 2*propagationChInLength; i ++)
	{
		out1 << setw(10) << pPropagationChOut[i];
	}
	out1.close();

}

// End of file 

⌨️ 快捷键说明

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