Example #1
0
File: timer.cpp Project: nvmd/itpp
int main()
{
  //Declare the scalars used:
  long i, sum, N;

  //Declare tt as an instance of the timer class:
  Real_Timer tt;

  //Initiate the variables:
  N = 1000000;
  sum = 0;

  //Start and reset the timer:
  tt.tic();

  //Do some processing
  for (i = 0; i < N; i++) {
    sum += i;
  }

  // Print the elapsed time
  tt.toc_print();

  //Print the result of the processing:
  cout << "The sum of all integers from 0 to " << N - 1 << " equals " << sum << endl;

  //Exit program:
  return 0;

}
Example #2
0
void pause(double t)
{
  if (t == -1) {
    std::cout << "(Press enter to continue)" << std::endl;
    getchar();
  }
  else {
    Real_Timer T;
    T.start();
    while (T.get_time() < t);
  }
}
Example #3
0
void MOG_diag_EM_sup::ml_iterate()
{
  using std::cout;
  using std::endl;
  using std::setw;
  using std::showpos;
  using std::noshowpos;
  using std::scientific;
  using std::fixed;
  using std::flush;
  using std::setprecision;

  double avg_log_lhood_old = -1.0 * std::numeric_limits<double>::max();

  Real_Timer tt;

  if (verbose) {
    cout << "MOG_diag_EM_sup::ml_iterate()" << endl;
    cout << setw(14) << "iteration";
    cout << setw(14) << "avg_loglhood";
    cout << setw(14) << "delta";
    cout << setw(10) << "toc";
    cout << endl;
  }

  for (int i = 0; i < max_iter; i++) {
    sanitise_params();
    update_internals();

    if (verbose) tt.tic();
    double avg_log_lhood_new = ml_update_params();

    if (verbose) {
      double delta = avg_log_lhood_new - avg_log_lhood_old;

      cout << noshowpos << fixed;
      cout << setw(14) << i;
      cout << showpos << scientific << setprecision(3);
      cout << setw(14) << avg_log_lhood_new;
      cout << setw(14) << delta;
      cout << noshowpos << fixed;
      cout << setw(10) << tt.toc();
      cout << endl << flush;
    }

    if (avg_log_lhood_new <= avg_log_lhood_old)  break;

    avg_log_lhood_old = avg_log_lhood_new;
  }
}
int main(int argc, char *argv[])
{
	int i,j,k;			//Counter variable
	
	bvec b_source_bits, b_decoded_bits, b_encoded_bits;
	ivec i_decoded_bits, i_decoded_symbols;
	cvec c_received_signals;
	vec  d_received_llr;
	ivec No_of_Errors, No_of_Bits, No_of_BlockErrors, No_of_Blocks;
	
	//Channel setup
	cvec channel_gains;
	TDL_Channel ray_channel; // default: uncorrelated Rayleigh fading channel
        AWGN_Channel awgn_channel; // AWGN channel
	
	//-------------------------------------------------------CONV
	//Convolutional codes module, CONV
	CONV conv;
	char *ctrlfile;
	if(argc==1) ctrlfile = "control.conv.par";
	else        ctrlfile = argv[1];
	
	conv.set_parameters_from_file(ctrlfile);
	conv.initialise();
	
	//result file
	FILE *f;  char *resultfile= conv.get_result_filename();
	f=fopen(resultfile, "w");
	
	//print out parameters
	conv.print_parameters(stdout);    
	conv.print_parameters(f);
	//-------------------------------------------------------CONV	
	
	// read simulation parameters
	FILE *sim_file;
	FILESERVICE fileser;
	sim_file = fopen("control.sim.par", "r");
	if(sim_file==NULL) it_error("control.sim.par not found");
	int max_nrof_frame = fileser.scan_integer(sim_file);
	double SNR_start  = fileser.scan_double(sim_file); 
	double SNR_step   = fileser.scan_double(sim_file);
	double SNR_end    = fileser.scan_double(sim_file);
	double G_sd        = fileser.scan_double(sim_file);
	double G_sr        = fileser.scan_double(sim_file);
	double G_rd        = fileser.scan_double(sim_file);
	int channel_type   = fileser.scan_integer(sim_file);
	int mapper_bps     = fileser.scan_integer(sim_file);
	fclose(sim_file);
	char text[100];
	sprintf( text, "%f:%f:%f", SNR_start, SNR_step, SNR_end );
	
	//SNR setup
	vec SNRdB = text;	//Setting the simulation Eb/N0 range in dB


	int M = 1<<mapper_bps;
        PSK psk(M);
	cvec source_frame;
	
	double rate = (double)mapper_bps * conv.get_rate(); 

	printf (  "! %d-PSK (mapper_bps=%d) :: Overall_Rate=%.4f\n!\n", M, mapper_bps, rate);
	fprintf(f,"! %d-PSK (mapper_bps=%d) :: Overall_Rate=%.4f\n!\n", M, mapper_bps, rate);

	vec SNR    = pow(10.0, SNRdB/10.0);
	vec N0     = 1.0/SNR;
	vec sigma2 = N0/2;

	
	BERC berc;			//BER counter
	BLERC blerc;			//FER counter
	Real_Timer tt;			//Time counter

	vec ber(SNRdB.length());	//allocate memory for vector to store BER
	vec bler(SNRdB.length());	//allocate memory for vector to store FER
	
	ber.clear();			//Clear up buffer of BER counter
	bler.clear();			//Clear up buffer of FER counter	
	
	blerc.set_blocksize((long)conv.get_info_bit_length());	//set blocksize of the FER counter
	
	tt.tic();					//Start timer
	
	//RNG_randomize();				//construct random source 
	RNG_reset(0);                                   //reset random seed 
	
	b_source_bits.set_size(conv.get_info_bit_length(), false);
	b_encoded_bits.set_size(conv.get_coded_bit_length(), false);
	c_received_signals.set_size(conv.get_sym_length(), false);
	d_received_llr.set_size(conv.get_coded_bit_length(), false);
	b_decoded_bits.set_size(conv.get_info_bit_length(), false);
	
	No_of_Errors.set_size(SNRdB.length(), false);		//Set the length
	No_of_Bits.set_size(SNRdB.length(), false);		//for ivectors storing the no	
	No_of_BlockErrors.set_size(SNRdB.length(),false);	//of errors bits or error frames
	No_of_Blocks.set_size(SNRdB.length(),false);
			
	No_of_Errors.clear();
	No_of_Bits.clear();
	No_of_BlockErrors.clear();
	No_of_Blocks.clear();
	
	printf (  "!SNR(dB)\tEbN0(dB)\tBER\t\tFER\t\tnrof Frames\n");
	fprintf(f,"!SNR(dB)\tEbN0(dB)\tBER\t\tFER\t\tnrof Frames\n");
	
	for(i=0; i< SNRdB.length(); i++)
	{
		//Set channel noise level
		awgn_channel.set_noise(N0(i));
		
		for(j=0;j<max_nrof_frame;j++)
		{			
		        //Generate random source bits
			b_source_bits = randb(conv.get_info_bit_length());
			
			//CONV encode
			conv.encode_bits(b_source_bits, b_encoded_bits);
			
			source_frame = psk.modulate_bits(b_encoded_bits);
			
			// Fast Rayleigh channel + AWGN transmission 
			channel_gains = my_channel(channel_type, source_frame.length(), G_sd, ray_channel); 
			c_received_signals = elem_mult(source_frame, channel_gains);
			c_received_signals = awgn_channel( c_received_signals );
			
			//Demodulation to get llr
			psk.demodulate_soft_bits(c_received_signals, channel_gains, N0(i), d_received_llr);
			//Convert to Log(Pr=1/Pr=0)
                        d_received_llr = -1 * d_received_llr;

			//CONV decode
			conv.assign_apr_codeword(d_received_llr);
			conv.decode(i_decoded_bits, i_decoded_symbols);

			b_decoded_bits = to_bvec(i_decoded_bits.left(conv.get_info_bit_length())); 
			// remove dummy bits if trellis/code termination is used
			
			berc.clear();
			blerc.clear();
			
			berc.count (b_source_bits, b_decoded_bits);	//Count error bits in a word
			blerc.count (b_source_bits, b_decoded_bits);	//Count frame errors
			
			No_of_Errors(i) += berc.get_errors();		//Updating counters	
			No_of_Bits(i) += berc.get_errors()+berc.get_corrects();
			No_of_BlockErrors(i) +=blerc.get_errors();
			No_of_Blocks(i)++;
			
			if(No_of_Errors(i)>100000)
				break;
		}
		
		ber(i) = (double)No_of_Errors(i)/No_of_Bits(i);
		bler(i) = (double)No_of_BlockErrors(i)/No_of_Blocks(i);
		
		double EbN0dB = SNRdB(i) - 10*log10(rate);
		
		printf("%f\t%f\t%e\t%e\t%d\n",    SNRdB(i), EbN0dB, ber(i), bler(i), No_of_Blocks(i));
		fprintf(f,"%f\t%f\t%e\t%e\t%d\n", SNRdB(i), EbN0dB, ber(i), bler(i), No_of_Blocks(i));
		
		if(ber(i)<1e-5)  break;			
	}

	fprintf(f,"!Elapsed time = %d s\n", tt.get_time());		//output simulation time
	tt.toc();							//Stop timer and output simulation time
	
	fclose(f);							//close output file	
	return 0 ;							//exit program
}
Example #5
0
int main( int argc, char* argv[])
{
  //Arg initial here
  int Number_of_bits = 2048;

  //Declarations basic
  int i;
  double Ps, N0, dist_1, dist_2, h1, h2, Eb;
  double EbN0;
  double rcvd_power_1, rcvd_power_2;
  int nFFT, nCylicPrefix;

  //Read arg Simple
  if( argc == 4) {
    Number_of_bits = atoi( argv[1]);
    dist_1 = strtod(argv[2], NULL);
    dist_2 = strtod(argv[3], NULL);
  }

  //Declare vectors and others
  vec alpha;                                            //vec is a vector containing double
  vec bit_error_rate_1, bit_error_rate_2;
  vec ber_theo_1, ber_theo_2;                           // Theoretical results for multiple access

  bvec transmitted_bits_1, transmitted_bits_2;          //bvec is a vector containing bits
  bvec received_bits_1, received_bits_2;

  cvec transmitted_symbols_1, transmitted_symbols_2, transmitted_symbols; //cvec is a vector containing double_complex
  cvec received_symbols_1, received_symbols_2, feedback_symbols_1, feedback_symbols_2;
  cvec ofdm_symbols_1, ofdm_symbols_2;

  // Declarations of classes:
  QPSK mod;                     //The QPSK modulator class
  QAM qammod(16);                 //QAM-16
  //QAM mod(64);                 //   -64
  AWGN_Channel awgn_channel;     //The AWGN channel class
  it_file ff;                    //For saving the results to file
  BERC berc;                     //Used to count the bit errors
  Real_Timer tt;                 //The timer used to measure the execution time
  OFDM ofdm;                     //OFDM modulator class

  MA_Filter<std::complex<double>, std::complex<double>, std::complex<double> > multipath_channel; //Simulate multi-path enviornment
  int ch_nb_taps = 3;
  cvec ini_state = to_cvec( zeros(ch_nb_taps));
  vec ch_imp_response_real = "1.000000000000000 0.436232649307735 0.198694326007661";//randray( ch_nb_taps);
  ch_imp_response_real /= sqrt(sum_sqr( ch_imp_response_real));//normalized power profile
  cvec ch_imp_response = to_cvec( ch_imp_response_real);
  multipath_channel.set_coeffs( ch_imp_response);
  multipath_channel.set_state(ini_state);//inital state is zero

  //Reset and start the timer:
  tt.tic();

  //Init:
  //Ps = 5 * pow(10, -3);          //The transmitted energy per mod symbol is 1, 5e-3 W/MHz 802.11
  Ps = 4 * pow(10, 0.0);         //ETSI TS 136 104 V9.4.0 (2010-07) p17 LTE JAPAN 4 W/MHz  
  //Ps = 4 * pow(10, 1.0);         //max 46 dbm
  N0 = 4 * pow(10, -15);         //Thermal noise -144dBm/MHz
  //dist_1 = 800;                   //Distance form transmitter to receiver 1 (meter)
  //dist_2 = 1000;                  //                   *                  2
  double f = 2600; // Mhz 150-1500
  double hM = 2; // meter 1-10
  double hb = 120; // meter 30-200
  double C_H = 0.8 + (1.1 * log10(f) - 0.7) * hM - 1.56 * log10(f); // Hata medium size city
  double h1dB = 69.55 + 26.16 * log10(f) - 13.82*log10(hb) - C_H + (44.9 - 6.55*log10(hb)) * log10(dist_1/1000);
  double h2dB = 69.55 + 26.16 * log10(f) - 13.82*log10(hb) - C_H + (44.9 - 6.55*log10(hb)) * log10(dist_2/1000);
  //cout << "HatadB:" << h1dB << endl;
  //cout << "HatadB:" << h2dB << endl;
  
  //h1 = pow(10, -h1dB/10);
  //h2 = pow(10, -h2dB/10);
  h1 = pow(dist_1, -4.5);        //Channel gain for receiver 1
  h2 = pow(dist_2, -4.5);        //
  cout << "h1 = " << h1 << " h2 = " << h2 << endl;
  alpha = linspace(0.3, 0.0, 13);//Simulate for different weight on power
  nFFT = 2048;                   //FFT size, default is 2048 LTE-a
  nCylicPrefix = 144;            //Length of Prefix, standard 144 (first prefix is different in real case)

  // test upconv
  cvec ocsi;
  cvec t_spanned;
  double unit_time = 3.25520833 * pow(10,-8); // 1/(2048*15k)
  const double pi = 3.1415926535897;
  const std::complex<double> unit_img (0.0,1.0);
  double fc = 2.6 * pow(10,9); // 2.6GHz
  t_spanned.set_size(2048, false);
  for (int i = 0; i < 2048; i++) {
    t_spanned[i] = exp(2*pi*fc*unit_time*i*unit_img);
  }

  // Declarations for equalizer & NSC coding
  ivec gen = "07 05";                                          //octal notation
  int constraint_length = 3;                                   //constraint length
  int blockSize = 13;//permutation length                //encoder output block size
  // other parameters
  int nb_bits_tail = blockSize / gen.length();                  //encoder input size + tail size
  int nb_bits = nb_bits_tail - (constraint_length - 1);        //encoder block size
  int nb_blocks;                                               //number of blocks
  // Convolutional code
  Convolutional_Code nsc;
  nsc.set_generator_polynomials(gen, constraint_length);

  //Allocate storage space for the result vector.
  //The "false" argument means "Do not copy the old content of the vector to the new storage area."
  bit_error_rate_1.set_size(alpha.length(), false);
  bit_error_rate_2.set_size(alpha.length(), false);

  //Storage for theoretical ber
  ber_theo_1.set_size(alpha.length(), false);
  ber_theo_2.set_size(alpha.length(), false);

  //Randomize the random number generators in it++:
  RNG_randomize();

  //Set OFDM parameters
  ofdm.set_parameters(nFFT, nCylicPrefix);

  //Iterate over all EbN0dB values:
  for (i = 0; i < alpha.length(); i++) {

    //Show how the simulation progresses:
    //cout << "Now simulating alpha value = " << alpha(i); 
    //cout << " # " << i + 1 << "/" << alpha.length() << endl;

    //Generate a vector of random bits to transmit:
    transmitted_bits_1 = randb(Number_of_bits*2);
    transmitted_bits_2 = randb(Number_of_bits);

    //convolutional code encode
    //bvec out_binary_1 = encode(nsc, constraint_length, transmitted_bits_1, blockSize, false);
    //bvec out_binary_2 = encode(nsc, constraint_length, transmitted_bits_2, blockSize, false);
    bvec out_binary_1 = transmitted_bits_1;
    bvec out_binary_2 = transmitted_bits_2;

    //Modulate the bits to mod symbols:
    transmitted_symbols_1 = qammod.modulate_bits(out_binary_1);
    transmitted_symbols_2 = mod.modulate_bits(out_binary_2);

    //Multiplex two signals
    transmitted_symbols = transmitted_symbols_1 * pow(alpha(i), 0.5) + transmitted_symbols_2 * pow(1 - alpha(i), 0.5);
    transmitted_symbols = transmitted_symbols * pow(Ps, 0.5);
    //eval_avg_power(transmitted_symbols);

    //Fading
    transmitted_symbols_1 = transmitted_symbols * pow(h1, 0.5);
    transmitted_symbols_2 = transmitted_symbols * pow(h2, 0.5);

    //OFDM modulate
    zero_pad_back(transmitted_symbols_1, 2048);
    zero_pad_back(transmitted_symbols_2, 2048);
    ofdm.modulate(transmitted_symbols_1, ofdm_symbols_1);
    ofdm.modulate(transmitted_symbols_2, ofdm_symbols_2);

    //Set the noise variance of the AWGN channel:
    //ofdm_symbols_1 = concat(ofdm_symbols_1, zeros_c(2)); //B
    //ofdm_symbols_2 = concat(ofdm_symbols_2, zeros_c(2)); //B
    //ofdm_symbols_1 = concat(ofdm_symbols_1, zeros_c(4));   //A
    //ofdm_symbols_2 = concat(ofdm_symbols_2, zeros_c(4));   //A
    //cout << "N0: " << N0 << endl;
    awgn_channel.set_noise(N0);

    //Up-conversion

    //Run the transmited symbols through the channel using the () operator:
    //ofdm_symbols_1 = awgn_channel( multipath_channel( ofdm_symbols_1));
    //ofdm_symbols_2 = awgn_channel( multipath_channel( ofdm_symbols_2));
    ofdm_symbols_1 = awgn_channel( ofdm_symbols_1);
    ofdm_symbols_2 = awgn_channel( ofdm_symbols_2);
    //ofdm_symbols_1 = ofdm_symbols_1.get(0, ofdm_symbols_1.length()-3);    //B
    //ofdm_symbols_2 = ofdm_symbols_2.get(0, ofdm_symbols_2.length()-3);    //B

    // test zone!! KEEP OUT!! //A
    /*cvec ofdm_symbols_eqed_tmp;
    mmse_equalizer_simple(ofdm_symbols_1, ofdm_symbols_eqed_tmp, ch_imp_response_real, N0, false);
    ofdm_symbols_1 = ofdm_symbols_eqed_tmp;
    mmse_equalizer_simple(ofdm_symbols_2, ofdm_symbols_eqed_tmp, ch_imp_response_real, N0, false);
    ofdm_symbols_2 = ofdm_symbols_eqed_tmp;*/

    //alpha no greater than 0.5
    //OFDM demodulate
    ofdm.demodulate(ofdm_symbols_1, received_symbols_1);
    ofdm.demodulate(ofdm_symbols_2, received_symbols_2);
    //cvec FDMtmp;                                                          //B
    //FDE(received_symbols_1, FDMtmp, ch_imp_response_real, 2048, false);   //B
    //ofdm_symbols_1 = FDMtmp;                                              //B
    //FDE(received_symbols_2, FDMtmp, ch_imp_response_real, 2048, false);   //B
    //ofdm_symbols_2 = FDMtmp;                                              //B

    //Demodulate the received mod symbols into received bits: Layer 1
    received_bits_2 = mod.demodulate_bits(received_symbols_2 / pow(1-alpha(i), 0.5) / pow(Ps, 0.5) / pow(h2, 0.5));
    //bvec out_binary_recover_2 = decode(nsc, constraint_length, received_bits_2, blockSize, false);
    bvec out_binary_recover_2 = received_bits_2;
    
    //Demodulate the received mod symbols into received bits: Layer 2
    received_bits_1 = mod.demodulate_bits(received_symbols_1 / pow(1-alpha(i), 0.5) / pow(Ps, 0.5) / pow(h1, 0.5));
    feedback_symbols_2 = pow(Ps * (1-alpha(i)) * h1, 0.5) * mod.modulate_bits(received_bits_1);
    received_bits_1 = qammod.demodulate_bits((received_symbols_1-feedback_symbols_2) / pow(alpha(i), 0.5) / pow(Ps, 0.5) / pow(h1, 0.5));
    //bvec out_binary_recover_1 = decode(nsc, constraint_length, received_bits_1, blockSize, false);
    bvec out_binary_recover_1 = received_bits_1;

    //Calculate the bit error rate:
    berc.clear();                               //Clear the bit error rate counter
    berc.count(transmitted_bits_2, out_binary_recover_2.get(0, Number_of_bits)); //Count the bit errors
    bit_error_rate_2(i) = berc.get_errorrate();   //Save the estimated BER in the result vector

    berc.clear();
    berc.count(transmitted_bits_1, out_binary_recover_1.get(0, Number_of_bits));
    bit_error_rate_1(i) = berc.get_errorrate();
  }

  tt.toc();

  // Theoretical results for multiple access
  for (size_t i = 0; i < alpha.length(); ++i) {        //BER for theo 1
    EbN0 = (Ps * 0.5 * h1 * alpha(i)) / (N0 + Ps * 0.5 * h1 * (1 - alpha(i)));
    ber_theo_1(i) = 0.5*erfc(pow(EbN0, 0.5));
  }
  for (size_t i = 0; i < alpha.length(); ++i) {        //BER for theo 2
    EbN0 = (Ps * 0.5 * h2 * (1-alpha(i))) / N0;
    ber_theo_2(i) = 0.5*erfc(pow(EbN0, 0.5));
  }

  //Print the results:
  cout << endl;
  time_t rawtime;
  time (&rawtime);
  std::string nowTime( ctime( &rawtime));
  /*cout << nowTime << endl;
  cout << "alpha = " << alpha << " " << endl;
  cout << "BER 1 = " << bit_error_rate_1 << endl;
  cout << "BER 2 = " << bit_error_rate_2 << endl;
  cout << "Theoretical BER 1 = " << ber_theo_1 << endl;
  cout << "Theoretical BER 2 = " << ber_theo_2 << endl;
  cout << endl;*/
  
  //Chech feasibility
  bool feasibility = false;
  double threshold = 0.01;
  for (size_t i = 1; i < alpha.length(); i++) {
    if (bit_error_rate_1(i) < threshold && bit_error_rate_2(i) < threshold) {
      feasibility = true;
    }
  }
  if (feasibility) {
    cout << "  PASS  " << endl;
  }

  //Save the results to file:
  const char xFilename[] = "result";
  char oFilename[100];
  int fileIndex = 0;
  strcpy( oFilename, xFilename);
  while(std::ifstream( oFilename)){
    ++fileIndex;
    sprintf( oFilename, "%s%d", xFilename, fileIndex);
  }
  cout << "Saving results to " << oFilename << endl;
  std::ofstream oo;
  oo.open(oFilename);
    oo << alpha << endl;
    oo << bit_error_rate_1 << endl;
    oo << bit_error_rate_2 << endl;
  oo.close();

  //Exit program:
  return 0;

}
int main()
{
  //Declarations of scalars and vectors:
  int i, Number_of_bits;
  double Ps, N0, dist_1, dist_2, h1, h2, Eb;
  double EbN0;
  double rcvd_power_1, rcvd_power_2;

  vec alpha;                                            //vec is a vector containing double
  vec bit_error_rate_1, bit_error_rate_2;
  vec ber_theo_1, ber_theo_2;                           // Theoretical results for multiple access

  bvec transmitted_bits_1, transmitted_bits_2;          //bvec is a vector containing bits
  bvec received_bits_1, received_bits_2;

  cvec transmitted_symbols_1, transmitted_symbols_2, transmitted_symbols; //cvec is a vector containing double_complex
  cvec received_symbols_1, received_symbols_2, feedback_symbols_1;

  //Declarations of classes:
  QPSK qpsk;                     //The QPSK modulator class
  QAM qam16(16);
  QAM qam64(64);
  AWGN_Channel awgn_channel;     //The AWGN channel class
  it_file ff;                    //For saving the results to file
  BERC berc;                     //Used to count the bit errors
  Real_Timer tt;                 //The timer used to measure the execution time

  //Reset and start the timer:
  tt.tic();

  //Init:
  Ps = 5 * pow(10, -3);          //The transmitted energy per QPSK symbol is 1, 5e-3 W/MHz
  N0 = 4 * pow(10, -15);         //Thermal noise -144dBm/MHz
  dist_1 = 150;                  //Distance form transmitter to receiver 1 (meter)
  dist_2 = 220;                  //                   *                  2
  h1 = pow(dist_1, -4.5);        //Channel gain for receiver 1
  h2 = pow(dist_2, -4.5);        //
  cout << "h1 = " << h1 << " h2 = " << h2 << endl;
  alpha = linspace(1.0, 0.0, 21);//Simulate for different weight on power

//  Eb = Ec / 2.0;                 //The transmitted energy per bit is 0.5.
//  EbN0dB = linspace(0.0, 9.0, 10); //Simulate for 10 Eb/N0 values from 0 to 9 dB.
//  EbN0 = inv_dB(EbN0dB);         //Calculate Eb/N0 in a linear scale instead of dB.
//  N0 = Eb * pow(EbN0, -1.0);     //N0 is the variance of the (complex valued) noise.
  Number_of_bits = 1000000;       //One hundred thousand bits is transmitted for each Eb/N0 value
//  alpha = 0.8;                   //Ratio of allocated power

  //Allocate storage space for the result vector.
  //The "false" argument means "Do not copy the old content of the vector to the new storage area."
  bit_error_rate_1.set_size(alpha.length(), false);
  bit_error_rate_2.set_size(alpha.length(), false);

  //Storage for theoretical ber
  ber_theo_1.set_size(alpha.length(), false);
  ber_theo_2.set_size(alpha.length(), false);

  //Randomize the random number generators in it++:
  RNG_randomize();

  //Iterate over all EbN0dB values:
  for (i = 0; i < alpha.length(); i++) {

    //Show how the simulation progresses:
    cout << "Now simulating alpha value = " << alpha(i); 
    cout << " # " << i + 1 << "/" << alpha.length() << endl;

    //Generate a vector of random bits to transmit:
    transmitted_bits_1 = randb(Number_of_bits*2);
    transmitted_bits_2 = randb(Number_of_bits);

    //Modulate the bits to QPSK symbols:
    transmitted_symbols_1 = qam16.modulate_bits(transmitted_bits_1);
    transmitted_symbols_2 = qpsk.modulate_bits(transmitted_bits_2);

    //Multiplex two signals
    transmitted_symbols = transmitted_symbols_1 * pow(alpha(i), 0.5) + transmitted_symbols_2 * pow(1 - alpha(i), 0.5);
    transmitted_symbols = transmitted_symbols * pow(Ps, 0.5);
    //eval_avg_power(transmitted_symbols);

    //Fading
    transmitted_symbols_1 = transmitted_symbols * pow(h1, 0.5);
    transmitted_symbols_2 = transmitted_symbols * pow(h2, 0.5);

    //Set the noise variance of the AWGN channel:
    awgn_channel.set_noise(N0);

    //Run the transmited symbols through the channel using the () operator:
    received_symbols_1 = awgn_channel(transmitted_symbols_1);
    received_symbols_2 = awgn_channel(transmitted_symbols_2);

    //Demodulate the received QPSK symbols into received bits: Layer 1
    received_bits_1 = qam16.demodulate_bits(received_symbols_1 * pow(Ps * alpha(i) * h1, -0.5));

    //Demodulate the received QPSK symbols into received bits: Layer 2
    received_bits_2 = qam16.demodulate_bits(received_symbols_2 * pow(Ps * alpha(i) * h2, -0.5));
    feedback_symbols_1 = pow(Ps * alpha(i) * h2, 0.5) * qam16.modulate_bits(received_bits_2);
    received_bits_2 = qpsk.demodulate_bits(received_symbols_2 - feedback_symbols_1);

    //Calculate the bit error rate:
    berc.clear();                               //Clear the bit error rate counter
    berc.count(transmitted_bits_1, received_bits_1); //Count the bit errors
    bit_error_rate_1(i) = berc.get_errorrate();   //Save the estimated BER in the result vector

    berc.clear();
    berc.count(transmitted_bits_2, received_bits_2);
    bit_error_rate_2(i) = berc.get_errorrate();
  }

  tt.toc();

  // Theoretical results for multiple access
  for (size_t i = 0; i < alpha.length(); ++i) {        //BER for theo 1
    EbN0 = (Ps * 0.5 * h1 * alpha(i)) / (N0 + Ps * 0.5 * h1 * (1 - alpha(i)));
    ber_theo_1(i) = 0.5*erfc(pow(EbN0, 0.5));
  }
  for (size_t i = 0; i < alpha.length(); ++i) {        //BER for theo 2
    EbN0 = (Ps * 0.5 * h2 * (1-alpha(i))) / N0;
    ber_theo_2(i) = 0.5*erfc(pow(EbN0, 0.5));
  }

  //Print the results:
  cout << endl;
  cout << "alpha = " << alpha << " " << endl;
  cout << "BER 1 = " << bit_error_rate_1 << endl;
  cout << "BER 2 = " << bit_error_rate_2 << endl;
  cout << "Theoretical BER 1 = " << ber_theo_1 << endl;
  cout << "Theoretical BER 2 = " << ber_theo_2 << endl;
  cout << "Saving results to ./qam16_result_file.it" << endl;
  cout << endl;

  //Save the results to file:
  std::ofstream oo;
  oo.open("qam16_result_file.it");
  oo << alpha;
  oo << bit_error_rate_1;
  oo << bit_error_rate_2;
  ff.close();

  //Exit program:
  return 0;

}
Example #7
0
namespace itpp
{

//! Global object for tic and toc functions
Real_Timer __tic_toc_timer;

//----------------------------------------------------------------------------
// class Timer
//----------------------------------------------------------------------------
Timer::Timer()
{
  reset();
}

void Timer::start(void)
{
  if (!running) {
    start_time = get_current_time();
    running = true;
  }
}

double Timer::stop(void)
{
  if (running) {
    stop_time = get_current_time();
    elapsed_time += stop_time - start_time;
    running = false;
  }

  return elapsed_time;
}

void Timer::reset(double t)
{
  elapsed_time = t;
  start_time = 0;
  stop_time = 0;
  running = false;
}

double Timer::get_time() const
{
  return running ?
         elapsed_time + get_current_time() - start_time :
         elapsed_time;
}

void Timer::tic(void)
{
  reset();
  start();
}

double Timer::toc(void)
{
  return get_time() ;
}

void Timer::toc_print(void)
{
  std::cout << "Elapsed time = " << get_time() << " seconds" << std::endl;
}

//----------------------------------------------------------------------------
// class CPU_Timer
//----------------------------------------------------------------------------
double CPU_Timer::get_current_time() const
{
  return static_cast<double>(clock()) / CLOCKS_PER_SEC;
}

//----------------------------------------------------------------------------
// class Real_Timer
//----------------------------------------------------------------------------
double Real_Timer::get_current_time() const
{
  struct timeval t;
  gettimeofday(&t, 0);
  return t.tv_sec + t.tv_usec * 1.0e-6;
}


void tic()
{
  __tic_toc_timer.tic();
}

double toc()
{
  return __tic_toc_timer.toc();
}

void toc_print()
{
  __tic_toc_timer.toc_print();
}

void pause(double t)
{
  if (t == -1) {
    std::cout << "(Press enter to continue)" << std::endl;
    getchar();
  }
  else {
    Real_Timer T;
    T.start();
    while (T.get_time() < t);
  }
}

} // namespace itpp
Example #8
0
void toc_print()
{
  __tic_toc_timer.toc_print();
}
Example #9
0
double toc()
{
  return __tic_toc_timer.toc();
}
Example #10
0
void tic()
{
  __tic_toc_timer.tic();
}
// This is the searcher process. It requests captured data from the main
// thread and launches a new thread for every cell it finds. Each new
// cell thread then requests sample data from the main thread.
void searcher_thread(
    capbuf_sync_t & capbuf_sync,
    global_thread_data_t & global_thread_data,
    tracked_cell_list_t & tracked_cell_list
) {
    if (verbosity>=1) {
        cout << "Searcher process has been launched." << endl;
    }

    global_thread_data.searcher_thread_id=syscall(SYS_gettid);

    if (nice(20)==-1) {
        cerr << "Error: could not reduce searcher process priority" << endl;
        ABORT(-1);
    }

    // Keep track of serial numbers to be used when launching a new
    // tracker thread.
    ivec serial_num(504);
    serial_num=1;

    // Shortcut
    const double & fc_requested=global_thread_data.fc_requested;
    const double & fc_programmed=global_thread_data.fc_programmed;
    const double & fs_programmed=global_thread_data.fs_programmed;
//  double freq_correction;

    const bool sampling_carrier_twist = global_thread_data.sampling_carrier_twist();
    double k_factor = global_thread_data.k_factor();
//  double correction = global_thread_data.correction();

    vec coef(( sizeof( chn_6RB_filter_coef )/sizeof(float) ));
    for (uint16 i=0; i<length(coef); i++) {
        coef(i) = chn_6RB_filter_coef[i];
    }

    // Calculate the threshold vector
    const uint8 thresh1_n_nines=12;
    double rx_cutoff=(6*12*15e3/2+4*15e3)/(FS_LTE/16/2);

    // for PSS correlate
    //cout << "DS_COMB_ARM override!!!" << endl;
#define DS_COMB_ARM 2
    mat xc_incoherent_collapsed_pow;
    imat xc_incoherent_collapsed_frq;
    vector <mat> xc_incoherent_single(3);
    vector <mat> xc_incoherent(3);
    vec sp_incoherent;
    vector <mat> xc(3);
    vec sp;

    // for SSS detection
#define THRESH2_N_SIGMA 3
    vec sss_h1_np_est_meas;
    vec sss_h2_np_est_meas;
    cvec sss_h1_nrm_est_meas;
    cvec sss_h2_nrm_est_meas;
    cvec sss_h1_ext_est_meas;
    cvec sss_h2_ext_est_meas;
    mat log_lik_nrm;
    mat log_lik_ext;

    // for time frequency grid
    // Extract time and frequency grid
    cmat tfg;
    vec tfg_timestamp;
    // Compensate for time and frequency offsets
    cmat tfg_comp;
    vec tfg_comp_timestamp;

    vec period_ppm;
    double xcorr_pss_time;

    Real_Timer tt; // for profiling

    // Get the current frequency offset (because it won't change anymore after main thread launches this thread, so move it outside loop)
    vec f_search_set(1);
    cmat pss_fo_set;// pre-generate frequencies offseted pss time domain sequence
    // because it is already included in global_thread_data.frequency_offset();
    f_search_set(0)=global_thread_data.frequency_offset();
    pss_fo_set_gen(f_search_set, pss_fo_set);

//  freq_correction = fc_programmed*(correction-1)/correction;

//  cout << opencl_platform << " " << opencl_device <<  " " << sampling_carrier_twist << "\n";
    uint16 opencl_platform = global_thread_data.opencl_platform();
    uint16 opencl_device = global_thread_data.opencl_device();
    lte_opencl_t lte_ocl(opencl_platform, opencl_device);

#ifdef USE_OPENCL
    uint16 filter_workitem = global_thread_data.filter_workitem();
    uint16 xcorr_workitem = global_thread_data.xcorr_workitem();
#ifdef FILTER_MCHN_SIMPLE_KERNEL
    lte_ocl.setup_filter_mchn((string)"filter_mchn_simple_kernel.cl", CAPLENGTH, length(f_search_set)*3, pss_fo_set.cols(), xcorr_workitem);
#else
    lte_ocl.setup_filter_mchn((string)"filter_mchn_kernels.cl", CAPLENGTH, length(f_search_set)*3, pss_fo_set.cols(), xcorr_workitem);
#endif
    lte_ocl.setup_filter_my((string)"filter_my_kernels.cl", CAPLENGTH, filter_workitem);
#endif

    // Loop forever.
    while (true) {
        // Used to measure searcher cycle time.
        tt.tic();

        // Request data.
        {
            boost::mutex::scoped_lock lock(capbuf_sync.mutex);
            capbuf_sync.request=true;

            // Wait for data to become ready.
            capbuf_sync.condition.wait(lock);
        }

        // Results are stored in this vector.
        list<Cell> detected_cells;

        // Local reference to the capture buffer.
        cvec &capbuf=capbuf_sync.capbuf;

        capbuf = capbuf - mean(capbuf); // remove DC

#ifdef USE_OPENCL
        lte_ocl.filter_my(capbuf); // be careful! capbuf.zeros() will slow down the xcorr part pretty much!
#else
        filter_my(coef, capbuf);
#endif

        // Correlate
        uint16 n_comb_xc;
        uint16 n_comb_sp;
        if (verbosity>=2) {
            cout << "  Calculating PSS correlations" << endl;
        }

        sampling_ppm_f_search_set_by_pss(lte_ocl, 0, capbuf, pss_fo_set, 1, 0, f_search_set, period_ppm, xc, xcorr_pss_time);

        xcorr_pss(capbuf,f_search_set,DS_COMB_ARM,fc_requested,fc_programmed,fs_programmed,xc,xc_incoherent_collapsed_pow,xc_incoherent_collapsed_frq,xc_incoherent_single,xc_incoherent,sp_incoherent,sp,n_comb_xc,n_comb_sp,sampling_carrier_twist,k_factor);

        // Calculate the threshold vector
        double R_th1=chi2cdf_inv(1-pow(10.0,-thresh1_n_nines),2*n_comb_xc*(2*DS_COMB_ARM+1));
        vec Z_th1=R_th1*sp_incoherent/rx_cutoff/137/n_comb_xc/(2*DS_COMB_ARM+1);

        // Search for the peaks
        if (verbosity>=2) {
            cout << "  Searching for and examining correlation peaks..." << endl;
        }
        peak_search(xc_incoherent_collapsed_pow,xc_incoherent_collapsed_frq,Z_th1,f_search_set,fc_requested,fc_programmed,xc_incoherent_single,DS_COMB_ARM, sampling_carrier_twist, (const double)k_factor, detected_cells);

        // Loop and check each peak
        list<Cell>::iterator iterator=detected_cells.begin();
        int tdd_flag = 1;
        while (iterator!=detected_cells.end()) {
            tdd_flag = !tdd_flag;

            // Detect SSS if possible
#define THRESH2_N_SIGMA 3
            (*iterator)=sss_detect((*iterator),capbuf,THRESH2_N_SIGMA,fc_requested,fc_programmed,fs_programmed,sss_h1_np_est_meas,sss_h2_np_est_meas,sss_h1_nrm_est_meas,sss_h2_nrm_est_meas,sss_h1_ext_est_meas,sss_h2_ext_est_meas,log_lik_nrm,log_lik_ext,sampling_carrier_twist,tdd_flag);
            if ((*iterator).n_id_1!=-1) {
                if (verbosity>=2) {
                    cout << "Detected PSS/SSS correspoding to cell ID: " << (*iterator).n_id_cell() << endl;
                }

                // Check to see if this cell has already been detected previously.
                bool match=false;
                {
                    boost::mutex::scoped_lock lock(tracked_cell_list.mutex);
                    list<tracked_cell_t *>::iterator tci=tracked_cell_list.tracked_cells.begin();
                    match=false;
                    while (tci!=tracked_cell_list.tracked_cells.end()) {
                        if ((*(*tci)).n_id_cell==(*iterator).n_id_cell()) {
                            match=true;
                            break;
                        }
                        ++tci;
                    }
                }
                if (match) {
                    if (verbosity>=2) {
                        cout << "Cell already being tracked..." << endl;
                    }
                    ++iterator;
                    continue;
                }

                // Fine FOE
                (*iterator)=pss_sss_foe((*iterator),capbuf,fc_requested,fc_programmed,fs_programmed,sampling_carrier_twist,tdd_flag);

                // Extract time and frequency grid
                extract_tfg((*iterator),capbuf,fc_requested,fc_programmed,fs_programmed,tfg,tfg_timestamp,sampling_carrier_twist);

                // Create object containing all RS
                RS_DL rs_dl((*iterator).n_id_cell(),6,(*iterator).cp_type);

                // Compensate for time and frequency offsets
                (*iterator)=tfoec((*iterator),tfg,tfg_timestamp,fc_requested,fc_programmed,rs_dl,tfg_comp,tfg_comp_timestamp,sampling_carrier_twist);

                // Finally, attempt to decode the MIB
                (*iterator)=decode_mib((*iterator),tfg_comp,rs_dl);
                //cout << (*iterator) << endl << endl;
                //sleep(100000);
                if ((*iterator).n_rb_dl==-1) {
                    // No MIB could be successfully decoded.
                    iterator=detected_cells.erase(iterator);
                    continue;
                }

                /*
                if (verbosity>=1) {
                  cout << "Detected a new cell!" << endl;
                  cout << "  cell ID: " << (*iterator).n_id_cell() << endl;
                  cout << "  RX power level: " << db10((*iterator).pss_pow) << " dB" << endl;
                  cout << "  residual frequency offset: " << (*iterator).freq_superfine << " Hz" << endl;
                  cout << "  frame start: " << (*iterator).frame_start << endl;
                }
                */

                //      cout << ((*iterator).frame_start) <<  " " << k_factor << " " << capbuf_sync.late << (*iterator).k_factor <<  "\n";
                //      (*iterator).frame_start = 3619.95;
                //      capbuf_sync.late = 0;
                // Launch a cell tracker process!
                //tracked_cell_t * new_cell = new tracked_cell_t((*iterator).n_id_cell(),(*iterator).n_ports,(*iterator).cp_type,(*iterator).frame_start/k_factor+capbuf_sync.late,serial_num((*iterator).n_id_cell()));
                tracked_cell_t * new_cell = new tracked_cell_t(
                    (*iterator).n_id_cell(),
                    (*iterator).n_ports,
                    (*iterator).duplex_mode,
                    (*iterator).cp_type,
                    (*iterator).n_rb_dl,
                    (*iterator).phich_duration,
                    (*iterator).phich_resource,
                    (*iterator).frame_start*(FS_LTE/16)/(fs_programmed*k_factor)+capbuf_sync.late,
                    serial_num((*iterator).n_id_cell())//,
                    //        (*iterator).freq_superfine
                );

                serial_num((*iterator).n_id_cell())++;
                // Cannot launch thread here. If thread was launched here, it would
                // have the same (low) priority as the searcher thread.
                {
                    boost::mutex::scoped_lock lock(tracked_cell_list.mutex);
                    tracked_cell_list.tracked_cells.push_back(new_cell);
                }
                //CALLGRIND_START_INSTRUMENTATION;
#define MAX_DETECTED 1e6
                static uint32 n_found=0;
                n_found++;
                if (n_found==MAX_DETECTED) {
                    cout << "Searcher thread has stopped!" << endl;
                    sleep(1000000);
                }

                ++iterator;

            } else {
                // No SSS detected.
                iterator=detected_cells.erase(iterator);
                continue;
            }
        }

        global_thread_data.searcher_cycle_time(tt.toc());
//    global_thread_data.searcher_cycle_time(xcorr_pss_time);
    }
    // Will never reach here...
}