?? 123.cpp
字號(hào):
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "random.h"
// make the complex structure
typedef struct complex1
{
double real;
double imag;
}complex;
// define the number of the iteration
#define ITERATION pow(10, 7)
double modulation(int data);
void demodulation(double * r_data);
int bercheck(int data, double d_data);
void main()
{
FILE *fp1;
double Noisepower;
complex Noise;
int data[4];
int codedcode[12];
int data1[2];
complex m_data;
complex r_data;
double d_datareal[2];
double d_dataimag[2];
int error;
int error1;
int error2;
int error3;
double avgerr;
int EbofN0;
// open and make the text file
//fp1=fopen("result_16qam.txt","w");
for(EbofN0=0; EbofN0<=10; EbofN0+=1)
{
double sumerr=0.0;
int run;
for(run=0;run<ITERATION;run++)
{
// generate the real part data
generate_random(&data[0], 4);
convolution(&data, &codedcode);
// generate the imaginary part data
generate_random(&data1[0], 2);
// modulated the real part signal
m_data.real = modulation(data);
// modulated the imaginary part signal
m_data.imag = modulation(data1);
// make the noisepower
Noisepower=1/(4*pow(10,(double)EbofN0/10));
// make the real part of noise
Noise.real = sqrt(Noisepower/2)*GaussRand();
// make the imaginary part of noise
Noise.imag = sqrt(Noisepower/2)*GaussRand();
// receive the real part of the signal
r_data.real = sqrt(1.0/10.0)*m_data.real + Noise.real;
// receive the imaginary part of the signal
r_data.imag = sqrt(1.0/10.0)*m_data.imag + Noise.imag;
d_datareal[0] = r_data.real;
d_dataimag[0] = r_data.imag;
// demodulated the real part of the signal
demodulation(&d_datareal);
// demodulated the imaginary part of the signal
demodulation(&d_dataimag);
// check the error
error= bercheck(data[0], d_datareal[0]);
error1= bercheck(data[1], d_datareal[1]);
error2= bercheck(data1[0], d_dataimag[0]);
error3= bercheck(data1[1], d_dataimag[1]);
sumerr = sumerr + error + error1 + error2 + error3;
}
//calculated the BER
avgerr = sumerr / (ITERATION*4);
fprintf(fp1,"%.7f\n",avgerr);
printf("%.7f\n", avgerr);
}
fclose(fp1);
}
//convolutional code
void convolution(int *data, int *codeddata)
{
int a = data[0];
int b = 0;
int c = 0;
int codeddata[12];
for(int N=0;N<=5;N++)
{
codeddata[2*N] = a+b+c;
codeddata[2*N+1] = a + c;
a = data[N+1];
b = data[N];
if(N == 0)
{
c = 0;
}else
{
c = data[N-1];
}
}
}
// modulation function
double modulation(int *data)
{
int m_data;
if (*data==1)
{
if(*(data+1)==1)
{
m_data=1;
}
else
{
m_data=3;
}
}
else
{
if(*(data+1)==1)
{
m_data=-1;
}
else
{
m_data=-3;
}
}
return (m_data);
}
// demodulation function
void demodulation(double *r_data)
{
if (r_data[0]>=0.0)
{
if (r_data[0]>=sqrt(1.0/10.0)*2.0)
{
r_data[0]=1;
r_data[1]=0;
}
else
{
r_data[0]=1;
r_data[1]=1;
}
}
else
{
if (r_data[0]<=sqrt(1.0/10.0)*(-2.0))
{
r_data[0]=0;
r_data[1]=0;
}
else
{
r_data[0]=0;
r_data[1]=1;
}
}
}
// error check function
int bercheck(int data, double d_data)
{
int error=0;
if ((double)data==d_data)
{
error=0;
}
else
{
error=1;
//printf("error occured\n");
}
return (error);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -