📄 swchannel.cpp
字号:
#include <SWchannel.h>
#include <comp_def.h>
const double PI 3.1415926;
/*---------------------------------------------------------------
FUNCTION:
random_num()
DESCRIPTION:
Generate a random number between 0 and 1.
RETURN VALUE:
The random number between 0 and 1.
---------------------------------------------------------------*/
float random_num()
{
long z,k;
static long s1 = 12345L;
static long s2 = 1234546346L;
k= s1 / 53668L;
s1 = 40014L * (s1 - k*53668L) - k*12211L;
if (s1<0)
s1 = s1 + 2147483563L;
k = s2 / 52774;
s2 = 40692L * (s2 - k*52774L) - k*3791L;
if (s2<0)
s2 = s2 + 2147483399L;
z=s1 - s2;
if (z<1)
z = z + 2147483562L;
return (float) z / (float) 2147483563.0;
}
/*---------------------------------------------------------------
FUNCTION:
void gen_source(int *data, int length)
DESCRIPTION:
This function generate the source bits for simulation.
PARAMETERS:
INPUT:
length - Length of needed data.
OUTPUT:
data - Contains pointer to source data sequence.
RETURN VALUE:
None
---------------------------------------------------------------*/
void gen_source(int *data, int length)
{
double temp;
int i;
for (i=0; i<length; i++)
{
//temp = (double)rand()/RAND_MAX;
temp = random_num();
if (temp <= 0.5)
{
*(data+i) = 0;
}
else
{
*(data+i) = 1;
}
}
}
/*---------------------------------------------------------------
* 函数介绍:产生长度为n的复高斯随机序列
* 输入参数:mean:均值
sigma:标准差
* 返回值: gn:返回一个复高斯数
-----------------------------------------------------------------*/
complex gngauss(double mean,double sigma)
{
double u,z;
complex gn;
u=(double)rand()/RAND_MAX; //a uniform random variable in (0,1)
if(u==1) u=0.9999999999;
z=sigma*(sqrt(2*log(1/(1-u)))); //a Rayleigh distributed random variable
u=(double)rand()/RAND_MAX;
if(u==1) u=0.9999999999;
gn.real=mean+z*cos(2*pi*u);
gn.imag=mean+z*sin(2*pi*u);
return gn;
}
/*---------------------------------------------------------------
FUNCTION:
void AWGN(complex *send,complex *r,double mean,double sigma,int totallength)
DESCRIPTION:
This function simulate a AWGN channel.
PARAMETERS:
INPUT:
send - Input signal sequence need to add noise.
sigma - Standard deviation of AWGN noise
mean - mean of AWGN noise
totallength - Length of "send".
OUTPUT:
r - Contains pointer to the data sequence added with gaussian white noise.
RETURN VALUE:
None
---------------------------------------------------------------*/
void AWGN(complex *send,complex *r,double mean,double sigma,int totallength)
{
int i;
complex *noise = (complex *)malloc(sizeof(complex)*totallength);
for(i=0; i<totallength; i++)
{
*(noise+i)=gngauss(mean,sigma);
*(r+i) = *(send+i) + *(noise+i);
}
free(noise);
}
/*---------------------------------------------------------------
FUNCTION:
void Rayleigh(complex *send, double *r, double mean,double sigma, int totallength)
DESCRIPTION:
This function simulate a Rayleigh channel.
PARAMETERS:
INPUT:
send - Input signal sequence need to add noise.
sigma - Standard deviation of AWGN noise
mean - mean of AWGN noise
totallength - Length of "send".
OUTPUT:
r - Contains pointer to the data sequence added with gaussian white noise.
RETURN VALUE:
None
---------------------------------------------------------------*/
void Rayleigh(complex *send,complex *r,double fd, double ts, int M, double mean,double sigma,int totallength)
{
int i;
complex *noise = (complex *)malloc(sizeof(complex)*totallength);
complex Ray;
for(i=0; i<totallength; i++)
{
Ray=gngauss(0,0.707); //**暂时这样取**
*(noise+i)=gngauss(mean,sigma);
*(r+i) = (*(send+i))*Ray + *(noise+i);
}
free(noise);
}
/*------------------------------------------------------------------------
FUNCTION:
void BPSK(int *module_in, complex *module_out,int totallength)
DESCRIPTION:
This function perform a BPSK modulation.
PARAMETERS:
INPUT:
module_in - Input bit sequence need to modulate.
totallength - Length of "send".
OUTPUT:
module_out - Output modulated sequence
RETURN VALUE:
None
---------------------------------------------------------------*/
void BPSK(int *module_in, complex *module_out,int totallength)
{
int i;
double temp;
for(i=0;i<totallength;i++)
{
temp=2*(double)(*(module_in+i))-1;
*(module_out+i)=set(temp,0.0);
}
}
/*------------------------------------------------------------------------
FUNCTION:
void De_BPSK(complex *demodule_in, double *demodule_out,int length)
DESCRIPTION:
This function perform a BPSK demodulation.
PARAMETERS:
INPUT:
demodule_in - Input bit sequence need to demodulate.
length - Length of "send".
OUTPUT:
demodule_out - Output demodulated sequence
RETURN VALUE:
None
---------------------------------------------------------------*/
void De_BPSK(complex *demodule_in, double *demodule_out,int length)
{
int i;
complex temp;
for(i=0;i<length;i++)
{
temp=*(demodule_in+i);
*(demodule_out+i)=temp.real;
}
}
int bitxor(int a,int b)
{
int c;
c = (a==b)?0:1;
return c;
}
void DBPSK(int *module_in, complex *module_out,int totallength)
{
int i;
double temp;
int *diffcode = (int *)malloc(sizeof(int)*(totallength+1));
*diffcode = 0;
for(i=0;i<totallength;i++)
{
*(diffcode+i+1) = bitxor(*(diffcode+i),*(module_in+i));
}
for(i=0;i<totallength;i++)
{
temp=2*(double)(*(diffcode+i+1))-1;
*(module_out+i)=set(temp,0.0);
}
}
void De_DBPSK(complex *demodule_in, double *demodule_out,int length)
/*------------------------------------------------------------------------
FUNCTION:
void QPSK(int *module_in, complex *module_out,int totallength)
DESCRIPTION:
This function perform a QPSK modulation.
PARAMETERS:
INPUT:
module_in - Input bit sequence need to modulate.
totallength - Length of "send", even number.
OUTPUT:
module_out - Output modulated sequence
RETURN VALUE:
None
---------------------------------------------------------------*/
void QPSK(int *module_in, complex *module_out,int totallength)
{
int i;
double rtp,itp;
for(i=0;i<totallength/2;i++)
{
rtp=2*(double)(*(module_in+2*i))-1;
itp=2*(double)(*(module_in+2*i+1))-1;
*(module_out+i)=set(rtp,itp);
}
}
/*------------------------------------------------------------------------
FUNCTION:
void De_QPSK(complex *demodule_in, double *demodule_out,int length)
DESCRIPTION:
This function perform a QPSK demodulation.
PARAMETERS:
INPUT:
demodule_in - Input bit sequence need to demodulate.
length - Length of "send".
OUTPUT:
demodule_out - Output demodulated sequence
RETURN VALUE:
None
---------------------------------------------------------------*/
void De_QPSK(complex *demodule_in, double *demodule_out,int length)
{
int i;
complex temp;
for(i=0;i<length;i++)
{
temp=*(demodule_in+i);
*(demodule_out+2*i)=temp.real;
*(demodule_out+2*i+1)=temp.imag;
}
}
/*------------------------------------------------------------------------
FUNCTION:
void QAM16(int *module_in, complex *module_out,int totallength)
DESCRIPTION:
This function perform a QAM16 modulation.
PARAMETERS:
INPUT:
module_in - Input bit sequence need to modulate.
totallength - Length of "send", 4*n number.
OUTPUT:
module_out - Output modulated sequence
RETURN VALUE:
None
---------------------------------------------------------------*/
void QAM16(int *module_in, complex *module_out,int totallength)
{
int i;
int a,b,c,d;
double rtp,itp;
for(i=0;i<totallength/4;i++)
{
a=*(module_in+2*i);
b=*(module_in+2*i+1);
c=*(module_in+2*i+2);
d=*(module_in+2*i+3);
if(a==0 && c==0)
rtp=-sqrt(2)/2;
elseif(a==0 && c==1)
rtp=-sqrt(2)/6;
elseif(a==1 && c==0)
rtp=sqrt(2)/6;
else
rtp=sqrt(2)/2;
if(b==0 && d==0)
itp=-sqrt(2)/2;
elseif(b==0 && d==1)
itp=-sqrt(2)/6;
elseif(b==1 && d==0)
itp=sqrt(2)/6;
else
itp=sqrt(2)/2;
*(module_out+i)=set(rtp,itp);
}
}
/*------------------------------------------------------------------------
FUNCTION: 还未完成,想编写一个软判决的QAM16解调程序
void De_QAM16(complex *demodule_in, double *demodule_out,int length)
DESCRIPTION:
This function perform a QAM16 demodulation.
PARAMETERS:
INPUT:
demodule_in - Input bit sequence need to demodulate.
length - Length of "send".
OUTPUT:
demodule_out - Output demodulated sequence
RETURN VALUE:
None
---------------------------------------------------------------*/
void De_QPSK(complex *demodule_in, double *demodule_out,int length)
{
int i;
complex temp;
for(i=0;i<length;i++)
{
temp=*(demodule_in+i);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -