root / host / examples / tx_waveforms.cpp @ 02e339cc
History | View | Annotate | Download (6.9 kB)
| 1 |
//
|
|---|---|
| 2 |
// Copyright 2010 Ettus Research LLC
|
| 3 |
//
|
| 4 |
// This program is free software: you can redistribute it and/or modify
|
| 5 |
// it under the terms of the GNU General Public License as published by
|
| 6 |
// the Free Software Foundation, either version 3 of the License, or
|
| 7 |
// (at your option) any later version.
|
| 8 |
//
|
| 9 |
// This program is distributed in the hope that it will be useful,
|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
// GNU General Public License for more details.
|
| 13 |
//
|
| 14 |
// You should have received a copy of the GNU General Public License
|
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 16 |
//
|
| 17 |
|
| 18 |
#include <uhd/utils/thread_priority.hpp> |
| 19 |
#include <uhd/utils/safe_main.hpp> |
| 20 |
#include <uhd/utils/static.hpp> |
| 21 |
#include <uhd/usrp/simple_usrp.hpp> |
| 22 |
#include <boost/program_options.hpp> |
| 23 |
#include <boost/thread/thread_time.hpp> //system time |
| 24 |
#include <boost/math/special_functions/round.hpp> |
| 25 |
#include <boost/format.hpp> |
| 26 |
#include <boost/function.hpp> |
| 27 |
#include <iostream> |
| 28 |
#include <complex> |
| 29 |
#include <cmath> |
| 30 |
|
| 31 |
namespace po = boost::program_options;
|
| 32 |
|
| 33 |
/***********************************************************************
|
| 34 |
* Waveform generators
|
| 35 |
**********************************************************************/
|
| 36 |
float gen_const(float){ |
| 37 |
return 1; |
| 38 |
} |
| 39 |
|
| 40 |
float gen_square(float x){ |
| 41 |
return float((std::fmod(x, 1) < float(0.5))? 0 : 1); |
| 42 |
} |
| 43 |
|
| 44 |
float gen_ramp(float x){ |
| 45 |
return std::fmod(x, 1)*2 - 1; |
| 46 |
} |
| 47 |
|
| 48 |
#define sine_table_len 2048 |
| 49 |
static float sine_table[sine_table_len]; |
| 50 |
UHD_STATIC_BLOCK(gen_sine_table){
|
| 51 |
static const float m_pi = std::acos(float(-1)); |
| 52 |
for (size_t i = 0; i < sine_table_len; i++) |
| 53 |
sine_table[i] = std::sin((2*m_pi*i)/sine_table_len);
|
| 54 |
} |
| 55 |
|
| 56 |
float gen_sine(float x){ |
| 57 |
return sine_table[size_t(x*sine_table_len)%sine_table_len];
|
| 58 |
} |
| 59 |
|
| 60 |
int UHD_SAFE_MAIN(int argc, char *argv[]){ |
| 61 |
uhd::set_thread_priority_safe(); |
| 62 |
|
| 63 |
//variables to be set by po
|
| 64 |
std::string args, wave_type;
|
| 65 |
size_t total_duration, spb; |
| 66 |
double rate, freq, wave_freq;
|
| 67 |
float ampl, gain;
|
| 68 |
|
| 69 |
//setup the program options
|
| 70 |
po::options_description desc("Allowed options");
|
| 71 |
desc.add_options() |
| 72 |
("help", "help message") |
| 73 |
("args", po::value<std::string>(&args)->default_value(""), "simple uhd device address args") |
| 74 |
("duration", po::value<size_t>(&total_duration)->default_value(3), "number of seconds to transmit") |
| 75 |
("spb", po::value<size_t>(&spb)->default_value(10000), "samples per buffer") |
| 76 |
("rate", po::value<double>(&rate)->default_value(1.5e6), "rate of outgoing samples") |
| 77 |
("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz") |
| 78 |
("ampl", po::value<float>(&l)->default_value(float(0.3)), "amplitude of the waveform") |
| 79 |
("gain", po::value<float>(&gain)->default_value(float(0)), "gain for the RF chain") |
| 80 |
("wave-type", po::value<std::string>(&wave_type)->default_value("CONST"), "waveform type (CONST, SQUARE, RAMP, SINE)") |
| 81 |
("wave-freq", po::value<double>(&wave_freq)->default_value(0), "waveform frequency in Hz") |
| 82 |
; |
| 83 |
po::variables_map vm; |
| 84 |
po::store(po::parse_command_line(argc, argv, desc), vm); |
| 85 |
po::notify(vm); |
| 86 |
|
| 87 |
//print the help message
|
| 88 |
if (vm.count("help")){ |
| 89 |
std::cout << boost::format("UHD TX Waveforms %s") % desc << std::endl;
|
| 90 |
return ~0; |
| 91 |
} |
| 92 |
|
| 93 |
//create a usrp device
|
| 94 |
std::cout << std::endl; |
| 95 |
std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
|
| 96 |
uhd::usrp::simple_usrp::sptr sdev = uhd::usrp::simple_usrp::make(args); |
| 97 |
uhd::device::sptr dev = sdev->get_device(); |
| 98 |
std::cout << boost::format("Using Device: %s") % sdev->get_pp_string() << std::endl;
|
| 99 |
|
| 100 |
//set the tx sample rate
|
| 101 |
std::cout << boost::format("Setting TX Rate: %f Msps...") % (rate/1e6) << std::endl; |
| 102 |
sdev->set_tx_rate(rate); |
| 103 |
std::cout << boost::format("Actual TX Rate: %f Msps...") % (sdev->get_tx_rate()/1e6) << std::endl << std::endl; |
| 104 |
|
| 105 |
//set the tx center frequency
|
| 106 |
std::cout << boost::format("Setting TX Freq: %f Mhz...") % (freq/1e6) << std::endl; |
| 107 |
sdev->set_tx_freq(freq); |
| 108 |
std::cout << boost::format("Actual TX Freq: %f Mhz...") % (sdev->get_tx_freq()/1e6) << std::endl << std::endl; |
| 109 |
|
| 110 |
//set the tx rf gain
|
| 111 |
std::cout << boost::format("Setting TX Gain: %f dB...") % gain << std::endl;
|
| 112 |
sdev->set_tx_gain(gain); |
| 113 |
std::cout << boost::format("Actual TX Gain: %f dB...") % sdev->get_tx_gain() << std::endl << std::endl;
|
| 114 |
|
| 115 |
//for the const wave, set the wave freq for small samples per period
|
| 116 |
if (wave_freq == 0 and wave_type == "CONST"){ |
| 117 |
wave_freq = sdev->get_tx_rate()/2;
|
| 118 |
} |
| 119 |
|
| 120 |
//error when the waveform is not possible to generate
|
| 121 |
if (std::abs(wave_freq) > sdev->get_tx_rate()/2){ |
| 122 |
throw std::runtime_error("wave freq out of Nyquist zone"); |
| 123 |
} |
| 124 |
if (sdev->get_tx_rate()/std::abs(wave_freq) > sine_table_len/2 and wave_type == "SINE"){ |
| 125 |
throw std::runtime_error("sine freq too small for table"); |
| 126 |
} |
| 127 |
|
| 128 |
//store the generator function for the selected waveform
|
| 129 |
boost::function<float(float)> wave_gen; |
| 130 |
if (wave_type == "CONST") wave_gen = &gen_const; |
| 131 |
else if (wave_type == "SQUARE") wave_gen = &gen_square; |
| 132 |
else if (wave_type == "RAMP") wave_gen = &gen_ramp; |
| 133 |
else if (wave_type == "SINE") wave_gen = &gen_sine; |
| 134 |
else throw std::runtime_error("unknown waveform type: " + wave_type); |
| 135 |
|
| 136 |
//allocate the buffer and precalculate values
|
| 137 |
std::vector<std::complex<float> > buff(spb);
|
| 138 |
const float cps = float(wave_freq/sdev->get_tx_rate()); |
| 139 |
const float i_off = (wave_freq > 0)? float(0.25) : 0; |
| 140 |
const float q_off = (wave_freq < 0)? float(0.25) : 0; |
| 141 |
float theta = 0; |
| 142 |
|
| 143 |
//setup the metadata flags
|
| 144 |
uhd::tx_metadata_t md; |
| 145 |
md.start_of_burst = true; //always SOB (good for continuous streaming) |
| 146 |
md.end_of_burst = false;
|
| 147 |
|
| 148 |
//send the data in multiple packets
|
| 149 |
boost::system_time end_time(boost::get_system_time() + boost::posix_time::seconds(total_duration)); |
| 150 |
while(end_time > boost::get_system_time()){
|
| 151 |
|
| 152 |
//fill the buffer with the waveform
|
| 153 |
for (size_t n = 0; n < buff.size(); n++){ |
| 154 |
buff[n] = std::complex<float>(
|
| 155 |
ampl*wave_gen(i_off + theta), |
| 156 |
ampl*wave_gen(q_off + theta) |
| 157 |
); |
| 158 |
theta += cps; |
| 159 |
} |
| 160 |
|
| 161 |
//bring the theta back into range [0, 1)
|
| 162 |
theta = std::fmod(theta, 1);
|
| 163 |
|
| 164 |
//send the entire contents of the buffer
|
| 165 |
dev->send( |
| 166 |
&buff.front(), buff.size(), md, |
| 167 |
uhd::io_type_t::COMPLEX_FLOAT32, |
| 168 |
uhd::device::SEND_MODE_FULL_BUFF |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
//send a mini EOB packet
|
| 173 |
md.start_of_burst = false;
|
| 174 |
md.end_of_burst = true;
|
| 175 |
dev->send(NULL, 0, md, |
| 176 |
uhd::io_type_t::COMPLEX_FLOAT32, |
| 177 |
uhd::device::SEND_MODE_FULL_BUFF |
| 178 |
); |
| 179 |
|
| 180 |
//finished
|
| 181 |
std::cout << std::endl << "Done!" << std::endl << std::endl;
|
| 182 |
|
| 183 |
return 0; |
| 184 |
} |