Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
QPSK BER in C++
#include <math.h>
#include <complex>
#include <ctime>
#include <stdio.h>
#include <cstdlib>
#include <random>
struct result
{
double snr[20];
double ber[20];
};
result simulate(int n ,bool runtype);
double seqlink(double k);
int main()
{
double snr[20];
double ber[20];
struct result ans;
clock_t start = clock();
// program goes here
ans=simulate(20,false);
clock_t ends = clock();
printf("Time elapsed %fms",(float) 1000*(ends - start) / CLOCKS_PER_SEC);
printf("\nSNR = [");
for(int k=0;k<20;k++) printf("%5.2f ",ans.snr[k]);printf("]");
printf("\nBER= [");
for(int k=0;k<20;k++) printf("%f ",ans.ber[k]);printf("]");
printf("\n Done..\n");
}
result simulate(int n ,bool runtype)
{
result ans;
double f[n];
double snr[n];
double initSNR= 0;
for (int k = 0; k < n; k++) {
double currentSNR=initSNR+double(k);
ans.snr[k]=currentSNR;
ans.ber[k]=seqlink(currentSNR);
}
}
return ans;
}
double seqlink(double k) {
int Nbits=1024*100; //*(50-int(k))
std::complex <double> symbols[4];
symbols[0]=std::complex <double> (0.7,0.7); //,+0.7-0.7i,-0.7+0.7i,-0.7-0.7i}
symbols[1]=std::complex <double> (0.7,-0.7); //,+0.7-0.7i,-0.7+0.7i,-0.7-0.7i}
symbols[2]=std::complex <double> (-0.7,0.7); //,+0.7-0.7i,-0.7+0.7i,-0.7-0.7i}
symbols[3]=std::complex <double> (-0.7,-0.7); //,+0.7-0.7i,-0.7+0.7i,-0.7-0.7i}
int txbits[Nbits];
for (int i=0;i<Nbits;i++){
txbits[i]=rand() % 4;
}
std::complex<double> txsymbols[Nbits];
std::complex<double> noise[Nbits];
std::complex<double> rxnoise[Nbits];
double lsnr=double(pow(10,(k/10)));
double Es=1;
double noisevar=Es/lsnr;
int errorbits=0;
std::default_random_engine generator;
std::normal_distribution<double> distribution;
for (int i=0;i<Nbits;i++){
txsymbols[i]=symbols[txbits[i]];
double re=distribution(generator);
double im=distribution(generator);
noise[i]=std::complex<double>(re*sqrt(noisevar*0.5),im*sqrt(noisevar*0.5));
std::complex <double> tmp=txsymbols[i]+noise[i];
{
if(real(tmp)>0 && imag(tmp)>0) rxnoise[i]=symbols[0];
if(real(tmp)>0 && imag(tmp)<0)rxnoise[i]=symbols[1];
if(real(tmp)<0 && imag(tmp)>0)rxnoise[i]=symbols[2];
if(real(tmp)<0 && imag(tmp)<0)rxnoise[i]=symbols[3];
}
if (rxnoise[i]!=txsymbols[i]){
errorbits++;
}
}
double ber =double(errorbits)/double(Nbits);
return ber;
}
@phani0055

This comment has been minimized.

Copy link

phani0055 commented Jan 31, 2019

Time elapsed 1403.682007ms
SNR = [ 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 ]
BER= [0.295244 0.251191 0.201748 0.152852 0.113027 0.076904 0.046738 0.025527 0.013203 0.005537 0.001846 0.000469 0.000068 0.000010 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ]
Done..
I am getting output BER as zeroes for last values of SNR but wouldn't be zero right when comparing it with the theorectical BER vs SNR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.