awgnclass.cpp

来自「三种模拟调制与解调的算法」· C++ 代码 · 共 78 行

CPP
78
字号
// AWGNClass.cpp: implementation of the AWGNClass class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "mod_demod.h"
#include "AWGNClass.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AWGNClass::AWGNClass(double vmean,double vsigma,
					 long vpoints,CString filename)
{
	
	mean=vmean;
	out_put_filename=filename;
	sigma=vsigma;
	srand(32760);
	s=rand();
	num_of_points=vpoints;
	
}

AWGNClass::~AWGNClass()
{

}
//////////////////////////////
double AWGNClass::uniform(double a,double b,long *seed)
{
	double t;
	*seed=2045*(*seed)+1;
	*seed=*seed-(*seed/1048576)*1048576;
	t=(*seed)/1048576.0;
	t=a+(b-a)*t;
	return t;
}
double AWGNClass::gauss(double mean,double sigma,long *s)
{
	int i;
	double x,y;
	for(x=0,i=0;i<12;i++)
		x+=uniform(0.0,1.0,s);
	x=x-6.0;
	y=mean+x*sigma;
	return y;
	
}
////////////////////////////////////////
void AWGNClass::run()
{
	CFile outf;
	outf.Open(out_put_filename,CFile::modeCreate|CFile::modeWrite);
	if(!outf)
	{
		cout<<"Error occured when create file "<<out_put_filename<<endl;
		getch();
		exit(1);
	}
	long i;
	float *x;
	x=(float *)malloc(num_of_points*sizeof(float));
	if(!x) exit(1);
	for(i=0;i<num_of_points;i++)
		x[i]=gauss(mean,sigma,&s);
	outf.WriteHuge(x,num_of_points*sizeof(float));
	outf.Close();
	free(x);
	cout<<"AWGN generation success."<<endl;
}

⌨️ 快捷键说明

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