Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Check my code for the ber vs eb/no calculation & plotting for QPSK,QAM,16 QAM,64 QAM

Status
Not open for further replies.

cadenceUK

Member level 5
Joined
Aug 15, 2007
Messages
90
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,862
Check my code for the ber vs eb/no calculation & plotting for QPSK,QAM,16 QAM,64 QAM

i have to make programs for qpsk,qam,16 qam,64 qam,and calcualte the ber vs eb/no and plot them.

i have made some programs which im also sending see if u make cahnge and corect them.










#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

#define N 2000
#define block_size 2
#define amplitude 1.414
void modulate_encoder(int,int,float trans_code[],int);
void demodulate_decoder(float,float,int decode_values[],int);
void gaussian_noise (float sigma,float *nre,float *nim);
void channel(float trans_real[], float trans_imag[],float snr,float received[]);
int error_count(int code_packet[],int decode_values[]);

// channel function with added white gaussian noise have power of specific snr.
//void channel(float trans_real[],float trans_imag[],int N,float snr,float received_real[],float received_imag[]);
float gen_sigma(float snr);
void gaussian_noise (float sigma,float *nre,float *nim);

// random number generator
long int rran(long int seed);
long int rran2(long int seed);





void main()
{
FILE *perfQPSK;
int bits[N];
float trans_real[block_size/2],trans_imag[block_size/2];
float rectrans_real[block_size/2],rectrans_imag[block_size/2];
float trans_code[block_size];
int recdecoded_bits[block_size];
int decode_values[block_size],accum_errors,errors,block;

int i,j;
int code_packet[block_size];
double Pb;
float snr=2;
float nre,nim,Eb_No,received[block_size]; // for noise of the channel


int decoded_bits[20],inter_length=2;

if((perfQPSK=fopen("perf_QPSK.txt","w"))==NULL)
printf("\n unable to open the file\n");

for(i=0;i<N;i++)
bits=rand()%2; // generates '1' or '0'


for(Eb_No=0;Eb_No<100;Eb_No=Eb_No+5) //in dB
{
accum_errors=0;
block=0;

for(i=0 ;i<N; i=i+block_size)
{
for(j=0;j<block_size;j++)
{
code_packet[j]=bits[i+j];
}
for(j=0;j<block_size;j=j+2)
{
modulate_encoder(code_packet[j],code_packet[j+1],trans_code,j);
trans_real[j/2]=(amplitude/1.414)* trans_code[j];
trans_imag[j/2]=(amplitude/1.414)*(trans_code[j+1]);
}

channel(trans_real, trans_imag,snr,received);


for (j=0;j<block_size;j=j+2)
{
rectrans_real[j/2]=received[j];
rectrans_imag[j/2]=received[j+1];
demodulate_decoder(rectrans_real[j/2],rectrans_imag[j/2],decode_values,j);
recdecoded_bits[j/2]=decode_values[j];
recdecoded_bits[(j/2)+1]=decode_values[j+1];
}

errors = error_count(code_packet,recdecoded_bits);
accum_errors += errors;
block++;
}

Pb=(double)accum_errors/(block*block_size);

//calculate BER probability
printf("Eb_No=%0.1f\t%d blocks\tBER=%0.25lf\n",Eb_No,block,Pb); //output string to screen
perfQPSK=fopen("perf_QPSK.txt","a"); //open a file
fprintf(perfQPSK,"Eb_No=%0.1f %d blocks BER=%0.20lf\n",Eb_No,block,Pb); //write string to file
}

}
void channel(float trans_real[], float trans_imag[],float snr,float received[])
{// This is the channel where the noise is added to the modulated data.

int i;
float nre,nim;

//AWGN channel
for(i=0;i<2;i+=2)
{
gaussian_noise(gen_sigma(snr),&nre,&nim);

received = trans_real[i/2] + nre;
received[i+1]=trans_imag[i/2]+nim;
}
}

float gen_sigma(float snr)
{
float sigma;

sigma = sqrt(1/(2*powf(10,snr*0.1)));

return sigma;
}

//Gaussian number generation
void gaussian_noise (float sigma,float *nre,float *nim)
{
float r,v1,v2,f,ran1,ran2;
r=1;
while (r>=1)
{
ran1=rran(0)/pow(2,30);
ran2=rran2(0)/pow(2,30);
v1=2*ran1-1;
v2=2*ran2-1;

r=pow(v1,2)+pow(v2,2);
}
f=(float) sqrt(-2*pow(sigma,2)*log(r)/r);
*nre=v1*f;
*nim=v2*f;

}


void modulate_encoder(int localbit1,int localbit2,float codevalue[], int i)
{


if(localbit1==0)
codevalue=1;
else
codevalue=-1;
if(localbit2==0)
codevalue[i+1]=1;
else
codevalue[i+1]=-1;

}

void demodulate_decoder(float local_receive_real, float local_receive_imag,int decode_values[], int i)
{
if(local_receive_real>0)
decode_values=0;
else
decode_values=1;

if(local_receive_imag>0)
decode_values[i+1]=0;
else
decode_values[i+1]=1;
}


int error_count(int trans_code[],int decoded_bits[])
{
int i,dec_errs,inter_length=2;
dec_errs=0;

for(i=0;i<inter_length;i++) /* number of errors after decoding are counted. */
{
if(trans_code != decoded_bits)
dec_errs++;

}
return(dec_errs);
}


















#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>



void modulate_encoder(int *localword,float * codevalue);
void demodulate_decoder(float local_receive_real, float local_receive_imag,int *decodevalue, float amplitude);

// channel function with added white gaussian noise have power of specific snr.
//void channel(float trans_real[],float trans_imag[],int N,float snr,float received_real[],float received_imag[]);
float gen_sigma(float snr);
void gaussian_noise (float sigma,float *nre,float *nim);

// random number generator
long int rran(long int seed);
long int rran2(long int seed);
void main()
{
//clrscr();
int bits[20],info_word[4];
float trans_real[5],trans_imag[5],A;//, received_real[10],received_imag[10];
float trans_code[2];
int decode_values[2];
int decoded_bits[20];
int i,j, local_bitval, N;
float snr=2;
float nre,nim; // for noise of the channel

printf("The number bits must be less than 20 and multiple of 2");
printf("\nEnter the total number of bits to be encoded:");
scanf("%d",&N);
printf("\n Enter the basic amplitude of the QAM modulation:");
scanf("%f",&A);

if(!((N%4==0)||(N<=20)))
{
printf("The number that entered is not a multiple of 4 or not less than 20");
printf("\nEnter the total number of bits to be encoded:");
scanf("%d",&N);
}

printf("Enter the bits to be encoded");

for(i=0;i<N;i++)
{
scanf("%d",&local_bitval);
bits=local_bitval;
}

for(i=0;i<N;i=i+4)
{
for(j=0;j<4;j++)
{
info_word[j]=bits[i+j];
}
modulate_encoder(&info_word[0],& trans_code[0]);
//printf("\nreal value %f , imag value %f \n", trans_code[0],trans_code[1]);
trans_real[i/4]=trans_code[0] * A;
trans_imag[i/4]=(trans_code[1])* A;
}
}
int i;
float nre,nim;

//AWGN channel
for(i=0;i<inner_interleaver;i++)
{
gaussian_noise(gen_sigma(snr),&nre,&nim);

received = code_packet + nre;
}
}

float gen_sigma(float snr)
{
float sigma;

sigma = sqrt(1/(2*powf(10,snr*0.1)));

return sigma;
}


AWGN channel
for(i=0;i<N;i=i+2)
{
gaussian_noise(gen_sigma(snr),&nre,&nim);

received_real[i/2] = trans_real[i/2] + nre;
received_imag[i/2] = trans_imag[i/2] + nim;
}

for (i=0;i<N;i=i+2)
{
printf("\ncoded values: %f\t %f \n received values: %f\t %f \n\n",trans_real[i/2],trans_imag[i/2],received_real[i/2],received_imag[i/2]);
} */


for (i=0;i<N;i=i+4)
{
demodulate_decoder(trans_real[i/4],trans_imag[i/4],& decode_values[0], A);//received_real[i/2],received_imag[i/2],& decode_values[0]);
//printf("\nvalues %d and %d",decode_values[0],decode_values[1]);
decoded_bits=decode_values[0];
decoded_bits[i+1]=decode_values[1];
decoded_bits[i+2]=decode_values[2];
decoded_bits[i+3]=decode_values[3];
}

for(i=0;i<N; i=i+4)
{
printf("The boolean value of bits: %d %d %d %d \nThe encoded value %f +j %f\n\n",bits,bits[i+1],bits[i+2],bits[i+3],trans_real[i/4],trans_imag[i/4]);
}

printf("\nThe encoded bit stream of inphase :");
for(i=0;i<N;i=i+4)
{
printf("%f ",trans_real[i/4]);

}
printf("\nThe encoded bit stream of quadrature :");
for(i=0;i<N;i=i+4)
{
printf("%f ",trans_imag[i/4]);

}

printf("\nThe decoded value of bits:");
for(i=0;i<N; i=i+4)
{
printf("%d %d %d %d ",decoded_bits,decoded_bits[i+1],decoded_bits[i+2],decoded_bits[i+3]);
}
}



float gen_sigma(float snr)
{
float sigma;

sigma = sqrt(1/(2*powf(10,snr*0.1)));

return sigma;
}

//Gaussian number generation
void gaussian_noise (float sigma,float *nre,float *nim)
{
float r,v1,v2,f,ran1,ran2;
r=1;
while (r>=1)
{
ran1=rran(0)/pow(2,30);
ran2=rran2(0)/pow(2,30);
v1=2*ran1-1;
v2=2*ran2-1;

r=pow(v1,2)+pow(v2,2);
}
f=(float) sqrt(-2*pow(sigma,2)*log(r)/r);
*nre=v1*f;
*nim=v2*f;
}

















void modulate_encoder(int *localword,float *codevalue)
{

printf("To check the number of time the finction called\n");

// inphase component generation

if ((*(localword)==0) & ( *(localword+1)==0) )
*codevalue=1/1.414;
else if ((*(localword)==0) & ( *(localword+1)==1) )
*codevalue=3/1.414;
else if ((*(localword)==1) & ( *(localword+1)==0) )
*codevalue=-1/1.414;
else if ((*(localword)==1) & ( *(localword+1)==1) )
*codevalue=-3/1.414;

//quadrature component generation

if ((*(localword+2)==0) & ( *(localword+3)==0) )
*(codevalue+1)=1/1.414;
else if ((*(localword+2)==0) & ( *(localword+3)==1) )
*(codevalue+1)=3/1.414;
else if ((*(localword+2)==1) & ( *(localword+3)==0) )
*(codevalue+1)=-1/1.414;
else if ((*(localword+2)==1) & ( *(localword+3)==1) )
*(codevalue+1)=-3/1.414;

}
void demodulate_decoder(float local_receive_real, float local_receive_imag,int *decodevalue, float A)
{
if(local_receive_real>0)
{
*decodevalue=0;
if(local_receive_real>A*(1.414))
*((decodevalue)+1)=1;
else
*((decodevalue)+1)=0;
}
else
{
*decodevalue=1;
if(local_receive_real<A*(-1.414))
*((decodevalue)+1)=1;
else
*((decodevalue)+1)=0;
}
if(local_receive_imag>0)
{
*(decodevalue+2)=0;
if(local_receive_imag>2.828)
*((decodevalue)+3)=1;
else
*((decodevalue)+3)=0;
}
else
{
*(decodevalue+2)=1;
if(local_receive_imag<-2.828)
*((decodevalue)+3)=1;
else
*((decodevalue)+3)=0;
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top