/*
*
*文件名称:8psk_func.c
*
*作者:chen
*
*日期:2008.12.17
*
*程序说明: 该程序用C语言实现了8psk信号通过AWGN信道,并计算误比特率的仿真。
* 通过输入平均比特信噪比和仿真比特数,可以求得不同参数要求下的误比特率。
*
*/
#include "8psk.h"
double GaussRandGen(double mu, double sigma)
{
static double V1, V2, S;
static int phase = 0;
double X;
if (phase == 0)
{
do
{
double U1 = (double)rand() / RAND_MAX;
double U2 = (double)rand() / RAND_MAX;
V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
}
while((S>=1) || (S==0));
X = V1 * sqrt(-2 * log(S) / S);
}
else
{
X = V2 * sqrt(-2 * log(S) / S);
}
phase = 1 - phase;
return X*sigma+mu;
}//end of GaussRandGen()
void SrcSeqGen(void) //generate random sequence 0,1,1,0,0..
{
int i = 0;
for (i=0; i<=seqLength-1; i++)
{
pSrcSeq[i] = rand() % 2;
}
}//end of SrcSeqGen()
void SymbolMapping(void)
{
int k = 0;
int i;
int phase;
for (i=0; i<=seqLength-1; i+=3) //8psk_maping
{
phase = pSrcSeq[i] * 4 + pSrcSeq[i+1] * 2 + pSrcSeq[i+2];
switch(phase)
{
case 0: pIk[k] = 1; pQk[k] = 0;
break;
case 1: pIk[k] = BETA; pQk[k] = BETA;
break;
case 3: pIk[k] = 0; pQk[k] = 1;
break;
case 2: pIk[k] = -BETA; pQk[k] = BETA;
break;
case 6: pIk[k] = -1; pQk[k] = 0;
break;
case 7: pIk[k] = -BETA; pQk[k] = -BETA;
break;
case 5: pIk[k] = 0; pQk[k] = -1;
break;
case 4: pIk[k] = BETA; pQk[k] = -BETA;
break;
default:
break;
}//end of switch
k++;
}//end of loop
}//end of SymbolMapping()
void AWGN(double mu, double sigma)
{
int i = 0;
for (i=0; i<=seqLength/3-1; i++)
{
pIk[i] = pIk[i] + GaussRandGen(mu, sigma); //通过均值为mu,方差为sigma的高斯信道
pQk[i] = pQk[i] + GaussRandGen(mu, sigma);
}
}//end of AWGN()
void Judge(void)
{
int i;
int k;
double x_temp, y_temp;
double temp;
double min_dis = 100;
double x[8] = {1, BETA, 0, -BETA, -1, -BETA, 0, BETA};
double y[8] = {0, BETA, 1, BETA, 0, -BETA, -1, -BETA};
for (i=0; i<=seqLength/3-1; i++) //对接受数据进行检测判决
{
x_temp = pIk[i];
y_temp = pQk[i];
for (k=0; k<=7; k++)
{
temp = (x_temp - x[k]) * (x_temp - x[k])
+(y_temp - y[k]) * (y_temp - y[k]);
if (temp < min_dis)
{
min_dis = temp;
pIk[i] = x[k];
pQk[i] = y[k];
}//end of if
}// end of inner loop
min_dis=100; //init min_dis for next loop
} //end of outer loop
}// end of Judge()
void SymbolDecode()
{
int k = 0;
int i;
for (i=0; i<=seqLength/3-1; i++)
{
if ((pIk[i] == 1) && (pQk[i] == 0))
{
pRxSeq[k] = 0;
pRxSeq[k+1] = 0;
pRxSeq[k+2] = 0;
}
else
if ((pIk[i] == BETA) && (pQk[i] == BETA))
{
pRxSeq[k] = 0;
pRxSeq[k+1] = 0;
pRxSeq[k+2] = 1;
}
else
if ((pIk[i] == 0) && (pQk[i] == 1))
{
pRxSeq[k] = 0;
pRxSeq[k+1] = 1;
pRxSeq[k+2] = 1;
}
else
if((pIk[i] == -BETA) && (pQk[i] == BETA))
{
pRxSeq[k] = 0;
pRxSeq[k+1] = 1;
pRxSeq[k+2] = 0;
}
else
if ((pIk[i] == -1) && (pQk[i] == 0))
{
pRxSeq[k] = 1;
pRxSeq[k+1] = 1;
pRxSeq[k+2] = 0;
}
else
if ((pIk[i] == -BETA) && (pQk[i] == -BETA))
{
pRxSeq[k] = 1;
pRxSeq[k+1] = 1;
pRxSeq[k+2] = 1;
}
else
if ((pIk[i] == 0) && (pQk[i] == -1))
{
pRxSeq[k] = 1;
pRxSeq[k+1] = 0;
pRxSeq[k+2] = 1;
}
else
{
pRxSeq[k] = 1;
pRxSeq[k+1] = 0;
pRxSeq[k+2] = 0;
}//end of if...else if...
k=k+3;
}//end of for
}//end of SymbolDecode()
int CountErrorBit(void)
{
int i;
int bit = 0;
for (i=0; i<=seqLength-1; i++)
{
if (pSrcSeq[i] != pRxSeq[i])
{
bit++;
}
}
return bit;
}//end of CountErrorBit()
void FreeMemorySpace(void)
{
free(pSrcSeq);
free(pRxSeq);
free(pIk);
free(pQk);
}