Пример #1
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;
}
Пример #2
0
int main(void)
{
    //general parameters
    string mud_method = "maxlogTMAP";
    int nb_usr = 2;
    int spreading_factor = 16;
#ifdef USE_CC
    string map_metric="maxlogMAP";
    ivec gen = "037 021";
    int constraint_length = 5;
    spreading_factor = 8;
#endif
    double threshold_value = 50;
    int ch_nb_taps = 4;//number of channel multipaths
    int nb_errors_lim = 1500;
    int nb_bits_lim = int(1e3);//int(1e6);
    int perm_len = 1024;//38400;//permutation length
    int nb_iter = 15;//number of iterations in the turbo decoder
    vec EbN0_dB = "10";//"0:10:20";
    double Ec = 1.0;//chip energy

#ifdef USE_CC
    int inv_R = spreading_factor*gen.length();
#else
    int inv_R = spreading_factor;
#endif
    double R = 1.0/double(inv_R);//coding rate

    //other parameters
    string filename = "IDMA_"+mud_method+"_"+to_str(nb_usr)+".it";
#ifdef USE_CC
    filename = "cc"+filename;
#endif
    filename = "Res/"+filename;
    int nb_bits = perm_len/inv_R;//number of bits in a block
    vec sigma2 = (0.5*Ec/R)*pow(inv_dB(EbN0_dB), -1.0);//N0/2
    int nb_blocks = 0;//number of blocks
    int nb_errors = 0;//number of errors
    bmat bits(nb_usr,nb_bits);//data bits
#ifdef USE_CC
    bvec coded_bits(nb_bits*gen.length());
    vec mod_bits(nb_bits*gen.length());
#else
    vec mod_bits(nb_bits);
#endif
    vec chips(perm_len);
    imat perm(nb_usr,perm_len);
    imat inv_perm(nb_usr,perm_len);
    vec em(perm_len);
    vec rec(perm_len+ch_nb_taps-1);//padding zeros are added

    //SISO MUD
    mat mud_apriori_data(nb_usr,perm_len);
    mat mud_extrinsic_data;

    //SISO decoder (scrambler or CC)
    vec dec_intrinsic_coded(perm_len);
    vec dec_apriori_data(nb_bits);
    dec_apriori_data.zeros();//always zero
    vec dec_extrinsic_coded;
    vec dec_extrinsic_data;

    //decision
    bvec rec_bits(nb_bits);
    int snr_len = EbN0_dB.length();
    mat ber(nb_iter,snr_len);
    ber.zeros();
    register int en,n,u;

#ifdef USE_CC
    //CC
    Convolutional_Code nsc;
    nsc.set_generator_polynomials(gen, constraint_length);
#endif

    //BPSK
    BPSK bpsk;

    //scrambler pattern
    vec pattern = kron(ones(spreading_factor/2), vec("1.0 -1.0"));

    //AWGN
    AWGN_Channel awgn;
    //multipath channel impulse response (Rayleigh fading) with real coefficients
    vec single_ch(ch_nb_taps);
    mat ch_imp_response(nb_usr, ch_nb_taps);
    vec ini_state = zeros(ch_nb_taps);
    MA_Filter<double,double,double> multipath_channel(ini_state);
    multipath_channel.set_state(ini_state);//initial state is always 0 due to Zero Padding technique
    vec padding_zeros = zeros(ch_nb_taps-1);

    //SISO blocks
    SISO siso;
    siso.set_scrambler_pattern(pattern);
    siso.set_mud_method(mud_method);
#ifdef USE_CC
    siso.set_generators(gen, constraint_length);
    siso.set_map_metric(map_metric);
    siso.set_tail(false);
#endif

    //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++)
    {
        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
        {
            rec.zeros();
            for (u=0; u<nb_usr; u++)
            {
                //permutation
                perm.set_row(u, sort_index(randu(perm_len)));
                //inverse permutation
                inv_perm.set_row(u, sort_index(perm.get_row(u)));

                //bits generation
                bits.set_row(u, randb(nb_bits));

#ifdef USE_CC
                //convolutional code
                nsc.encode(bits.get_row(u), coded_bits);//no tail

                //BPSK modulation (1->-1,0->+1)
                mod_bits = bpsk.modulate_bits(coded_bits);
#else
                //BPSK modulation (1->-1,0->+1)
                mod_bits = bpsk.modulate_bits(bits.get_row(u));
#endif

                //scrambler
                chips = kron(mod_bits, pattern);

                //permutation
                em = chips(perm.get_row(u));

                //multipath channel
                single_ch = randray(ch_nb_taps);
                single_ch /= sqrt(sum_sqr(single_ch));//normalized power profile
                ch_imp_response.set_row(u, single_ch);
                multipath_channel.set_coeffs(ch_imp_response.get_row(u));
                rec += multipath_channel(concat(em, padding_zeros));//Zero Padding
            }
            rec = awgn(rec);

            //turbo MUD
            mud_apriori_data.zeros();//a priori LLR of emitted symbols
            siso.set_impulse_response(ch_imp_response);
            for (n=0; n<nb_iter; n++)
            {
                //MUD
                siso.mud(mud_extrinsic_data, rec, mud_apriori_data);
                berc.clear();//mean error rate over all users
                for (u=0; u<nb_usr; u++)
                {
                    //deinterleave
                    dec_intrinsic_coded = mud_extrinsic_data.get_row(u)(inv_perm.get_row(u));
#ifdef USE_CC
                    //decoder+descrambler
                    siso.nsc(dec_extrinsic_coded, dec_extrinsic_data, dec_intrinsic_coded, dec_apriori_data);
#else
                    //descrambler
                    siso.descrambler(dec_extrinsic_coded, dec_extrinsic_data, dec_intrinsic_coded, dec_apriori_data);
#endif
                    //decision
                    rec_bits = bpsk.demodulate_bits(-dec_extrinsic_data);//suppose that a priori info is zero
                    //count errors
                    berc.count(bits.get_row(u), rec_bits);
                    //interleave+threshold
                    mud_apriori_data.set_row(u, threshold(dec_extrinsic_coded(perm.get_row(u)), threshold_value));
                }
                ber(n,en) += berc.get_errorrate();
            }//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();

    //save results to file
#ifdef TO_FILE
    it_file ff(filename);
    ff << Name("BER") << ber;
    ff << Name("EbN0_dB") << EbN0_dB;
    ff << Name("nb_usr") << nb_usr;
    ff << Name("gen") << spreading_factor;
    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;
#ifdef USE_CC
    ff << Name("gen") << gen;
#endif
    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;
}
Пример #4
0
int main(int argc, char *argv[])
{
	//receiver parameters
	ivec gen = "0133 0171";
	int constraint_length = 7;
	int const_size = 16;//constellation size
	int coherence_time = 512;//expressed in symbol durations, multiple of T, T<=coherence_time<=tx_duration, T is the ST code duration
	double threshold_value = 50;
	string map_metric="maxlogMAP";
	string demapper_method = "mmsePIC";//Hassibi_maxlogMAP or GA or sGA or mmsePIC or zfPIC or Alamouti_maxlogMAP
	int nb_errors_lim = 1500;
	int nb_bits_lim = int(1e6);
	int perm_len = pow2i(14);//permutation length
	int nb_iter = 5;//number of iterations in the turbo decoder
	int rec_antennas = 2;//number of reception antennas
	vec EbN0_dB = "0:20";
	double Es = 1.0;//mean symbol energy
	int em_antennas = 2;//number of emission antennas
	int channel_uses = 1;//ST code duration
	string code_name = "V-BLAST_MxN";//V-BLAST_MxN, Golden_2x2, Damen_2x2, Alamouti_2xN
	bool ideal_channel = false;
	bool to_file = false;

	//get parameters if any
	if (EXIT_FAILURE == get_opts(argc, argv, demapper_method, const_size,
			nb_errors_lim, nb_bits_lim, perm_len, nb_iter, rec_antennas, em_antennas,
			channel_uses, code_name, ideal_channel, to_file, EbN0_dB))
	{
		print_help(argv[0]);
		return EXIT_FAILURE;
	}

	//convolutional code generator polynomials
	Convolutional_Code nsc;
	nsc.set_generator_polynomials(gen, constraint_length);
	double coding_rate = 1.0/2.0;

	//QAM modulator class
	QAM mod(const_size);

	//Space-Time code parameters
	STC st_block_code(code_name, const_size, em_antennas, channel_uses);//generate matrices for LD code (following Hassibi's approach)
	int symb_block = st_block_code.get_nb_symbols_per_block();
	em_antennas = st_block_code.get_nb_emission_antenna();//these parameters could by changed depending on the selected code
	channel_uses = st_block_code.get_channel_uses();

	//recompute interleaver length
	int G = coherence_time*mod.bits_per_symbol()*symb_block;
	perm_len = G*(perm_len/G);//recompute interleaver length
	int block_len = int(coding_rate*perm_len);//informational block length
	int nb_symb = perm_len/mod.bits_per_symbol();//number of symbols at the modulator output
	int nb_subblocks = nb_symb/symb_block;//number of blocks of ST code emitted in an interleaver period
	int tx_duration = channel_uses*nb_subblocks;//transmission duration expressed in number of symbol periods

	//show configuration parameters
	std::cout << "const_size = " << const_size
	<< "\ndemapper_method = " << demapper_method
	<< "\nnb_errors_lim = " << nb_errors_lim
	<< "\nnb_bits_lim = " << nb_bits_lim
	<< "\nperm_len = " << perm_len
	<< "\ncode_name = " << code_name
	<< "\nem_antennas = " << em_antennas
	<< "\nchannel_uses = " << channel_uses
	<< "\nnb_iter = " << nb_iter
	<< "\nrec_antennas = " << rec_antennas
	<< "\nEbN0_dB = " << EbN0_dB << std::endl;
	if (true == ideal_channel)
	{
		if (em_antennas == rec_antennas)
		{
			std::cout << "Using ideal AWGN MIMO channel (no MAI)" << std::endl;
		} else
		{
			std::cout << "Warning: cannot use ideal AWGN MIMO channel" << std::endl;
			ideal_channel = false;
		}
	} else
	{
		std::cout << "Using random MIMO channel (with MAI)" << std::endl;
	}

	//fading channel parameters
	if (coherence_time%channel_uses)//check if the coherence time is a multiple of channel_uses
	{
		coherence_time = channel_uses*(coherence_time/channel_uses);
		std::cout << "Warning! The coherence time must be a multiple of T. Choosing coherence_time=channel_uses*floor(coherence_time/channel_uses) = "\
				<< coherence_time << std::endl;
	}
	if (coherence_time>tx_duration)
	{
		coherence_time = channel_uses*(tx_duration/channel_uses);
		std::cout << "Warning! The coherence time must be <= tx_duration. Choosing coherence_time = channel_uses*floor(tx_duration/channel_uses) = "\
				<< coherence_time << std::endl;
	}
	cmat fading_pattern = ones_c(1, coherence_time/channel_uses);

	//other parameters
	string filename = "STBICM_"+map_metric+"_"+demapper_method+".it";
	if (true == to_file)
	{
		std::cout << "Saving results to " << filename << std::endl;
	}
	double R = coding_rate*double(mod.bits_per_symbol()*symb_block)/double(channel_uses);//ST code rate in (info.) bits/channel use
	vec sigma2 = (0.5*Es/(R*double(mod.bits_per_symbol())))*pow(inv_dB(EbN0_dB), -1.0);//N0/2
	int nb_blocks;//number of blocks
	int nb_errors;
	bvec bits(block_len);//data bits
	bvec coded_bits(perm_len);//no tail
	cvec em(nb_symb);
	ivec perm(perm_len);
	ivec inv_perm(perm_len);
	//SISO demapper
	vec demapper_apriori_data(perm_len);
	vec demapper_extrinsic_data(perm_len);
	//SISO NSC
	vec nsc_intrinsic_coded(perm_len);
	vec nsc_apriori_data(block_len);
	nsc_apriori_data.zeros();//always zero
	vec nsc_extrinsic_coded(perm_len);
	vec nsc_extrinsic_data(block_len);
	//decision
	bvec rec_bits(block_len);
	int snr_len = EbN0_dB.length();
	mat ber(nb_iter,snr_len);
	ber.zeros();
	register int en,n,ns;
	cmat S(tx_duration, em_antennas);
	cmat rec(tx_duration,rec_antennas);

	//Rayleigh fading
	cmat ch_attenuations(em_antennas*rec_antennas,tx_duration/channel_uses);

	//SISO blocks
	SISO siso;
	siso.set_map_metric(map_metric);
	siso.set_generators(gen, constraint_length);
	siso.set_demapper_method(demapper_method);
	siso.set_constellation(mod.bits_per_symbol(), mod.get_symbols()/sqrt(em_antennas), mod.get_bits2symbols());
	siso.set_st_block_code(st_block_code.get_nb_symbols_per_block(), st_block_code.get_1st_gen_matrix(), st_block_code.get_2nd_gen_matrix(), rec_antennas);

	//decision
	BPSK bpsk;

	//BER
	BERC berc;

	//Randomize generators
	RNG_randomize();

	//main loop
	std::cout << std::endl;
	for (en=0;en<snr_len;en++)
	{
		std::cout << "EbN0_dB = " << EbN0_dB[en] << std::endl;
		siso.set_noise(sigma2(en));
		nb_errors = 0;
		nb_blocks = 0;
		while ((nb_errors<nb_errors_lim) && (nb_blocks*block_len<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(block_len);

			//convolutional code
			nsc.encode(bits, coded_bits);//no tail

			//permutation+QAM modulation
			em = mod.modulate_bits(coded_bits(perm))/sqrt(em_antennas);//normalize emitted symbols

			//ST code
			S = st_block_code.encode(em);

			/* channel matrices (there are tx_duration/tau_c different channel matrices MxN)
			 * a channel matrix is represented as a M*Nx1 vector (first M elements are the first column of the channel matrix)
			 * the channel matrix is constant over tau_c symbol periods (a multiple of T symbol durations)
			 * the channel matrix is the transpose of the true channel matrix
			 */
			if (false == ideal_channel)
			{
				ch_attenuations = kron(randn_c(em_antennas*rec_antennas, tx_duration/coherence_time), fading_pattern);
			} else
			{
				cmat mimo_channel = kron(reshape(eye_c(em_antennas),
						em_antennas*rec_antennas, 1), ones_c(1, tx_duration/coherence_time));
				ch_attenuations = kron(mimo_channel, fading_pattern);
			}

			//flat-fading MIMO channel
			for (ns=0;ns<nb_subblocks;ns++)
				rec.set_submatrix(ns*channel_uses, 0, \
						S(ns*channel_uses, (ns+1)*channel_uses-1, 0, em_antennas-1)*reshape(ch_attenuations.get_col(ns), em_antennas, rec_antennas));
			rec += sqrt(2*sigma2(en))*randn_c(tx_duration,rec_antennas);//sigma2 is the variance on each dimension

			//turbo receiver
			demapper_apriori_data.zeros();//a priori information of emitted bits
			siso.set_impulse_response(ch_attenuations);
			for (n=0;n<nb_iter;n++)
			{
				//first decoder
				siso.demapper(demapper_extrinsic_data, rec, demapper_apriori_data);

				//deinterleave+threshold
				nsc_intrinsic_coded = SISO::threshold(demapper_extrinsic_data(inv_perm), threshold_value);

				//second decoder
				siso.nsc(nsc_extrinsic_coded, nsc_extrinsic_data, nsc_intrinsic_coded, nsc_apriori_data, false);

				//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);
				ber(n,en) += berc.get_errorrate();

				//interleave
				demapper_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);
	}

	if (true == 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("coding_rate") << coding_rate;
		ff << Name("nb_iter") << nb_iter;
		ff << Name("block_len") << block_len;
		ff << Name("nb_errors_lim") << nb_errors_lim;
		ff << Name("nb_bits_lim") << nb_bits_lim;
		ff << Name("const_size") << const_size;
		ff.close();
	} else
	{
		//show BER
		cout << "BER = " << ber << endl;
	}

	return 0;
}