Statistics
| Branch: | Tag: | Revision:

root / host / utils / uhd_cal_rx_iq_balance.cpp @ f68a9271

History | View | Annotate | Download (11 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 "usrp_cal_utils.hpp"
19
#include <uhd/utils/thread_priority.hpp>
20
#include <uhd/utils/safe_main.hpp>
21
#include <uhd/utils/paths.hpp>
22
#include <uhd/utils/algorithm.hpp>
23
#include <uhd/usrp/multi_usrp.hpp>
24
#include <boost/program_options.hpp>
25
#include <boost/format.hpp>
26
#include <boost/thread/thread.hpp>
27
#include <boost/math/special_functions/round.hpp>
28
#include <iostream>
29
#include <complex>
30
#include <cmath>
31
#include <ctime>
32

    
33
namespace po = boost::program_options;
34

    
35
/***********************************************************************
36
 * Transmit thread
37
 **********************************************************************/
38
static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_ampl){
39
    uhd::set_thread_priority_safe();
40

    
41
    //create a transmit streamer
42
    uhd::stream_args_t stream_args("fc32"); //complex floats
43
    uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args);
44

    
45
    //setup variables and allocate buffer
46
    uhd::tx_metadata_t md;
47
    md.has_time_spec = false;
48
    std::vector<std::complex<float> > buff(tx_stream->get_max_num_samps()*10);
49

    
50
    //fill buff and send until interrupted
51
    while (not boost::this_thread::interruption_requested()){
52
        for (size_t i = 0; i < buff.size(); i++){
53
            buff[i] = float(tx_wave_ampl);
54
        }
55
        tx_stream->send(&buff.front(), buff.size(), md);
56
    }
57

    
58
    //send a mini EOB packet
59
    md.end_of_burst = true;
60
    tx_stream->send("", 0, md);
61
}
62

    
63
/***********************************************************************
64
 * Tune RX and TX routine
65
 **********************************************************************/
66
static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double rx_lo_freq, const double tx_offset){
67
    //tune the receiver with no cordic
68
    uhd::tune_request_t rx_tune_req(rx_lo_freq);
69
    rx_tune_req.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL;
70
    rx_tune_req.dsp_freq = 0;
71
    usrp->set_rx_freq(rx_tune_req);
72

    
73
    //tune the transmitter with no cordic
74
    uhd::tune_request_t tx_tune_req(usrp->get_rx_freq() - tx_offset);
75
    tx_tune_req.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL;
76
    tx_tune_req.dsp_freq = 0;
77
    usrp->set_tx_freq(tx_tune_req);
78

    
79
    //wait for the LOs to become locked
80
    boost::this_thread::sleep(boost::posix_time::milliseconds(50));
81
    boost::system_time start = boost::get_system_time();
82
    while (not usrp->get_tx_sensor("lo_locked").to_bool() or not usrp->get_rx_sensor("lo_locked").to_bool()){
83
        if (boost::get_system_time() > start + boost::posix_time::milliseconds(100)){
84
            throw std::runtime_error("timed out waiting for TX and/or RX LO to lock");
85
        }
86
    }
87

    
88
    return usrp->get_rx_freq();
89
}
90

    
91
/***********************************************************************
92
 * Data capture routine
93
 **********************************************************************/
94
static void capture_samples(uhd::usrp::multi_usrp::sptr usrp, uhd::rx_streamer::sptr rx_stream, std::vector<std::complex<float> > &buff){
95
    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
96
    stream_cmd.num_samps = buff.size();
97
    stream_cmd.stream_now = true;
98
    usrp->issue_stream_cmd(stream_cmd);
99
    uhd::rx_metadata_t md;
100
    const size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md);
101

    
102
    //validate the received data
103
    if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){
104
        throw std::runtime_error(str(boost::format(
105
            "Unexpected error code 0x%x"
106
        ) % md.error_code));
107
    }
108
    if (num_rx_samps != buff.size()){
109
        throw std::runtime_error("did not get all the samples requested");
110
    }
111
}
112

    
113
/***********************************************************************
114
 * Main
115
 **********************************************************************/
116
int UHD_SAFE_MAIN(int argc, char *argv[]){
117
    std::string args;
118
    double rate, tx_wave_ampl, tx_offset;
119
    double freq_start, freq_stop, freq_step;
120
    size_t nsamps;
121

    
122
    po::options_description desc("Allowed options");
123
    desc.add_options()
124
        ("help", "help message")
125
        ("verbose", "enable some verbose")
126
        ("args", po::value<std::string>(&args)->default_value(""), "device address args [default = \"\"]")
127
        ("rate", po::value<double>(&rate)->default_value(12.5e6), "RX and TX sample rate in Hz")
128
        ("tx_wave_ampl", po::value<double>(&tx_wave_ampl)->default_value(0.7), "Transmit wave amplitude in counts")
129
        ("tx_offset", po::value<double>(&tx_offset)->default_value(.9344e6), "TX LO offset from the RX LO in Hz")
130
        ("freq_start", po::value<double>(&freq_start), "Frequency start in Hz (do not specify for default)")
131
        ("freq_stop", po::value<double>(&freq_stop), "Frequency stop in Hz (do not specify for default)")
132
        ("freq_step", po::value<double>(&freq_step)->default_value(default_freq_step), "Step size for LO sweep in Hz")
133
        ("nsamps", po::value<size_t>(&nsamps)->default_value(default_num_samps), "Samples per data capture")
134
    ;
135

    
136
    po::variables_map vm;
137
    po::store(po::parse_command_line(argc, argv, desc), vm);
138
    po::notify(vm);
139

    
140
    //print the help message
141
    if (vm.count("help")){
142
        std::cout << boost::format("USRP Generate RX IQ Balance Calibration Table %s") % desc << std::endl;
143
        std::cout <<
144
            "This application measures leakage between RX and TX on an XCVR daughterboard to self-calibrate.\n"
145
            << std::endl;
146
        return ~0;
147
    }
148

    
149
    //create a usrp device
150
    std::cout << std::endl;
151
    std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
152
    uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);
153

    
154
    //set the antennas to cal
155
    if (not uhd::has(usrp->get_rx_antennas(), "CAL") or not uhd::has(usrp->get_tx_antennas(), "CAL")){
156
        throw std::runtime_error("This board does not have the CAL antenna option, cannot self-calibrate.");
157
    }
158
    usrp->set_rx_antenna("CAL");
159
    usrp->set_tx_antenna("CAL");
160

    
161
    //set optimum gain settings
162
    set_optimum_gain(usrp);
163

    
164
    //set the sample rates
165
    usrp->set_rx_rate(rate);
166
    usrp->set_tx_rate(rate);
167

    
168
    //create a receive streamer
169
    uhd::stream_args_t stream_args("fc32"); //complex floats
170
    uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
171

    
172
    //create a transmitter thread
173
    boost::thread_group threads;
174
    threads.create_thread(boost::bind(&tx_thread, usrp, tx_wave_ampl));
175

    
176
    //re-usable buffer for samples
177
    std::vector<std::complex<float> > buff(nsamps);
178

    
179
    //store the results here
180
    std::vector<result_t> results;
181

    
182
    if (not vm.count("freq_start")) freq_start = usrp->get_rx_freq_range().start() + 50e6;
183
    if (not vm.count("freq_stop")) freq_stop = usrp->get_rx_freq_range().stop() - 50e6;
184

    
185
    for (double rx_lo_i = freq_start; rx_lo_i <= freq_stop; rx_lo_i += freq_step){
186
        const double rx_lo = tune_rx_and_tx(usrp, rx_lo_i, tx_offset);
187

    
188
        //frequency constants for this tune event
189
        const double actual_rx_rate = usrp->get_rx_rate();
190
        const double actual_tx_freq = usrp->get_tx_freq();
191
        const double actual_rx_freq = usrp->get_rx_freq();
192
        const double bb_tone_freq = actual_tx_freq - actual_rx_freq;
193
        const double bb_imag_freq = -bb_tone_freq;
194

    
195
        //capture initial uncorrected value
196
        usrp->set_rx_iq_balance(std::polar<double>(1.0, 0.0));
197
        capture_samples(usrp, rx_stream, buff);
198
        const double initial_suppression = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate) - compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate);
199

    
200
        //bounds and results from searching
201
        std::complex<double> best_correction;
202
        double phase_corr_start = -.3, phase_corr_stop = .3, phase_corr_step;
203
        double ampl_corr_start = -.3, ampl_corr_stop = .3, ampl_corr_step;
204
        double best_suppression = 0, best_phase_corr = 0, best_ampl_corr = 0;
205

    
206
        for (size_t i = 0; i < num_search_iters; i++){
207

    
208
            phase_corr_step = (phase_corr_stop - phase_corr_start)/(num_search_steps-1);
209
            ampl_corr_step = (ampl_corr_stop - ampl_corr_start)/(num_search_steps-1);
210

    
211
            for (double phase_corr = phase_corr_start; phase_corr <= phase_corr_stop + phase_corr_step/2; phase_corr += phase_corr_step){
212
            for (double ampl_corr = ampl_corr_start; ampl_corr <= ampl_corr_stop + ampl_corr_step/2; ampl_corr += ampl_corr_step){
213

    
214
                const std::complex<double> correction = std::polar(ampl_corr+1, phase_corr*tau);
215
                usrp->set_rx_iq_balance(correction);
216

    
217
                //receive some samples
218
                capture_samples(usrp, rx_stream, buff);
219

    
220
                const double tone_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate);
221
                const double imag_dbrms = compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate);
222
                const double suppression = tone_dbrms - imag_dbrms;
223

    
224
                if (suppression > best_suppression){
225
                    best_correction = correction;
226
                    best_suppression = suppression;
227
                    best_phase_corr = phase_corr;
228
                    best_ampl_corr = ampl_corr;
229
                }
230

    
231
            }}
232

    
233
            //std::cout << "best_phase_corr " << best_phase_corr << std::endl;
234
            //std::cout << "best_ampl_corr " << best_ampl_corr << std::endl;
235
            //std::cout << "best_suppression " << best_suppression << std::endl;
236

    
237
            phase_corr_start = best_phase_corr - phase_corr_step;
238
            phase_corr_stop = best_phase_corr + phase_corr_step;
239
            ampl_corr_start = best_ampl_corr - ampl_corr_step;
240
            ampl_corr_stop = best_ampl_corr + ampl_corr_step;
241
        }
242

    
243
        if (best_suppression > 30){ //most likely valid, keep result
244
            result_t result;
245
            result.freq = rx_lo;
246
            result.real_corr = best_correction.real();
247
            result.imag_corr = best_correction.imag();
248
            result.best = best_suppression;
249
            result.delta = best_suppression - initial_suppression;
250
            results.push_back(result);
251
            if (vm.count("verbose")){
252
                std::cout << boost::format("%f MHz: best suppression %fdB, corrected %fdB") % (rx_lo/1e6) % result.best % result.delta << std::endl;
253
            }
254
            else std::cout << "." << std::flush;
255
        }
256

    
257
    }
258
    std::cout << std::endl;
259

    
260
    //stop the transmitter
261
    threads.interrupt_all();
262
    threads.join_all();
263

    
264
    store_results(usrp, results, "RX", "rx", "iq");
265

    
266
    return 0;
267
}