Simulation of a convolutional encoder and decoder

In this example we will show how to use the convolutional encoder/decoder class in it++. The Viterbi decoder uses the soft received values.

#include <itpp/itcomm.h>
using namespace itpp;
//These lines are needed for use of cout and endl
using std::cout;
using std::endl;
int main()
{
//Scalars
int constraint_length, MaxNrofErrors, Nobits, MaxIterations, p, i;
double Ec, Eb;
//Vectors
ivec generators;
vec EbN0dB, EbN0, N0, ber, trans_symbols, rec_symbols;
bvec uncoded_bits, coded_bits, decoded_bits;
//Classes
BPSK bpsk;
BERC berc;
Convolutional_Code conv_code;
AWGN_Channel channel;
/*
Set up the convolutional encoder/decoder class:
The generators are given in octal form by adding a zero in front of the numbers.
In this example we will simulate a rate 1/3 code that is listed in J. G. Proakis,
"Digital communications". The encoder has constraint length 7.
*/
generators.set_size(3, false);
generators(0) = 0133;
generators(1) = 0145;
generators(2) = 0175;
constraint_length = 7;
conv_code.set_generator_polynomials(generators, constraint_length);
//Init: Calculate some simulation specific parameters:
Ec = 1.0;
EbN0dB = linspace(-2, 6, 5);
EbN0 = inv_dB(EbN0dB);
Eb = Ec / conv_code.get_rate();
N0 = Eb * pow(EbN0, -1);
MaxNrofErrors = 100;
Nobits = 10000;
MaxIterations = 10;
ber.set_size(EbN0dB.length(), false);
ber.clear();
//Randomize the random number generators.
RNG_randomize();
for (p = 0; p < EbN0dB.length(); p++) {
cout << "Now simulating point " << p + 1 << " out of " << EbN0dB.length() << endl;
berc.clear(); //Clear the bit error rate counter.
channel.set_noise(N0(p) / 2.0); //Set the noise value of the AWGN channel.
for (i = 0; i < MaxIterations; i++) {
uncoded_bits = randb(Nobits); //The uncoded bits.
coded_bits = conv_code.encode(uncoded_bits); //The convolutional encoder function.
bpsk.modulate_bits(coded_bits, trans_symbols); //The BPSK modulator.
rec_symbols = channel(trans_symbols); //The AWGN channel.
decoded_bits = conv_code.decode(rec_symbols); //The Viterbi decoder function.
berc.count(uncoded_bits, decoded_bits); //Count the errors.
ber(p) = berc.get_errorrate();
//Break the simulation on this point if sufficient number of bit errors were observed:
if (berc.get_errors() > MaxNrofErrors) {
cout << "Breaking on point " << p + 1 << " with " << berc.get_errors() << " errors." << endl;
break;
}
}
}
//Print the results:
cout << "BER = " << ber << endl;
cout << "EbN0dB = " << EbN0dB << endl;
//Exit program:
return 0;
}
itpp::AWGN_Channel
Ordinary AWGN Channel for cvec or vec inputs and outputs.
Definition channel.h:1089
itpp::AWGN_Channel::set_noise
void set_noise(double noisevar)
Set noise variance (for complex-valued channels the sum of real and imaginary parts).
Definition channel.h:1094
itpp::BERC
Bit Error Rate Counter (BERC) Class.
Definition error_counters.h:68
itpp::BERC::clear
void clear()
Clears the bit error counter.
Definition error_counters.h:91
itpp::BERC::get_errors
double get_errors() const
Returns the counted number of bit errors.
Definition error_counters.h:97
itpp::BERC::get_errorrate
double get_errorrate() const
Returns the estimated bit error rate.
Definition error_counters.h:103
itpp::BERC::count
void count(const bvec &in1, const bvec &in2)
Cumulative error counter.
Definition error_counters.cpp:49
itpp::BPSK
BPSK modulator with real symbols.
Definition modulator.h:877
itpp::BPSK::modulate_bits
void modulate_bits(const bvec &bits, vec &output) const
Modulate bits into BPSK symbols in complex domain.
Definition modulator.cpp:310
itpp::Convolutional_Code
Binary Convolutional rate 1/n class.
Definition convcode.h:105
itpp::Convolutional_Code::get_rate
virtual double get_rate(void) const
Return rate of code (not including the rate-loss).
Definition convcode.h:279
itpp::Convolutional_Code::set_generator_polynomials
void set_generator_polynomials(const ivec &gen, int constraint_length)
Set generator polynomials. Given in Proakis integer form.
Definition convcode.cpp:555
itpp::Convolutional_Code::decode
virtual void decode(const bvec &coded_bits, bvec &decoded_bits)
Decode a bvec of coded data.
Definition convcode.cpp:735
itpp::Convolutional_Code::encode
virtual void encode(const bvec &input, bvec &output)
Encode an input binary vector using specified method (Tail by default).
Definition convcode.cpp:622
itpp::pow
vec pow(const double x, const vec &y)
Calculates x to the power of y (x^y).
Definition log_exp.h:176
itpp::inv_dB
double inv_dB(double x)
Inverse of decibel of x.
Definition log_exp.h:73
itpp::RNG_randomize
void RNG_randomize()
Set a random seed for all Random Number Generators in the current thread.
Definition random.cpp:254
itpp::randb
bin randb(void)
Generates a random bit (equally likely 0s and 1s).
Definition random.h:793
itpp::linspace
vec linspace(double from, double to, int points)
linspace (works in the same way as the MATLAB version)
Definition specmat.cpp:106
itcomm.h
Include file for the IT++ communications module.
itpp
itpp namespace
Definition itmex.h:37

When you run this program, the output will look something like this:

Now simulating point 1 out of 5
Breaking on point 1 with 3297 errors.
Now simulating point 2 out of 5
Breaking on point 2 with 781 errors.
Now simulating point 3 out of 5
Breaking on point 3 with 112 errors.
Now simulating point 4 out of 5
Now simulating point 5 out of 5
BER = [0.330858 0.0783743 0.00280983 0 0]
EbN0dB = [-2 0 2 4 6]

Generated on for IT++ by Doxygen 1.15.0