📄 turbo_other_functions.c
字号:
/*----------------------------------------------------------
* Copyright (c) 2003, 北京邮电大学移动通信实验室
* All rights reserved.
*
* 文件名称:turbo_other_functions.c
* 文件标识:
* 摘 要:Turbo仿真用文件.
*
* 当前版本:1.0
* 作 者:张鹏
* 完成日期:2003年12月1日
----------------------------------------------------------*/
#include "turbo_code_Log_MAP.h"
#include "turbo_other_functions.h"
extern double *alpha_channel;
double time_jarks = 0;
/*---------------------------------------------------------------
函数:
void gen_source(int *data, int length)
介绍:
产生随机信源.
参数:
输入参数: length - 所需生成序列的长度.
输出参数: data - 所生成bit序列的首址.
返回值:
无
---------------------------------------------------------------*/
void gen_source(int *data, int length)
{
double temp;
int i;
for (i=0; i<length; i++)
{
//temp = (double)rand()/RAND_MAX;
temp = random_turbo();
if (temp <= 0.5)
{
*(data+i) = 0;
}
else
{
*(data+i) = 1;
}
}
}
/*---------------------------------------------------------------
函数:
void AWGN(int *send, double *r, double sigma, int totallength)
介绍:
AWGN信道.
参数:
输入参数: send - int型发送数据首址.
sigma - 噪声标准差.
totallength - 序列长度.
输出参数: r - 经AWGN信道后的数据序列的首址.
返回值:
无
---------------------------------------------------------------*/
void AWGN(double *send, double *r, double sigma, int totallength)
{
int i;
double *noise = (double *)malloc(sizeof(double)*totallength);
double seed = (double)(3.0 - (double)((rand() & RAND_MAX)/(double)RAND_MAX)/10e6);
mgrns(0,sigma,seed,totallength,noise);
for(i=0; i<totallength; i++)
{
*(r+i) = (double)( *(send+i) + *(noise+i) );
}
free(noise);
}
/*---------------------------------------------------------------
函数:
void mgrns(double mean,double sigma,double seed,int n,double *a)
介绍:
产生长度为n的高斯随机序列.
参数:
输入参数: mean - 均值
sigma - 标准差
seed - 一个随机种子
输出参数: a - 长度为n的高斯随机序列.
返回值:
无
---------------------------------------------------------------*/
void mgrns(double mean,double sigma,double seed,int n,double *a)
{ int i,k,m;
double s,w,v,t;
s=65536.0; w=2053.0; v=13849.0;
for (k=0; k<=n-1; k++)
{
t=0.0;
for (i=1; i<=12; i++)
{
seed=seed*w+v; m=(int)(seed/s);
seed=seed-m*s; t=t+(seed)/s;
}/*按照中心极限定理产生服从高斯分布的随机数*/
*(a+k)=mean+(double)(sigma*(t-6.0));
}
return;
}
/*---------------------------------------------------------------
函数:
void rayleigh_channel_1226(double *send, double *r, double sigma, int totallength)
介绍:
Rayleigh信道.
参数:
输入参数: send - double型发送数据首址.
sigma - 噪声标准差.
totallength - 序列长度.
输出参数: r - 经Rayleigh信道后的数据序列的首址.
返回值:
无
---------------------------------------------------------------*/
void rayleigh_channel_1226(float *send, float *r, double sigma, int totallength)
{
int i;
double *noise = (double *)malloc(sizeof(double)*totallength);
int seed=abs((rand()*100000)%RAND_MAX);
chan_gauss(0, sigma, totallength, noise, &seed);
for(i=0; i<totallength; i++)
{
*(r+i) =(float)( *(send+i) * (*(alpha_channel+i)) + *(noise+i));
}
free(noise);
}
/*---------------------------------------------------------------
函数:
void chan_gauss(double mean, double sigma,int Ns, double *gauss, int *seed)
介绍:
产生长度为Ns的高斯随机序列.
参数:
输入参数: mean - 均值
sigma - 标准差
seed - 一个随机种子
输出参数: gauss - 长度为Ns的高斯随机序列.
返回值:
无
---------------------------------------------------------------*/
void chan_gauss(double mean,double sigma, int Ns, double *gauss, int *seed)
{
int i;
double xita;
double z;
double ssigma=-2*sigma*sigma;
for(i=0;i<Ns;i++)
{
z=(double)(sqrt(ssigma*log(1-chan_uniform(seed))));
xita=chan_uniform(seed);
gauss[i]= (double)(mean+z*cos(2*3.14159265*xita));
}
}
/*---------------------------------------------------------------
函数:
double chan_uniform(int *seed)
介绍:
0-1均匀分布随机数发生器.
参数:
输入参数: seed - 指向随机种子的指针.
输出参数: 无.
返回值:
生成的随机数.
---------------------------------------------------------------*/
double chan_uniform(int *seed)
{
double t;
*seed=2045*(*seed)+1;
*seed=*seed-(*seed/1048576)*1048576;
t=(*seed)/(double) 1048576.0;
return(t);
}
/*---------------------------------------------------------------
函数:
void chan_jakes(double *rayleigh, double fd, double fs, int Ns)
介绍:
Jakes模型,用以产生rayleigh衰落系数.
参数:
输入参数: fd - 多谱勒频移.
fs - rayleigh采样系数.
Ns - 序列长度.
输出参数: rayleigh - 生成的rayleigh序列的首址.
返回值:
无
---------------------------------------------------------------*/
void chan_jakes(double *rayleigh, double fd, double fs, int Ns)
{
int i,j,N;
double t,temp_1,temp_2,fai,*fai_i,*fai_q,*wn,wm,nn0,nn1,nn21,nn22,nn3;
double *rayleigh_I;
double *rayleigh_Q;
double deta;
N=2*(2*N0+1);
nn0=(double)(1/(N0+1.0));
nn1=(double)(1/(N+0.0));
nn21=(double)(1/sqrt(2*N0));
nn22=(double)(1/sqrt(2*N0+2));
//allocate memory
if (((fai_i)= (double *) malloc((N0+1)*sizeof(double))) == NULL)
{
printf("Not enough memory to allocate buffer ex_array\n");
exit(1);
}
if (((fai_q)= (double *) malloc((N0+1)*sizeof(double))) == NULL)
{
printf("Not enough memory to allocate buffer ex_array\n");
exit(1);
}
if (((wn)= (double *) malloc((N0+1)*sizeof(double))) == NULL)
{
printf("Not enough memory to allocate buffer ex_array\n");
exit(1);
}
if (((rayleigh_I)= (double *) malloc(Ns*sizeof(double))) == NULL)
{
printf("Not enough memory to allocate buffer rayleigh_I\n");
exit(1);
}
if (((rayleigh_Q)= (double *) malloc(Ns*sizeof(double))) == NULL)
{
printf("Not enough memory to allocate buffer rayleigh_Q\n");
exit(1);
}
fai_i[N0]=1;
fai_q[N0]=0;
wm=(double)(2*PI*fd);
temp_1=0;
temp_2=0;
for (i=0;i<N0;i++)
{
fai=(double)(PI*(i+1)*nn0);
*(fai_i+i)=(double) (cos(fai));
*(fai_q+i)=(double) (sin(fai));
*(wn+i)=(double) (wm*cos(2*PI*(i+1)*nn1));
}
//pruduce Ns sample point
deta=1/fs;
for(i=0,t=time_jarks;i<Ns;i++,t+=deta)
{
for(j=0;j<N0;j++)
{
nn3=(double)(cos(*(wn+j)*t));
temp_1+=(double) (*(fai_i+j)*nn3);
temp_2+=(double) (*(fai_q+j)*nn3);
}
*(rayleigh_I+i)=(double) ((2*temp_1+1.4142136*fai_i[N0]*cos(wm*t))*nn21);
*(rayleigh_Q+i)=(double) ((2*temp_2+1.4142136*fai_q[N0]*cos(wm*t))*nn22);
*(rayleigh+i)=(double) (sqrt(*(rayleigh_I+i)*(*(rayleigh_I+i))+*(rayleigh_Q+i)*(*(rayleigh_Q+i))));
temp_1=0;
temp_2=0;
}
time_jarks = t;
//set point free
free(fai_i);
free(fai_q);
free(wn);
free(rayleigh_I);
free(rayleigh_Q);
}
void combo(int *trafficflow_sourceA,int *trafficflow_sourceB,int *trafficflow_sourceAB)
{
int i;
for(i=0;i<FRAME_LENGTH;i++)
{
*(trafficflow_sourceAB+2*i)=*(trafficflow_sourceA+i);
*(trafficflow_sourceAB+2*i+1)=*(trafficflow_sourceB+i);
}
}
void doublecombo(double *Le_turboA,double *Le_turboB,double *Le_turboAB)
{
int i;
for(i=0;i<FRAME_LENGTH;i++)
{
*(Le_turboAB+2*i)= *(Le_turboA+i);
*(Le_turboAB+2*i+1)= *(Le_turboB+i);
}
}
void doubledecombo(double *La_turboAB,double *La_turboA,double *La_turboB)
{
int i;
for(i=0;i<FRAME_LENGTH;i++)
{
*(La_turboA+i)=*(La_turboAB+2*i);
*(La_turboB+i)=*(La_turboAB+2*i+1);
}
}
void comboinfoAB(double *infoAB,double *yk_turboA,double *yk_turboB)
{
int i;
for(i=0 ; i<FRAME_LENGTH;i++)
{
*(infoAB+2*i)=*(yk_turboA+2*i);
*(infoAB+2*i+1)=*(yk_turboB+2*i);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -