In this example we will introduce a few new features compared to the BPSK example. We will use the it_ifile class to store the results of the simulation. We will use the Real_Timer class to measure the execution time of our program. Furthermore we will use one of the channel classes, the AWGN_Channel. We will also show how to read the results in to Matlab with the load_it function. The program is as follows:
using std::cout;
using std::endl;
int main()
{
int i, Number_of_bits;
double Ec, Eb;
vec EbN0dB, EbN0, N0, noise_variance, bit_error_rate;
bvec transmitted_bits, received_bits;
cvec transmitted_symbols, received_symbols;
QPSK qpsk;
AWGN_Channel awgn_channel;
it_file ff;
BERC berc;
Real_Timer tt;
tt.tic();
Ec = 1.0;
Eb = Ec / 2.0;
N0 = Eb *
pow(EbN0, -1.0);
Number_of_bits = 100000;
bit_error_rate.set_size(EbN0dB.length(), false);
for (i = 0; i < EbN0dB.length(); i++) {
cout << "Now simulating Eb/N0 value number " << i + 1 << " of " << EbN0dB.length() << endl;
transmitted_bits =
randb(Number_of_bits);
transmitted_symbols = qpsk.modulate_bits(transmitted_bits);
awgn_channel.set_noise(N0(i));
received_symbols = awgn_channel(transmitted_symbols);
received_bits = qpsk.demodulate_bits(received_symbols);
berc.clear();
berc.count(transmitted_bits, received_bits);
bit_error_rate(i) = berc.get_errorrate();
}
tt.toc();
cout << endl;
cout << "EbN0dB = " << EbN0dB << " [dB]" << endl;
cout << "BER = " << bit_error_rate << endl;
cout << "Saving results to ./qpsk_result_file.it" << endl;
cout << endl;
ff.open("qpsk_result_file.it");
ff << Name("EbN0dB") << EbN0dB;
ff << Name("ber") << bit_error_rate;
ff.close();
return 0;
}
Vec< double > vec
Definition of double vector type.
Vec< bin > bvec
Definition of binary vector type.
Vec< std::complex< double > > cvec
Definition of complex<double> vector type.
vec pow(const double x, const vec &y)
Calculates x to the power of y (x^y)
double inv_dB(double x)
Inverse of decibel of x.
void RNG_randomize()
Set a random seed for all Random Number Generators in the current thread.
bin randb(void)
Generates a random bit (equally likely 0s and 1s)
vec linspace(double from, double to, int points)
linspace (works in the same way as the MATLAB version)
Include file for the IT++ communications module.
When you run this program, the output will look something like this:
Now simulating Eb/N0 value number 1 of 10
Now simulating Eb/N0 value number 2 of 10
Now simulating Eb/N0 value number 3 of 10
Now simulating Eb/N0 value number 4 of 10
Now simulating Eb/N0 value number 5 of 10
Now simulating Eb/N0 value number 6 of 10
Now simulating Eb/N0 value number 7 of 10
Now simulating Eb/N0 value number 8 of 10
Now simulating Eb/N0 value number 9 of 10
Now simulating Eb/N0 value number 10 of 10
Elapsed time = 0.460899 seconds
EbN0dB = [0 1 2 3 4 5 6 7 8 9] [dB]
BER = [0.07968 0.0559 0.03729 0.02294 0.01243 0.0058 0.0025 0.00076 0.00013 6e-05]
Saving results to ./qpsk_result_file.it
Now it is time to plot the simulation results in Matlab and compare with theory using the Matlab code below. The results will be stored in a file called "qpsk_result_file.it". Make sure load_it.m is in your Matlab path (look in /matlab) and that you run the example code below from the directory where qpsk_result_file.it is located
figure(1); clf;
load_it qpsk_result_file.it
h1 = semilogy(EbN0dB,ber,'*-r'); hold on
ebn0db = 0:.1:10;
ebn0 = 10.^(ebn0db/10);
h2 = semilogy(ebn0db,Pb);
set(gca,'fontname','times','fontsize',16);
xlabel('{\it E_b} / {\it N}_0 [dB]');
ylabel('BER')
title('QPSK on an AWGN Channel');
legend([h1 h2],'Simulation','Theory');
grid on;
vec erfc(const vec &x)
Complementary error function.
vec sqrt(const vec &x)
Square root of the elements.