In this example we will show how to use the convolutional encoder/decoder class in it++. The Viterbi decoder uses the soft received values.
using std::cout;
using std::endl;
int main()
{
int constraint_length, MaxNrofErrors, Nobits, MaxIterations, p, i;
double Ec, Eb;
ivec generators;
vec EbN0dB, EbN0, N0, ber, trans_symbols, rec_symbols;
bvec uncoded_bits, coded_bits, decoded_bits;
BPSK bpsk;
BERC berc;
Convolutional_Code conv_code;
AWGN_Channel channel;
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);
Ec = 1.0;
Eb = Ec / conv_code.get_rate();
MaxNrofErrors = 100;
Nobits = 10000;
MaxIterations = 10;
ber.set_size(EbN0dB.length(), false);
ber.clear();
for (p = 0; p < EbN0dB.length(); p++) {
cout << "Now simulating point " << p + 1 << " out of " << EbN0dB.length() << endl;
berc.clear();
channel.set_noise(N0(p) / 2.0);
for (i = 0; i < MaxIterations; i++) {
uncoded_bits =
randb(Nobits);
coded_bits = conv_code.encode(uncoded_bits);
bpsk.modulate_bits(coded_bits, trans_symbols);
rec_symbols = channel(trans_symbols);
decoded_bits = conv_code.decode(rec_symbols);
berc.count(uncoded_bits, decoded_bits);
ber(p) = berc.get_errorrate();
if (berc.get_errors() > MaxNrofErrors) {
cout << "Breaking on point " << p + 1 << " with " << berc.get_errors() << " errors." << endl;
break;
}
}
}
cout << "BER = " << ber << endl;
cout << "EbN0dB = " << EbN0dB << endl;
return 0;
}
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.
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]