FSK baseband Modulation and Demodulation and Computing the BER

 

/*About ITPP
IT++ is a C++ library of mathematical, signal processing and communication classes and functions. Its main use is in the simulation of communication systems and for performing research in the area of communications. The kernel of the library consists of generic vector and matrix classes, and a set of accompanying routines. Such a kernel makes IT++ similar to MATLAB, GNU Octave or SciPy. The IT++ library originates from the former department of Information Theory at the Chalmers University of Technology, Gothenburg, Sweden. Because the library is coded in C++, the name IT++ seemed like a good idea at the time. While departments come and go, IT++ have developed a life of its’ own and is now released under the terms of the GNU General Public License (GPL) for you to enjoy. IT++ is being developed and widely used by researchers who work in the area of communications, both in the industry and at universities. In 2005, 2006 and 2007, IT++ was developed as a part of the European Network of Excellence in Wireless Communications (NEWCOM). IT++ makes an extensive use of existing open-source or commercial libraries for increased functionality, speed, and accuracy. In particular BLAS, LAPACK and FFTW libraries can be used. Instead of the reference BLAS and LAPACK implementations, some optimized platform-specific libraries can be used as well, i.e.: ATLAS (Automatically Tuned Linear Algebra Software) – includes optimised BLAS and a limited set of LAPACK routines MKL (Intel Math Kernel Library) – includes all required BLAS, LAPACK and FFT routines (FFTW not required) ACML (AMD Core Math Library) – includes BLAS, LAPACK and FFT routines (FFTW not required) It is possible to compile and use IT++ without any of the above-listed libraries, but the functionality will be reduced. IT++ should work on GNU/Linux, Sun Solaris, Microsoft Windows (with Cygwin, MinGW/MSYS or Microsoft Visual C++) and Mac OS X operating systems.*/

//—————————————-
// FSK MODULATION
//—————————————-
//Including all the libraries
#include<iostream>
#include<itpp/itbase.h>
#include<itpp/itsignal.h>
#include<vector>
#include <iomanip>
#include <itpp/itcomm.h>

using namespace itpp;
using namespace std;

void gnuplot_load(string fn)
{
string comand=”gnuplot -persist\n”;
FILE *pipe= popen(comand.c_str() , “w”);
ostringstream oss;
oss.str(“”); oss.clear();
oss<<“load ‘”<<fn<<“‘”<<endl;
fprintf(pipe, “%s”,oss.str().c_str());
fflush(pipe);
int r=pclose(pipe);
if(r<0)cout<<“# error in pclose(), while using unix pipes!”<
}

//for generation of a file to store the vectors

void save_vectors__com_fn_vecs(string com,string fn,vector<vec>V)
{
int N=V.size();
ofstream fout;
fout.open(fn.c_str());
fout<<“# “<<com<<endl;

for(int i=0;i<size(V[0]);i++)
{
vec a;
for(int k=0;k<N;k++)
{ a=V[k];
fout<<a[i]<<” “;
}
fout<<endl;

}
fout.close();
}

//Function for BaseBand FSK modulation
complex<double> FSK_baseband__Ac_t_sym(float Ac, int sym)
{
complex<double>jj(0.,1.);
return Ac*exp(jj*(M_PI/2.+(M_PI*-1*sym/2.)));
}

//Function for BaseBand FSK Demodulation
int FSK_baseband_correlation_Xr(complex<double>*sym,complex<double> & qpskn)
{
double mindis,disi; int dsym;
for(int i=0; i<2; i++)
{
disi=pow(abs(sym[i]-qpskn),2);
if(i==0){mindis=disi; dsym=0;}
else
{
if(disi<mindis){mindis=disi; dsym=i;}
}
}
return dsym;
}

//Calculating the received power using los formula
double recpow_LOS_watts(double Ptdbm,double GtdB,double GrdB,double d,double f0)
{
double pt=pow(10.,Ptdbm/10)*1e-3;
double Gt=pow(10.,GtdB/10);
double Gr=pow(10.,GrdB/10);
double lambda0=2.998e8/f0;
return pt*Gt*Gr/pow(((4.*M_PI*d)/lambda0),2);
}
vec vec_x1_dx_x2(float x1, float dx, float x2)
{
vec r(0);
for(int i=0; i<(i+1); i++)
{
r.ins(i,x1+i*dx);
if(r[i]>=x2)break;
}
r[size(r)-1]=x2;
return r;
}

//———–
//main program
//————
int main()
{

//——————-// 
// Input Data
//…………………..//
double R=9.6e3; 
double Ptdbm=10;
double NF=pow(10,80/10); 
double fc=433e6;
double GtdB=0.;
double GrdB=0.;
double Fs=4*fc;
vec d=linspace(10,500,51);
int nexp=1000000; /// Monte Carlo expts
//—————————————————————————-//
// Noise Figure to Noise Power
//……………………….//
double kB=1.38e-23;
double N0=kB*300*(NF-1.0); // power spectral density of noise
double pn=(N0/2)*Fs;

int Nbit=1;
double Tb=1/R; 
double Tsymb=Tb; 
double tstep=1./Fs;
float t0=0.0;
float te=Tb*Nbit; 
vec t=vec_x1_dx_x2(t0,tstep, te); //cout<<t<<endl;exit(0); </t<<
Normal_RNG g;

vector <vec> vecs(2);
vec ber(d);

for(int k=0; k<size(d); k++)
{
double rec_pow=recpow_LOS_watts(Ptdbm,GtdB,GrdB,d[k],fc);//calcation of received power
double Ai=sqrt(2.*rec_pow);//calculation of amplitude of a signal based on received power
complex<double>fsksymb[2],sig,y_AWGN,jj(0.,1.);//declaration of complex variables

//————–
//FSK modulation
//————–
fsksymb[0]=FSK_baseband__Ac_t_sym(1.,0);
fsksymb[1]=FSK_baseband__Ac_t_sym(1.,1);

double Es=rec_pow*Tsymb;
double EsN0=Es/N0;   //calculation of ESN0

double var=rec_pow/(EsN0);//calculation of variance
g.setup(0.0,var/2); 
int nfail=0;

//running monticarlo simulation for finding BER
for(int iexp=0; iexp<nexp ; iexp++)
{
int tsymb=randi(0,1);
complex<double>sig=FSK_baseband__Ac_t_sym(Ai,tsymb);
y_AWGN=sig+g()+jj*g();
int rsymb=FSK_baseband_correlation_Xr(fsksymb,y_AWGN);
if(tsymb!=rsymb)nfail++;
}
ber[k]=double(nfail)/double(nexp);
cout<<setw(12)<<d[k]<<setw(12)<<ber[k]<<endl;
}
vecs[0]=d;
vecs[1]=ber;

save_vectors__com_fn_vecs(“gaus dat”,”g1.txt”,vecs);
ofstream fout;
fout.open(“fgnuplot”);
fout<<“set terminal wxt”<<endl;
fout<<“set xlabel ‘distance’”<<endl;
fout<<“set ylabel ‘BER’”<<endl;
fout<<“plot ‘g1.txt’ u 1:2 w lp lt 1 t ‘dis vs BER’”<<endl;
fout.close();
gnuplot_load(“fgnuplot”);
}

Leave a comment

Website Powered by WordPress.com.

Up ↑