Statistics
| Branch: | Tag: | Revision:

root / host / examples / tx_waveforms.cpp @ 605483a2

History | View | Annotate | Download (7.2 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/usrp/simple_usrp.hpp>
21
#include <boost/program_options.hpp>
22
#include <boost/thread/thread_time.hpp> //system time
23
#include <boost/math/special_functions/round.hpp>
24
#include <boost/format.hpp>
25
#include <iostream>
26
#include <complex>
27
#include <cmath>
28

    
29
namespace po = boost::program_options;
30

    
31
int UHD_SAFE_MAIN(int argc, char *argv[]){
32
    uhd::set_thread_priority_safe();
33

    
34
    //variables to be set by po
35
    std::string args, wave_type;
36
    size_t total_duration, mspb;
37
    double rate, freq, wave_freq, aepb;
38
    float ampl, gain;
39

    
40
    //setup the program options
41
    po::options_description desc("Allowed options");
42
    desc.add_options()
43
        ("help", "help message")
44
        ("args", po::value<std::string>(&args)->default_value(""), "simple uhd device address args")
45
        ("duration", po::value<size_t>(&total_duration)->default_value(3), "number of seconds to transmit")
46
        ("mspb", po::value<size_t>(&mspb)->default_value(10000), "mimimum samples per buffer")
47
        ("aepb", po::value<double>(&aepb)->default_value(1e-5), "allowed error per buffer")
48
        ("rate", po::value<double>(&rate)->default_value(100e6/16), "rate of outgoing samples")
49
        ("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz")
50
        ("ampl", po::value<float>(&ampl)->default_value(float(0.3)), "amplitude of the waveform")
51
        ("gain", po::value<float>(&gain)->default_value(float(0)), "gain for the RF chain")
52
        ("wave-type", po::value<std::string>(&wave_type)->default_value("SINE"), "waveform type (CONST, SQUARE, RAMP, SINE)")
53
        ("wave-freq", po::value<double>(&wave_freq)->default_value(0), "waveform frequency in Hz")
54
    ;
55
    po::variables_map vm;
56
    po::store(po::parse_command_line(argc, argv, desc), vm);
57
    po::notify(vm);
58

    
59
    //print the help message
60
    if (vm.count("help")){
61
        std::cout << boost::format("UHD TX Waveforms %s") % desc << std::endl;
62
        return ~0;
63
    }
64

    
65
    //create a usrp device
66
    std::cout << std::endl;
67
    std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
68
    uhd::usrp::simple_usrp::sptr sdev = uhd::usrp::simple_usrp::make(args);
69
    uhd::device::sptr dev = sdev->get_device();
70
    std::cout << boost::format("Using Device: %s") % sdev->get_pp_string() << std::endl;
71

    
72
    //set the tx sample rate
73
    std::cout << boost::format("Setting TX Rate: %f Msps...") % (rate/1e6) << std::endl;
74
    sdev->set_tx_rate(rate);
75
    std::cout << boost::format("Actual TX Rate: %f Msps...") % (sdev->get_tx_rate()/1e6) << std::endl << std::endl;
76

    
77
    //set the tx center frequency
78
    std::cout << boost::format("Setting TX Freq: %f Mhz...") % (freq/1e6) << std::endl;
79
    sdev->set_tx_freq(freq);
80
    std::cout << boost::format("Actual TX Freq: %f Mhz...") % (sdev->get_tx_freq()/1e6) << std::endl << std::endl;
81

    
82
    //set the tx rf gain
83
    std::cout << boost::format("Setting TX Gain: %f dB...") % gain << std::endl;
84
    sdev->set_tx_gain(gain);
85
    std::cout << boost::format("Actual TX Gain: %f dB...") % sdev->get_tx_gain() << std::endl << std::endl;
86

    
87
    //for the const wave, set the wave freq for small samples per period
88
    if (wave_freq == 0 and wave_type == "CONST"){
89
        wave_freq = sdev->get_tx_rate()/2;
90
    }
91

    
92
    //error when the waveform is not possible to generate
93
    if (std::abs(wave_freq)/sdev->get_tx_rate() < 0.5/mspb){
94
        throw std::runtime_error("wave freq/tx rate too small");
95
    }
96
    if (std::abs(wave_freq) > sdev->get_tx_rate()/2){
97
        throw std::runtime_error("wave freq out of Nyquist zone");
98
    }
99

    
100
    //how many periods should we have per buffer to mimimize error
101
    double samps_per_period = sdev->get_tx_rate()/std::abs(wave_freq);
102
    std::cout << boost::format("Samples per waveform period: %d") % samps_per_period << std::endl;
103
    size_t periods_per_buff = std::max<size_t>(1, mspb/samps_per_period);
104
    while (true){
105
        double num_samps_per_buff = periods_per_buff*samps_per_period;
106
        double sample_error = num_samps_per_buff - boost::math::round(num_samps_per_buff);
107
        if (std::abs(sample_error) <= aepb) break;
108
        periods_per_buff++;
109
    }
110

    
111
    //allocate data to send (fill with several periods worth of IQ samples)
112
    std::vector<std::complex<float> > buff(samps_per_period*periods_per_buff);
113
    const double i_ahead = (wave_freq > 0)? samps_per_period/4 : 0;
114
    const double q_ahead = (wave_freq < 0)? samps_per_period/4 : 0;
115
    std::cout << boost::format("Samples per send buffer: %d") % buff.size() << std::endl;
116
    if (wave_type == "CONST"){
117
        for (size_t n = 0; n < buff.size(); n++){
118
            buff[n] = std::complex<float>(ampl, ampl);
119
        }
120
    }
121
    else if (wave_type == "SQUARE"){
122
        for (size_t n = 0; n < buff.size(); n++){
123
            float I = (std::fmod(n+i_ahead, samps_per_period) > samps_per_period/2)? ampl : 0;
124
            float Q = (std::fmod(n+q_ahead, samps_per_period) > samps_per_period/2)? ampl : 0;
125
            buff[n] = std::complex<float>(I, Q);
126
        }
127
    }
128
    else if (wave_type == "RAMP"){
129
        for (size_t n = 0; n < buff.size(); n++){
130
            float I = float(std::fmod(n+i_ahead, samps_per_period)/samps_per_period * 2*ampl - ampl);
131
            float Q = float(std::fmod(n+q_ahead, samps_per_period)/samps_per_period * 2*ampl - ampl);
132
            buff[n] = std::complex<float>(I, Q);
133
        }
134
    }
135
    else if (wave_type == "SINE"){
136
        for (size_t n = 0; n < buff.size(); n++){
137
            float I = float(ampl*std::sin(2*M_PI*(n+i_ahead)/samps_per_period));
138
            float Q = float(ampl*std::sin(2*M_PI*(n+q_ahead)/samps_per_period));
139
            buff[n] = std::complex<float>(I, Q);
140
        }
141
    }
142
    else throw std::runtime_error("unknown waveform type: " + wave_type);
143

    
144
    //setup the metadata flags
145
    uhd::tx_metadata_t md;
146
    md.start_of_burst = true; //always SOB (good for continuous streaming)
147
    md.end_of_burst   = false;
148

    
149
    //send the data in multiple packets
150
    boost::system_time end_time(boost::get_system_time() + boost::posix_time::seconds(total_duration));
151
    while(end_time > boost::get_system_time()) dev->send(
152
        &buff.front(), buff.size(), md,
153
        uhd::io_type_t::COMPLEX_FLOAT32,
154
        uhd::device::SEND_MODE_FULL_BUFF
155
    );
156

    
157
    //send a mini EOB packet
158
    md.start_of_burst = false;
159
    md.end_of_burst   = true;
160
    dev->send(NULL, 0, md,
161
        uhd::io_type_t::COMPLEX_FLOAT32,
162
        uhd::device::SEND_MODE_FULL_BUFF
163
    );
164

    
165
    //finished
166
    std::cout << std::endl << "Done!" << std::endl << std::endl;
167

    
168
    return 0;
169
}