RMOL Logo Get Revenue Management Optimisation Library at SourceForge.net. Fast, secure and Free Open Source software downloads
qpsk_simulation.cpp
Go to the documentation of this file.
00001 #include <itpp/itcomm.h>
00002 
00003 using namespace itpp;
00004 
00005 //These lines are needed for use of cout and endl
00006 using std::cout;
00007 using std::endl;
00008 
00009 int main()
00010 {
00011   //Declarations of scalars and vectors: 
00012   int i, Number_of_bits;
00013   double Ec, Eb;
00014   vec EbN0dB, EbN0, N0, noise_variance, bit_error_rate; //vec is a vector containing double
00015   bvec transmitted_bits, received_bits;                 //bvec is a vector containing bits
00016   cvec transmitted_symbols, received_symbols;           //cvec is a vector containing double_complex
00017 
00018   //Declarations of classes:
00019   QPSK qpsk;                     //The QPSK modulator class
00020   AWGN_Channel awgn_channel;     //The AWGN channel class
00021   it_file ff;                    //For saving the results to file
00022   BERC berc;                     //Used to count the bit errors
00023   Real_Timer tt;                 //The timer used to measure the execution time
00024 
00025   //Reset and start the timer:
00026   tt.tic();
00027 
00028   //Init:
00029   Ec = 1.0;                      //The transmitted energy per QPSK symbol is 1.
00030   Eb = Ec / 2.0;                 //The transmitted energy per bit is 0.5.
00031   EbN0dB = linspace(0.0,9.0,10); //Simulate for 10 Eb/N0 values from 0 to 9 dB.
00032   EbN0 = inv_dB(EbN0dB);         //Calculate Eb/N0 in a linear scale instead of dB. 
00033   N0 = Eb * pow(EbN0,-1.0);      //N0 is the variance of the (complex valued) noise.
00034   Number_of_bits = 100000;       //One hundred thousand bits is transmitted for each Eb/N0 value
00035 
00036   //Allocate storage space for the result vector. 
00037   //The "false" argument means "Do not copy the old content of the vector to the new storage area."
00038   bit_error_rate.set_size(EbN0dB.length(),false);
00039 
00040   //Randomize the random number generators in it++:
00041   RNG_randomize();
00042 
00043   //Iterate over all EbN0dB values:
00044   for (i=0; i<EbN0dB.length(); i++) {
00045 
00046     //Show how the simulation progresses:
00047     cout << "Now simulating Eb/N0 value number " << i+1 << " of " << EbN0dB.length() << endl;
00048 
00049     //Generate a vector of random bits to transmit:
00050     transmitted_bits = randb(Number_of_bits);
00051 
00052     //Modulate the bits to QPSK symbols:
00053     transmitted_symbols = qpsk.modulate_bits(transmitted_bits);
00054 
00055     //Set the noise variance of the AWGN channel:
00056     awgn_channel.set_noise(N0(i));
00057 
00058     //Run the transmited symbols through the channel using the () operator:
00059     received_symbols = awgn_channel(transmitted_symbols);
00060 
00061     //Demodulate the received QPSK symbols into received bits: 
00062     received_bits = qpsk.demodulate_bits(received_symbols);
00063 
00064     //Calculate the bit error rate:
00065     berc.clear();                               //Clear the bit error rate counter
00066     berc.count(transmitted_bits,received_bits); //Count the bit errors
00067     bit_error_rate(i) = berc.get_errorrate();   //Save the estimated BER in the result vector
00068 
00069   }
00070 
00071   tt.toc();
00072 
00073   //Print the results:
00074   cout << endl;
00075   cout << "EbN0dB = " << EbN0dB << " [dB]" << endl;
00076   cout << "BER = " << bit_error_rate << endl;
00077   cout << "Saving results to ./qpsk_result_file.it" << endl;
00078   cout << endl;
00079 
00080   //Save the results to file:
00081   ff.open("qpsk_result_file.it");
00082   ff << Name("EbN0dB") << EbN0dB;
00083   ff << Name("ber") << bit_error_rate;
00084   ff.close();
00085 
00086   //Exit program:
00087   return 0;
00088 
00089 }
00090