예제 #1
0
파일: rx_60GHz.cpp 프로젝트: tony2909/green
int UHD_SAFE_MAIN(int argc, char *argv[]) {


    // Set priority of the main thread
    int which = PRIO_PROCESS;
    id_t pid;
    int priority = -19;
    int ret;

    pid = getpid();
    ret = setpriority(which, pid, priority);
    if(ret!=0) {
        std::cout << "Main priority went wrong: " << ret << std::endl ;
    }


    //Seting priority in the processor to run faster -> run with sudo
    if (!(uhd::set_thread_priority_safe(1,true))) {
        std::cout << "Problem setting thread priority" << std::endl;
        return 1;
    };


    if (!(uhd::set_thread_priority_safe(1,true))) {
        std::cout << "Problem setting thread priority" << std::endl;
        return 1;
    };


    //variables to be set by po
    //double seconds_in_future=0.01;
    size_t total_num_samps;
    double rx_rate, freq, LOoffset;
    bool use_external_10MHz;
    bool realTime;
    double scaling_8bits;
    std::string filename;
    float gain;
    uhd::device_addr_t dev_addr;
    uhd::usrp::multi_usrp::sptr dev;
    uhd::tune_result_t tr;
    uhd::stream_args_t stream_args;
    uhd::rx_streamer::sptr rx_stream;


    //setup the program options
    po::options_description desc("Allowed options");
    desc.add_options()
    ("help", "help message")
    ("nsamps", po::value<size_t>(&total_num_samps)->default_value(150000), "total number of samples to receive")
    ("rxrate", po::value<double>(&rx_rate)->default_value(100e6/4), "rate of incoming samples")
    ("freq", po::value<double>(&freq)->default_value(70e6), "rf center frequency in Hz")
    ("LOoffset", po::value<double>(&LOoffset)->default_value(0), "Offset between main LO and center frequency")
    ("10MHz",po::value<bool>(&use_external_10MHz)->default_value(false), "external 10MHz on 'REF CLOCK' connector (true=1=yes)")
    //  ("PPS",po::value<bool>(&trigger_with_pps)->default_value(false), "trigger reception with 'PPS IN' connector (true=1=yes)")
    ("filename",po::value<std::string>(&filename)->default_value("data_from_usrp.dat"), "output filename")
    ("gain",po::value<float>(&gain)->default_value(5), "set the receiver gain (0-15)")
    ("8bits_scaling",po::value<double>(&scaling_8bits)->default_value(0.0),
     "input scaling (invers) when 8bits is used, set to zero to get 16bits")
    ("realTime",po::value<bool>(&realTime)->default_value(true), "receives in loop and compares with synch sequence")
    ;

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    //print the help message
    if (vm.count("help")) {
        std::cout << boost::format("rx %s") % desc << std::endl;
        return ~0;
    }


    dev_addr["addr0"]="192.168.10.2";
    //dev_addr["addr0"]="192.168.10.2";
    dev = uhd::usrp::multi_usrp::make(dev_addr);

    //dev->set_rx_subdev_spec(uhd::usrp::subdev_spec_t("A:A"), 0); // 60GHz

    stream_args.cpu_format="sc16";
    if (scaling_8bits==0.0) {
        stream_args.otw_format="sc16";
    } else {
        stream_args.otw_format="sc8";
        std::stringstream temp_ss;
        temp_ss << scaling_8bits;
        stream_args.args["peak"]=temp_ss.str();
    };

    rx_stream=dev->get_rx_stream(stream_args);

    uhd::clock_config_t my_clock_config;


    if (use_external_10MHz) {
        my_clock_config.ref_source=uhd::clock_config_t::REF_SMA;
    };

    //print the help message
    if (vm.count("help")) {
        std::cout << boost::format("UHD RX Timed Samples %s") % desc << std::endl;
        return ~0;
    }

    //create a usrp device
    std::cout << std::endl;
    uhd::device::sptr udev = dev->get_device();
    dev->set_rx_rate(rx_rate);
    uhd::tune_request_t trq(freq,LOoffset);
    tr=dev->set_rx_freq(trq);


    uhd::usrp::dboard_iface::sptr db_iface;
    db_iface=dev->get_tx_dboard_iface(0);

    board_60GHz_RX my_60GHz_RX(db_iface);    // 60GHz
    my_60GHz_RX.set_gain(gain);    // 60GHz


    if (use_external_10MHz) {
        dev->set_clock_config(my_clock_config);
        usleep(1e6); // Wait for the 10MHz to lock
    };

    size_t buffer_size=1000; // Select buffer size
    int nStorage=2*total_num_samps;

    dev->set_time_now(uhd::time_spec_t(0.0));

    std::cout << boost::format("Setting device timestamp to 0...") << std::endl;


    //    % total_num_samps % seconds_in_future << std::endl;
    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);



    stream_cmd.num_samps = buffer_size;
    stream_cmd.stream_now = true;

    stream_cmd.stream_mode=uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;


    dev->issue_stream_cmd(stream_cmd);






    //////////////////////////////////Read data to buff_short and do processing////////////

    //Launch threads
    sem_init(&usrpReady, 0,0);
    sem_init(&detectionReady, 0,0);
    std::thread usrpT(usrpGetData, rx_stream, dev, buffer_size, &my_60GHz_RX);
    std::thread detectionT(detection, buffer_size, nStorage);

    // Set highest priority for usrpT
    sched_param sch;
    int policy;
    pthread_getschedparam(usrpT.native_handle(), &policy, &sch);
    sch.sched_priority = 99;
    if(pthread_setschedparam(usrpT.native_handle(), SCHED_FIFO, &sch)) {
        std::cout << "Failed to setschedparam: " << std::strerror(errno) << '\n';
    }
    // pthread_getschedparam(detectionT.native_handle(), &policy, &sch);
    // sch.sched_priority = 99;
    // if(pthread_setschedparam(detectionT.native_handle(), SCHED_FIFO, &sch)) {
    //     std::cout << "Failed to setschedparam: " << std::strerror(errno) << '\n';
    // }

    std::cout << "Priority set"<< std::endl;

    usrpT.join();
    detectionT.join();

    //finished
    std::cout << std::endl << "Done!" << std::endl << std::endl;

    return 0;
}
예제 #2
0
파일: tx_60GHz.cpp 프로젝트: tony2909/green
int UHD_SAFE_MAIN(int argc, char *argv[]){



    if (uhd::set_thread_priority_safe(1,true)) {
       std::cout << "set priority went well " << std::endl;
    };


    //variables to be set by po
    std::string args;
    double seconds_in_future;
    size_t total_num_samps;
    double tx_rate, freq, LOoffset;
    float gain;
    bool forever, use_8bits;
    bool use_external_10MHz; 
    std::string filename;
    uhd::tx_streamer::sptr tx_stream;
    uhd::device_addr_t dev_addr;
    uhd::usrp::multi_usrp::sptr dev;
    uhd::stream_args_t stream_args;

    //setup the program options
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "help message")
        ("args", po::value<std::string>(&args)->default_value(""), "simple uhd device address args")
        ("secs", po::value<double>(&seconds_in_future)->default_value(3), "number of seconds in the future to transmit")
        ("nsamps", po::value<size_t>(&total_num_samps)->default_value(1000), "total number of samples to transmit")
        ("txrate", po::value<double>(&tx_rate)->default_value(100e6/4), "rate of outgoing samples")
        ("freq", po::value<double>(&freq)->default_value(70e6), "rf center frequency in Hz")
        ("LOoffset", po::value<double>(&LOoffset)->default_value(0), "Offset between main LO and center frequency")
        ("forever",po::value<bool>(&forever)->default_value(true), "run indefinetly")
        ("10MHz",po::value<bool>(&use_external_10MHz)->default_value(false), 
	     "external 10MHz on 'REF CLOCK' connector (true=1=yes)")
      //("PPS",po::value<bool>(&trigger_with_pps)->default_value(false), 
      //      "trigger reception with 'PPS IN' connector (true=1=yes)")
        ("filename",po::value<std::string>(&filename)->default_value("data_to_usrp.dat"), "input filename")
        ("gain",po::value<float>(&gain)->default_value(13), "gain of transmitter(0-13) ")
        ("8bits",po::value<bool>(&use_8bits)->default_value(false), "Use eight bits/sample to increase bandwidth")
    ;

    
    
    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    //print the help message
    if (vm.count("help")){
        std::cout << boost::format("tx %s") % desc << std::endl;
        return ~0;
    }

    /* Create buffer storage */
    std::complex<int16_t> *buffer;
    buffer = new std::complex<int16_t>[total_num_samps];


    /* Read input from disc */
    FILE *fp = 0;
    fp = fopen(filename.c_str(), "rb");    
    if (fp == 0){
	perror(filename.c_str());
	return 1;
    }
    int r=fread(buffer, sizeof(uint32_t),total_num_samps, fp);
    printf("r=%d \n",r);
    fclose(fp);


    //create a usrp device and streamer
    dev_addr["addr0"]="192.168.10.2";
    dev = uhd::usrp::multi_usrp::make(dev_addr);    


    // Internal variables 
    uhd::clock_config_t my_clock_config; 

    if (!forever) {
      dev->set_time_source("external");
    };

    if (use_external_10MHz) { 
      dev->set_clock_source("external");
    }
    else {
      dev->set_clock_source("internal");
    };


    uhd::usrp::dboard_iface::sptr db_iface;
    db_iface=dev->get_tx_dboard_iface(0);

    board_60GHz_TX  my_60GHz_TX(db_iface);  //60GHz 
    my_60GHz_TX.set_gain(gain); // 60GHz

    uhd::tune_result_t tr;
    uhd::tune_request_t trq(freq,LOoffset); //std::min(tx_rate,10e6));
    tr=dev->set_tx_freq(trq,0);
    

    //dev->set_tx_gain(gain);
    std::cout << tr.to_pp_string() << "\n";
 

    stream_args.cpu_format="sc16";
    if (use_8bits)
      stream_args.otw_format="sc8";
    else
      stream_args.otw_format="sc16";

    tx_stream=dev->get_tx_stream(stream_args);


    //set properties on the device
    std::cout << boost::format("Setting TX Rate: %f Msps...") % (tx_rate/1e6) << std::endl;
    dev->set_tx_rate(tx_rate);
    std::cout << boost::format("Actual TX Rate: %f Msps...") % (dev->get_tx_rate()/1e6) << std::endl;
    std::cout << boost::format("Setting device timestamp to 0...") << std::endl;


    

    uhd::tx_metadata_t md;

    if (forever) {

      std::cout << "Stop the transmitter by pressing ctrl-c \n";

      md.start_of_burst = true;
      md.end_of_burst = false;
      md.has_time_spec = false;
      
     
      //send the entire buffer, let the driver handle fragmentation

      /*
      num_tx_samps = dev->send(
      buffer, total_num_samps, md,
      uhd::io_type_t::COMPLEX_INT16,
      uhd::device::SEND_MODE_FULL_BUFF);
      */
      tx_stream->send(buffer,total_num_samps,md,60);
      
      md.start_of_burst = false;

      while (1) {

	/*
	num_tx_samps = dev->send(
        buffer, total_num_samps, md,
        uhd::io_type_t::COMPLEX_INT16,
        uhd::device::SEND_MODE_FULL_BUFF
         );
	*/
        tx_stream->send(buffer,total_num_samps,md,3);

	md.start_of_burst = false;
	md.end_of_burst = false;
	md.has_time_spec = false;

      }
    }
    else
    {
    
    dev->set_time_now(uhd::time_spec_t(0.0));
    md.start_of_burst = true;
    md.end_of_burst = true;
    md.has_time_spec = true;
    md.time_spec = uhd::time_spec_t(seconds_in_future);

    //send the entire buffer, let the driver handle fragmentation

    /*
      num_tx_samps = dev->send(
	buffer, total_num_samps, md,
        //&buff.front(), buff.size(), md,
        uhd::io_type_t::COMPLEX_FLOAT32,
        uhd::device::SEND_MODE_FULL_BUFF);
    */
    
    tx_stream->send(buffer,total_num_samps,md,60);
    

    };

    //finished
    std::cout << std::endl << "Done!" << std::endl << std::endl;


    return 0;
}
예제 #3
0
파일: rx.cpp 프로젝트: tony2909/green
int UHD_SAFE_MAIN(int argc, char *argv[]){
    
     //Seting priority in the processor to run faster -> run with sudo
    if (!(uhd::set_thread_priority_safe(1,true))) {
      std::cout << "Problem setting thread priority" << std::endl;
      return 1;
    };


    //variables to be set by po -> Set when initializing the rx program
    //double seconds_in_future=0.01;
    size_t total_num_samps;
    double rx_rate, freq, LOoffset;
    bool use_external_10MHz;
    double scaling_8bits;
    std::string filename;
    float gain;
    bool realTime;
    uhd::device_addr_t dev_addr;
    uhd::usrp::multi_usrp::sptr dev;
    uhd::tune_result_t tr;
    uhd::stream_args_t stream_args;
    uhd::rx_streamer::sptr rx_stream;


    //setup the program options-> Passing it from terminal with boost library 
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "help message")
        ("nsamps", po::value<size_t>(&total_num_samps)->default_value(1000), "total number of samples to receive")
        ("rxrate", po::value<double>(&rx_rate)->default_value(100e6/4), "rate of incoming samples")
        ("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz")
        ("LOoffset", po::value<double>(&LOoffset)->default_value(0), "Offset between main LO and center frequency")
        ("10MHz",po::value<bool>(&use_external_10MHz)->default_value(false), "external 10MHz on 'REF CLOCK' connector (true=1=yes)")
      //  ("PPS",po::value<bool>(&trigger_with_pps)->default_value(false), "trigger reception with 'PPS IN' connector (true=1=yes)")
        ("filename",po::value<std::string>(&filename)->default_value("data_from_usrp.dat"), "output filename") 
        ("gain",po::value<float>(&gain)->default_value(0), "set the receiver gain") 
        ("8bits_scaling",po::value<double>(&scaling_8bits)->default_value(0.0), 
    "input scaling (invers) when 8bits is used, set to zero to get 16bits")
      
      /////////////////////////////
      ("realTime",po::value<bool>(&realTime)->default_value(false), "receives in loop and compares with synch sequence")
    ;
   
    //Variables stored in boost objects
    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

     //print the help message
    if (vm.count("help")){
        std::cout << boost::format("rx %s") % desc << std::endl;
        return ~0;
    }

    ///////////Set device variables to read data////////////////
    
    dev_addr["addr0"]="192.168.10.2";
    dev = uhd::usrp::multi_usrp::make(dev_addr);//Create the device

    //receiving format
    stream_args.cpu_format="sc16";
    if (scaling_8bits==0.0) {
      stream_args.otw_format="sc16";     
    } else {
      stream_args.otw_format="sc8";
      std::stringstream temp_ss;
      temp_ss << scaling_8bits;
      stream_args.args["peak"]=temp_ss.str();
    };

    rx_stream=dev->get_rx_stream(stream_args); //set/get the streamer values to the device

    uhd::clock_config_t my_clock_config; 

    /*
    if (trigger_with_pps) {
      my_clock_config.pps_source=uhd::clock_config_t::PPS_SMA; 
    };
    */

    if (use_external_10MHz) { 
      my_clock_config.ref_source=uhd::clock_config_t::REF_SMA; 
    }; 

    

   /////////////////Create an USRP device////////////////////////
    std::cout << std::endl;
    //uhd::device::sptr udev = dev->get_device();
    dev->set_rx_rate(rx_rate);

    bool is_xcvr2450=false;
    uhd::dict<std::string, std::string> rx_info;    
    rx_info=dev->get_usrp_rx_info(0);

    if (rx_info.has_key("rx_subdev_name")) {
      std::string str=rx_info.get("rx_subdev_name");
      uint temp=str.find("XCVR2450");
      if (temp<str.length()) {
	is_xcvr2450=true;
      };
    };

    if (is_xcvr2450) {
      dev->set_tx_antenna("J2");
      dev->set_rx_antenna("J1");      
      //uhd::meta_range_t range=dev->get_tx_bandwidth_range();

      if (LOoffset>=9e6) {
	dev->set_rx_bandwidth(3.96e+07);
      };      
    };



    std::cout << "rx_rate=" << rx_rate << std::endl;
    std::cout << "freq=" << freq << std::endl;
    std::cout << "gain=" << gain << std::endl;
    uhd::tune_request_t trq(freq,LOoffset); 


    //dev->set_rx_bandwidth(36e6);

    tr=dev->set_rx_freq(trq);
    dev->set_rx_gain(gain);

    std::cout << "tr=" << tr.actual_rf_freq << std::endl;

    /*
        double target_rf_freq;
        double actual_rf_freq;
        double target_dsp_freq;
        double actual_dsp_freq;
    */

    // 
    //dev->set_tx_antenna("J2");
    //dev->set_rx_antenna("J1");
    

    if (use_external_10MHz) {
      dev->set_clock_config(my_clock_config); // PZ
      usleep(1e6); // Wait for the 10MHz to lock
    }; 

    size_t buffer_size=1000; // Select buffer size
    short *buff_short, *storage_short; //Always read short buffers
   
    storage_short=new short[2*total_num_samps]; // Create storage for the entire received signal to be saved on disk (2* for handling complex).
    
    buff_short=new short[2*buffer_size]; // Create storage for a single buffer


    /*if (trigger_with_pps) {
      dev->set_time_next_pps(uhd::time_spec_t(0.0));
      usleep(1e6); 
      } */
    //else {
      dev->set_time_now(uhd::time_spec_t(0.0));
      //};
    std::cout << boost::format("Setting device timestamp to 0...") << std::endl;

    //setup streaming
    //std::cout << std::endl;
    //std::cout << boost::format("Begin streaming %u samples, %d seconds in the future...")
    //    % total_num_samps % seconds_in_future << std::endl;
    

    //////////////////////////////////Read data to buff_short and do processing////////////

    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);

    if (realTime == false){

      
    //Reads just one time the USRP rx dev and process data

    stream_cmd.num_samps = buffer_size;
    stream_cmd.stream_now = true;

    
    stream_cmd.stream_mode=uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;

    //stream_cmd.time_spec = uhd::time_spec_t(seconds_in_future);
    

    dev->issue_stream_cmd(stream_cmd);

    size_t num_rx_samps=0;
    size_t num_rx_samps_latest_call;

    uhd::rx_metadata_t md;
    
    //keep reading until everything is read


    while (num_rx_samps<total_num_samps) {
      
     
      num_rx_samps_latest_call=0;   

       
      while (num_rx_samps_latest_call==0) {
	 num_rx_samps_latest_call= 
	   rx_stream->recv(&buff_short[0],buffer_size, md, 3.0);
       };
      
       if (num_rx_samps_latest_call!=buffer_size) {
	 std::cerr << "I expect the buffer size to be always the same!\n";
	 std::cout<<"Read only:"<<num_rx_samps_latest_call<<"\n";
	 std::cout<<"Buffer:"<<buffer_size<<"\n";
         exit(1); 
	 //May be changed->don't want program to crash when data is not available
       };

       /* Process the just received buffer -> Put all toghether in one buffer*/
       int i1=2*num_rx_samps;
       int i2=0;
      
       while ((i1<(int) (2*total_num_samps)) && (i2<2*((int) buffer_size))) 
       {	  
	  storage_short[i1]=buff_short[i2];
	  i1++; i2++;
       };
	 
       num_rx_samps=num_rx_samps+num_rx_samps_latest_call;
       std::cout << "num_rx_samps=" << num_rx_samps  << std::endl;
    };


    // Save output to disc

    //Computes total power:
    double P=powerTotArray( storage_short, 2*total_num_samps);

    std::cout << "\nTotal power=" << P <<"\n";

    //Computes power in the output of 25 filters:
    int num_filt=25;
       
    double shiftindex = 0.04;//how big the shift between the filters is (equidistantial)

    double *power_band;
    power_band=new double[num_filt];

     double *buff_double;
     buff_double=new double[2*buffer_size];
    

    for(int ii=0;ii<(int)(2*buffer_size);ii++){
      	 buff_double[ii]=(double)buff_short[ii];

    };

         
    powerOfFreqBands(buff_double, 2*buffer_size, shiftindex, power_band,num_filt);
	     

     for(int ii=0; ii< num_filt; ii++){
       DispVal(power_band[ii]);
     };

    std::ofstream s1(filename.c_str(), std::ios::binary);   


    s1.write((char *) storage_short,4*total_num_samps); 
    s1.flush(); //PZ
    s1.close(); //PZ
    

    //Process the received data with MATLAB from written file

    //finished reading 
    std::cout << std::endl << "Done reading->Data to MATLAB!" << std::endl << std::endl;

    }else{

      //////////REAL TIME IMPLEMENTATION////////////////////////////////////////////////////////////////////
      //Reads in loop untill it finds someone is trying to transmit -> Power detection and training sequence match///////////////////////////////////////////////////////////////////////////////////////////
    
  
	  
    std::cout << "Stop the transmitter by pressing ctrl-c\n";
	  
     
       size_t num_rx_samps_latest_call;
       size_t num_rx_samps;
          
      
       uhd::rx_metadata_t md;
      
       
       int num_filt=25;
       
       double shiftindex = 0.04;//how big the shift between the filters is (equidistantial)
       

       double *power_band;
       power_band=new double[num_filt];
     
       double *total_bandPower;
       total_bandPower=new double[num_filt];

       
       //Cycle to read continuously data

	 num_rx_samps=0;
	    
	 // std::cout<<"buffer:"<<buffer_size<<"\n";

	 for(int ii=0;ii<num_filt;ii++){
	   total_bandPower[ii]=0.0;
	 }
	  
	 int loop_count=0;

	 int size_all=100;
	 
	 total_num_samps= 4000;

	 double * all[total_num_samps];
	 
	 for(int ii=0;ii<size_all;ii++){
	   all[ii]= new double[2*(int)(total_num_samps)];
	 }


	  while(loop_count<size_all){

	    //DispVal(loop_count);

	     stream_cmd.num_samps = buffer_size;
	     stream_cmd.stream_now = true;

	     stream_cmd.stream_mode=uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;

	     //stream_cmd.time_spec = uhd::time_spec_t(seconds_in_future);

	     dev->issue_stream_cmd(stream_cmd);

	    while (num_rx_samps<total_num_samps) {

	     
	     num_rx_samps_latest_call=0;   
	     	
       
	     while (num_rx_samps_latest_call==0) {
	       num_rx_samps_latest_call= 
		 rx_stream->recv(&buff_short[0],buffer_size, md, 3.0);
	      };

	    

	     
	     if (num_rx_samps_latest_call!=buffer_size) {
	       std::cerr << "I expect the buffer size to be always the same!\n";
	       std::cout<<"Read only:"<<num_rx_samps_latest_call<<"\n";
	       std::cout<<"Buffer:"<<buffer_size<<"\n";
	       exit(1); 
	       //May be changed->don't want program to crash when data is not available
	     };

	     /* Process the just received buffer -> Put all toghether in one buffer*/
	     int i1=2*num_rx_samps;
	     int i2=0;
      
	     while ((i1<(int) (2*total_num_samps)) && (i2<2*((int) buffer_size))) 
	       {	  
		all[loop_count][i1]=(double)buff_short[i2];
		 i1++; i2++;
	       };
	 
	     num_rx_samps=num_rx_samps+num_rx_samps_latest_call;
	     //std::cout << "num_rx_samps=" << num_rx_samps  << std::endl;
  
	   };

	   
	    stream_cmd.stream_now = false;
    
	    stream_cmd.stream_mode=uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;

	    //stream_cmd.time_spec = uhd::time_spec_t(seconds_in_future);
	    
	    dev->issue_stream_cmd(stream_cmd);

	     powerOfFreqBands( all[loop_count], total_num_samps,  shiftindex, power_band, num_filt);

	     for(int ii=0;ii<num_filt+1;ii++){
	       total_bandPower[ii]=total_bandPower[ii]+power_band[ii]/size_all;
	     }
	     if(loop_count==99){
	       loop_count=0;
	       for(int ii=0;ii<num_filt+1;ii++){
	       double shift=shiftindex*ii;
	       std::cout << "power: " << power_band[ii] << " at w0: " << shift << std::endl;
	     }
	       std::cout<<"-------------------------------------\n";
	     }else{
	       loop_count++;
	     }
	    num_rx_samps=0;

	 }
	  
    };

    //finished
    std::cout << std::endl << "Done receiving!" << std::endl << std::endl; 

    return 0;
}
예제 #4
0
파일: tx_old.cpp 프로젝트: tony2909/green
int UHD_SAFE_MAIN(int argc, char *argv[]){


  
  //Seting priority in the processor to run faster -> run with sudo
     if (uhd::set_thread_priority_safe(1,true)) {
       std::cout << "set priority went well " << std::endl;
       };


    //variables to be set by po -> Set when initializing the tx program
    std::string args;
    double seconds_in_future;
    size_t total_num_samps;
    double tx_rate, freq, LOoffset;
    float gain;
    bool forever, use_8bits;
    bool use_external_10MHz, trigger_with_pps;
    bool readFile;
    std::string filename;
    uhd::tx_streamer::sptr tx_stream;
    uhd::device_addr_t dev_addr;
    uhd::usrp::multi_usrp::sptr dev;
    uhd::stream_args_t stream_args;

    //setup the program options -> Passing it from terminal with boost library 
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "help message")
        ("args", po::value<std::string>(&args)->default_value(""), "simple uhd device address args")
        ("secs", po::value<double>(&seconds_in_future)->default_value(3), "number of seconds in the future to transmit")
        ("nsamps", po::value<size_t>(&total_num_samps)->default_value(1000), "total number of samples to transmit")
        ("txrate", po::value<double>(&tx_rate)->default_value(100e6/16), "rate of outgoing samples")
        ("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz")
        ("LOoffset", po::value<double>(&LOoffset)->default_value(0), "Offset between main LO and center frequency")
        ("forever",po::value<bool>(&forever)->default_value(false), "run indefinetly")
        ("10MHz",po::value<bool>(&use_external_10MHz)->default_value(false), 
	     "external 10MHz on 'REF CLOCK' connector (true=1=yes)")
      ("PPS",po::value<bool>(&trigger_with_pps)->default_value(false), 
            "trigger reception with 'PPS IN' connector (true=1=yes)")
        ("filename",po::value<std::string>(&filename)->default_value("data_to_usrp.dat"), "input filename")
        ("gain",po::value<float>(&gain)->default_value(0), "gain of transmitter")
        ("8bits",po::value<bool>(&use_8bits)->default_value(false), "Use eight bits/sample to increase bandwidth")
  
      ////////////////////////////////
      ("readFile",po::value<bool>(&readFile)->default_value(false), "defines if program reads data from file or generate data itself")
      ;

    
    //Variables stored in boost objects
    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    //print the help message
    if (vm.count("help")){
        std::cout << boost::format("tx %s") % desc << std::endl;
        return ~0;
    }

    //////// Create buffer storage to pass to USRP -> complex short always
 
    std::complex<short> *buffer;
    buffer = new std::complex<short>[total_num_samps];

    ////////////////// Create data to be transmitted ///////////////////////
   
    if(readFile==true){

      /* Read input from disc -> data previous generated by MATLAB*/ 
      /*
      FILE *fp = 0;
      fp = fopen(filename.c_str(), "rb");    
      if (fp == 0){
	perror(filename.c_str());
	return 1;
      }
      int r=fread(buffer, sizeof(uint32_t),total_num_samps, fp);
      printf("r=%d \n",r);
      fclose(fp);
      */

// Read data from file
std::ifstream ifs3( "sent.dat" , std::ifstream::in );
ifs3.read((char * )buffer,total_num_samps*sizeof(short));
ifs3.close();
      printf("USING MATLAB DATA!\n");

    }else{

    
      /*Creates data to be transmitted->Call functions to generate data*/
    
      //Process data in double or complex<double>

      //Continuous waveform test:
    double amp=5000;
    double cw_freq=3e6;

    double *seq;
    seq = new double[2*total_num_samps+1];
    
    create_data_CW ( seq, 2*total_num_samps,  cw_freq/tx_rate,  amp );

    complex<double> * seq_c=(complex<double> *) seq;

    //Test casting when necessary:
    //for(int ii=0;ii<12;ii++){
    //    std::cout << seq[ii]/amp<< "->value \n";
    // };
    //std::cout << "\n casting \n";
   
    

    //ATENTION:Always convert to complex<short> to send to the USRP transmitter

    compDoubleToCompShort(seq_c, total_num_samps, buffer);

  FILE * xFile;
    xFile = fopen("sent2.bin","wb");
    fwrite(buffer, 2*sizeof(short),total_num_samps/2,xFile);
    fclose(xFile);


    printf("USING CPP IMPLEMENTATION!\n");
    
    };





    /////////////////////////////////////////////////////////////////////////

    
    //create a USRP device and streamer
    dev_addr["addr0"]="192.168.10.2";
    

    dev = uhd::usrp::multi_usrp::make(dev_addr);    


    ///////////// Internal variables of USRP (Not important now, set as default)
    
    uhd::clock_config_t my_clock_config; 

    if (!forever) {
      //my_clock_config.ref_source=uhd::clock_config_t::REF_SMA; 
      dev->set_time_source("external");
    };

    if (use_external_10MHz) { 
      //my_clock_config.pps_source=uhd::clock_config_t::PPS_SMA; 
      dev->set_clock_source("external");
    }
    else {
      //my_clock_config.pps_source=uhd::clock_config_t::PPS_SMA; 
      dev->set_clock_source("internal");
    };


    //dev->set_tx_bandwidth(36e6);


    std::cout << "freq=" << freq << "\n";
    std::cout << "LOoffset=" << LOoffset << "\n";
    

    uhd::tune_result_t tr;
    uhd::tune_request_t trq(freq,LOoffset); //std::min(tx_rate,10e6));
    tr=dev->set_tx_freq(trq,0);
    
    bool is_xcvr2450=false;
    uhd::dict<std::string, std::string> tx_info;    
    tx_info=dev->get_usrp_tx_info(0);

    if (tx_info.has_key("tx_subdev_name")) {
      std::string str=tx_info.get("tx_subdev_name");
      uint temp=str.find("XCVR2450");
      if (temp<str.length()) {
	is_xcvr2450=true;
      };
    };

    if (is_xcvr2450) {
      dev->set_tx_antenna("J2");
      dev->set_rx_antenna("J1");
      
      //uhd::meta_range_t range=dev->get_tx_bandwidth_range();

      if (abs(LOoffset)>=6e6) {
	//dev->set_tx_bandwidth(28e6);
	dev->set_tx_bandwidth(3.96e+07);
      };
      
    };

    dev->set_tx_gain(gain);
    std::cout << tr.to_pp_string() << "\n";
 
      //dev->set_clock_config(my_clock_config); // PZ

    stream_args.cpu_format="sc16";
    if (use_8bits)
      stream_args.otw_format="sc8";
    else
      stream_args.otw_format="sc16";

   


    tx_stream=dev->get_tx_stream(stream_args);

    


    //set properties on the device
    std::cout << boost::format("Setting TX Rate: %f Msps...") % (tx_rate/1e6) << std::endl;
    dev->set_tx_rate(tx_rate);
    std::cout << boost::format("Actual TX Rate: %f Msps...") % (dev->get_tx_rate()/1e6) << std::endl;
    std::cout << boost::format("Setting device timestamp to 0...") << std::endl;


        //Check what is being sent

    // for(int ii=0;ii<11;ii++){
    //   std::cout << real(buffer[ii])<< "->value \n";
    //   std::cout << imag(buffer[ii])<< "->value\n \n"; 
    // };


    ///////////////Transmission of data///////////////////////////////////

    /*----> send data in loop
      ----> send data once and let receiver handle it */
    
    uhd::tx_metadata_t md;

    if (forever) {

      std::cout << "Stop the transmitter by pressing ctrl-c \n";

      md.start_of_burst = true;
      md.end_of_burst = false;
      md.has_time_spec = false;
      
     
      //send the entire buffer, let the driver handle fragmentation

      /*
      num_tx_samps = dev->send(
      buffer, total_num_samps, md,
      uhd::io_type_t::COMPLEX_INT16,
      uhd::device::SEND_MODE_FULL_BUFF);
      */
      tx_stream->send(buffer,total_num_samps,md,60);

      
      md.start_of_burst = false;

      while (1) {

	/*
	num_tx_samps = dev->send(
        buffer, total_num_samps, md,
        uhd::io_type_t::COMPLEX_INT16,
        uhd::device::SEND_MODE_FULL_BUFF
         );
	*/

        tx_stream->send(buffer,total_num_samps,md,3);

	md.start_of_burst = false;
	md.end_of_burst = false;
	md.has_time_spec = false;
	
	std::cout << "Sending data in loop... \n";

      }
    }
    else
    {

      //Transmit data once and let receiver handle it
    
    dev->set_time_now(uhd::time_spec_t(0.0));
    md.start_of_burst = true;
    md.end_of_burst = true;
    md.has_time_spec = true;
    md.time_spec = uhd::time_spec_t(seconds_in_future);

    //send the entire buffer, let the driver handle fragmentation

    /*
      num_tx_samps = dev->send(
	buffer, total_num_samps, md,
        //&buff.front(), buff.size(), md,
        uhd::io_type_t::COMPLEX_FLOAT32,
        uhd::device::SEND_MODE_FULL_BUFF);
    */
    

    tx_stream->send(buffer,total_num_samps,md,60);
    
    std::cout << "\nData only sent once \n";

    };
 
    //finished


    std::cout << std::endl << "Done! Transmission completed" << std::endl << std::endl;

    return 0;
}
예제 #5
0
파일: rx.cpp 프로젝트: tony2909/green
int UHD_SAFE_MAIN(int argc, char *argv[]){
    
    
    if (!(uhd::set_thread_priority_safe(1,true))) {
      std::cout << "Problem setting thread priority" << std::endl;
      return 1;
    };


    //variables to be set by po
    //double seconds_in_future=0.01;
    size_t total_num_samps;
    double rx_rate, freq, LOoffset;
    bool use_external_10MHz;
    double scaling_8bits;
    std::string filename;
    float gain;
    uhd::device_addr_t dev_addr;
    uhd::usrp::multi_usrp::sptr dev;
    uhd::tune_result_t tr;
    uhd::stream_args_t stream_args;
    uhd::rx_streamer::sptr rx_stream;


    //setup the program options
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "help message")
        ("nsamps", po::value<size_t>(&total_num_samps)->default_value(1000), "total number of samples to receive")
        ("rxrate", po::value<double>(&rx_rate)->default_value(100e6/4), "rate of incoming samples")
        ("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz")
        ("LOoffset", po::value<double>(&LOoffset)->default_value(0), "Offset between main LO and center frequency")
        ("10MHz",po::value<bool>(&use_external_10MHz)->default_value(false), "external 10MHz on 'REF CLOCK' connector (true=1=yes)")
      //  ("PPS",po::value<bool>(&trigger_with_pps)->default_value(false), "trigger reception with 'PPS IN' connector (true=1=yes)")
        ("filename",po::value<std::string>(&filename)->default_value("data_from_usrp.dat"), "output filename") 
        ("gain",po::value<float>(&gain)->default_value(0), "set the receiver gain") 
        ("8bits_scaling",po::value<double>(&scaling_8bits)->default_value(0.0), 
    "input scaling (invers) when 8bits is used, set to zero to get 16bits")
    ;
    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);


    dev_addr["addr0"]="192.168.10.2";
    //dev_addr["addr0"]="192.168.10.2";
    dev = uhd::usrp::multi_usrp::make(dev_addr);

    stream_args.cpu_format="sc16";
    if (scaling_8bits==0.0) {
      stream_args.otw_format="sc16";     
    } else {
      stream_args.otw_format="sc8";
      std::stringstream temp_ss;
      temp_ss << scaling_8bits;
      stream_args.args["peak"]=temp_ss.str();
    };

    rx_stream=dev->get_rx_stream(stream_args);

    uhd::clock_config_t my_clock_config; 

    #if 0
    if (trigger_with_pps) {
      my_clock_config.pps_source=uhd::clock_config_t::PPS_SMA; 
    };
    #endif

    if (use_external_10MHz) { 
      my_clock_config.ref_source=uhd::clock_config_t::REF_SMA; 
    }; 

    //print the help message
    if (vm.count("help")){
        std::cout << boost::format("UHD RX Timed Samples %s") % desc << std::endl;
        return ~0;
    }

    //create a usrp device
    std::cout << std::endl;
    uhd::device::sptr udev = dev->get_device();
    dev->set_rx_rate(rx_rate);

    bool is_xcvr2450=false;
    uhd::dict<std::string, std::string> rx_info;    
    rx_info=dev->get_usrp_rx_info(0);

    if (rx_info.has_key("rx_subdev_name")) {
      std::string str=rx_info.get("rx_subdev_name");
      uint temp=str.find("XCVR2450");
      if (temp<str.length()) {
	is_xcvr2450=true;
      };
    };

    if (is_xcvr2450) {
      dev->set_tx_antenna("J2");
      dev->set_rx_antenna("J1");      
      //uhd::meta_range_t range=dev->get_tx_bandwidth_range();

      if (LOoffset>=9e6) {
	dev->set_rx_bandwidth(3.96e+07);
      };      
    };



    std::cout << "rx_rate=" << rx_rate << std::endl;
    std::cout << "freq=" << freq << std::endl;
    std::cout << "gain=" << gain << std::endl;
    uhd::tune_request_t trq(freq,LOoffset); 


    //dev->set_rx_bandwidth(36e6);

    tr=dev->set_rx_freq(trq);
    dev->set_rx_gain(gain);

    std::cout << "tr=" << tr.actual_rf_freq << std::endl;

    /*
        double target_rf_freq;
        double actual_rf_freq;
        double target_dsp_freq;
        double actual_dsp_freq;
    */

    // 
    //dev->set_tx_antenna("J2");
    //dev->set_rx_antenna("J1");
    

    if (use_external_10MHz) {
      dev->set_clock_config(my_clock_config); // PZ
      usleep(1e6); // Wait for the 10MHz to lock
    }; 

    size_t buffer_size=1000; // Select buffer size
    short *buff_short, *storage_short;
    storage_short=new short[2*total_num_samps]; // Create storage for the 
    // entire received signal to be saved on disk.
    buff_short=new short[2*buffer_size]; // Create storage for a single 
                                                // buffer




    /*if (trigger_with_pps) {
      dev->set_time_next_pps(uhd::time_spec_t(0.0));
      usleep(1e6); 
      } */
    //else {
      dev->set_time_now(uhd::time_spec_t(0.0));
      //};
    std::cout << boost::format("Setting device timestamp to 0...") << std::endl;

    //setup streaming
    //std::cout << std::endl;
    //std::cout << boost::format("Begin streaming %u samples, %d seconds in the future...")
    //    % total_num_samps % seconds_in_future << std::endl;
    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
    
    

    stream_cmd.num_samps = buffer_size;
    stream_cmd.stream_now = true;
    
    stream_cmd.stream_mode=uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;

    //stream_cmd.time_spec = uhd::time_spec_t(seconds_in_future);
    dev->issue_stream_cmd(stream_cmd);


    size_t num_rx_samps=0;
    size_t num_rx_samps_latest_call;

    uhd::rx_metadata_t md;
    while (num_rx_samps<total_num_samps) {
 

       num_rx_samps_latest_call=0;             
       while (num_rx_samps_latest_call==0) {
	 num_rx_samps_latest_call= 
	   rx_stream->recv(&buff_short[0],buffer_size, md, 3.0);
       };
       if (num_rx_samps_latest_call!=buffer_size)  {
	 std::cerr << "I expect the buffer size to be always the same!";
         exit(1); 
       };

       /* Process the just received buffer */
       int i1=2*num_rx_samps;
       int i2=0;
       while ((i1<(int) (2*total_num_samps)) && (i2<2*((int) buffer_size))) 
       {	  
	  storage_short[i1]=buff_short[i2];
	  i1++; i2++;
       };
	 
       num_rx_samps=num_rx_samps+num_rx_samps_latest_call;
       std::cout << "num_rx_samps=" << num_rx_samps  << std::endl;
    };




    //finished
    std::cout << std::endl << "Done!" << std::endl << std::endl; 

    // Save output to disc

    //std::ofstream s1(filename.c_str(), std::ios::binary);   


    //s1.write((char *) storage_short,4*total_num_samps); 
    //s1.flush(); //PZ
    //s1.close(); //PZ

    //Process the received data


    short tp =  powerTotArray( storage_short, 2*total_num_samps);

    std::cout << "Total power" << tp << "\nDone!\n";


    std::complex<short> * comp_rec=(std::complex<short> *) storage_short;

   


    return 0;
}
예제 #6
0
파일: tx_60GHz.cpp 프로젝트: tony2909/green
int UHD_SAFE_MAIN(int argc, char *argv[]){



    if (uhd::set_thread_priority_safe(1,true)) {
       std::cout << "set priority went well " << std::endl;
    };


    //variables to be set by po
    std::string args;
    double seconds_in_future;
    size_t total_num_samps;
    double tx_rate, freq, LOoffset;
    float gain;
    bool demoMode, use_8bits;
    bool use_external_10MHz; 
    std::string filename;
    uhd::tx_streamer::sptr tx_stream;
    uhd::device_addr_t dev_addr;
    uhd::usrp::multi_usrp::sptr dev;
    uhd::stream_args_t stream_args;

    //setup the program options
    po::options_description desc("Allowed options");
    desc.add_options()
      ("help", "help message")
      ("args", po::value<std::string>(&args)->default_value(""), "simple uhd device address args")
      ("secs", po::value<double>(&seconds_in_future)->default_value(3), "number of seconds in the future to transmit")
      ("nsamps", po::value<size_t>(&total_num_samps)->default_value(37028), "total number of samples to transmit")//9428
      ("txrate", po::value<double>(&tx_rate)->default_value(100e6/4), "rate of outgoing samples")
      ("freq", po::value<double>(&freq)->default_value(70e6), "rf center frequency in Hz")
      ("LOoffset", po::value<double>(&LOoffset)->default_value(0), "Offset between main LO and center frequency")
      ("demoMode",po::value<bool>(&demoMode)->default_value(true), "demo mode")
      ("10MHz",po::value<bool>(&use_external_10MHz)->default_value(false), 
       "external 10MHz on 'REF CLOCK' connector (true=1=yes)")
      ("filename",po::value<std::string>(&filename)->default_value("codedData.dat"), "input filename")
      ("gain",po::value<float>(&gain)->default_value(0), "gain of transmitter(0-13) ")
      ("8bits",po::value<bool>(&use_8bits)->default_value(false), "Use eight bits/sample to increase bandwidth")
    ;

    
    
    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    
    //print the help message
    if (vm.count("help")){
      std::cout << boost::format("tx %s") % desc << std::endl;
      return ~0;
    }
    
 ///////////////////////////////////////////////////////////////// START PROCESSING /////////////////////////////////////////////////////////////////////
    
    std::complex<int16_t> *buffer0;
    buffer0 = new std::complex<int16_t>[total_num_samps];
    std::complex<int16_t> *buffer1;
    buffer1 = new std::complex<int16_t>[total_num_samps];
    std::complex<int16_t> *buffer2;
    buffer2 = new std::complex<int16_t>[total_num_samps];
    std::complex<int16_t> *buffer3;
    buffer3 = new std::complex<int16_t>[total_num_samps];
    std::complex<int16_t> *buffer4;
    buffer4 = new std::complex<int16_t>[total_num_samps];

    
    int16_t *aux0;
    aux0 = new int16_t[2*total_num_samps];
    int16_t *aux1;
    aux1 = new int16_t[2*total_num_samps];
    int16_t *aux2;
    aux2 = new int16_t[2*total_num_samps];
    int16_t *aux3;
    aux3 = new int16_t[2*total_num_samps];
    int16_t *aux4;
    aux4 = new int16_t[2*total_num_samps];
    
    //generate the picture as grayscale
    int r=system("octave image_transmition.m &");
    if(r){
      std::cout<<" loading picture - check!\n";
    }
    
    int nPicRaw = 16384;//size of the image in grayscale 128*128
    double nBinPac = 27200;//size of binary data in one packet
 

    //loading picture as grayscale
    int16_t pictureRaw[nPicRaw];
    std::ifstream ifs( "data_toSend.dat", std::ifstream::in );
    ifs.read((char * )pictureRaw,nPicRaw*sizeof(int16_t));
    ifs.close();  
    
    //converting grayscale to binary and XOR with pseudonoise
    itpp::bvec picBinInter = prepairPic(pictureRaw,nPicRaw);//transforms grayscale in binary data

    //cutting the large binary data into 5 packets
    bvec dataBinTmp0;
    dataBinTmp0.ins(dataBinTmp0.length(),picBinInter.get(0,(nBinPac-1)));
    bvec dataBinTmp1;
    dataBinTmp1.ins(dataBinTmp1.length(),picBinInter.get(nBinPac,(2*nBinPac-1)));
    bvec dataBinTmp2;
    dataBinTmp2.ins(dataBinTmp2.length(),picBinInter.get(2*nBinPac,(3*nBinPac-1)));
    bvec dataBinTmp3;
    dataBinTmp3.ins(dataBinTmp3.length(),picBinInter.get(3*nBinPac,(4*nBinPac-1)));
    bvec dataBinTmp4;
    dataBinTmp4.ins(dataBinTmp4.length(),picBinInter.get(4*nBinPac,picBinInter.length()));
    dataBinTmp4.ins(dataBinTmp4.length(),randb(nBinPac-dataBinTmp4.length())); //filling the last packet with random data

    //saving the binary picture
    it_file my_file("binPicture.it");
    my_file << Name("picBinInter") << picBinInter;
    my_file.flush();
    my_file.close();
    
    
    //processing each packet
    tx_funct(aux0,dataBinTmp0,dataBinTmp0.length());
    tx_funct(aux1,dataBinTmp1,dataBinTmp1.length());
    tx_funct(aux2,dataBinTmp2,dataBinTmp2.length());
    tx_funct(aux3,dataBinTmp3,dataBinTmp3.length());
    tx_funct(aux4,dataBinTmp4,dataBinTmp4.length());
    
    //filling the output buffer
    for(int i=0,count1=0;i<(int)(2*total_num_samps);i=i+2){
      buffer0[count1]=std::complex<short>(aux0[i],aux0[i+1]);
      buffer1[count1]=std::complex<short>(aux1[i],aux1[i+1]);
      buffer2[count1]=std::complex<short>(aux2[i],aux2[i+1]);
      buffer3[count1]=std::complex<short>(aux3[i],aux3[i+1]);
      buffer4[count1]=std::complex<short>(aux4[i],aux4[i+1]);
      count1++;
    }
 
    
   
    // Save data to file to check what was sent
    std::ofstream ofs( "sent0.dat" , std::ifstream::out );
    ofs.write((char * ) buffer0, 2*total_num_samps*sizeof(int16_t));
    ofs.flush();
    ofs.close();
    // Save data to file to check what was sent
    std::ofstream ofs1( "sent1.dat" , std::ifstream::out );
    ofs1.write((char * ) buffer1, 2*total_num_samps*sizeof(int16_t));
    ofs1.flush();
    ofs1.close();
    // Save data to file to check what was sent
    std::ofstream ofs2( "sent2.dat" , std::ifstream::out );
    ofs2.write((char * ) buffer2, 2*total_num_samps*sizeof(int16_t));
    ofs2.flush();
    ofs2.close();
    // Save data to file to check what was sent
    std::ofstream ofs3( "sent3.dat" , std::ifstream::out );
    ofs3.write((char * ) buffer3, 2*total_num_samps*sizeof(int16_t));
    ofs3.flush();
    ofs3.close();
    // Save data to file to check what was sent
    std::ofstream ofs4( "sent4.dat" , std::ifstream::out );
    ofs4.write((char * ) buffer4, 2*total_num_samps*sizeof(int16_t));
    ofs4.flush();
    ofs4.close();

    //Conjugate!!!
    for(int i=0; i<(int)(total_num_samps);i++){
      buffer0[i]=std::conj(buffer0[i]);
      buffer1[i]=std::conj(buffer1[i]);
      buffer2[i]=std::conj(buffer2[i]);
      buffer3[i]=std::conj(buffer3[i]);
      buffer4[i]=std::conj(buffer4[i]);
    } 
    
    

    std::cout << " ----------- " << std::endl;
    std::cout<<" Conjugated! \n";
    std::cout << " ----------- " << std::endl;
    
    ///////////////////////////////////////////////////////////////// END  PROCESSING /////////////////////////////////////////////////////////////////////
    
    //create a usrp device and streamer
    dev_addr["addr0"]="192.168.10.2";
    dev = uhd::usrp::multi_usrp::make(dev_addr);    


    // Internal variables 
    uhd::clock_config_t my_clock_config; 

    if (!demoMode) {
      dev->set_time_source("external");
    };

    if (use_external_10MHz) { 
      dev->set_clock_source("external");
    }
    else {
      dev->set_clock_source("internal");
    };


    uhd::usrp::dboard_iface::sptr db_iface;
    db_iface=dev->get_tx_dboard_iface(0);

    board_60GHz_TX  my_60GHz_TX(db_iface);  //60GHz 
    my_60GHz_TX.set_gain(gain); // 60GHz 

    uhd::tune_result_t tr;
    uhd::tune_request_t trq(freq,LOoffset); //std::min(tx_rate,10e6));
    tr=dev->set_tx_freq(trq,0);
    

    //dev->set_tx_gain(gain);
    std::cout << tr.to_pp_string() << "\n";
 

    stream_args.cpu_format="sc16";
    if (use_8bits)
      stream_args.otw_format="sc8";
    else
      stream_args.otw_format="sc16";

    tx_stream=dev->get_tx_stream(stream_args);


    //set properties on the device
    std::cout << boost::format("Setting TX Rate: %f Msps...") % (tx_rate/1e6) << std::endl;
    dev->set_tx_rate(tx_rate);
    std::cout << boost::format("Actual TX Rate: %f Msps...") % (dev->get_tx_rate()/1e6) << std::endl;
    std::cout << boost::format("Setting device timestamp to 0...") << std::endl;


    

    uhd::tx_metadata_t md;


    if(demoMode){

    dev->set_time_now(uhd::time_spec_t(0.0));
    md.start_of_burst = true;
    md.end_of_burst = false;
    md.has_time_spec = false;
    md.time_spec = uhd::time_spec_t(seconds_in_future);

   
    tx_stream->send(buffer0,total_num_samps,md,60);
    
    tx_stream->send(buffer1,total_num_samps,md,3);
    
    tx_stream->send(buffer2,total_num_samps,md,3);

    tx_stream->send(buffer3,total_num_samps,md,3);

    tx_stream->send(buffer4,total_num_samps,md,3);

    tx_stream->send(buffer4,total_num_samps,md,3);

    md.start_of_burst = false;

    std::cout << " " << std::endl;
    std::cout<< "picture transmitted once!" << std::endl;
    std::cout << " " << std::endl;
    
    int f=system("octave toMatlab.m");
    if(f){
      std::cout << " Programm Paused - Press Any Key To leave! " << std::endl;
    }


    }
    else
    {
    
    dev->set_time_now(uhd::time_spec_t(0.0));
    md.start_of_burst = true;
    md.end_of_burst = false;
    md.has_time_spec = false;
    md.time_spec = uhd::time_spec_t(seconds_in_future);

      
    tx_stream->send(buffer0,total_num_samps,md,60);
    
    tx_stream->send(buffer1,total_num_samps,md,3);
    
    tx_stream->send(buffer2,total_num_samps,md,3);

    tx_stream->send(buffer3,total_num_samps,md,3);

    tx_stream->send(buffer4,total_num_samps,md,3);

    tx_stream->send(buffer4,total_num_samps,md,3);

    md.start_of_burst = false;

    std::cout << " " << std::endl;
    std::cout<< "picture transmitted once!" << std::endl;
    std::cout << " " << std::endl;

    };

    //finished
    std::cout << std::endl << "Done!" << std::endl << std::endl;


    return 0;
}
예제 #7
0
파일: rx.cpp 프로젝트: tony2909/green
int UHD_SAFE_MAIN(int argc, char *argv[]){

  // Set priority of the main thread
  int which = PRIO_PROCESS;
  id_t pid;
  int priority = -20;
  int ret;

  pid = getpid();
  ret = setpriority(which, pid, priority);
  if(ret!=0){  std::cout << "Main priority went wrong: " << ret << std::endl ;}

    
  //Seting priority in the processor to run faster -> run with sudo
  if (!(uhd::set_thread_priority_safe(1,true))) {
    std::cout << "Problem setting thread priority" << std::endl;
    return 1;
  };
  //variables to be set by po -> Set when initializing the rx program
 
  size_t total_num_samps;
  double rx_rate, freq, LOoffset;
  bool use_external_10MHz;
  double scaling_8bits;
  std::string filename;
  float gain;
  bool realTime;
  uhd::device_addr_t dev_addr;
  uhd::usrp::multi_usrp::sptr dev;
  uhd::tune_result_t tr;
  uhd::stream_args_t stream_args;
  uhd::rx_streamer::sptr rx_stream;


  //setup the program options-> Passing it from terminal with boost library 
  po::options_description desc("Allowed options");
  desc.add_options()
    ("help", "help message")
    ("nsamps", po::value<size_t>(&total_num_samps)->default_value(27840), "total number of samples to receive")
    ("rxrate", po::value<double>(&rx_rate)->default_value(100e6/4), "rate of incoming samples")
    ("freq", po::value<double>(&freq)->default_value(5.5e9), "rf center frequency in Hz")
    ("LOoffset", po::value<double>(&LOoffset)->default_value(10e6), "Offset between main LO and center frequency")
    ("10MHz",po::value<bool>(&use_external_10MHz)->default_value(false), "external 10MHz on 'REF CLOCK' connector (true=1=yes)")
    ("filename",po::value<std::string>(&filename)->default_value("data_from_usrp.dat"), "output filename") 
    ("gain",po::value<float>(&gain)->default_value(15), "set the receiver gain") 
    ("8bits_scaling",po::value<double>(&scaling_8bits)->default_value(0.0), 
     "input scaling (invers) when 8bits is used, set to zero to get 16bits")
    ("realTime",po::value<bool>(&realTime)->default_value(true), "receives in loop and compares with synch sequence")
    ;
   
  //Variables stored in boost objects
  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);

  //print the help message
  if (vm.count("help")){
    std::cout << boost::format("rx %s") % desc << std::endl;
    return ~0;
  }

  ///////////Set device variables to read data////////////////
    
  dev_addr["addr0"]="192.168.10.2";
  dev = uhd::usrp::multi_usrp::make(dev_addr);//Create the device

  //receiving format
  stream_args.cpu_format="sc16";
  if (scaling_8bits==0.0) {
    stream_args.otw_format="sc16";     
  } else {
    stream_args.otw_format="sc8";
    std::stringstream temp_ss;
    temp_ss << scaling_8bits;
    stream_args.args["peak"]=temp_ss.str();
  };

  rx_stream=dev->get_rx_stream(stream_args); //set/get the streamer values to the device

  uhd::clock_config_t my_clock_config; 

  if (use_external_10MHz) { 
    my_clock_config.ref_source=uhd::clock_config_t::REF_SMA; 
  };  

  /////////////////Create an USRP device////////////////////////
  std::cout << std::endl;
 
  dev->set_rx_rate(rx_rate);

  bool is_xcvr2450=false;
  uhd::dict<std::string, std::string> rx_info;    
  rx_info=dev->get_usrp_rx_info(0);

  if (rx_info.has_key("rx_subdev_name")) {
    std::string str=rx_info.get("rx_subdev_name");
    uint temp=str.find("XCVR2450");
    if (temp<str.length()) {
      is_xcvr2450=true;
    };
  };

  if (is_xcvr2450) {
    dev->set_tx_antenna("J2");
    dev->set_rx_antenna("J1");      

    if (LOoffset>=9e6) {
      dev->set_rx_bandwidth(3.96e+07);
    };      
  };

  std::cout << "rx_rate=" << rx_rate << std::endl;
  std::cout << "freq=" << freq << std::endl;
  std::cout << "gain=" << gain << std::endl;
  std::cout << "LOoffset="<<LOoffset <<std::endl;

  uhd::tune_request_t trq(freq,LOoffset); 

  tr=dev->set_rx_freq(trq);
  dev->set_rx_gain(gain);

  std::cout << "tr=" << tr.actual_rf_freq << std::endl;


  if (use_external_10MHz) {
    dev->set_clock_config(my_clock_config); // PZ
    usleep(1e6); // Wait for the 10MHz to lock
  }; 

  size_t buffer_size=1000; // Select buffer USRP and detection size. <25.000

  int nStorage=2*total_num_samps; // Size of the buffer for the processing part

  dev->set_time_now(uhd::time_spec_t(0.0));

  std::cout << boost::format("Setting device timestamp to 0...") << std::endl;

   

  // Initialisation  

    
  uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);

  //USRP parameters
  stream_cmd.num_samps = buffer_size;
  stream_cmd.stream_now = true;
  stream_cmd.stream_mode=uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;
  dev->issue_stream_cmd(stream_cmd);


  //////////////////////////////////Read data to buff_short and do processing////////////

  //Launch threads
  sem_init(&usrpReady, 0,0); 
  sem_init(&detectionReady, 0,0);
  std::thread usrpT(usrpGetData, rx_stream, dev, buffer_size);
  std::thread detectionT(detection, buffer_size, nStorage);
  std::thread processT(processing, nStorage);

  // Set highest priiority for usrpT
    sched_param sch;
    int policy; 
    pthread_getschedparam(usrpT.native_handle(), &policy, &sch);
    sch.sched_priority = 99;
    if(pthread_setschedparam(usrpT.native_handle(), SCHED_FIFO, &sch)) {
        std::cout << "Failed to setschedparam: " << std::strerror(errno) << '\n';
    }

    std::cout << "Priority set"<< std::endl;

  usrpT.join();
  detectionT.join();
  processT.join();

  //finished
  std::cout << std::endl << "Done receiving!" << std::endl << std::endl; 

  return 0;
}
예제 #8
0
파일: rx_3.cpp 프로젝트: tony2909/green
int UHD_SAFE_MAIN(int argc, char *argv[]){
    
  //Seting priority in the processor to run faster -> run with sudo
  if (!(uhd::set_thread_priority_safe(1,true))) {
    std::cout << "Problem setting thread priority" << std::endl;
    return 1;
  };
  //variables to be set by po -> Set when initializing the rx program
  //double seconds_in_future=0.01;
  size_t total_num_samps;
  double rx_rate, freq, LOoffset;
  bool use_external_10MHz;
  double scaling_8bits;
  std::string filename;
  float gain;
  bool realTime;
  uhd::device_addr_t dev_addr;
  uhd::usrp::multi_usrp::sptr dev;
  uhd::tune_result_t tr;
  uhd::stream_args_t stream_args;
  uhd::rx_streamer::sptr rx_stream;


  //setup the program options-> Passing it from terminal with boost library 
  po::options_description desc("Allowed options");
  desc.add_options()
    ("help", "help message")
    ("nsamps", po::value<size_t>(&total_num_samps)->default_value(1000), "total number of samples to receive")
    ("rxrate", po::value<double>(&rx_rate)->default_value(100e6/4), "rate of incoming samples")
    ("freq", po::value<double>(&freq)->default_value(5.5e9), "rf center frequency in Hz")
    ("LOoffset", po::value<double>(&LOoffset)->default_value(10e6), "Offset between main LO and center frequency")
    ("10MHz",po::value<bool>(&use_external_10MHz)->default_value(false), "external 10MHz on 'REF CLOCK' connector (true=1=yes)")
    //  ("PPS",po::value<bool>(&trigger_with_pps)->default_value(false), "trigger reception with 'PPS IN' connector (true=1=yes)")
    ("filename",po::value<std::string>(&filename)->default_value("data_from_usrp.dat"), "output filename") 
    ("gain",po::value<float>(&gain)->default_value(30), "set the receiver gain") 
    ("8bits_scaling",po::value<double>(&scaling_8bits)->default_value(0.0), 
     "input scaling (invers) when 8bits is used, set to zero to get 16bits")
      
    /////////////////////////////
    ("realTime",po::value<bool>(&realTime)->default_value(true), "receives in loop and compares with synch sequence")
    ;
   
  //Variables stored in boost objects
  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);

  //print the help message
  if (vm.count("help")){
    std::cout << boost::format("rx %s") % desc << std::endl;
    return ~0;
  }

  ///////////Set device variables to read data////////////////
    
  dev_addr["addr0"]="192.168.10.2";
  dev = uhd::usrp::multi_usrp::make(dev_addr);//Create the device

  //receiving format
  stream_args.cpu_format="sc16";
  if (scaling_8bits==0.0) {
    stream_args.otw_format="sc16";     
  } else {
    stream_args.otw_format="sc8";
    std::stringstream temp_ss;
    temp_ss << scaling_8bits;
    stream_args.args["peak"]=temp_ss.str();
  };

  rx_stream=dev->get_rx_stream(stream_args); //set/get the streamer values to the device

  uhd::clock_config_t my_clock_config; 

  /*
    if (trigger_with_pps) {
    my_clock_config.pps_source=uhd::clock_config_t::PPS_SMA; 
    };
  */

  if (use_external_10MHz) { 
    my_clock_config.ref_source=uhd::clock_config_t::REF_SMA; 
  };  

  /////////////////Create an USRP device////////////////////////
  std::cout << std::endl;
  //uhd::device::sptr udev = dev->get_device();
  dev->set_rx_rate(rx_rate);

  bool is_xcvr2450=false;
  uhd::dict<std::string, std::string> rx_info;    
  rx_info=dev->get_usrp_rx_info(0);

  if (rx_info.has_key("rx_subdev_name")) {
    std::string str=rx_info.get("rx_subdev_name");
    uint temp=str.find("XCVR2450");
    if (temp<str.length()) {
      is_xcvr2450=true;
    };
  };

  if (is_xcvr2450) {
    dev->set_tx_antenna("J2");
    dev->set_rx_antenna("J1");      
    //uhd::meta_range_t range=dev->get_tx_bandwidth_range();

    if (LOoffset>=9e6) {
      dev->set_rx_bandwidth(3.96e+07);
    };      
  };

  std::cout << "rx_rate=" << rx_rate << std::endl;
  std::cout << "freq=" << freq << std::endl;
  std::cout << "gain=" << gain << std::endl;
  uhd::tune_request_t trq(freq,LOoffset); 
  //dev->set_rx_bandwidth(36e6);
  tr=dev->set_rx_freq(trq);
  dev->set_rx_gain(gain);

  std::cout << "tr=" << tr.actual_rf_freq << std::endl;


  if (use_external_10MHz) {
    dev->set_clock_config(my_clock_config); // PZ
    usleep(1e6); // Wait for the 10MHz to lock
  }; 

  size_t buffer_size=1000; // Select buffer size

  uint nDetect=1000;
  

  dev->set_time_now(uhd::time_spec_t(0.0));
 
  std::cout << boost::format("Setting device timestamp to 0...") << std::endl;

  

   
  //////////////////////////////////Read data to buff_short and do processing////////////


  //Launch threads
  sem_init(&isReady, 0,0); 
  std::thread storeT(storeDataX, rx_stream, dev, buffer_size, nDetect);
  std::thread detectionT(detection, nDetect);

  storeT.join();
  detectionT.join();

  //finished
  std::cout << std::endl << "Done receiving!" << std::endl << std::endl; 

  return 0;
}
예제 #9
0
int UHD_SAFE_MAIN(int argc, char *argv[]){
    
    
    if (!(uhd::set_thread_priority_safe(1,true))) {
      std::cout << "Problem setting thread priority" << std::endl;
      return 1;
    };


    //variables to be set by po
    //double seconds_in_future=0.01;
    size_t total_num_samps;
    double rx_rate, freq, LOoffset, rf_freq, clock_freq;
    bool use_external_10MHz;
    double scaling_8bits;
    std::string filename;
    float gain;
    std::string dev_addr_str;
    uhd::device_addr_t dev_addr;
    uhd::usrp::multi_usrp::sptr dev;
    uhd::tune_result_t tr;
    uhd::stream_args_t stream_args;
    uhd::rx_streamer::sptr rx_stream;


    //setup the program options
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "help message")
        ("nsamps", po::value<size_t>(&total_num_samps)->default_value(1000), "total number of samples to receive")
        ("rxrate", po::value<double>(&rx_rate)->default_value(100e6/4), "rate of incoming samples")
        ("rf_freq", po::value<double>(&rf_freq)->default_value(60e9), "rf center frequency in Hz of 60GHz RX board")
        ("freq", po::value<double>(&freq)->default_value(60e9), "center frequency at input of basic daughterboard")
        ("LOoffset", po::value<double>(&LOoffset)->default_value(0), "Offset between main LO and center frequency")
        ("10MHz",po::value<bool>(&use_external_10MHz)->default_value(false), "external 10MHz on 'REF CLOCK' connector (true=1=yes)")
      //  ("PPS",po::value<bool>(&trigger_with_pps)->default_value(false), "trigger reception with 'PPS IN' connector (true=1=yes)")
        ("filename",po::value<std::string>(&filename)->default_value("data_from_usrp.dat"), "output filename") 
        ("gain",po::value<float>(&gain)->default_value(0), "set the receiver gain (0-15)") 
        ("8bits_scaling",po::value<double>(&scaling_8bits)->default_value(0.0), 
    "input scaling (invers) when 8bits is used, set to zero to get 16bits")
       ("dev_addr",po::value<std::string>(&dev_addr_str)->default_value("192.168.10.2"), 
    "IP address of USRP")
("clock_freq", po::value<double>(&clock_freq)->default_value(285.714), "Clock frequency of CLK board")

    ;
    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    dev_addr["addr0"]=dev_addr_str;
    dev = uhd::usrp::multi_usrp::make(dev_addr);


    //create a usrp device
    std::cout << std::endl;
    uhd::device::sptr udev = dev->get_device();
    dev->set_rx_rate(rx_rate);
    uhd::tune_request_t trq(freq,LOoffset); 
    tr=dev->set_rx_freq(trq);

    uhd::usrp::dboard_iface::sptr db_iface;
    db_iface=dev->get_tx_dboard_iface(0);
       
    board_60GHz_RX my_60GHz_RX(db_iface,clock_freq);    // 60GHz
    my_60GHz_RX.set_gain(gain);    // 60GHz
    if (rf_freq!=64e9) {
      my_60GHz_RX.set_freq(rf_freq);    // 60GHz
    };


    uhd::clock_config_t my_clock_config; 

    if (use_external_10MHz) {
      dev->set_clock_config(my_clock_config); 
      usleep(1e6); // Wait for the 10MHz to lock
    }; 


    if (scaling_8bits<0) {


      stream_args.cpu_format="sc16";
      stream_args.otw_format="sc16";     
      rx_stream=dev->get_rx_stream(stream_args);
      std::complex<int16_t> *d_buffer_rx;


    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);    

      uint32_t buffer_size=rx_stream->get_max_num_samps();
      stream_cmd.num_samps = buffer_size;
      stream_cmd.stream_now = true;    
      stream_cmd.stream_mode=uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;

      std::cout << "buffer_size=" << buffer_size << "\n";


      d_buffer_rx = new std::complex<int16_t>[buffer_size];   
      rx_stream->issue_stream_cmd(stream_cmd);


       uhd::rx_metadata_t md;
       size_t num_rx_samps_latest_call=0;             
       while (num_rx_samps_latest_call==0) {
	  num_rx_samps_latest_call= 
	   rx_stream->recv(&d_buffer_rx[0],buffer_size, md, 3.0);
       };


      

       double max_value=0.0;
       double new_value;
       for (uint32_t i2=10;i2<num_rx_samps_latest_call;i2++){ 
	  new_value=abs(d_buffer_rx[i2]);
	  if (new_value>max_value) {
	      max_value=new_value;
	  };
       };
       
       std::cout << "max_value=" << max_value << "\n";
       scaling_8bits=max_value*3.0518e-05*abs(scaling_8bits);
       if (scaling_8bits<0.0039062)
	 scaling_8bits=0.0039062;
       std::cout << "scaling_8bits=" << scaling_8bits << "\n";

    };






    stream_args.cpu_format="sc16";
    if (scaling_8bits==0.0) {
      stream_args.otw_format="sc16";     
    } else {
      stream_args.otw_format="sc8";
      std::stringstream temp_ss;
      temp_ss << scaling_8bits;
      stream_args.args["peak"]=temp_ss.str();
    };

    rx_stream=dev->get_rx_stream(stream_args);


    if (use_external_10MHz) { 
      my_clock_config.ref_source=uhd::clock_config_t::REF_SMA; 
    }; 

    //print the help message
    if (vm.count("help")){
        std::cout << boost::format("rx_60GHz %s") % desc << std::endl;
        return ~0;
    }


    size_t buffer_size=1000; // Select buffer size
    short *buff_short, *storage_short;
    storage_short=new short[2*total_num_samps]; // Create storage for the 
    // entire received signal to be saved on disk.
    buff_short=new short[2*buffer_size]; // Create storage for a single 
                                                // buffer


    //    dev->set_time_now(uhd::time_spec_t(0.0));
    //std::cout << boost::format("Setting device timestamp to 0...") << std::endl;

    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);    
    
    stream_cmd.num_samps = total_num_samps; //buffer_size;
    stream_cmd.stream_now = true;
    dev->issue_stream_cmd(stream_cmd);


    size_t num_rx_samps=0;
    size_t num_rx_samps_latest_call;

    uhd::rx_metadata_t md;
    while (num_rx_samps<total_num_samps) {
 

       num_rx_samps_latest_call=0;             
       while (num_rx_samps_latest_call==0) {
	 num_rx_samps_latest_call= 
	   rx_stream->recv(&buff_short[0],buffer_size, md, 3.0);
       };

       stream_cmd.stream_mode=uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;
       dev->issue_stream_cmd(stream_cmd);


       /* Process the just received buffer */
       int i1=2*num_rx_samps;
       int i2=0;
       while ((i1<(int) (2*total_num_samps)) && (i2<2*((int) num_rx_samps_latest_call ))) 
       {	  
	  storage_short[i1]=buff_short[i2];
	  i1++; i2++;
       };
	 

       num_rx_samps=num_rx_samps+num_rx_samps_latest_call;
    };




    //finished
    std::cout << std::endl << "Done!" << std::endl << std::endl; 

    // Save output to disc
    std::ofstream s1(filename.c_str(), std::ios::binary);   


    s1.write((char *) storage_short,4*total_num_samps); 
    s1.flush(); //PZ
    s1.close(); //PZ



    return 0;
}