IA_CoMP
Interference Alignment and Coordinated Multipoint
IA1/IA1_node.cpp
00001 // Copyright 2011-2013, Per Zetterberg, KTH Royal Institute of Technology
00002 // This program is free software: you can redistribute it and/or modify
00003 // it under the terms of the GNU General Public License as published by
00004 // the Free Software Foundation, either version 3 of the License, or
00005 // (at your option) any later version.
00006 //
00007 // This program is distributed in the hope that it will be useful,
00008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010 // GNU General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License
00013 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00014 //
00015 
00016 #include "IA1_node.hpp"
00017 #include <itpp/itcomm.h>
00018 #include <itpp/stat/misc_stat.h>
00019 
00020 
00021 
00022 IA1_node::IA1_node(uint32_t no_ant1, uint32_t no_ant2 , uint32_t skip_ant, 
00023 float start_gain,  uint32_t node_ix , double frequency,
00024                        bool use_same_antenna,
00025                    std::vector<std::string> IP_addresses,bool simulate,
00026                    int receiver_delay_samples,int seed, bool run_decoder,
00027 int scenario_in)
00028   : four_multi_node(20000,no_ant1,no_ant2,false,use_same_antenna,node_ix,IP_addresses,simulate,skip_ant)
00029 {
00030 
00031    
00032    bool use_pilot_carriers=false;
00033    uint32_t cyclic_prefix_length=12;
00034    uint32_t no_ant=no_ant1+no_ant2;  
00035    int noise_estimation_start_ix;
00036    uint32_t num_payload_symbols_before_pilots=10;
00037    uint32_t num_payload_symbols_after_pilots=10;
00038    uint32_t num_symbols=num_payload_symbols_before_pilots+num_payload_symbols_after_pilots+1;
00039    ivec known_pos;
00040    scenario=scenario_in; // 1=pure IA, 2=CoMP, 3=MIMO, 4=SIMO
00041    use_udp=true;
00042 
00043    use_old_rand=false;
00044    do_calculate_beamformers=true;
00045    offset_time=0.0; 
00046    scaling_tx_signal=50000;
00047    if ((scenario_in<3) && use_udp) { 
00048       time_between_frames=0.06;
00049    } else {
00050      time_between_frames=0.1;
00051    };
00052    shift_transmit_signal_samples=2000;
00053    noise_estimation_start_ix=1001;
00054    replicas_start=5000;
00055    replicas_spacing=3000;
00056    d_seed=seed;
00057    codec_ix=3;
00058    modulation_order=16;
00059 
00060 
00061    d_gain_rx=start_gain;
00062    d_node_ix=node_ix;
00063    d_frame_ix=0;
00064    d_frequency=frequency;
00065    d_gain_tx=20;
00066    d_frame_ix=0;
00067    d_no_frames=5;
00068    
00069    ai.resize(3);
00070 
00071 
00072    if (scenario==1) {
00073      num_links=3;   // Node 0,1,2 =BS 3,4,5 =MS
00074      num_streams=3;
00075      num_bs=3;
00076      num_ms=3;
00077      num_streams_per_bs=1;
00078      num_streams_per_ms=1;
00079      num_antennas_in_system=6;
00080      replica_positions_this_bs.set_length(num_streams_per_bs);
00081      replica_positions_this_bs(0)=replicas_start+(d_node_ix %  num_links)*replicas_spacing;
00082      replica_positions.set_length(num_streams);
00083      for (uint32_t i1=0;i1<num_streams;i1++) {
00084        replica_positions(i1)=replicas_start+i1*replicas_spacing;
00085      };
00086 
00087      stream_ix_this_node.set_length(1);
00088      stream_ix_this_node(0)=d_node_ix %  num_links;
00089 
00090      num_ant_ms=2;
00091      num_ant_bs=2;
00092 
00093      bs_to_stream_ix="0 ; 1 ; 2";
00094      stream_ix_to_ms_ix="0,1,2";
00095 
00096      if (node_ix<num_links)
00097        is_bs=true;
00098      else
00099        is_bs=false;
00100 
00101 
00102 
00103    };
00104    if (scenario==2) {
00105 
00106      num_ant_ms=2;
00107      num_ant_bs=6;
00108 
00109      num_bs=1;
00110      num_ms=3;
00111      num_links=3; // Node 0 =BS 1,2,3 =MS
00112      num_streams=3;
00113      num_streams_per_bs=3;
00114      num_streams_per_ms=1;
00115      num_antennas_in_system=6;
00116      if (node_ix==0) {
00117        is_bs=true;
00118        bs_to_stream_ix="0 1 2";
00119        stream_ix_to_ms_ix="0,1,2";
00120        stream_ix_this_node="0 1 2";
00121      } else {
00122        stream_ix_this_node.set_length(1);
00123        stream_ix_this_node(0)=d_node_ix-1;
00124        is_bs=false;
00125      };
00126      replica_positions_this_bs.set_length(num_streams_per_bs);
00127      for (uint32_t i1=0;i1<num_streams_per_bs;i1++) {
00128        replica_positions_this_bs(i1)=replicas_start+i1*replicas_spacing;
00129      };
00130      replica_positions.set_length(num_streams);
00131      for (uint32_t i1=0;i1<num_streams;i1++) {
00132        replica_positions(i1)=replicas_start+i1*replicas_spacing;
00133      };
00134 
00135 
00136    };
00137    if (scenario==3) { // Node 0,1,2 =BS 3,4,5 =MS, MIMO
00138      num_bs=3;
00139      num_ms=3;
00140 
00141      num_ant_ms=2;
00142      num_ant_bs=2;
00143 
00144      num_links=3;
00145      num_streams=2;
00146      num_streams_per_bs=2;
00147      num_streams_per_ms=2;
00148      num_antennas_in_system=6;
00149      replica_positions_this_bs.set_length(num_streams_per_bs);
00150      replica_positions_this_bs(0)=replicas_start;
00151      replica_positions_this_bs(1)=replicas_start+replicas_spacing;
00152      stream_ix_this_node="0 1";
00153      bs_to_stream_ix="0 1;0 1;0 1";
00154      do_calculate_beamformers=false;
00155 
00156      MIMO_active_tx_node_tx=0;
00157      MIMO_closed_loop=false;
00158      MIMO_all_active=false;
00159      stream_ix_to_ms_ix="0,0";
00160 
00161      if (node_ix<num_links)
00162        is_bs=true;
00163      else
00164        is_bs=false;
00165    };
00166    if (scenario==4) { // SIMO
00167 
00168      num_ant_ms=2;
00169      num_ant_bs=2;
00170 
00171      num_bs=3;
00172      num_ms=3;
00173      num_links=1;
00174      num_streams=1;
00175      num_streams_per_bs=1;
00176      num_streams_per_ms=1;
00177      num_antennas_in_system=6;
00178      replica_positions_this_bs.set_length(num_streams_per_bs);
00179      replica_positions_this_bs(0)=replicas_start+(d_node_ix %  num_links)*replicas_spacing;
00180      stream_ix_this_node.set_length(1);
00181      stream_ix_this_node(0)=d_node_ix %  num_links;
00182      do_calculate_beamformers=false;
00183      bs_to_stream_ix="0;0;0";  
00184 
00185      if (node_ix<num_bs)
00186        is_bs=true;
00187      else
00188        is_bs=false;
00189 
00190    };
00191 
00192    if (!is_bs) {
00193      demodulation_position.set_size(num_streams_per_ms);
00194      for (uint32_t i1=0;i1<num_streams_per_ms;i1++) {
00195        demodulation_position(i1)=shift_transmit_signal_samples;
00196        //demodulation_position(i1)=replica_positions(stream_ix_this_node(i1));
00197        demodulation_position(i1)+=receiver_delay_samples;
00198        
00199      };
00200    };
00201    
00202    antenna_pilot_position.set_length(num_antennas_in_system);
00203    antenna_pilot_position_this_bs.set_length(no_ant);
00204 
00205 
00206    for (uint32_t i1=0;i1<num_antennas_in_system;i1++) {
00207      antenna_pilot_position(i1)=num_payload_symbols_before_pilots+
00208      num_streams+i1;
00209    };
00210 
00211 
00212    if (scenario!=2) {
00213      for (uint32_t i1=0;i1<no_ant;i1++) {
00214        antenna_pilot_position_this_bs[i1]=antenna_pilot_position[(node_ix % num_links)*no_ant+i1];
00215      };
00216    } else
00217      antenna_pilot_position_this_bs=antenna_pilot_position;
00218 
00219 
00220 
00221    known_pos_all.set_length(num_streams);
00222    for (uint32_t i1=0;i1<num_streams;i1++) {
00223      reorder_all[i1].set_length(num_symbols);
00224      known_pos_all(i1)=num_payload_symbols_before_pilots+i1;
00225      for (uint32_t i2=0;i2<num_payload_symbols_before_pilots;i2++) {
00226        (reorder_all[i1])(i2)=i2;
00227      };
00228      reorder_all[i1](num_payload_symbols_before_pilots)=num_payload_symbols_before_pilots+i1;
00229      for (uint32_t i2=0;i2<num_payload_symbols_after_pilots;i2++) {
00230        (reorder_all[i1])(i2+num_payload_symbols_before_pilots+1)=
00231          num_payload_symbols_before_pilots+num_streams+num_antennas_in_system+i2;
00232      };
00233    };
00234    known_pos.set_length(1);
00235    known_pos(0)=num_payload_symbols_before_pilots;
00236 
00237    if (scenario==2) {
00238      if (node_ix==0) {
00239        for (uint32_t i1=0;i1<num_streams_per_bs;i1++) {
00240          
00241          ai[i1].init(use_pilot_carriers,false,cyclic_prefix_length,num_symbols,
00242                      known_pos,reorder_all[i1]);
00243 
00244          ai[i1].estimate_SINR=run_decoder;
00245        };
00246      }
00247      else {
00248 
00249         ai[0].init(use_pilot_carriers,false,cyclic_prefix_length,num_symbols,
00250                      known_pos,reorder_all[node_ix-1]);
00251         ai[0].estimate_SINR=run_decoder;
00252         ai[0].noise_estimation_start_ix=noise_estimation_start_ix;
00253         ai[0].noise_estimation_stop_ix=shift_transmit_signal_samples-10;
00254 
00255    };
00256 
00257    };
00258    if (scenario==3) {
00259      for (uint32_t i1=0;i1<num_streams_per_ms;i1++) {
00260        ai[i1].init(use_pilot_carriers,false,cyclic_prefix_length,num_symbols,
00261                    known_pos,reorder_all[i1]);
00262        ai[i1].estimate_SINR=run_decoder;
00263        ai[i1].noise_estimation_start_ix=noise_estimation_start_ix;
00264        ai[i1].noise_estimation_stop_ix=shift_transmit_signal_samples-10;
00265 
00266      };                                  
00267    };
00268    if ((scenario==1) | (scenario==4)) {
00269      ai[0].init(use_pilot_carriers,false,cyclic_prefix_length,num_symbols,
00270                   known_pos,reorder_all[node_ix % num_links]);
00271      ai[0].estimate_SINR=run_decoder;
00272      ai[0].noise_estimation_start_ix=noise_estimation_start_ix;
00273      ai[0].noise_estimation_stop_ix=shift_transmit_signal_samples-10;
00274    };
00275    
00276 
00277    if (!is_bs) {
00278      for (int i1=0;i1<stream_ix_this_node.length();i1++) {
00279        ivec interferer_pos(num_streams-1);
00280        int i10=0;
00281        for (int i2=0;i2<(int) num_streams;i2++) {
00282          if (stream_ix_this_node(i1)!=i2) {
00283            interferer_pos(i10)=known_pos_all(i2);
00284            i10++;
00285          };
00286        };
00287        ai[i1].init_multi_antenna(no_ant, d_buffer_length, interferer_pos);
00288      };
00289    };
00290    if (node_ix==0) {
00291       for (uint32_t i1=0;i1<num_antennas_in_system;i1++) {
00292         for (uint32_t i2=0;i2<num_ms;i2++) {
00293           H[i1][i2].set_size(num_ant_ms,ai[0].length_ix_all);
00294         };
00295       };
00296 
00297       for (uint32_t i1=0;i1<num_streams;i1++) { 
00298         Vbig[i1].set_size(no_ant,ai[0].length_ix_all); 
00299         //Ubig[i1].set_size(no_ant,ai[0].length_ix_all);
00300         Vbig[i1]=randn_c(no_ant,ai[0].length_ix_all); 
00301         normalize_columns(Vbig[i1]);
00302         
00303       };
00304    };
00305            
00306    //int bits_per_symbol=log2(qam_order);
00307 
00308    word_length=(ai[0].length_ix)*5*2*4;
00309    amc.init(word_length,true);
00310    amc.only_raw_bits=true;
00311    if (run_decoder) {
00312      amc.only_raw_bits=false;
00313      d_run_decoder=run_decoder;
00314    };
00315    transmitted.set_length(word_length);
00316    transmitted_hat.set_length(word_length);
00317    message_hat.set_length(amc.codec_message_size(amc.no_of_codecs()-1));
00318    decoded_bits.set_length(amc.codec_message_size(amc.no_of_codecs()-1));
00319    input.set_length(amc.codec_message_size(amc.no_of_codecs()-1));
00320 
00321    //soft_bit.set_length((ai[0].Ns-1)*ai[0].length_ix*bits_per_symbol);
00322 
00323    waveform_rx.set_size(no_ant,d_buffer_length);
00324    waveform_tx.set_length(d_buffer_length);
00325    waveform_tx_temp.set_length(d_buffer_length);
00326    waveform_rx.zeros();
00327    waveform_tx.zeros();
00328    waveform_tx_temp.zeros();
00329 
00330    BERraw.set_length(100);
00331    BER.set_length(100);
00332    EVM.set_length(100);
00333    SINR.set_length(100);
00334 
00335    ber_i=0;
00336 
00337 
00338    symbols.set_length((ai[0].Ns-1)*ai[0].length_ix);
00339    
00340 }
00341 
00342 IA1_node::~IA1_node(void) {
00343 };
00344 
00345 frame_settings IA1_node::node_init(void) {
00346 
00347   ber_i=0;
00348   d_frame_ix=0;
00349   feedback_buffer_size=num_antennas_in_system*num_ant_ms*
00350     sizeof(std::complex<int16_t>)*ai[0].Nfftr();
00351 
00352   feedback_buffer_size_tx=num_ant_bs*
00353     sizeof(std::complex<int16_t>)*ai[0].length_ix_all*num_streams_per_bs;
00354 
00355 
00356 
00357   if (feedback_buffer_size>feedback_buffer_size_tx) {
00358     feedback_buffer=(std::complex<int16_t> *) malloc(feedback_buffer_size);
00359     feedback_buffer0=(std::complex<int16_t> *) malloc(feedback_buffer_size);
00360     feedback_buffer1=(std::complex<int16_t> *) malloc(feedback_buffer_size);
00361     feedback_buffer2=(std::complex<int16_t> *) malloc(feedback_buffer_size);
00362 
00363   } else {
00364     feedback_buffer=(std::complex<int16_t> *) malloc(feedback_buffer_size_tx);
00365     feedback_buffer0=(std::complex<int16_t> *) malloc(feedback_buffer_size);
00366     feedback_buffer1=(std::complex<int16_t> *) malloc(feedback_buffer_size);
00367     feedback_buffer2=(std::complex<int16_t> *) malloc(feedback_buffer_size);
00368   };
00369 
00370   if (other_node_ixs.size()==0) {
00371     other_node_ixs.push_back(0); // Changed later on
00372     other_node_ixs.push_back(1);
00373     other_node_ixs.push_back(2);
00374     pointers_to_data.push_back(feedback_buffer0);
00375     pointers_to_data.push_back(feedback_buffer1);
00376     pointers_to_data.push_back(feedback_buffer2);
00377     num_bytess.push_back(feedback_buffer_size);
00378     num_bytess.push_back(feedback_buffer_size);
00379     num_bytess.push_back(feedback_buffer_size);
00380   };
00381 
00382 
00383   if ((scenario==3) && (!MIMO_closed_loop)) {
00384     do_calculate_beamformers=false;
00385     if (MIMO_all_active) {
00386       if ((d_node_ix==0) | (d_node_ix==3))
00387         stream_ix_this_node="0 1";
00388       if ((d_node_ix==1) | (d_node_ix==4))
00389         stream_ix_this_node="2 3";
00390       if ((d_node_ix==2) | (d_node_ix==5))
00391         stream_ix_this_node="4 5";
00392     };
00393   };
00394    
00395 
00396 
00397   if (is_bs) {
00398 
00399 
00400    // Initialize pre-coding matrices
00401    if (is_bs) {
00402      for (uint32_t i1=0;i1<num_streams_per_bs;i1++) {
00403        v[i1].set_size(d_no_ant,ai[i1].length_ix_all);
00404        v[i1].zeros();
00405        if ((d_node_ix==0) & (i1==0)) {
00406          v[i1].set_row(0,ones_c(ai[i1].length_ix_all));
00407          //Vbig[i1]=v[i1];
00408        };
00409      };
00410    };
00411 
00412 
00413 
00414     return node_process(); // TX
00415   } else { //RX
00416     frame_settings frame;
00417     std::vector<double> gain_rx_vec(1);
00418     std::vector<double> gain_tx_vec(1);
00419     std::vector<double> freq_vec(1);
00420 
00421     
00422     gain_rx_vec[0]=d_gain_rx;
00423     gain_tx_vec[0]=d_gain_tx;
00424     freq_vec[0]=d_frequency;
00425     frame.what=RX;
00426     frame.gain_rx=gain_rx_vec;
00427     frame.gain_tx=gain_tx_vec;
00428     frame.frequency=freq_vec;
00429     frame.time=offset_time;
00430 
00431 
00432     return frame;
00433   };
00434 
00435 };
00436 
00437 
00438 void IA1_node::end_of_run(){
00439   free(feedback_buffer);
00440   free(feedback_buffer0);
00441   free(feedback_buffer1);
00442   free(feedback_buffer2);
00443 
00444   BER=BER.mid(0,ber_i);
00445   BERraw=BERraw.mid(0,ber_i);
00446   EVM=EVM.mid(0,ber_i);
00447   SINR=SINR.mid(0,ber_i);
00448 };
00449 
00450 frame_settings IA1_node::node_process(void) {
00451 
00452 
00453   frame_settings frame;
00454   std::vector<double> gain_rx_vec(1);
00455   std::vector<double> gain_tx_vec(1);
00456   std::vector<double> freq_vec(1);
00457 
00458 
00459   gain_rx_vec[0]=d_gain_rx;
00460   gain_tx_vec[0]=d_gain_tx;
00461   freq_vec[0]=d_frequency;
00462 
00463   frame.frequency=freq_vec;
00464   frame.gain_rx=gain_rx_vec;
00465   frame.gain_tx=gain_tx_vec;
00466 
00467   d_frame_ix++;
00468 
00469 
00470   if (!is_bs) { /****** RX ***********/
00471 
00472     std::cout << "Processing in the receiver \n";
00473 
00474 
00475         // Feedback raw signal samples during common pilots
00476         if (true) { // Need only be done ones
00477 
00478             std::complex<int16_t> *p1;
00479             int c=0;
00480             for (uint32_t i2=0;i2<num_antennas_in_system;i2++) {
00481               
00482 
00483               int s=(ai[0].Nfftr()+ai[0].Np)*antenna_pilot_position(i2)+
00484                 demodulation_position(0); 
00485 
00486               for (uint32_t i3=0;i3<d_no_ant;i3++) {
00487                 
00488                 p1=(std::complex<int16_t> *) d_rx_buffers[i3];          
00489                 int d=ai[0].Nfftr()*sizeof(std::complex<int16_t>);              
00490                 memcpy(&feedback_buffer[c*ai[0].Nfftr()],&p1[s],d);             
00491                 c++;
00492                 
00493               };     
00494             };
00495 
00496 
00497             if (do_calculate_beamformers) {
00498                x2_tx(0,feedback_buffer,feedback_buffer_size);
00499             } else {
00500                x2_tx(0,feedback_buffer,10);
00501             };
00502         };
00503 
00504 
00505       frame.time=d_frame_ix*time_between_frames+offset_time;
00506 
00507       /* Data is available in d_rx_buffers[0] */
00508 
00509       for (uint32_t i2=0;i2<d_no_ant;i2++) {
00510         std::complex<int16_t> *p1;
00511         p1=(std::complex<int16_t> *) d_rx_buffers[i2];
00512         for (uint32_t i1=0;i1<d_buffer_length;i1++) 
00513           waveform_rx(i2,i1)=p1[i1];
00514       };
00515 
00516 
00517       for (uint32_t i1=0;i1<num_streams_per_ms;i1++) {
00518 
00519 
00520         ai[i1].extract_multi_antenna(waveform_rx,demodulation_position(i1),symbols);
00521 
00522         noise_variance_norm=amc.noise_variance(symbols,modulation_order);       
00523         ai[i1].de_interleave_symbols(symbols,stream_ix_this_node(i1)+d_frame_ix);
00524 
00525 
00526         SINR_temp=ai[i1].SINR;
00527         decode_rx_symbols(codec_ix,modulation_order,stream_ix_this_node(i1));
00528 
00529 
00530 
00531 
00532         //itpp::it_file d1;
00533         //d1.open("d1.it");
00534         //d1<< itpp::Name("w_rx") << waveform_rx;
00535         //d1.close();
00536 
00537 
00538                 
00539       };
00540       #if 0
00541       if ((d_frame_ix==2) & (d_node_ix>3)){
00542         cout << "d_node_ix=" << d_node_ix << endl;
00543         itpp::it_file d1;
00544         d1.open("d1.it");
00545         d1<< itpp::Name("waveform_rx") << waveform_rx;
00546         d1<< itpp::Name("symbols") << symbols;
00547         d1<< itpp::Name("h") << ai[0].h ;
00548         //d1<< itpp::Name("part_waveform_rx") << 
00549         //  waveform_rx.get_row(1).mid(ai[0].start_of_known_symbol()(0)+
00550         //                           demodulation_position(0),ai[0].Nfftr());
00551 
00552         //d1<< itpp::Name("Const") << ai[0].ConstellationStorage ;
00553         d1.close();
00554         exit(1);
00555       };
00556       #endif
00557 
00558       frame.what=RX;
00559 
00560       if (d_frame_ix==(d_no_frames)) 
00561         frame.what=END;
00562 
00563 
00564 
00565       return frame;
00566 
00567 
00568   };
00569 
00570   if (is_bs) { // ***** TX ******
00571 
00572 
00573 
00574     frame.time=(d_frame_ix-1)*time_between_frames+offset_time;
00575     std::cout << "Processing in the transmitter \n";  
00576     double rms_power=-10; // Disable calculation of rms_power
00577 
00578     
00579     if ((d_node_ix==0) & (d_frame_ix>1)){
00580 
00581       
00582       // Receive data from feedback buffer
00583       // node_ix=0 is the boss!
00584 
00585       cvec waveform_temp(ai[0].Nfftr());
00586       double nvn, nv;
00587       cvec h(ai[0].length_ix_all);
00588 
00589 
00590       
00591       other_node_ixs[0]=num_bs;
00592       other_node_ixs[1]=num_bs+1;
00593       other_node_ixs[2]=num_bs+2;
00594 
00595       std::vector<uint32_t> num_bytess_short(3);
00596       num_bytess_short[0]=10;
00597       num_bytess_short[1]=10;
00598       num_bytess_short[2]=10;
00599 
00600 
00601       if (do_calculate_beamformers) {
00602         x2_rx(other_node_ixs ,pointers_to_data,num_bytess);
00603       } else {
00604         x2_rx(other_node_ixs ,pointers_to_data,num_bytess_short);
00605       };
00606       std::complex<int16_t> *fe;
00607 
00608       for (uint32_t i1=0;i1<num_ms;i1++) {
00609           fe=(std::complex<short int>*) pointers_to_data[i1];
00610           std::cout << "i1=" << i1 << "\n";
00611           int i40=0;
00612           for (uint32_t i2=0;i2<num_antennas_in_system;i2++) {
00613             for (uint32_t i3=0;i3<num_ant_ms;i3++) {        
00614               for (uint32_t i4=0;i4<ai[0].Nfftr();i4++) {
00615                 waveform_temp(i4)=fe[i40+i4];
00616               }; // i4
00617               i40+=ai[0].Nfftr();
00618               ai[0].estimate_channel(waveform_temp,nvn,h,0,nv);
00619               H[i2][i1].set_row(i3,h);
00620             }; // i3
00621           }; // i2        
00622       }; //i1
00623       
00624       
00625       // Calculate beamformers for all streams
00626 
00627       timer.reset();
00628       timer.start();
00629       if (do_calculate_beamformers) {
00630         calculate_beamformers();
00631       };
00632       timer.stop();
00633       //std::cout << "timer.get_time()=" << timer.get_time() << std::endl;
00634       
00635       if (!do_calculate_beamformers)  {
00636         
00637         for (uint32_t i1=0;i1<num_streams;i1++) {
00638           for (uint32_t i2=0;i2<ai[0].length_ix_all;i2++) {
00639             for (int i3=0;i3<Vbig[i1].rows();i3++) {
00640         
00641               if (scenario==3) {                
00642                 if (((int) i3)==i1) {
00643                   Vbig[i1](i3,i2)=1;
00644                 } else {
00645                   Vbig[i1](i3,i2)=0;
00646                 }; // if
00647               } else { // scenario==4
00648                 if (((int) i3)==(d_frame_ix % num_ant_bs)) {
00649                      Vbig[i1](i3,i2)=1;
00650                   } else {
00651                      Vbig[i1](i3,i2)=0;
00652                 }; // if                
00653               };   
00654             }; // i3
00655           }; // i2
00656         }; // i1
00657       };
00658 
00659       
00660       if (d_simulate) {
00661           stringstream filename;
00662           stringstream variable_name;
00663           filename << "Vbig" << d_frame_ix-1 << "_" << scenario << ".it";
00664           itpp::it_file d1;
00665           d1.open(filename.str());
00666           
00667           
00668           d1<< itpp::Name("Vbig0") << Vbig[0];
00669           if (scenario!=4) 
00670           d1<< itpp::Name("Vbig1") << Vbig[1];
00671           if ((scenario!=3) && (scenario!=4)) 
00672             d1<< itpp::Name("Vbig2") << Vbig[2];
00673           d1.close();
00674       };
00675       
00676 
00677     };
00678 
00679 
00680     if (d_node_ix==0) {
00681 
00682       // Transmit precoders to the other TX:s
00683       //std::cout << "Transmit precoders to the other TX:s \n";
00684       
00685       if (d_frame_ix>1) {       
00686         for (uint32_t i1=1;i1<num_bs;i1++) {
00687           int i30=0;
00688           for (uint32_t i4=0;i4<num_streams_per_bs;i4++) {
00689             for (uint32_t i2=0;i2<d_no_ant;i2++) {
00690               for (uint32_t i3=0;i3<ai[0].length_ix_all;i3++) {         
00691                 feedback_buffer[i3+i30]=10000*(Vbig[bs_to_stream_ix(i1,i4)]
00692                                                (i2,i3));
00693                 
00694               };
00695               i30+=ai[0].length_ix_all;
00696             };
00697           };
00698           x2_tx(i1,feedback_buffer,feedback_buffer_size_tx);
00699         };      
00700         v[0]=Vbig[0];
00701         if (scenario==2) {
00702           v[1]=Vbig[1];
00703           v[2]=Vbig[2];
00704         };
00705         if (scenario==3) {
00706           v[1]=Vbig[1];
00707         };
00708       };
00709       //std::cout << "Done transmit precoders to the other TX:s \n";
00710 
00711       
00712      } else { 
00713 
00714       if (d_frame_ix>1) {
00715 
00716         // Receive precoders from node_ix=0
00717         
00718         x2_rx(0,feedback_buffer,feedback_buffer_size_tx);
00719       
00720         int i30=0;
00721         complex<double> temp;
00722         for (uint32_t i4=0;i4<num_streams_per_bs;i4++) {
00723           for (uint32_t i2=0;i2<d_no_ant;i2++) {        
00724             for (uint32_t i3=0;i3<ai[0].length_ix_all;i3++) {
00725               temp=feedback_buffer[i3+i30];
00726               temp=temp*1e-4;
00727               v[i4](i2,i3)=temp;
00728             };
00729             i30+=ai[0].length_ix_all;
00730           };
00731         };
00732       };
00733     };
00734 
00735    
00736     if (scenario==4) {
00737       (v[0]).zeros();
00738     };
00739     
00740 
00741     if ((scenario==3) && (!(d_node_ix==MIMO_active_tx_node_tx)) && (!MIMO_all_active)) {
00742       (v[0]).zeros();
00743       (v[1]).zeros();
00744     };
00745 
00746 
00747     // Format data to be transmitted in the next burst
00748     //timer.reset();
00749     //timer.start();
00750 
00751 
00752     
00753     for (uint32_t i2=0;i2<d_no_ant;i2++) {
00754       
00755 
00756 
00757       waveform_tx.zeros();
00758       ai[0].insert_pilot(waveform_tx,antenna_pilot_position_this_bs[i2], 
00759                    shift_transmit_signal_samples,
00760                                           ones_c(ai[0].length_ix_all));
00761       
00762 
00763       // Payload data
00764       for (uint32_t i1=0;i1<num_streams_per_bs;i1++) {
00765 
00766         
00767         format_tx_symbols(codec_ix,modulation_order,stream_ix_this_node(i1));
00768         ai[i1].interleave_symbols(symbols,stream_ix_this_node(i1)+d_frame_ix);
00769 
00770         
00771         waveform_tx_temp.zeros();
00772         ai[i1].modulate(waveform_tx_temp,symbols,shift_transmit_signal_samples,
00773                         rms_power,v[i1].get_row(i2));
00774         waveform_tx+=waveform_tx_temp;
00775 
00776         // Create replica
00777         waveform_tx.replace_mid(replica_positions_this_bs(i1),
00778                  waveform_tx_temp.mid(shift_transmit_signal_samples,replicas_spacing));
00779         
00780 
00781        ai[0].insert_pilot(waveform_tx,antenna_pilot_position_this_bs(i2), 
00782                    replica_positions_this_bs(i1),
00783                                           ones_c(ai[0].length_ix_all));
00784 
00785 
00786 
00787       };
00788       
00789 
00790       
00791 
00792       waveform_tx=scaling_tx_signal*waveform_tx;
00793 
00794      std::complex<int16_t> *p1;
00795       p1=(std::complex<int16_t> *) d_tx_buffers[i2];
00796       for (uint32_t i1=0;i1<d_buffer_length;i1++) {
00797         p1[i1]=waveform_tx(i1);
00798       };      
00799 
00800       //timer.stop();
00801       //std::cout << "Time formatting data timer.get_time()=" << timer.get_time() << std::endl;
00802 
00803 
00804 
00805     };
00806 
00807 
00808     //cout << "w=" << waveform_tx.mid(0,200) << endl;
00809     frame.what=TX;
00810 
00811     if (d_frame_ix==(d_no_frames+1)) 
00812       frame.what=END;
00813 
00814   };
00815   
00816       
00817   return frame;
00818   
00819 
00820 }
00821 
00822 
00823 void IA1_node::normalize_columns(cmat &M) {
00824   double n;
00825   for (int i1=0; i1<M.cols();i1++) {
00826     
00827     n=norm(M.get_col(i1),2);
00828     M.set_col(i1,M.get_col(i1)/n);
00829 
00830   };
00831 };
00832 
00833 
00834 
00835 void IA1_node::format_tx_symbols(uint32_t codec_ix, uint32_t modulation_order,int stream_ix) 
00836 {
00837 
00838   
00839   int l=amc.codec_message_size(codec_ix);
00840 
00841   if (use_old_rand) {
00842     RNG_reset(100+10000*stream_ix+d_seed);
00843     input.set_subvector(0,randb(l));
00844   } else {
00845     temp_seed=100+10000*stream_ix+d_seed;
00846     amc.randb_thread_safe(0,l,input,&temp_seed) ;
00847   };
00848 
00849   amc.modulate(modulation_order,codec_ix,input.mid(0,l),
00850                transmitted,symbols,0) ;    
00851 
00852 
00853 
00854   if (modulation_order>15) {
00855       if (use_old_rand) {
00856         RNG_reset(110+10000*stream_ix+d_seed);
00857         input.set_subvector(0,randb(l));
00858       } else {
00859         temp_seed=110+10000*stream_ix+d_seed;
00860         amc.randb_thread_safe(0,l,input,&temp_seed) ;
00861       };
00862 
00863       amc.modulate(modulation_order,codec_ix,input.mid(0,l),
00864            transmitted,symbols, word_length/(log2(modulation_order))) ;
00865   };
00866 
00867   if (modulation_order>63) {
00868       if (use_old_rand) {
00869         RNG_reset(120+10000*stream_ix+d_seed);
00870         input.set_subvector(0,randb(l));
00871       } else {
00872         temp_seed=120+10000*stream_ix+d_seed;
00873         amc.randb_thread_safe(0,l,input,&temp_seed) ;
00874       };
00875 
00876       amc.modulate(modulation_order,codec_ix,input.mid(0,l),
00877                    transmitted,symbols,2*word_length/(log2(modulation_order)));
00878     
00879   };
00880   if (modulation_order>255) {
00881 
00882       if (use_old_rand) {
00883         RNG_reset(130+10000*stream_ix+d_seed);
00884         input.set_subvector(0,randb(l));
00885       } else {
00886         temp_seed=130+10000*stream_ix+d_seed;
00887         amc.randb_thread_safe(0,l,input,&temp_seed) ;
00888       };
00889       amc.modulate(modulation_order,codec_ix,input.mid(0,l),
00890                    transmitted,symbols,3*word_length/(log2(modulation_order)));
00891     
00892   };
00893 
00894 };
00895 
00896 
00897 void IA1_node::decode_rx_symbols(uint32_t codec_ix, uint32_t modulation_order, int stream_ix)
00898 { 
00899 
00900 
00901   cout << "(" << d_node_ix << "," << stream_ix << ")" << endl;
00902     
00903   int l=amc.codec_message_size(codec_ix);
00904 
00905   if (use_old_rand) {
00906     RNG_reset(100+10000*stream_ix+d_seed);
00907     input.set_subvector(0,randb(l));
00908   } else {
00909      temp_seed=100+10000*stream_ix+d_seed;
00910      amc.randb_thread_safe(0,l,input,&temp_seed) ;
00911   };
00912 
00913   amc.encode(codec_ix,input.mid(0,l),transmitted) ;
00914   amc.demodulate(symbols.mid(0,word_length/log2( modulation_order)),
00915                           noise_variance_norm,
00916                   modulation_order,codec_ix,message_hat,transmitted_hat);
00917 
00918 
00919   if (d_run_decoder) {
00920      EVM[ber_i]=amc.evm(modulation_order,transmitted,symbols,0);
00921      SINR[ber_i]=SINR_temp;
00922   };
00923 
00924   berc.clear();
00925   berc.count(transmitted,transmitted_hat);  
00926   BERraw[ber_i]=berc.get_errorrate();
00927   SINR[ber_i]=SINR_temp;
00928   berc.clear();
00929   berc.count(input.mid(0,l),message_hat.mid(0,l));  
00930   BER[ber_i]=berc.get_errorrate();
00931   ber_i++;
00932   
00933   
00934 
00935 
00936   if (modulation_order>15) {
00937 
00938     if (use_old_rand) {
00939       RNG_reset(110+10000*stream_ix+d_seed);
00940       input.set_subvector(0,randb(l));
00941     } else {
00942       temp_seed=110+10000*stream_ix+d_seed;
00943       amc.randb_thread_safe(0,l,input,&temp_seed) ;
00944     };
00945     amc.encode(codec_ix,input.mid(0,l),transmitted) ;
00946 
00947     amc.demodulate(symbols.mid(word_length/log2( modulation_order),
00948                                  word_length/log2( modulation_order)),
00949                           noise_variance_norm,
00950                   modulation_order,codec_ix,message_hat,transmitted_hat);
00951     
00952     
00953     if (d_run_decoder) {
00954       EVM[ber_i]=amc.evm(modulation_order,transmitted,symbols,
00955                 word_length/(log2(modulation_order)));
00956       SINR[ber_i]=SINR_temp;
00957     };
00958       
00959     berc.clear();
00960     berc.count(transmitted,transmitted_hat);  
00961     BERraw[ber_i]=berc.get_errorrate();
00962     //cout << "BER(raw)=" << BERraw[ber_i] << endl;
00963     berc.clear();
00964     berc.count(input.mid(0,l),message_hat.mid(0,l));  
00965     BER[ber_i]=berc.get_errorrate();
00966     //cout << "BER=" << BER[ber_i] << endl;
00967     ber_i++;
00968 
00969   };
00970 
00971   if (modulation_order>63) {
00972 
00973     if (use_old_rand) {
00974       RNG_reset(120+10000*stream_ix+d_seed);
00975       input.set_subvector(0,randb(l));
00976     } else {
00977      temp_seed=120+10000*stream_ix+d_seed;
00978      amc.randb_thread_safe(0,l,input,&temp_seed) ;
00979 
00980     };
00981     amc.encode(codec_ix,input.mid(0,l),transmitted) ;
00982 
00983     amc.demodulate(symbols.mid(2*word_length/log2( modulation_order),
00984                                  word_length/log2( modulation_order)),
00985                           noise_variance_norm,
00986                   modulation_order,codec_ix,message_hat,transmitted_hat);
00987 
00988 
00989     if (d_run_decoder) {
00990       EVM[ber_i]=amc.evm(modulation_order,transmitted,symbols,
00991                          2*word_length/(log2(modulation_order)));
00992       SINR[ber_i]=SINR_temp;
00993     };
00994       
00995     berc.clear();
00996     berc.count(transmitted,transmitted_hat);  
00997     BERraw[ber_i]=berc.get_errorrate();
00998     //cout << "BER(raw)=" << BER[0] << endl;
00999     berc.clear();
01000     berc.count(input.mid(0,l),message_hat.mid(0,l));  
01001     BER[ber_i]=berc.get_errorrate();
01002     //cout << "BER=" << BER[0] << endl;
01003     ber_i++;
01004 
01005   };
01006   if (modulation_order>255) {
01007 
01008     if (use_old_rand) {
01009       RNG_reset(130+10000*stream_ix+d_seed);
01010       input.set_subvector(0,randb(l));
01011     } else {
01012      temp_seed=130+10000*stream_ix+d_seed;
01013      amc.randb_thread_safe(0,l,input,&temp_seed) ;
01014     };
01015 
01016     amc.encode(codec_ix,input.mid(0,l),transmitted) ;
01017     amc.demodulate(symbols.mid(3*word_length/log2( modulation_order),
01018                                  word_length/log2( modulation_order)),
01019                           noise_variance_norm,
01020                   modulation_order,codec_ix,message_hat,transmitted_hat);
01021 
01022     if (d_run_decoder) {
01023      EVM[ber_i]=amc.evm(modulation_order,transmitted,symbols,
01024                         3*word_length/(log2(modulation_order)));
01025      SINR[ber_i]=SINR_temp;
01026     };
01027 
01028     berc.clear();
01029     berc.count(transmitted,transmitted_hat);  
01030     BERraw[ber_i]=berc.get_errorrate();
01031     //cout << "BER(raw)=" << BER[0] << endl;
01032     berc.clear();
01033     berc.count(input.mid(0,l),message_hat.mid(0,l));  
01034     BER[ber_i]=berc.get_errorrate();
01035     //cout << "BER=" << BER[0] << endl;
01036     ber_i++;
01037 
01038   };
01039   
01040 };
 All Classes Functions Variables