bvec encode(Convolutional_Code& nsc, int constraint_length, const bvec& encoder_input, int blockSize, bool verbose) { if (verbose) {cout << "input : " << encoder_input << endl;} int codedLen = 2 * (blockSize + (constraint_length - 1)); int nBlocks = encoder_input.length() / blockSize; ivec window(blockSize); for (int j = 0; j < blockSize; j++) { window[j] = j; } bvec nsc_coded_bits(codedLen); bvec tr_coded_bits; for (int j = 0; j < nBlocks; j++) { nsc.encode_tail(encoder_input(window), nsc_coded_bits); window = window + blockSize; tr_coded_bits = concat(tr_coded_bits, nsc_coded_bits); } // Deal with residual sources if remainder exsists if (nBlocks*blockSize != encoder_input.length()) { bvec residual_bits = encoder_input.get(nBlocks*blockSize, encoder_input.length()-1); nsc.encode_tail(residual_bits, nsc_coded_bits); tr_coded_bits = concat(tr_coded_bits, nsc_coded_bits); } if (verbose) {cout << "encoder output: " << tr_coded_bits << endl;} return tr_coded_bits; }
int main(int argc, char **argv) { // -- modulation and channel parameters (taken from command line input) -- int nC; // type of constellation (1=QPSK, 2=16-QAM, 3=64-QAM) int nRx; // number of receive antennas int nTx; // number of transmit antennas int Tc; // coherence time (number of channel vectors with same H) if (argc != 5) { cout << "Usage: cm nTx nRx nC Tc" << endl << "Example: cm 2 2 1 100000 (2x2 QPSK MIMO on slow fading channel)" << endl; exit(1); } else { sscanf(argv[1], "%i", &nTx); sscanf(argv[2], "%i", &nRx); sscanf(argv[3], "%i", &nC); sscanf(argv[4], "%i", &Tc); } cout << "Initializing.. " << nTx << " TX antennas, " << nRx << " RX antennas, " << (1 << nC) << "-PAM per dimension, coherence time " << Tc << endl; // -- simulation control parameters -- const vec EbN0db = "-5:0.5:50"; // SNR range const int Nmethods = 2; // number of demodulators to try const int Nbitsmax = 50000000; // maximum number of bits to ever simulate per SNR point const int Nu = 1000; // length of data packet (before applying channel coding) int Nbers, Nfers; // target number of bit/frame errors per SNR point double BERmin, FERmin; // BER/FER at which to terminate simulation if (Tc == 1) { // Fast fading channel, BER is of primary interest BERmin = 0.001; // stop simulating a given method if BER<this value FERmin = 1.0e-10; // stop simulating a given method if FER<this value Nbers = 1000; // move to next SNR point after counting 1000 bit errors Nfers = 200; // do not stop on this condition } else { // Slow fading channel, FER is of primary interest here BERmin = 1.0e-15; // stop simulating a given method if BER<this value FERmin = 0.01; // stop simulating a given method if FER<this value Nbers = -1; // do not stop on this condition Nfers = 200; // move to next SNR point after counting 200 frame errors } // -- Channel code parameters -- Convolutional_Code code; ivec generator(3); generator(0) = 0133; // use rate 1/3 code generator(1) = 0165; generator(2) = 0171; double rate = 1.0 / 3.0; code.set_generator_polynomials(generator, 7); bvec dummy; code.encode_tail(randb(Nu), dummy); const int Nc = length(dummy); // find out how long the coded blocks are // ============= Initialize ==================================== const int Nctx = (int)(2 * nC * nTx * ceil(double(Nc) / double(2 * nC * nTx))); // Total number of bits to transmit const int Nvec = Nctx / (2 * nC * nTx); // Number of channel vectors to transmit const int Nbitspvec = 2 * nC * nTx; // Number of bits per channel vector // initialize MIMO channel with uniform QAM per complex dimension and Gray coding ND_UQAM chan; chan.set_M(nTx, 1 << (2*nC)); cout << chan << endl; // initialize interleaver Sequence_Interleaver<bin> sequence_interleaver_b(Nctx); Sequence_Interleaver<int> sequence_interleaver_i(Nctx); sequence_interleaver_b.randomize_interleaver_sequence(); sequence_interleaver_i.set_interleaver_sequence(sequence_interleaver_b.get_interleaver_sequence()); // RNG_randomize(); Array<cvec> Y(Nvec); // received data Array<cmat> H(Nvec / Tc + 1); // channel matrix (new matrix for each coherence interval) ivec Contflag = ones_i(Nmethods); // flag to determine whether to run a given demodulator if (pow(2.0, nC*2.0*nTx) > 256) { // ML decoder too complex.. Contflag(1) = 0; } if (nTx > nRx) { Contflag(0) = 0; // ZF not for underdetermined systems } cout << "Running methods: " << Contflag << endl; cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout.precision(5); // ================== Run simulation ======================= for (int nsnr = 0; nsnr < length(EbN0db); nsnr++) { const double Eb = 1.0; // transmitted energy per information bit const double N0 = inv_dB(-EbN0db(nsnr)); const double sigma2 = N0; // Variance of each scalar complex noise sample const double Es = rate * 2 * nC * Eb; // Energy per complex scalar symbol // (Each transmitted scalar complex symbol contains rate*2*nC // information bits.) const double Ess = sqrt(Es); Array<BERC> berc(Nmethods); // counter for coded BER Array<BERC> bercu(Nmethods); // counter for uncoded BER Array<BLERC> ferc(Nmethods); // counter for coded FER for (int i = 0; i < Nmethods; i++) { ferc(i).set_blocksize(Nu); } long int nbits = 0; while (nbits < Nbitsmax) { nbits += Nu; // generate and encode random data bvec inputbits = randb(Nu); bvec txbits; code.encode_tail(inputbits, txbits); // coded block length is not always a multiple of the number of // bits per channel vector txbits = concat(txbits, randb(Nctx - Nc)); txbits = sequence_interleaver_b.interleave(txbits); // -- generate channel and data ---- for (int k = 0; k < Nvec; k++) { /* A complex valued channel matrix is used here. An alternative (with equivalent result) would be to use a real-valued (structured) channel matrix of twice the dimension. */ if (k % Tc == 0) { // generate a new channel realization every Tc intervals H(k / Tc) = Ess * randn_c(nRx, nTx); } // modulate and transmit bits bvec bitstmp = txbits(k * 2 * nTx * nC, (k + 1) * 2 * nTx * nC - 1); cvec x = chan.modulate_bits(bitstmp); cvec e = sqrt(sigma2) * randn_c(nRx); Y(k) = H(k / Tc) * x + e; } // -- demodulate -- Array<QLLRvec> LLRin(Nmethods); for (int i = 0; i < Nmethods; i++) { LLRin(i) = zeros_i(Nctx); } QLLRvec llr_apr = zeros_i(nC * 2 * nTx); // no a priori input to demodulator QLLRvec llr_apost = zeros_i(nC * 2 * nTx); for (int k = 0; k < Nvec; k++) { // zero forcing demodulation if (Contflag(0)) { chan.demodulate_soft_bits(Y(k), H(k / Tc), sigma2, llr_apr, llr_apost, ND_UQAM::ZF_LOGMAP); LLRin(0).set_subvector(k*Nbitspvec, llr_apost); } // ML demodulation if (Contflag(1)) { chan.demodulate_soft_bits(Y(k), H(k / Tc), sigma2, llr_apr, llr_apost); LLRin(1).set_subvector(k*Nbitspvec, llr_apost); } } // -- decode and count errors -- for (int i = 0; i < Nmethods; i++) { bvec decoded_bits; if (Contflag(i)) { bercu(i).count(txbits(0, Nc - 1), LLRin(i)(0, Nc - 1) < 0); // uncoded BER LLRin(i) = sequence_interleaver_i.deinterleave(LLRin(i), 0); // QLLR values must be converted to real numbers since the convolutional decoder wants this vec llr = chan.get_llrcalc().to_double(LLRin(i).left(Nc)); // llr=-llr; // UNCOMMENT THIS LINE IF COMPILING WITH 3.10.5 OR EARLIER (BEFORE HARMONIZING LLR CONVENTIONS) code.decode_tail(llr, decoded_bits); berc(i).count(inputbits(0, Nu - 1), decoded_bits(0, Nu - 1)); // coded BER ferc(i).count(inputbits(0, Nu - 1), decoded_bits(0, Nu - 1)); // coded FER } } /* Check whether it is time to terminate the simulation. Terminate when all demodulators that are still running have counted at least Nbers or Nfers bit/frame errors. */ int minber = 1000000; int minfer = 1000000; for (int i = 0; i < Nmethods; i++) { if (Contflag(i)) { minber = min(minber, round_i(berc(i).get_errors())); minfer = min(minfer, round_i(ferc(i).get_errors())); } } if (Nbers > 0 && minber > Nbers) { break;} if (Nfers > 0 && minfer > Nfers) { break;} } cout << "-----------------------------------------------------" << endl; cout << "Eb/N0: " << EbN0db(nsnr) << " dB. Simulated " << nbits << " bits." << endl; cout << " Uncoded BER: " << bercu(0).get_errorrate() << " (ZF); " << bercu(1).get_errorrate() << " (ML)" << endl; cout << " Coded BER: " << berc(0).get_errorrate() << " (ZF); " << berc(1).get_errorrate() << " (ML)" << endl; cout << " Coded FER: " << ferc(0).get_errorrate() << " (ZF); " << ferc(1).get_errorrate() << " (ML)" << endl; cout.flush(); /* Check wheter it is time to terminate simulation. Stop when all methods have reached the min BER/FER of interest. */ int contflag = 0; for (int i = 0; i < Nmethods; i++) { if (Contflag(i)) { if (berc(i).get_errorrate() > BERmin) { contflag = 1; } else { Contflag(i) = 0; } if (ferc(i).get_errorrate() > FERmin) { contflag = 1; } else { Contflag(i) = 0; } } } if (contflag) { continue; } else {break; } } return 0; }
int main(void) { //general parameters double threshold_value = 50; string map_metric="logMAP"; ivec gen = "037 021";//octal form int constraint_length = 5; int nb_errors_lim = 1500; int nb_bits_lim = int(1e6); int perm_len = pow2i(14);//permutation length int nb_iter = 10;//number of iterations in the turbo decoder vec EbN0_dB = "0:0.1:5"; double R = 1.0/4.0;//coding rate (non punctured SCCC) double Ec = 1.0;//coded bit energy //other parameters string filename = "Res/sccc_"+map_metric+".it"; int nb_bits_tail = perm_len/gen.length(); int nb_bits = nb_bits_tail-(constraint_length-1);//number of bits in a block (without tail) vec sigma2 = (0.5*Ec/R)*pow(inv_dB(EbN0_dB), -1.0);//N0/2 double Lc;//scaling factor for intrinsic information int nb_blocks;//number of blocks int nb_errors; bvec bits(nb_bits);//data bits bvec nsc_coded_bits;//tail is added bmat rsc_parity_bits; ivec perm(perm_len); ivec inv_perm(perm_len); int rec_len = gen.length()*perm_len; bvec coded_bits(rec_len); vec rec(rec_len); //SISO RSC vec rsc_intrinsic_coded(rec_len); vec rsc_apriori_data(perm_len); vec rsc_extrinsic_coded; vec rsc_extrinsic_data; //SISO NSC vec nsc_intrinsic_coded(perm_len); vec nsc_apriori_data(nb_bits_tail); nsc_apriori_data.zeros();//always zero vec nsc_extrinsic_coded; vec nsc_extrinsic_data; //decision bvec rec_bits(nb_bits_tail); int snr_len = EbN0_dB.length(); mat ber(nb_iter,snr_len); ber.zeros(); register int en,n; //Non recursive non Systematic Convolutional Code Convolutional_Code nsc; nsc.set_generator_polynomials(gen, constraint_length); //Recursive Systematic Convolutional Code Rec_Syst_Conv_Code rsc; rsc.set_generator_polynomials(gen, constraint_length);//initial state should be the zero state //BPSK modulator BPSK bpsk; //AWGN channel AWGN_Channel channel; //SISO blocks SISO siso; siso.set_generators(gen, constraint_length); siso.set_map_metric(map_metric); //BER BERC berc; //Progress timer tr::Progress_Timer timer; timer.set_max(snr_len); //Randomize generators RNG_randomize(); //main loop timer.progress(0.0); for (en=0;en<snr_len;en++) { channel.set_noise(sigma2(en)); Lc = -2.0/sigma2(en);//take into account the BPSK mapping nb_errors = 0; nb_blocks = 0; while ((nb_errors<nb_errors_lim) && (nb_blocks*nb_bits<nb_bits_lim))//if at the last iteration the nb. of errors is inferior to lim, then process another block { //permutation perm = sort_index(randu(perm_len)); //inverse permutation inv_perm = sort_index(perm); //bits generation bits = randb(nb_bits); //serial concatenated convolutional code nsc.encode_tail(bits, nsc_coded_bits);//tail is added here to information bits to close the trellis nsc_coded_bits = nsc_coded_bits(perm);//interleave rsc.encode(nsc_coded_bits, rsc_parity_bits);//no tail added for(n=0;n<perm_len;n++) { coded_bits(2*n) = nsc_coded_bits(n);//systematic output coded_bits(2*n+1) = rsc_parity_bits(n,0);//parity output } //BPSK modulation (1->-1,0->+1) + channel rec = channel(bpsk.modulate_bits(coded_bits)); //turbo decoder rsc_intrinsic_coded = Lc*rec;//intrinsic information of coded bits rsc_apriori_data.zeros();//a priori LLR for information bits for (n=0;n<nb_iter;n++) { //first decoder siso.rsc(rsc_extrinsic_coded, rsc_extrinsic_data, rsc_intrinsic_coded, rsc_apriori_data, false); //deinterleave+threshold nsc_intrinsic_coded = threshold(rsc_extrinsic_data(inv_perm), threshold_value); //second decoder siso.nsc(nsc_extrinsic_coded, nsc_extrinsic_data, nsc_intrinsic_coded, nsc_apriori_data, true); //decision rec_bits = bpsk.demodulate_bits(-nsc_extrinsic_data);//suppose that a priori info is zero //count errors berc.clear(); berc.count(bits, rec_bits.left(nb_bits)); ber(n,en) += berc.get_errorrate(); //interleave rsc_apriori_data = nsc_extrinsic_coded(perm); }//end iterations nb_errors += int(berc.get_errors());//get number of errors at the last iteration nb_blocks++; }//end blocks (while loop) //compute BER over all tx blocks ber.set_col(en, ber.get_col(en)/nb_blocks); //show progress timer.progress(1+en); } timer.toc_print(); #ifdef TO_FILE //save results to file it_file ff(filename); ff << Name("BER") << ber; ff << Name("EbN0_dB") << EbN0_dB; ff << Name("gen") << gen; ff << Name("R") << R; ff << Name("nb_iter") << nb_iter; ff << Name("total_nb_bits") << nb_bits; ff << Name("nb_errors_lim") << nb_errors_lim; ff << Name("nb_bits_lim") << nb_bits_lim; ff.close(); #else //show BER cout << ber << endl; #endif return 0; }
int main(void) { //general parameters double threshold_value = 50; string map_metric = "maxlogMAP"; ivec gen = "07 05";//octal notation int constraint_length = 3; int ch_nb_taps = 4;//number of channel multipaths int nb_errors_lim = 3000; int nb_bits_lim = int(1e6); int perm_len = pow2i(14);//permutation length int nb_iter = 10;//number of iterations in the turbo decoder vec EbN0_dB = "0:0.5:10"; double R = 1.0 / 2.0;//coding rate of FEC double Ec = 1.0;//coded bit energy #ifdef USE_PRECODER ivec prec_gen = "03 02";//octal notation int prec_gen_length = 2; #endif //other parameters int nb_bits_tail = perm_len / gen.length(); int nb_bits = nb_bits_tail - (constraint_length - 1);//number of bits in a block (without tail) vec sigma2 = (0.5 * Ec / R) * pow(inv_dB(EbN0_dB), -1.0);//N0/2 int nb_blocks;//number of blocks int nb_errors; bvec bits(nb_bits);//data bits bvec nsc_coded_bits(perm_len);//tail is added bvec em_bits(perm_len); bmat parity_bits; ivec perm(perm_len); ivec inv_perm(perm_len); vec rec(perm_len); //SISO equalizer vec eq_apriori_data(perm_len); vec eq_extrinsic_data; //SISO NSC vec nsc_intrinsic_coded(perm_len); vec nsc_apriori_data(nb_bits_tail); nsc_apriori_data.zeros();//always zero vec nsc_extrinsic_coded; vec nsc_extrinsic_data; //decision bvec rec_bits(nb_bits_tail); int snr_len = EbN0_dB.length(); mat ber(nb_iter, snr_len); ber.zeros(); register int en, n; //CCs Convolutional_Code nsc; nsc.set_generator_polynomials(gen, constraint_length); #ifdef USE_PRECODER Rec_Syst_Conv_Code prec; prec.set_generator_polynomials(prec_gen, prec_gen_length); #endif //BPSK BPSK bpsk; //AWGN AWGN_Channel awgn; //multipath channel impulse response (Rayleigh fading) with real coefficients vec ch_imp_response(ch_nb_taps); vec ini_state = ones(ch_nb_taps);//initial state is zero MA_Filter<double, double, double> multipath_channel; //SISO blocks SISO siso; siso.set_generators(gen, constraint_length); siso.set_map_metric(map_metric); #ifdef USE_PRECODER siso.set_precoder_generator(prec_gen(0), prec_gen_length); #endif //BER BERC berc; //Randomize generators RNG_randomize(); //main loop for (en = 0;en < snr_len;en++) { cout << "EbN0_dB = " << EbN0_dB(en) << endl; awgn.set_noise(sigma2(en)); siso.set_noise(sigma2(en)); nb_errors = 0; nb_blocks = 0; while ((nb_errors < nb_errors_lim) && (nb_blocks*nb_bits < nb_bits_lim))//if at the last iteration the nb. of errors is inferior to lim, then process another block { //permutation perm = sort_index(randu(perm_len)); //inverse permutation inv_perm = sort_index(perm); //bits generation bits = randb(nb_bits); //convolutional code nsc.encode_tail(bits, nsc_coded_bits);//tail is added here to information bits to close the trellis //permutation em_bits = nsc_coded_bits(perm); #ifdef USE_PRECODER //precoder prec.encode(em_bits, parity_bits); em_bits = parity_bits.get_col(0); #endif //BPSK modulation (1->-1,0->+1) + multipath channel ch_imp_response = randray(ch_nb_taps); ch_imp_response /= sqrt(sum_sqr(ch_imp_response));//normalized power profile multipath_channel.set_coeffs(ch_imp_response); multipath_channel.set_state(ini_state);//inital state is zero rec = awgn(multipath_channel(bpsk.modulate_bits(em_bits))); //turbo equalizer eq_apriori_data.zeros();//a priori information of emitted symbols siso.set_impulse_response(ch_imp_response); for (n = 0;n < nb_iter;n++) { //first decoder siso.equalizer(eq_extrinsic_data, rec, eq_apriori_data, false);//no tail //deinterleave+threshold nsc_intrinsic_coded = SISO::threshold(eq_extrinsic_data(inv_perm), threshold_value); //second decoder siso.nsc(nsc_extrinsic_coded, nsc_extrinsic_data, nsc_intrinsic_coded, nsc_apriori_data, true);//tail //decision rec_bits = bpsk.demodulate_bits(-nsc_extrinsic_data);//assume that a priori info is zero //count errors berc.clear(); berc.count(bits, rec_bits.left(nb_bits)); ber(n, en) += berc.get_errorrate(); //interleave eq_apriori_data = nsc_extrinsic_coded(perm); }//end iterations nb_errors += int(berc.get_errors());//get number of errors at the last iteration nb_blocks++; }//end blocks (while loop) //compute BER over all tx blocks ber.set_col(en, ber.get_col(en) / nb_blocks); } //save results to file it_file ff("turbo_equalizer_bersim_multipath.it"); ff << Name("BER") << ber; ff << Name("EbN0_dB") << EbN0_dB; ff.close(); return 0; }