root / host / utils / uhd_cal_tx_iq_balance.cpp @ f68a9271
History | View | Annotate | Download (11.3 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 <ctime> |
| 31 |
|
| 32 |
namespace po = boost::program_options;
|
| 33 |
|
| 34 |
/***********************************************************************
|
| 35 |
* Transmit thread
|
| 36 |
**********************************************************************/
|
| 37 |
static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_freq, const double tx_wave_ampl){ |
| 38 |
uhd::set_thread_priority_safe(); |
| 39 |
|
| 40 |
//create a transmit streamer
|
| 41 |
uhd::stream_args_t stream_args("fc32"); //complex floats |
| 42 |
uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args); |
| 43 |
|
| 44 |
//setup variables and allocate buffer
|
| 45 |
uhd::tx_metadata_t md; |
| 46 |
md.has_time_spec = false;
|
| 47 |
std::vector<std::complex<float> > buff(tx_stream->get_max_num_samps()*10); |
| 48 |
|
| 49 |
//values for the wave table lookup
|
| 50 |
size_t index = 0;
|
| 51 |
const double tx_rate = usrp->get_tx_rate(); |
| 52 |
const size_t step = boost::math::iround(wave_table_len * tx_wave_freq/tx_rate);
|
| 53 |
|
| 54 |
//fill buff and send until interrupted
|
| 55 |
while (not boost::this_thread::interruption_requested()){ |
| 56 |
for (size_t i = 0; i < buff.size(); i++){ |
| 57 |
buff[i] = float(tx_wave_ampl) * wave_table_lookup(index += step);
|
| 58 |
} |
| 59 |
tx_stream->send(&buff.front(), buff.size(), md); |
| 60 |
} |
| 61 |
|
| 62 |
//send a mini EOB packet
|
| 63 |
md.end_of_burst = true;
|
| 64 |
tx_stream->send("", 0, md); |
| 65 |
} |
| 66 |
|
| 67 |
/***********************************************************************
|
| 68 |
* Tune RX and TX routine
|
| 69 |
**********************************************************************/
|
| 70 |
static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double tx_lo_freq, const double rx_offset){ |
| 71 |
//tune the transmitter with no cordic
|
| 72 |
uhd::tune_request_t tx_tune_req(tx_lo_freq); |
| 73 |
tx_tune_req.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL; |
| 74 |
tx_tune_req.dsp_freq = 0;
|
| 75 |
usrp->set_tx_freq(tx_tune_req); |
| 76 |
|
| 77 |
//tune the receiver
|
| 78 |
usrp->set_rx_freq(usrp->get_tx_freq() - rx_offset); |
| 79 |
|
| 80 |
//wait for the LOs to become locked
|
| 81 |
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
|
| 82 |
boost::system_time start = boost::get_system_time(); |
| 83 |
while (not usrp->get_tx_sensor("lo_locked").to_bool() or not usrp->get_rx_sensor("lo_locked").to_bool()){ |
| 84 |
if (boost::get_system_time() > start + boost::posix_time::milliseconds(100)){ |
| 85 |
throw std::runtime_error("timed out waiting for TX and/or RX LO to lock"); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return usrp->get_tx_freq();
|
| 90 |
} |
| 91 |
|
| 92 |
/***********************************************************************
|
| 93 |
* Data capture routine
|
| 94 |
**********************************************************************/
|
| 95 |
static void capture_samples(uhd::usrp::multi_usrp::sptr usrp, uhd::rx_streamer::sptr rx_stream, std::vector<std::complex<float> > &buff){ |
| 96 |
uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE); |
| 97 |
stream_cmd.num_samps = buff.size(); |
| 98 |
stream_cmd.stream_now = true;
|
| 99 |
usrp->issue_stream_cmd(stream_cmd); |
| 100 |
uhd::rx_metadata_t md; |
| 101 |
const size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md);
|
| 102 |
|
| 103 |
//validate the received data
|
| 104 |
if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){
|
| 105 |
throw std::runtime_error(str(boost::format(
|
| 106 |
"Unexpected error code 0x%x"
|
| 107 |
) % md.error_code)); |
| 108 |
} |
| 109 |
if (num_rx_samps != buff.size()){
|
| 110 |
throw std::runtime_error("did not get all the samples requested"); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/***********************************************************************
|
| 115 |
* Main
|
| 116 |
**********************************************************************/
|
| 117 |
int UHD_SAFE_MAIN(int argc, char *argv[]){ |
| 118 |
std::string args;
|
| 119 |
double rate, tx_wave_freq, tx_wave_ampl, rx_offset;
|
| 120 |
double freq_start, freq_stop, freq_step;
|
| 121 |
size_t nsamps; |
| 122 |
|
| 123 |
po::options_description desc("Allowed options");
|
| 124 |
desc.add_options() |
| 125 |
("help", "help message") |
| 126 |
("verbose", "enable some verbose") |
| 127 |
("args", po::value<std::string>(&args)->default_value(""), "device address args [default = \"\"]") |
| 128 |
("rate", po::value<double>(&rate)->default_value(12.5e6), "RX and TX sample rate in Hz") |
| 129 |
("tx_wave_freq", po::value<double>(&tx_wave_freq)->default_value(507.123e3), "Transmit wave frequency in Hz") |
| 130 |
("tx_wave_ampl", po::value<double>(&tx_wave_ampl)->default_value(0.7), "Transmit wave amplitude in counts") |
| 131 |
("rx_offset", po::value<double>(&rx_offset)->default_value(.9344e6), "RX LO offset from the TX LO in Hz") |
| 132 |
("freq_start", po::value<double>(&freq_start), "Frequency start in Hz (do not specify for default)") |
| 133 |
("freq_stop", po::value<double>(&freq_stop), "Frequency stop in Hz (do not specify for default)") |
| 134 |
("freq_step", po::value<double>(&freq_step)->default_value(default_freq_step), "Step size for LO sweep in Hz") |
| 135 |
("nsamps", po::value<size_t>(&nsamps)->default_value(default_num_samps), "Samples per data capture") |
| 136 |
; |
| 137 |
|
| 138 |
po::variables_map vm; |
| 139 |
po::store(po::parse_command_line(argc, argv, desc), vm); |
| 140 |
po::notify(vm); |
| 141 |
|
| 142 |
//print the help message
|
| 143 |
if (vm.count("help")){ |
| 144 |
std::cout << boost::format("USRP Generate TX IQ Balance Calibration Table %s") % desc << std::endl;
|
| 145 |
std::cout << |
| 146 |
"This application measures leakage between RX and TX on an XCVR daughterboard to self-calibrate.\n"
|
| 147 |
<< std::endl; |
| 148 |
return ~0; |
| 149 |
} |
| 150 |
|
| 151 |
//create a usrp device
|
| 152 |
std::cout << std::endl; |
| 153 |
std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
|
| 154 |
uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args); |
| 155 |
|
| 156 |
//set the antennas to cal
|
| 157 |
if (not uhd::has(usrp->get_rx_antennas(), "CAL") or not uhd::has(usrp->get_tx_antennas(), "CAL")){ |
| 158 |
throw std::runtime_error("This board does not have the CAL antenna option, cannot self-calibrate."); |
| 159 |
} |
| 160 |
usrp->set_rx_antenna("CAL");
|
| 161 |
usrp->set_tx_antenna("CAL");
|
| 162 |
|
| 163 |
//set optimum gain settings
|
| 164 |
set_optimum_gain(usrp); |
| 165 |
|
| 166 |
//set the sample rates
|
| 167 |
usrp->set_rx_rate(rate); |
| 168 |
usrp->set_tx_rate(rate); |
| 169 |
|
| 170 |
//create a receive streamer
|
| 171 |
uhd::stream_args_t stream_args("fc32"); //complex floats |
| 172 |
uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args); |
| 173 |
|
| 174 |
//create a transmitter thread
|
| 175 |
boost::thread_group threads; |
| 176 |
threads.create_thread(boost::bind(&tx_thread, usrp, tx_wave_freq, tx_wave_ampl)); |
| 177 |
|
| 178 |
//re-usable buffer for samples
|
| 179 |
std::vector<std::complex<float> > buff(nsamps);
|
| 180 |
|
| 181 |
//store the results here
|
| 182 |
std::vector<result_t> results; |
| 183 |
|
| 184 |
if (not vm.count("freq_start")) freq_start = usrp->get_tx_freq_range().start() + 50e6; |
| 185 |
if (not vm.count("freq_stop")) freq_stop = usrp->get_tx_freq_range().stop() - 50e6; |
| 186 |
|
| 187 |
for (double tx_lo_i = freq_start; tx_lo_i <= freq_stop; tx_lo_i += freq_step){ |
| 188 |
const double tx_lo = tune_rx_and_tx(usrp, tx_lo_i, rx_offset); |
| 189 |
|
| 190 |
//frequency constants for this tune event
|
| 191 |
const double actual_rx_rate = usrp->get_rx_rate(); |
| 192 |
const double actual_tx_freq = usrp->get_tx_freq(); |
| 193 |
const double actual_rx_freq = usrp->get_rx_freq(); |
| 194 |
const double bb_tone_freq = actual_tx_freq + tx_wave_freq - actual_rx_freq; |
| 195 |
const double bb_imag_freq = actual_tx_freq - tx_wave_freq - actual_rx_freq; |
| 196 |
|
| 197 |
//capture initial uncorrected value
|
| 198 |
usrp->set_tx_iq_balance(std::polar<double>(1.0, 0.0)); |
| 199 |
capture_samples(usrp, rx_stream, buff); |
| 200 |
const double initial_suppression = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate) - compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate); |
| 201 |
|
| 202 |
//bounds and results from searching
|
| 203 |
std::complex<double> best_correction;
|
| 204 |
double phase_corr_start = -.3, phase_corr_stop = .3, phase_corr_step; |
| 205 |
double ampl_corr_start = -.3, ampl_corr_stop = .3, ampl_corr_step; |
| 206 |
double best_suppression = 0, best_phase_corr = 0, best_ampl_corr = 0; |
| 207 |
|
| 208 |
for (size_t i = 0; i < num_search_iters; i++){ |
| 209 |
|
| 210 |
phase_corr_step = (phase_corr_stop - phase_corr_start)/(num_search_steps-1);
|
| 211 |
ampl_corr_step = (ampl_corr_stop - ampl_corr_start)/(num_search_steps-1);
|
| 212 |
|
| 213 |
for (double phase_corr = phase_corr_start; phase_corr <= phase_corr_stop + phase_corr_step/2; phase_corr += phase_corr_step){ |
| 214 |
for (double ampl_corr = ampl_corr_start; ampl_corr <= ampl_corr_stop + ampl_corr_step/2; ampl_corr += ampl_corr_step){ |
| 215 |
|
| 216 |
const std::complex<double> correction = std::polar(ampl_corr+1, phase_corr*tau); |
| 217 |
usrp->set_tx_iq_balance(correction); |
| 218 |
|
| 219 |
//receive some samples
|
| 220 |
capture_samples(usrp, rx_stream, buff); |
| 221 |
|
| 222 |
const double tone_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate); |
| 223 |
const double imag_dbrms = compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate); |
| 224 |
const double suppression = tone_dbrms - imag_dbrms; |
| 225 |
|
| 226 |
if (suppression > best_suppression){
|
| 227 |
best_correction = correction; |
| 228 |
best_suppression = suppression; |
| 229 |
best_phase_corr = phase_corr; |
| 230 |
best_ampl_corr = ampl_corr; |
| 231 |
} |
| 232 |
|
| 233 |
}} |
| 234 |
|
| 235 |
//std::cout << "best_phase_corr " << best_phase_corr << std::endl;
|
| 236 |
//std::cout << "best_ampl_corr " << best_ampl_corr << std::endl;
|
| 237 |
//std::cout << "best_suppression " << best_suppression << std::endl;
|
| 238 |
|
| 239 |
phase_corr_start = best_phase_corr - phase_corr_step; |
| 240 |
phase_corr_stop = best_phase_corr + phase_corr_step; |
| 241 |
ampl_corr_start = best_ampl_corr - ampl_corr_step; |
| 242 |
ampl_corr_stop = best_ampl_corr + ampl_corr_step; |
| 243 |
} |
| 244 |
|
| 245 |
if (best_suppression > 30){ //most likely valid, keep result |
| 246 |
result_t result; |
| 247 |
result.freq = tx_lo; |
| 248 |
result.real_corr = best_correction.real(); |
| 249 |
result.imag_corr = best_correction.imag(); |
| 250 |
result.best = best_suppression; |
| 251 |
result.delta = best_suppression - initial_suppression; |
| 252 |
results.push_back(result); |
| 253 |
if (vm.count("verbose")){ |
| 254 |
std::cout << boost::format("%f MHz: best suppression %fdB, corrected %fdB") % (tx_lo/1e6) % result.best % result.delta << std::endl; |
| 255 |
} |
| 256 |
else std::cout << "." << std::flush; |
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
std::cout << std::endl; |
| 261 |
|
| 262 |
//stop the transmitter
|
| 263 |
threads.interrupt_all(); |
| 264 |
threads.join_all(); |
| 265 |
|
| 266 |
store_results(usrp, results, "TX", "tx", "iq"); |
| 267 |
|
| 268 |
return 0; |
| 269 |
} |