2.4.7. txrx_loopback_to_file.cpp (TX)#

2.4.7.1. Source code#

  1//
  2// Copyright 2010-2012,2014-2015 Ettus Research LLC
  3// Copyright 2018 Ettus Research, a National Instruments Company
  4//
  5// SPDX-License-Identifier: GPL-3.0-or-later
  6//
  7
  8#include "wavetable.hpp"
  9#include <uhd/exception.hpp>
 10#include <uhd/types/tune_request.hpp>
 11#include <uhd/usrp/multi_usrp.hpp>
 12#include <uhd/utils/safe_main.hpp>
 13#include <uhd/utils/static.hpp>
 14#include <uhd/utils/thread.hpp>
 15#include <boost/algorithm/string.hpp>
 16#include <boost/filesystem.hpp>
 17#include <boost/format.hpp>
 18#include <boost/program_options.hpp>
 19#include <cmath>
 20#include <csignal>
 21#include <fstream>
 22#include <functional>
 23#include <iostream>
 24#include <thread>
 25
 26namespace po = boost::program_options;
 27
 28/***********************************************************************
 29 * Signal handlers
 30 **********************************************************************/
 31static bool stop_signal_called = false;
 32void sig_int_handler(int)
 33{
 34    stop_signal_called = true;
 35}
 36
 37/***********************************************************************
 38 * Utilities
 39 **********************************************************************/
 40//! Change to filename, e.g. from usrp_samples.dat to usrp_samples.00.dat,
 41//  but only if multiple names are to be generated.
 42std::string generate_out_filename(
 43    const std::string& base_fn, size_t n_names, size_t this_name)
 44{
 45    if (n_names == 1) {
 46        return base_fn;
 47    }
 48
 49    boost::filesystem::path base_fn_fp(base_fn);
 50    base_fn_fp.replace_extension(boost::filesystem::path(
 51        str(boost::format("%02d%s") % this_name % base_fn_fp.extension().string())));
 52    return base_fn_fp.string();
 53}
 54
 55
 56/***********************************************************************
 57 * transmit_worker function
 58 * A function to be used in a thread for transmitting
 59 **********************************************************************/
 60void transmit_worker(std::vector<std::complex<float>> buff,
 61    wave_table_class wave_table,
 62    uhd::tx_streamer::sptr tx_streamer,
 63    uhd::tx_metadata_t metadata,
 64    size_t step,
 65    size_t index,
 66    int num_channels)
 67{
 68    std::vector<std::complex<float>*> buffs(num_channels, &buff.front());
 69
 70    // send data until the signal handler gets called
 71    while (not stop_signal_called) {
 72        // fill the buffer with the waveform
 73        for (size_t n = 0; n < buff.size(); n++) {
 74            buff[n] = wave_table(index += step);
 75        }
 76
 77        // send the entire contents of the buffer
 78        tx_streamer->send(buffs, buff.size(), metadata);
 79
 80        metadata.start_of_burst = false;
 81        metadata.has_time_spec  = false;
 82    }
 83
 84    // send a mini EOB packet
 85    metadata.end_of_burst = true;
 86    tx_streamer->send("", 0, metadata);
 87}
 88
 89
 90/***********************************************************************
 91 * recv_to_file function
 92 **********************************************************************/
 93template <typename samp_type>
 94void recv_to_file(uhd::usrp::multi_usrp::sptr usrp,
 95    const std::string& cpu_format,
 96    const std::string& wire_format,
 97    const std::string& file,
 98    size_t samps_per_buff,
 99    int num_requested_samples,
100    double settling_time,
101    std::vector<size_t> rx_channel_nums)
102{
103    int num_total_samps = 0;
104    // create a receive streamer
105    uhd::stream_args_t stream_args(cpu_format, wire_format);
106    stream_args.channels             = rx_channel_nums;
107    uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
108
109    // Prepare buffers for received samples and metadata
110    uhd::rx_metadata_t md;
111    std::vector<std::vector<samp_type>> buffs(
112        rx_channel_nums.size(), std::vector<samp_type>(samps_per_buff));
113    // create a vector of pointers to point to each of the channel buffers
114    std::vector<samp_type*> buff_ptrs;
115    for (size_t i = 0; i < buffs.size(); i++) {
116        buff_ptrs.push_back(&buffs[i].front());
117    }
118
119    // Create one ofstream object per channel
120    // (use shared_ptr because ofstream is non-copyable)
121    std::vector<std::shared_ptr<std::ofstream>> outfiles;
122    for (size_t i = 0; i < buffs.size(); i++) {
123        const std::string this_filename = generate_out_filename(file, buffs.size(), i);
124        outfiles.push_back(std::shared_ptr<std::ofstream>(
125            new std::ofstream(this_filename.c_str(), std::ofstream::binary)));
126    }
127    UHD_ASSERT_THROW(outfiles.size() == buffs.size());
128    UHD_ASSERT_THROW(buffs.size() == rx_channel_nums.size());
129    bool overflow_message = true;
130    // We increase the first timeout to cover for the delay between now + the
131    // command time, plus 500ms of buffer. In the loop, we will then reduce the
132    // timeout for subsequent receives.
133    double timeout = settling_time + 0.5f;
134
135    // setup streaming
136    uhd::stream_cmd_t stream_cmd((num_requested_samples == 0)
137                                     ? uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS
138                                     : uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
139    stream_cmd.num_samps  = num_requested_samples;
140    stream_cmd.stream_now = false;
141    stream_cmd.time_spec  = usrp->get_time_now() + uhd::time_spec_t(settling_time);
142    rx_stream->issue_stream_cmd(stream_cmd);
143
144    while (not stop_signal_called
145           and (num_requested_samples > num_total_samps or num_requested_samples == 0)) {
146        size_t num_rx_samps = rx_stream->recv(buff_ptrs, samps_per_buff, md, timeout);
147        timeout             = 0.1f; // small timeout for subsequent recv
148
149        if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) {
150            std::cout << "Timeout while streaming" << std::endl;
151            break;
152        }
153        if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW) {
154            if (overflow_message) {
155                overflow_message = false;
156                std::cerr
157                    << boost::format(
158                           "Got an overflow indication. Please consider the following:\n"
159                           "  Your write medium must sustain a rate of %fMB/s.\n"
160                           "  Dropped samples will not be written to the file.\n"
161                           "  Please modify this example for your purposes.\n"
162                           "  This message will not appear again.\n")
163                           % (usrp->get_rx_rate() * sizeof(samp_type) / 1e6);
164            }
165            continue;
166        }
167        if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE) {
168            throw std::runtime_error("Receiver error " + md.strerror());
169        }
170
171        num_total_samps += num_rx_samps;
172
173        for (size_t i = 0; i < outfiles.size(); i++) {
174            outfiles[i]->write(
175                (const char*)buff_ptrs[i], num_rx_samps * sizeof(samp_type));
176        }
177    }
178
179    // Shut down receiver
180    stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;
181    rx_stream->issue_stream_cmd(stream_cmd);
182
183    // Close files
184    for (size_t i = 0; i < outfiles.size(); i++) {
185        outfiles[i]->close();
186    }
187}
188
189
190/***********************************************************************
191 * Main function
192 **********************************************************************/
193int UHD_SAFE_MAIN(int argc, char* argv[])
194{
195    // transmit variables to be set by po
196    std::string tx_args, wave_type, tx_ant, tx_subdev, ref, otw, tx_channels;
197    double tx_rate, tx_freq, tx_gain, wave_freq, tx_bw;
198    float ampl;
199
200    // receive variables to be set by po
201    std::string rx_args, file, type, rx_ant, rx_subdev, rx_channels;
202    size_t total_num_samps, spb;
203    double rx_rate, rx_freq, rx_gain, rx_bw;
204    double settling;
205
206    // setup the program options
207    po::options_description desc("Allowed options");
208    // clang-format off
209    desc.add_options()
210        ("help", "help message")
211        ("tx-args", po::value<std::string>(&tx_args)->default_value(""), "uhd transmit device address args")
212        ("rx-args", po::value<std::string>(&rx_args)->default_value(""), "uhd receive device address args")
213        ("file", po::value<std::string>(&file)->default_value("usrp_samples.dat"), "name of the file to write binary samples to")
214        ("type", po::value<std::string>(&type)->default_value("short"), "sample type in file: double, float, or short")
215        ("nsamps", po::value<size_t>(&total_num_samps)->default_value(0), "total number of samples to receive")
216        ("settling", po::value<double>(&settling)->default_value(double(0.2)), "settling time (seconds) before receiving")
217        ("spb", po::value<size_t>(&spb)->default_value(0), "samples per buffer, 0 for default")
218        ("tx-rate", po::value<double>(&tx_rate), "rate of transmit outgoing samples")
219        ("rx-rate", po::value<double>(&rx_rate), "rate of receive incoming samples")
220        ("tx-freq", po::value<double>(&tx_freq), "transmit RF center frequency in Hz")
221        ("rx-freq", po::value<double>(&rx_freq), "receive RF center frequency in Hz")
222        ("ampl", po::value<float>(&ampl)->default_value(float(0.3)), "amplitude of the waveform [0 to 0.7]")
223        ("tx-gain", po::value<double>(&tx_gain), "gain for the transmit RF chain")
224        ("rx-gain", po::value<double>(&rx_gain), "gain for the receive RF chain")
225        ("tx-ant", po::value<std::string>(&tx_ant), "transmit antenna selection")
226        ("rx-ant", po::value<std::string>(&rx_ant), "receive antenna selection")
227        ("tx-subdev", po::value<std::string>(&tx_subdev), "transmit subdevice specification")
228        ("rx-subdev", po::value<std::string>(&rx_subdev), "receive subdevice specification")
229        ("tx-bw", po::value<double>(&tx_bw), "analog transmit filter bandwidth in Hz")
230        ("rx-bw", po::value<double>(&rx_bw), "analog receive filter bandwidth in Hz")
231        ("wave-type", po::value<std::string>(&wave_type)->default_value("CONST"), "waveform type (CONST, SQUARE, RAMP, SINE)")
232        ("wave-freq", po::value<double>(&wave_freq)->default_value(0), "waveform frequency in Hz")
233        ("ref", po::value<std::string>(&ref)->default_value("internal"), "clock reference (internal, external, mimo)")
234        ("otw", po::value<std::string>(&otw)->default_value("sc16"), "specify the over-the-wire sample mode")
235        ("tx-channels", po::value<std::string>(&tx_channels)->default_value("0"), "which TX channel(s) to use (specify \"0\", \"1\", \"0,1\", etc)")
236        ("rx-channels", po::value<std::string>(&rx_channels)->default_value("0"), "which RX channel(s) to use (specify \"0\", \"1\", \"0,1\", etc)")
237        ("tx-int-n", "tune USRP TX with integer-N tuning")
238        ("rx-int-n", "tune USRP RX with integer-N tuning")
239    ;
240    // clang-format on
241    po::variables_map vm;
242    po::store(po::parse_command_line(argc, argv, desc), vm);
243    po::notify(vm);
244
245    // print the help message
246    if (vm.count("help")) {
247        std::cout << "UHD TXRX Loopback to File " << desc << std::endl;
248        return ~0;
249    }
250
251    // create a usrp device
252    std::cout << std::endl;
253    std::cout << boost::format("Creating the transmit usrp device with: %s...") % tx_args
254              << std::endl;
255    uhd::usrp::multi_usrp::sptr tx_usrp = uhd::usrp::multi_usrp::make(tx_args);
256    std::cout << std::endl;
257    std::cout << boost::format("Creating the receive usrp device with: %s...") % rx_args
258              << std::endl;
259    uhd::usrp::multi_usrp::sptr rx_usrp = uhd::usrp::multi_usrp::make(rx_args);
260
261    // always select the subdevice first, the channel mapping affects the other settings
262    if (vm.count("tx-subdev"))
263        tx_usrp->set_tx_subdev_spec(tx_subdev);
264    if (vm.count("rx-subdev"))
265        rx_usrp->set_rx_subdev_spec(rx_subdev);
266
267    // detect which channels to use
268    std::vector<std::string> tx_channel_strings;
269    std::vector<size_t> tx_channel_nums;
270    boost::split(tx_channel_strings, tx_channels, boost::is_any_of("\"',"));
271    for (size_t ch = 0; ch < tx_channel_strings.size(); ch++) {
272        size_t chan = std::stoi(tx_channel_strings[ch]);
273        if (chan >= tx_usrp->get_tx_num_channels()) {
274            throw std::runtime_error("Invalid TX channel(s) specified.");
275        } else
276            tx_channel_nums.push_back(std::stoi(tx_channel_strings[ch]));
277    }
278    std::vector<std::string> rx_channel_strings;
279    std::vector<size_t> rx_channel_nums;
280    boost::split(rx_channel_strings, rx_channels, boost::is_any_of("\"',"));
281    for (size_t ch = 0; ch < rx_channel_strings.size(); ch++) {
282        size_t chan = std::stoi(rx_channel_strings[ch]);
283        if (chan >= rx_usrp->get_rx_num_channels()) {
284            throw std::runtime_error("Invalid RX channel(s) specified.");
285        } else
286            rx_channel_nums.push_back(std::stoi(rx_channel_strings[ch]));
287    }
288
289    // Lock mboard clocks
290    if (vm.count("ref")) {
291        tx_usrp->set_clock_source(ref);
292        rx_usrp->set_clock_source(ref);
293    }
294
295    std::cout << "Using TX Device: " << tx_usrp->get_pp_string() << std::endl;
296    std::cout << "Using RX Device: " << rx_usrp->get_pp_string() << std::endl;
297
298    // set the transmit sample rate
299    if (not vm.count("tx-rate")) {
300        std::cerr << "Please specify the transmit sample rate with --tx-rate"
301                  << std::endl;
302        return ~0;
303    }
304    std::cout << boost::format("Setting TX Rate: %f Msps...") % (tx_rate / 1e6)
305              << std::endl;
306    tx_usrp->set_tx_rate(tx_rate);
307    std::cout << boost::format("Actual TX Rate: %f Msps...")
308                     % (tx_usrp->get_tx_rate() / 1e6)
309              << std::endl
310              << std::endl;
311
312    // set the receive sample rate
313    if (not vm.count("rx-rate")) {
314        std::cerr << "Please specify the sample rate with --rx-rate" << std::endl;
315        return ~0;
316    }
317    std::cout << boost::format("Setting RX Rate: %f Msps...") % (rx_rate / 1e6)
318              << std::endl;
319    rx_usrp->set_rx_rate(rx_rate);
320    std::cout << boost::format("Actual RX Rate: %f Msps...")
321                     % (rx_usrp->get_rx_rate() / 1e6)
322              << std::endl
323              << std::endl;
324
325    // set the transmit center frequency
326    if (not vm.count("tx-freq")) {
327        std::cerr << "Please specify the transmit center frequency with --tx-freq"
328                  << std::endl;
329        return ~0;
330    }
331
332    for (size_t ch = 0; ch < tx_channel_nums.size(); ch++) {
333        size_t channel = tx_channel_nums[ch];
334        if (tx_channel_nums.size() > 1) {
335            std::cout << "Configuring TX Channel " << channel << std::endl;
336        }
337        std::cout << boost::format("Setting TX Freq: %f MHz...") % (tx_freq / 1e6)
338                  << std::endl;
339        uhd::tune_request_t tx_tune_request(tx_freq);
340        if (vm.count("tx-int-n"))
341            tx_tune_request.args = uhd::device_addr_t("mode_n=integer");
342        tx_usrp->set_tx_freq(tx_tune_request, channel);
343        std::cout << boost::format("Actual TX Freq: %f MHz...")
344                         % (tx_usrp->get_tx_freq(channel) / 1e6)
345                  << std::endl
346                  << std::endl;
347
348        // set the rf gain
349        if (vm.count("tx-gain")) {
350            std::cout << boost::format("Setting TX Gain: %f dB...") % tx_gain
351                      << std::endl;
352            tx_usrp->set_tx_gain(tx_gain, channel);
353            std::cout << boost::format("Actual TX Gain: %f dB...")
354                             % tx_usrp->get_tx_gain(channel)
355                      << std::endl
356                      << std::endl;
357        }
358
359        // set the analog frontend filter bandwidth
360        if (vm.count("tx-bw")) {
361            std::cout << boost::format("Setting TX Bandwidth: %f MHz...") % tx_bw
362                      << std::endl;
363            tx_usrp->set_tx_bandwidth(tx_bw, channel);
364            std::cout << boost::format("Actual TX Bandwidth: %f MHz...")
365                             % tx_usrp->get_tx_bandwidth(channel)
366                      << std::endl
367                      << std::endl;
368        }
369
370        // set the antenna
371        if (vm.count("tx-ant"))
372            tx_usrp->set_tx_antenna(tx_ant, channel);
373    }
374
375    for (size_t ch = 0; ch < rx_channel_nums.size(); ch++) {
376        size_t channel = rx_channel_nums[ch];
377        if (rx_channel_nums.size() > 1) {
378            std::cout << "Configuring RX Channel " << channel << std::endl;
379        }
380
381        // set the receive center frequency
382        if (not vm.count("rx-freq")) {
383            std::cerr << "Please specify the center frequency with --rx-freq"
384                      << std::endl;
385            return ~0;
386        }
387        std::cout << boost::format("Setting RX Freq: %f MHz...") % (rx_freq / 1e6)
388                  << std::endl;
389        uhd::tune_request_t rx_tune_request(rx_freq);
390        if (vm.count("rx-int-n"))
391            rx_tune_request.args = uhd::device_addr_t("mode_n=integer");
392        rx_usrp->set_rx_freq(rx_tune_request, channel);
393        std::cout << boost::format("Actual RX Freq: %f MHz...")
394                         % (rx_usrp->get_rx_freq(channel) / 1e6)
395                  << std::endl
396                  << std::endl;
397
398        // set the receive rf gain
399        if (vm.count("rx-gain")) {
400            std::cout << boost::format("Setting RX Gain: %f dB...") % rx_gain
401                      << std::endl;
402            rx_usrp->set_rx_gain(rx_gain, channel);
403            std::cout << boost::format("Actual RX Gain: %f dB...")
404                             % rx_usrp->get_rx_gain(channel)
405                      << std::endl
406                      << std::endl;
407        }
408
409        // set the receive analog frontend filter bandwidth
410        if (vm.count("rx-bw")) {
411            std::cout << boost::format("Setting RX Bandwidth: %f MHz...") % (rx_bw / 1e6)
412                      << std::endl;
413            rx_usrp->set_rx_bandwidth(rx_bw, channel);
414            std::cout << boost::format("Actual RX Bandwidth: %f MHz...")
415                             % (rx_usrp->get_rx_bandwidth(channel) / 1e6)
416                      << std::endl
417                      << std::endl;
418        }
419
420        // set the receive antenna
421        if (vm.count("rx-ant"))
422            rx_usrp->set_rx_antenna(rx_ant, channel);
423    }
424
425    // Align times in the RX USRP (the TX USRP does not require time-syncing)
426    if (rx_usrp->get_num_mboards() > 1) {
427        rx_usrp->set_time_unknown_pps(uhd::time_spec_t(0.0));
428    }
429
430    // for the const wave, set the wave freq for small samples per period
431    if (wave_freq == 0 and wave_type == "CONST") {
432        wave_freq = tx_usrp->get_tx_rate() / 2;
433    }
434
435    // error when the waveform is not possible to generate
436    if (std::abs(wave_freq) > tx_usrp->get_tx_rate() / 2) {
437        throw std::runtime_error("wave freq out of Nyquist zone");
438    }
439    if (tx_usrp->get_tx_rate() / std::abs(wave_freq) > wave_table_len / 2) {
440        throw std::runtime_error("wave freq too small for table");
441    }
442
443    // pre-compute the waveform values
444    const wave_table_class wave_table(wave_type, ampl);
445    const size_t step = std::lround(wave_freq / tx_usrp->get_tx_rate() * wave_table_len);
446    size_t index = 0;
447
448    // create a transmit streamer
449    // linearly map channels (index0 = channel0, index1 = channel1, ...)
450    uhd::stream_args_t stream_args("fc32", otw);
451    stream_args.channels             = tx_channel_nums;
452    uhd::tx_streamer::sptr tx_stream = tx_usrp->get_tx_stream(stream_args);
453
454    // allocate a buffer which we re-use for each channel
455    if (spb == 0)
456        spb = tx_stream->get_max_num_samps() * 10;
457    std::vector<std::complex<float>> buff(spb);
458    int num_channels = tx_channel_nums.size();
459
460    // setup the metadata flags
461    uhd::tx_metadata_t md;
462    md.start_of_burst = true;
463    md.end_of_burst   = false;
464    md.has_time_spec  = true;
465    md.time_spec = uhd::time_spec_t(0.5); // give us 0.5 seconds to fill the tx buffers
466
467    // Check Ref and LO Lock detect
468    std::vector<std::string> tx_sensor_names, rx_sensor_names;
469    tx_sensor_names = tx_usrp->get_tx_sensor_names(0);
470    if (std::find(tx_sensor_names.begin(), tx_sensor_names.end(), "lo_locked")
471        != tx_sensor_names.end()) {
472        uhd::sensor_value_t lo_locked = tx_usrp->get_tx_sensor("lo_locked", 0);
473        std::cout << boost::format("Checking TX: %s ...") % lo_locked.to_pp_string()
474                  << std::endl;
475        UHD_ASSERT_THROW(lo_locked.to_bool());
476    }
477    rx_sensor_names = rx_usrp->get_rx_sensor_names(0);
478    if (std::find(rx_sensor_names.begin(), rx_sensor_names.end(), "lo_locked")
479        != rx_sensor_names.end()) {
480        uhd::sensor_value_t lo_locked = rx_usrp->get_rx_sensor("lo_locked", 0);
481        std::cout << boost::format("Checking RX: %s ...") % lo_locked.to_pp_string()
482                  << std::endl;
483        UHD_ASSERT_THROW(lo_locked.to_bool());
484    }
485
486    tx_sensor_names = tx_usrp->get_mboard_sensor_names(0);
487    if ((ref == "mimo")
488        and (std::find(tx_sensor_names.begin(), tx_sensor_names.end(), "mimo_locked")
489                != tx_sensor_names.end())) {
490        uhd::sensor_value_t mimo_locked = tx_usrp->get_mboard_sensor("mimo_locked", 0);
491        std::cout << boost::format("Checking TX: %s ...") % mimo_locked.to_pp_string()
492                  << std::endl;
493        UHD_ASSERT_THROW(mimo_locked.to_bool());
494    }
495    if ((ref == "external")
496        and (std::find(tx_sensor_names.begin(), tx_sensor_names.end(), "ref_locked")
497                != tx_sensor_names.end())) {
498        uhd::sensor_value_t ref_locked = tx_usrp->get_mboard_sensor("ref_locked", 0);
499        std::cout << boost::format("Checking TX: %s ...") % ref_locked.to_pp_string()
500                  << std::endl;
501        UHD_ASSERT_THROW(ref_locked.to_bool());
502    }
503
504    rx_sensor_names = rx_usrp->get_mboard_sensor_names(0);
505    if ((ref == "mimo")
506        and (std::find(rx_sensor_names.begin(), rx_sensor_names.end(), "mimo_locked")
507                != rx_sensor_names.end())) {
508        uhd::sensor_value_t mimo_locked = rx_usrp->get_mboard_sensor("mimo_locked", 0);
509        std::cout << boost::format("Checking RX: %s ...") % mimo_locked.to_pp_string()
510                  << std::endl;
511        UHD_ASSERT_THROW(mimo_locked.to_bool());
512    }
513    if ((ref == "external")
514        and (std::find(rx_sensor_names.begin(), rx_sensor_names.end(), "ref_locked")
515                != rx_sensor_names.end())) {
516        uhd::sensor_value_t ref_locked = rx_usrp->get_mboard_sensor("ref_locked", 0);
517        std::cout << boost::format("Checking RX: %s ...") % ref_locked.to_pp_string()
518                  << std::endl;
519        UHD_ASSERT_THROW(ref_locked.to_bool());
520    }
521
522    if (total_num_samps == 0) {
523        std::signal(SIGINT, &sig_int_handler);
524        std::cout << "Press Ctrl + C to stop streaming..." << std::endl;
525    }
526
527    // reset usrp time to prepare for transmit/receive
528    std::cout << boost::format("Setting device timestamp to 0...") << std::endl;
529    tx_usrp->set_time_now(uhd::time_spec_t(0.0));
530
531    // start transmit worker thread
532    std::thread transmit_thread([&]() {
533        transmit_worker(buff, wave_table, tx_stream, md, step, index, num_channels);
534    });
535
536    // recv to file
537    if (type == "double")
538        recv_to_file<std::complex<double>>(
539            rx_usrp, "fc64", otw, file, spb, total_num_samps, settling, rx_channel_nums);
540    else if (type == "float")
541        recv_to_file<std::complex<float>>(
542            rx_usrp, "fc32", otw, file, spb, total_num_samps, settling, rx_channel_nums);
543    else if (type == "short")
544        recv_to_file<std::complex<short>>(
545            rx_usrp, "sc16", otw, file, spb, total_num_samps, settling, rx_channel_nums);
546    else {
547        // clean up transmit worker
548        stop_signal_called = true;
549        transmit_thread.join();
550        throw std::runtime_error("Unknown type " + type);
551    }
552
553    // clean up transmit worker
554    stop_signal_called = true;
555    transmit_thread.join();
556
557    // finished
558    std::cout << std::endl << "Done!" << std::endl << std::endl;
559    return EXIT_SUCCESS;
560}