#include "AWGN.h"
AWGN::AWGN()
{
}
AWGN::~AWGN()
{
}
void AWGN::attn(complexd* in,double ebn0,double factor1,int factor2,long count,double &attn)
{
//------------attenuation calculation------------------------
long double spow,attn1,temp1,temp2;
spow=0;
attn1=0;
for(i=0;i<count;i++)
{
temp1=in[i].real();
temp2=in[i].imag();
spow=spow+(pow(temp1,2)+pow(temp2,2));
}
spow=spow/(count)*factor1;//factor1:code rate
//cout<<"spow:"<<spow<<endl;
attn1=0.5*spow*(1.0/factor2)*pow(10,-ebn0/10.0);//1.0/2(modulation level)//factor2:modulation rate
attn=sqrt(attn1);
//cout<<"attn:"<<attn<<endl;
}
void AWGN::aw(int *in,double* out,long count,double &attn)
{
//cout<<attn<<endl;
//-------------AWGN noise add--------------------------------
for (i=0; i<count; i++)
{
out[i] = in[i]+(Rand()*attn);
}
}
//---------------randn-------------------------------------------
void AWGN::Rnd521(void)
{
int i;
for (i=0;i<32;i++) x1[i] ^=x1[i+489];
for (i=32;i<521;i++) x1[i] ^=x1[i-32];
}
void AWGN::InitRnd(unsigned long seed) /* initize random number */
{
int i,j;
unsigned long u;
u=0;
for (i=0;i<=16;i++){
for (j=0;j<32;j++){
seed=seed*(unsigned long)1566083941+1;
u=(u>>1)|(seed & ((unsigned long)1 <<31));
}
x1[i]=u;
}
x1[16]=M32(x1[16]<<23)^(x1[0]>>9)^x1[15];
for (i=17;i<=520;i++)
x1[i]=M32(x1[i-17]<<23)^(x1[i-16]>>9)^x1[i-1];
Rnd521();
Rnd521();
Rnd521();
jrnd =520;
}
unsigned long AWGN::iRnd(void)
{
if (++jrnd >=521) {Rnd521();jrnd=0; }
return x1[jrnd];
}
double AWGN::UniRnd(void) /* Uniform random number */
{
return (1.0/((((unsigned long) 1 << 32)-1)+1.0))*iRnd();
}
double AWGN::Rand(void) /* Gaussian random number */
{
static int sw=0;
static double r1, r2, s;
if (sw==0) {
sw=1;
do {
r1 = 2*UniRnd()-1;
r2 = 2*UniRnd()-1;
s=r1*r1 + r2*r2;
}while ( s>1 || s==0);
s=sqrt(-2*log(s)/s);
return r1*s;
}
else{
sw=0;
return r2*s;
}
}
int AWGN::SeqGen(void)
{
if(UniRnd()>0.5)
return (1);
else
return (-1);
}
//---------------------the end------------------------------------
评论0