aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormichael-west <michael.west@ettus.com>2014-10-23 19:03:58 -0700
committerMartin Braun <martin.braun@ettus.com>2015-01-16 08:46:21 +0100
commit734180c44b438cf707c4ff48bf4b11a8c5a94136 (patch)
treebbff55a4c092588766a7a90f42e8c796eadcad01
parentb02fadef4af427cc06286a50cb0528a1f2aa2676 (diff)
downloaduhd-734180c44b438cf707c4ff48bf4b11a8c5a94136.tar.gz
uhd-734180c44b438cf707c4ff48bf4b11a8c5a94136.tar.bz2
uhd-734180c44b438cf707c4ff48bf4b11a8c5a94136.zip
UHD: Fixes for dboard calibration utilities.
-rw-r--r--host/utils/uhd_cal_rx_iq_balance.cpp160
-rw-r--r--host/utils/uhd_cal_tx_dc_offset.cpp172
-rw-r--r--host/utils/uhd_cal_tx_iq_balance.cpp169
-rw-r--r--host/utils/usrp_cal_utils.hpp231
4 files changed, 483 insertions, 249 deletions
diff --git a/host/utils/uhd_cal_rx_iq_balance.cpp b/host/utils/uhd_cal_rx_iq_balance.cpp
index 3188e02a0..2414ef007 100644
--- a/host/utils/uhd_cal_rx_iq_balance.cpp
+++ b/host/utils/uhd_cal_rx_iq_balance.cpp
@@ -1,5 +1,5 @@
//
-// Copyright 2010,2012 Ettus Research LLC
+// Copyright 2010,2012,2014 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@@ -37,7 +37,8 @@ namespace po = boost::program_options;
/***********************************************************************
* Transmit thread
**********************************************************************/
-static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_ampl){
+static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_ampl)
+{
uhd::set_thread_priority_safe();
//create a transmit streamer
@@ -50,10 +51,10 @@ static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_amp
std::vector<samp_type> buff(tx_stream->get_max_num_samps()*10);
//fill buff and send until interrupted
- while (not boost::this_thread::interruption_requested()){
- for (size_t i = 0; i < buff.size(); i++){
+ while (not boost::this_thread::interruption_requested())
+ {
+ for (size_t i = 0; i < buff.size(); i++)
buff[i] = float(tx_wave_ampl);
- }
tx_stream->send(&buff.front(), buff.size(), md);
}
@@ -65,26 +66,34 @@ static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_amp
/***********************************************************************
* Tune RX and TX routine
**********************************************************************/
-static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double rx_lo_freq, const double tx_offset){
+static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double rx_lo_freq, const double tx_offset)
+{
//tune the receiver with no cordic
uhd::tune_request_t rx_tune_req(rx_lo_freq);
rx_tune_req.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL;
rx_tune_req.dsp_freq = 0;
usrp->set_rx_freq(rx_tune_req);
- //tune the transmitter with no cordic
- uhd::tune_request_t tx_tune_req(usrp->get_rx_freq() - tx_offset);
+ //tune the transmitter
+ double tx_freq = usrp->get_rx_freq() + tx_offset;
+ double min_fe_tx_freq = usrp->get_fe_tx_freq_range().start();
+ double max_fe_tx_freq = usrp->get_fe_tx_freq_range().stop();
+ uhd::tune_request_t tx_tune_req(tx_freq);
tx_tune_req.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL;
tx_tune_req.dsp_freq = 0;
+ if (tx_freq < min_fe_tx_freq)
+ tx_tune_req.dsp_freq = tx_freq - min_fe_tx_freq;
+ else if (tx_freq > max_fe_tx_freq)
+ tx_tune_req.dsp_freq = tx_freq - max_fe_tx_freq;
usrp->set_tx_freq(tx_tune_req);
//wait for the LOs to become locked
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
boost::system_time start = boost::get_system_time();
- while (not usrp->get_tx_sensor("lo_locked").to_bool() or not usrp->get_rx_sensor("lo_locked").to_bool()){
- if (boost::get_system_time() > start + boost::posix_time::milliseconds(100)){
+ while (not usrp->get_tx_sensor("lo_locked").to_bool() or not usrp->get_rx_sensor("lo_locked").to_bool())
+ {
+ if (boost::get_system_time() > start + boost::posix_time::milliseconds(100))
throw std::runtime_error("timed out waiting for TX and/or RX LO to lock");
- }
}
return usrp->get_rx_freq();
@@ -93,11 +102,13 @@ static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double rx_l
/***********************************************************************
* Main
**********************************************************************/
-int UHD_SAFE_MAIN(int argc, char *argv[]){
+int UHD_SAFE_MAIN(int argc, char *argv[])
+{
std::string args, subdev, serial;
double tx_wave_ampl, tx_offset;
double freq_start, freq_stop, freq_step;
size_t nsamps;
+ double precision;
po::options_description desc("Allowed options");
desc.add_options()
@@ -110,7 +121,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
("freq_start", po::value<double>(&freq_start), "Frequency start in Hz (do not specify for default)")
("freq_stop", po::value<double>(&freq_stop), "Frequency stop in Hz (do not specify for default)")
("freq_step", po::value<double>(&freq_step)->default_value(default_freq_step), "Step size for LO sweep in Hz")
- ("nsamps", po::value<size_t>(&nsamps)->default_value(default_num_samps), "Samples per data capture")
+ ("nsamps", po::value<size_t>(&nsamps), "Samples per data capture")
+ ("precision", po::value<double>(&precision)->default_value(default_precision), "Correction precision (default=0.0001)")
;
po::variables_map vm;
@@ -130,6 +142,9 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
// Create a USRP device
uhd::usrp::multi_usrp::sptr usrp = setup_usrp_for_cal(args, subdev, serial);
+ if (not vm.count("nsamps"))
+ nsamps = size_t(usrp->get_rx_rate() / default_fft_bin_size);
+
//create a receive streamer
uhd::stream_args_t stream_args("fc32"); //complex floats
uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
@@ -144,11 +159,35 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
//store the results here
std::vector<result_t> results;
- if (not vm.count("freq_start")) freq_start = usrp->get_rx_freq_range().start() + 50e6;
- if (not vm.count("freq_stop")) freq_stop = usrp->get_rx_freq_range().stop() - 50e6;
+ if (not vm.count("freq_start")) freq_start = usrp->get_fe_rx_freq_range().start();
+ if (not vm.count("freq_stop")) freq_stop = usrp->get_fe_tx_freq_range().stop();
+
+ //check start and stop frequencies
+ if (freq_start < usrp->get_fe_rx_freq_range().start())
+ {
+ std::cerr << "freq_start must be " << usrp->get_fe_rx_freq_range().start() << " or greater for this daughter board" << std::endl;
+ return EXIT_FAILURE;
+ }
+ if (freq_stop > usrp->get_fe_rx_freq_range().stop())
+ {
+ std::cerr << "freq_stop must be " << usrp->get_fe_rx_freq_range().stop() << " or less for this daughter board" << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ //check tx_offset
+ double min_tx_offset = usrp->get_tx_freq_range().start() - usrp->get_fe_rx_freq_range().start();
+ double max_tx_offset = usrp->get_tx_freq_range().stop() - usrp->get_fe_rx_freq_range().stop();
+ if (tx_offset < min_tx_offset or tx_offset > max_tx_offset)
+ {
+ std::cerr << "tx_offset must be between " << min_tx_offset << " and "
+ << max_tx_offset << " for this daughter board" << std::endl;
+ return EXIT_FAILURE;
+ }
+
UHD_MSG(status) << boost::format("Calibration frequency range: %d MHz -> %d MHz") % (freq_start/1e6) % (freq_stop/1e6) << std::endl;
- for (double rx_lo_i = freq_start; rx_lo_i <= freq_stop; rx_lo_i += freq_step){
+ for (double rx_lo_i = freq_start; rx_lo_i <= freq_stop; rx_lo_i += freq_step)
+ {
const double rx_lo = tune_rx_and_tx(usrp, rx_lo_i, tx_offset);
//frequency constants for this tune event
@@ -158,73 +197,78 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
const double bb_tone_freq = actual_tx_freq - actual_rx_freq;
const double bb_imag_freq = -bb_tone_freq;
- //capture initial uncorrected value
+ //reset RX IQ balance
usrp->set_rx_iq_balance(0.0);
+
+ //set optimal RX gain setting for this frequency
+ set_optimal_rx_gain(usrp, rx_stream);
+
+ //capture initial uncorrected value
capture_samples(usrp, rx_stream, buff, nsamps);
const double initial_suppression = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate) - compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate);
//bounds and results from searching
- std::complex<double> best_correction;
- double phase_corr_start = -.3, phase_corr_stop = .3, phase_corr_step;
- double ampl_corr_start = -.3, ampl_corr_stop = .3, ampl_corr_step;
- double best_suppression = 0, best_phase_corr = 0, best_ampl_corr = 0;
-
- for (size_t i = 0; i < num_search_iters; i++){
-
- phase_corr_step = (phase_corr_stop - phase_corr_start)/(num_search_steps-1);
- ampl_corr_step = (ampl_corr_stop - ampl_corr_start)/(num_search_steps-1);
-
- for (double phase_corr = phase_corr_start; phase_corr <= phase_corr_stop + phase_corr_step/2; phase_corr += phase_corr_step){
- for (double ampl_corr = ampl_corr_start; ampl_corr <= ampl_corr_stop + ampl_corr_step/2; ampl_corr += ampl_corr_step){
-
- const std::complex<double> correction(ampl_corr, phase_corr);
- usrp->set_rx_iq_balance(correction);
-
- //receive some samples
- capture_samples(usrp, rx_stream, buff, nsamps);
-
- const double tone_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate);
- const double imag_dbrms = compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate);
- const double suppression = tone_dbrms - imag_dbrms;
-
- if (suppression > best_suppression){
- best_correction = correction;
- best_suppression = suppression;
- best_phase_corr = phase_corr;
- best_ampl_corr = ampl_corr;
+ double phase_corr_start = -1.0;
+ double phase_corr_stop = 1.0;
+ double phase_corr_step = (phase_corr_stop - phase_corr_start)/(num_search_steps+1);
+ double ampl_corr_start = -1.0;
+ double ampl_corr_stop = 1.0;
+ double ampl_corr_step = (ampl_corr_stop - ampl_corr_start)/(num_search_steps+1);
+ double best_suppression = 0;
+ double best_phase_corr = 0;
+ double best_ampl_corr = 0;
+ while (phase_corr_step >= precision or ampl_corr_step >= precision)
+ {
+ for (double phase_corr = phase_corr_start + phase_corr_step; phase_corr <= phase_corr_stop - phase_corr_step; phase_corr += phase_corr_step)
+ {
+ for (double ampl_corr = ampl_corr_start + ampl_corr_step; ampl_corr <= ampl_corr_stop - ampl_corr_step; ampl_corr += ampl_corr_step)
+ {
+ const std::complex<double> correction(ampl_corr, phase_corr);
+ usrp->set_rx_iq_balance(correction);
+
+ //receive some samples
+ capture_samples(usrp, rx_stream, buff, nsamps);
+ const double tone_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate);
+ const double imag_dbrms = compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate);
+ const double suppression = tone_dbrms - imag_dbrms;
+
+ if (suppression > best_suppression)
+ {
+ best_suppression = suppression;
+ best_phase_corr = phase_corr;
+ best_ampl_corr = ampl_corr;
+ }
}
-
- }}
-
- //std::cout << "best_phase_corr " << best_phase_corr << std::endl;
- //std::cout << "best_ampl_corr " << best_ampl_corr << std::endl;
- //std::cout << "best_suppression " << best_suppression << std::endl;
+ }
phase_corr_start = best_phase_corr - phase_corr_step;
phase_corr_stop = best_phase_corr + phase_corr_step;
+ phase_corr_step = (phase_corr_stop - phase_corr_start)/(num_search_steps+1);
ampl_corr_start = best_ampl_corr - ampl_corr_step;
ampl_corr_stop = best_ampl_corr + ampl_corr_step;
+ ampl_corr_step = (ampl_corr_stop - ampl_corr_start)/(num_search_steps+1);
}
- if (best_suppression > 30){ //most likely valid, keep result
+ if (best_suppression > initial_suppression) //keep result
+ {
result_t result;
result.freq = rx_lo;
- result.real_corr = best_correction.real();
- result.imag_corr = best_correction.imag();
+ result.real_corr = best_ampl_corr;
+ result.imag_corr = best_phase_corr;
result.best = best_suppression;
result.delta = best_suppression - initial_suppression;
results.push_back(result);
- if (vm.count("verbose")){
+ if (vm.count("verbose"))
std::cout << boost::format("RX IQ: %f MHz: best suppression %f dB, corrected %f dB") % (rx_lo/1e6) % result.best % result.delta << std::endl;
- }
- else std::cout << "." << std::flush;
+ else
+ std::cout << "." << std::flush;
}
-
}
std::cout << std::endl;
//stop the transmitter
threads.interrupt_all();
+ boost::this_thread::sleep(boost::posix_time::milliseconds(500)); //wait for threads to finish
threads.join_all();
store_results(results, "RX", "rx", "iq", serial);
diff --git a/host/utils/uhd_cal_tx_dc_offset.cpp b/host/utils/uhd_cal_tx_dc_offset.cpp
index b5c5293f0..8aa505010 100644
--- a/host/utils/uhd_cal_tx_dc_offset.cpp
+++ b/host/utils/uhd_cal_tx_dc_offset.cpp
@@ -1,5 +1,5 @@
//
-// Copyright 2010,2012 Ettus Research LLC
+// Copyright 2010,2012,2014 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@@ -35,9 +35,13 @@ namespace po = boost::program_options;
/***********************************************************************
* Transmit thread
**********************************************************************/
-static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_freq, const double tx_wave_ampl){
+static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_freq, const double tx_wave_ampl)
+{
uhd::set_thread_priority_safe();
+ // set max TX gain
+ usrp->set_tx_gain(usrp->get_tx_gain_range().stop());
+
//create a transmit streamer
uhd::stream_args_t stream_args("fc32"); //complex floats
uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args);
@@ -50,14 +54,14 @@ static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_fre
//values for the wave table lookup
size_t index = 0;
const double tx_rate = usrp->get_tx_rate();
- const size_t step = boost::math::iround(wave_table_len * tx_wave_freq/tx_rate);
+ const size_t step = boost::math::iround(wave_table_len * tx_wave_freq / tx_rate);
wave_table table(tx_wave_ampl);
//fill buff and send until interrupted
- while (not boost::this_thread::interruption_requested()){
- for (size_t i = 0; i < buff.size(); i++){
+ while (not boost::this_thread::interruption_requested())
+ {
+ for (size_t i = 0; i < buff.size(); i++)
buff[i] = table(index += step);
- }
tx_stream->send(&buff.front(), buff.size(), md);
}
@@ -69,7 +73,8 @@ static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_fre
/***********************************************************************
* Tune RX and TX routine
**********************************************************************/
-static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double tx_lo_freq, const double rx_offset){
+static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double tx_lo_freq, const double rx_offset)
+{
//tune the transmitter with no cordic
uhd::tune_request_t tx_tune_req(tx_lo_freq);
tx_tune_req.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL;
@@ -77,15 +82,25 @@ static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double tx_l
usrp->set_tx_freq(tx_tune_req);
//tune the receiver
- usrp->set_rx_freq(usrp->get_tx_freq() - rx_offset);
+ double rx_freq = usrp->get_tx_freq() - rx_offset;
+ double min_fe_rx_freq = usrp->get_fe_rx_freq_range().start();
+ double max_fe_rx_freq = usrp->get_fe_rx_freq_range().stop();
+ uhd::tune_request_t rx_tune_req(rx_freq);
+ rx_tune_req.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL;
+ rx_tune_req.dsp_freq = 0;
+ if (rx_freq < min_fe_rx_freq)
+ rx_tune_req.dsp_freq = rx_freq - min_fe_rx_freq;
+ else if (rx_freq > max_fe_rx_freq)
+ rx_tune_req.dsp_freq = rx_freq - max_fe_rx_freq;
+ usrp->set_rx_freq(rx_tune_req);
//wait for the LOs to become locked
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
boost::system_time start = boost::get_system_time();
- while (not usrp->get_tx_sensor("lo_locked").to_bool() or not usrp->get_rx_sensor("lo_locked").to_bool()){
- if (boost::get_system_time() > start + boost::posix_time::milliseconds(100)){
+ while (not usrp->get_tx_sensor("lo_locked").to_bool() or not usrp->get_rx_sensor("lo_locked").to_bool())
+ {
+ if (boost::get_system_time() > start + boost::posix_time::milliseconds(100))
throw std::runtime_error("timed out waiting for TX and/or RX LO to lock");
- }
}
return usrp->get_tx_freq();
@@ -94,11 +109,13 @@ static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double tx_l
/***********************************************************************
* Main
**********************************************************************/
-int UHD_SAFE_MAIN(int argc, char *argv[]){
+int UHD_SAFE_MAIN(int argc, char *argv[])
+{
std::string args, subdev, serial;
double tx_wave_freq, tx_wave_ampl, rx_offset;
double freq_start, freq_stop, freq_step;
size_t nsamps;
+ double precision;
po::options_description desc("Allowed options");
desc.add_options()
@@ -112,7 +129,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
("freq_start", po::value<double>(&freq_start), "Frequency start in Hz (do not specify for default)")
("freq_stop", po::value<double>(&freq_stop), "Frequency stop in Hz (do not specify for default)")
("freq_step", po::value<double>(&freq_step)->default_value(default_freq_step), "Step size for LO sweep in Hz")
- ("nsamps", po::value<size_t>(&nsamps)->default_value(default_num_samps), "Samples per data capture")
+ ("nsamps", po::value<size_t>(&nsamps), "Samples per data capture")
+ ("precision", po::value<double>(&precision)->default_value(default_precision), "Correction precision (default=0.0001)")
;
po::variables_map vm;
@@ -132,6 +150,9 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
// Create a USRP device
uhd::usrp::multi_usrp::sptr usrp = setup_usrp_for_cal(args, subdev, serial);
+ if (not vm.count("nsamps"))
+ nsamps = size_t(usrp->get_rx_rate() / default_fft_bin_size);
+
//create a receive streamer
uhd::stream_args_t stream_args("fc32"); //complex floats
uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
@@ -146,11 +167,38 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
//store the results here
std::vector<result_t> results;
- if (not vm.count("freq_start")) freq_start = usrp->get_tx_freq_range().start() + 50e6;
- if (not vm.count("freq_stop")) freq_stop = usrp->get_tx_freq_range().stop() - 50e6;
+ if (not vm.count("freq_start")) freq_start = usrp->get_fe_tx_freq_range().start();
+ if (not vm.count("freq_stop")) freq_stop = usrp->get_fe_tx_freq_range().stop();
+
+ //check start and stop frequencies
+ if (freq_start < usrp->get_fe_tx_freq_range().start())
+ {
+ std::cerr << "freq_start must be " << usrp->get_fe_tx_freq_range().start() << " or greater for this daughter board" << std::endl;
+ return EXIT_FAILURE;
+ }
+ if (freq_stop > usrp->get_fe_tx_freq_range().stop())
+ {
+ std::cerr << "freq_stop must be " << usrp->get_fe_tx_freq_range().stop() << " or less for this daughter board" << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ //check rx_offset
+ double min_rx_offset = usrp->get_rx_freq_range().start() - usrp->get_fe_tx_freq_range().start();
+ double max_rx_offset = usrp->get_rx_freq_range().stop() - usrp->get_fe_tx_freq_range().stop();
+ if (rx_offset < min_rx_offset or rx_offset > max_rx_offset)
+ {
+ std::cerr << "rx_offset must be between " << min_rx_offset << " and "
+ << max_rx_offset << " for this daughter board" << std::endl;
+ return EXIT_FAILURE;
+ }
+
UHD_MSG(status) << boost::format("Calibration frequency range: %d MHz -> %d MHz") % (freq_start/1e6) % (freq_stop/1e6) << std::endl;
- for (double tx_lo_i = freq_start; tx_lo_i <= freq_stop; tx_lo_i += freq_step){
+ //set RX gain
+ usrp->set_rx_gain(0);
+
+ for (double tx_lo_i = freq_start; tx_lo_i <= freq_stop; tx_lo_i += freq_step)
+ {
const double tx_lo = tune_rx_and_tx(usrp, tx_lo_i, rx_offset);
//frequency constants for this tune event
@@ -159,69 +207,75 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
const double actual_rx_freq = usrp->get_rx_freq();
const double bb_dc_freq = actual_tx_freq - actual_rx_freq;
- //capture initial uncorrected value
+ //reset TX DC offset
usrp->set_tx_dc_offset(std::complex<double>(0, 0));
+
+ //capture initial uncorrected value
capture_samples(usrp, rx_stream, buff, nsamps);
const double initial_dc_dbrms = compute_tone_dbrms(buff, bb_dc_freq/actual_rx_rate);
//bounds and results from searching
- double dc_i_start = -.01, dc_i_stop = .01, dc_i_step;
- double dc_q_start = -.01, dc_q_stop = .01, dc_q_step;
- double lowest_offset = 0, best_dc_i = 0, best_dc_q = 0;
-
- for (size_t i = 0; i < num_search_iters; i++){
-
- dc_i_step = (dc_i_stop - dc_i_start)/(num_search_steps-1);
- dc_q_step = (dc_q_stop - dc_q_start)/(num_search_steps-1);
-
- for (double dc_i = dc_i_start; dc_i <= dc_i_stop + dc_i_step/2; dc_i += dc_i_step){
- for (double dc_q = dc_q_start; dc_q <= dc_q_stop + dc_q_step/2; dc_q += dc_q_step){
-
- const std::complex<double> correction(dc_i, dc_q);
- usrp->set_tx_dc_offset(correction);
-
- //receive some samples
- capture_samples(usrp, rx_stream, buff, nsamps);
-
- const double dc_dbrms = compute_tone_dbrms(buff, bb_dc_freq/actual_rx_rate);
-
- if (dc_dbrms < lowest_offset){
- lowest_offset = dc_dbrms;
- best_dc_i = dc_i;
- best_dc_q = dc_q;
+ double i_corr_start = -1.0;
+ double i_corr_stop = 1.0;
+ double i_corr_step = (i_corr_stop - i_corr_start)/(num_search_steps+1);
+ double q_corr_start = -1.0;
+ double q_corr_stop = 1.0;
+ double q_corr_step= (q_corr_stop - q_corr_start)/(num_search_steps+1);
+ double best_dc_dbrms = initial_dc_dbrms;
+ double best_i_corr = 0;
+ double best_q_corr = 0;
+ while (i_corr_step >= precision or q_corr_step >= precision)
+ {
+ for (double i_corr = i_corr_start + i_corr_step; i_corr <= i_corr_stop - i_corr_step; i_corr += i_corr_step)
+ {
+ for (double q_corr = q_corr_start + q_corr_step; q_corr <= q_corr_stop - q_corr_step; q_corr += q_corr_step)
+ {
+ const std::complex<double> correction(i_corr, q_corr);
+ usrp->set_tx_dc_offset(correction);
+
+ //receive some samples
+ capture_samples(usrp, rx_stream, buff, nsamps);
+ const double dc_dbrms = compute_tone_dbrms(buff, bb_dc_freq/actual_rx_rate);
+
+ if (dc_dbrms < best_dc_dbrms)
+ {
+ best_dc_dbrms = dc_dbrms;
+ best_i_corr = i_corr;
+ best_q_corr = q_corr;
+ }
}
+ }
- }}
-
- //std::cout << "best_dc_i " << best_dc_i << std::endl;
- //std::cout << "best_dc_q " << best_dc_q << std::endl;
- //std::cout << "lowest_offset " << lowest_offset << std::endl;
-
- dc_i_start = best_dc_i - dc_i_step;
- dc_i_stop = best_dc_i + dc_i_step;
- dc_q_start = best_dc_q - dc_q_step;
- dc_q_stop = best_dc_q + dc_q_step;
+ i_corr_start = best_i_corr - i_corr_step;
+ i_corr_stop = best_i_corr + i_corr_step;
+ i_corr_step = (i_corr_stop - i_corr_start)/(num_search_steps+1);
+ q_corr_start = best_q_corr - q_corr_step;
+ q_corr_stop = best_q_corr + q_corr_step;
+ q_corr_step = (q_corr_stop - q_corr_start)/(num_search_steps+1);
}
- if (lowest_offset < initial_dc_dbrms){ //most likely valid, keep result
+ if (best_dc_dbrms < initial_dc_dbrms) //keep result
+ {
result_t result;
result.freq = tx_lo;
- result.real_corr = best_dc_i;
- result.imag_corr = best_dc_q;
- result.best = lowest_offset;
- result.delta = initial_dc_dbrms - lowest_offset;
+ result.real_corr = best_i_corr;
+ result.imag_corr = best_q_corr;
+ result.best = best_dc_dbrms;
+ result.delta = initial_dc_dbrms - best_dc_dbrms;
results.push_back(result);
- if (vm.count("verbose")){
+ if (vm.count("verbose"))
std::cout << boost::format("TX DC: %f MHz: lowest offset %f dB, corrected %f dB") % (tx_lo/1e6) % result.best % result.delta << std::endl;
- }
- else std::cout << "." << std::flush;
+ else
+ std::cout << "." << std::flush;
}
}
+
std::cout << std::endl;
//stop the transmitter
threads.interrupt_all();
+ boost::this_thread::sleep(boost::posix_time::milliseconds(500)); //wait for threads to finish
threads.join_all();
store_results(results, "TX", "tx", "dc", serial);
diff --git a/host/utils/uhd_cal_tx_iq_balance.cpp b/host/utils/uhd_cal_tx_iq_balance.cpp
index 6461b3d71..5952cccc4 100644
--- a/host/utils/uhd_cal_tx_iq_balance.cpp
+++ b/host/utils/uhd_cal_tx_iq_balance.cpp
@@ -1,5 +1,5 @@
//
-// Copyright 2010,2012 Ettus Research LLC
+// Copyright 2010,2012,2014 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@@ -31,9 +31,13 @@ namespace po = boost::program_options;
/***********************************************************************
* Transmit thread
**********************************************************************/
-static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_freq, const double tx_wave_ampl){
+static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_freq, const double tx_wave_ampl)
+{
uhd::set_thread_priority_safe();
+ // set max TX gain
+ usrp->set_tx_gain(usrp->get_tx_gain_range().stop());
+
//create a transmit streamer
uhd::stream_args_t stream_args("fc32"); //complex floats
uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args);
@@ -46,14 +50,14 @@ static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_fre
//values for the wave table lookup
size_t index = 0;
const double tx_rate = usrp->get_tx_rate();
- const size_t step = boost::math::iround(wave_table_len * tx_wave_freq/tx_rate);
+ const size_t step = boost::math::iround(wave_table_len * tx_wave_freq / tx_rate);
wave_table table(tx_wave_ampl);
//fill buff and send until interrupted
- while (not boost::this_thread::interruption_requested()){
- for (size_t i = 0; i < buff.size(); i++){
+ while (not boost::this_thread::interruption_requested())
+ {
+ for (size_t i = 0; i < buff.size(); i++)
buff[i] = table(index += step);
- }
tx_stream->send(&buff.front(), buff.size(), md);
}
@@ -65,7 +69,8 @@ static void tx_thread(uhd::usrp::multi_usrp::sptr usrp, const double tx_wave_fre
/***********************************************************************
* Tune RX and TX routine
**********************************************************************/
-static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double tx_lo_freq, const double rx_offset){
+static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double tx_lo_freq, const double rx_offset)
+{
//tune the transmitter with no cordic
uhd::tune_request_t tx_tune_req(tx_lo_freq);
tx_tune_req.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL;
@@ -73,15 +78,25 @@ static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double tx_l
usrp->set_tx_freq(tx_tune_req);
//tune the receiver
- usrp->set_rx_freq(usrp->get_tx_freq() - rx_offset);
+ double rx_freq = usrp->get_tx_freq() - rx_offset;
+ double min_fe_rx_freq = usrp->get_fe_rx_freq_range().start();
+ double max_fe_rx_freq = usrp->get_fe_rx_freq_range().stop();
+ uhd::tune_request_t rx_tune_req(rx_freq);
+ rx_tune_req.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL;
+ rx_tune_req.dsp_freq = 0;
+ if (rx_freq < min_fe_rx_freq)
+ rx_tune_req.dsp_freq = rx_freq - min_fe_rx_freq;
+ else if (rx_freq > max_fe_rx_freq)
+ rx_tune_req.dsp_freq = rx_freq - max_fe_rx_freq;
+ usrp->set_rx_freq(rx_tune_req);
//wait for the LOs to become locked
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
boost::system_time start = boost::get_system_time();
- while (not usrp->get_tx_sensor("lo_locked").to_bool() or not usrp->get_rx_sensor("lo_locked").to_bool()){
- if (boost::get_system_time() > start + boost::posix_time::milliseconds(100)){
+ while (not usrp->get_tx_sensor("lo_locked").to_bool() or not usrp->get_rx_sensor("lo_locked").to_bool())
+ {
+ if (boost::get_system_time() > start + boost::posix_time::milliseconds(100))
throw std::runtime_error("timed out waiting for TX and/or RX LO to lock");
- }
}
return usrp->get_tx_freq();
@@ -90,11 +105,13 @@ static double tune_rx_and_tx(uhd::usrp::multi_usrp::sptr usrp, const double tx_l
/***********************************************************************
* Main
**********************************************************************/
-int UHD_SAFE_MAIN(int argc, char *argv[]){
+int UHD_SAFE_MAIN(int argc, char *argv[])
+{
std::string args, subdev, serial;
double tx_wave_freq, tx_wave_ampl, rx_offset;
double freq_start, freq_stop, freq_step;
size_t nsamps;
+ double precision;
po::options_description desc("Allowed options");
desc.add_options()
@@ -108,7 +125,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
("freq_start", po::value<double>(&freq_start), "Frequency start in Hz (do not specify for default)")
("freq_stop", po::value<double>(&freq_stop), "Frequency stop in Hz (do not specify for default)")
("freq_step", po::value<double>(&freq_step)->default_value(default_freq_step), "Step size for LO sweep in Hz")
- ("nsamps", po::value<size_t>(&nsamps)->default_value(default_num_samps), "Samples per data capture")
+ ("nsamps", po::value<size_t>(&nsamps), "Samples per data capture")
+ ("precision", po::value<double>(&precision)->default_value(default_precision), "Correction precision (default=0.0001)")
;
po::variables_map vm;
@@ -116,7 +134,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
po::notify(vm);
//print the help message
- if (vm.count("help")){
+ if (vm.count("help"))
+ {
std::cout << boost::format("USRP Generate TX IQ Balance Calibration Table %s") % desc << std::endl;
std::cout <<
"This application measures leakage between RX and TX on a transceiver daughterboard to self-calibrate.\n"
@@ -128,6 +147,9 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
// Create a USRP device
uhd::usrp::multi_usrp::sptr usrp = setup_usrp_for_cal(args, subdev, serial);
+ if (not vm.count("nsamps"))
+ nsamps = size_t(usrp->get_rx_rate() / default_fft_bin_size);
+
//create a receive streamer
uhd::stream_args_t stream_args("fc32"); //complex floats
uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
@@ -142,11 +164,35 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
//store the results here
std::vector<result_t> results;
- if (not vm.count("freq_start")) freq_start = usrp->get_tx_freq_range().start() + 50e6;
- if (not vm.count("freq_stop")) freq_stop = usrp->get_tx_freq_range().stop() - 50e6;
+ if (not vm.count("freq_start")) freq_start = usrp->get_fe_tx_freq_range().start();
+ if (not vm.count("freq_stop")) freq_stop = usrp->get_fe_tx_freq_range().stop();
+
+ //check start and stop frequencies
+ if (freq_start < usrp->get_fe_tx_freq_range().start())
+ {
+ std::cerr << "freq_start must be " << usrp->get_fe_tx_freq_range().start() << " or greater for this daughter board" << std::endl;
+ return EXIT_FAILURE;
+ }
+ if (freq_stop > usrp->get_fe_tx_freq_range().stop())
+ {
+ std::cerr << "freq_stop must be " << usrp->get_fe_tx_freq_range().stop() << " or less for this daughter board" << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ //check rx_offset
+ double min_rx_offset = usrp->get_rx_freq_range().start() - usrp->get_fe_tx_freq_range().start();
+ double max_rx_offset = usrp->get_rx_freq_range().stop() - usrp->get_fe_tx_freq_range().stop();
+ if (rx_offset < min_rx_offset or rx_offset > max_rx_offset)
+ {
+ std::cerr << "rx_offset must be between " << min_rx_offset << " and "
+ << max_rx_offset << " for this daughter board" << std::endl;
+ return EXIT_FAILURE;
+ }
+
UHD_MSG(status) << boost::format("Calibration frequency range: %d MHz -> %d MHz") % (freq_start/1e6) % (freq_stop/1e6) << std::endl;
- for (double tx_lo_i = freq_start; tx_lo_i <= freq_stop; tx_lo_i += freq_step){
+ for (double tx_lo_i = freq_start; tx_lo_i <= freq_stop; tx_lo_i += freq_step)
+ {
const double tx_lo = tune_rx_and_tx(usrp, tx_lo_i, rx_offset);
//frequency constants for this tune event
@@ -156,73 +202,78 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
const double bb_tone_freq = actual_tx_freq + tx_wave_freq - actual_rx_freq;
const double bb_imag_freq = actual_tx_freq - tx_wave_freq - actual_rx_freq;
- //capture initial uncorrected value
+ //reset TX IQ balance
usrp->set_tx_iq_balance(0.0);
+
+ //set optimal RX gain setting for this frequency
+ set_optimal_rx_gain(usrp, rx_stream, tx_wave_freq);
+
+ //capture initial uncorrected value
capture_samples(usrp, rx_stream, buff, nsamps);
const double initial_suppression = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate) - compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate);
//bounds and results from searching
- std::complex<double> best_correction;
- double phase_corr_start = -.3, phase_corr_stop = .3, phase_corr_step;
- double ampl_corr_start = -.3, ampl_corr_stop = .3, ampl_corr_step;
- double best_suppression = 0, best_phase_corr = 0, best_ampl_corr = 0;
-
- for (size_t i = 0; i < num_search_iters; i++){
-
- phase_corr_step = (phase_corr_stop - phase_corr_start)/(num_search_steps-1);
- ampl_corr_step = (ampl_corr_stop - ampl_corr_start)/(num_search_steps-1);
-
- for (double phase_corr = phase_corr_start; phase_corr <= phase_corr_stop + phase_corr_step/2; phase_corr += phase_corr_step){
- for (double ampl_corr = ampl_corr_start; ampl_corr <= ampl_corr_stop + ampl_corr_step/2; ampl_corr += ampl_corr_step){
-
- const std::complex<double> correction(ampl_corr, phase_corr);
- usrp->set_tx_iq_balance(correction);
-
- //receive some samples
- capture_samples(usrp, rx_stream, buff, nsamps);
-
- const double tone_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate);
- const double imag_dbrms = compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate);
- const double suppression = tone_dbrms - imag_dbrms;
-
- if (suppression > best_suppression){
- best_correction = correction;
- best_suppression = suppression;
- best_phase_corr = phase_corr;
- best_ampl_corr = ampl_corr;
+ double phase_corr_start = -1.0;
+ double phase_corr_stop = 1.0;
+ double phase_corr_step = (phase_corr_stop - phase_corr_start)/(num_search_steps+1);
+ double ampl_corr_start = -1.0;
+ double ampl_corr_stop = 1.0;
+ double ampl_corr_step = (ampl_corr_stop - ampl_corr_start)/(num_search_steps+1);
+ double best_suppression = 0;
+ double best_phase_corr = 0;
+ double best_ampl_corr = 0;
+ while (phase_corr_step >= precision or ampl_corr_step >= precision)
+ {
+ for (double phase_corr = phase_corr_start + phase_corr_step; phase_corr <= phase_corr_stop - phase_corr_step; phase_corr += phase_corr_step)
+ {
+ for (double ampl_corr = ampl_corr_start + ampl_corr_step; ampl_corr <= ampl_corr_stop - ampl_corr_step; ampl_corr += ampl_corr_step)
+ {
+ const std::complex<double> correction(ampl_corr, phase_corr);
+ usrp->set_tx_iq_balance(correction);
+
+ //receive some samples
+ capture_samples(usrp, rx_stream, buff, nsamps);
+ const double tone_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate);
+ const double imag_dbrms = compute_tone_dbrms(buff, bb_imag_freq/actual_rx_rate);
+ const double suppression = tone_dbrms - imag_dbrms;
+
+ if (suppression > best_suppression)
+ {
+ best_suppression = suppression;
+ best_phase_corr = phase_corr;
+ best_ampl_corr = ampl_corr;
+ }
}
-
- }}
-
- //std::cout << "best_phase_corr " << best_phase_corr << std::endl;
- //std::cout << "best_ampl_corr " << best_ampl_corr << std::endl;
- //std::cout << "best_suppression " << best_suppression << std::endl;
+ }
phase_corr_start = best_phase_corr - phase_corr_step;
phase_corr_stop = best_phase_corr + phase_corr_step;
+ phase_corr_step = (phase_corr_stop - phase_corr_start)/(num_search_steps+1);
ampl_corr_start = best_ampl_corr - ampl_corr_step;
ampl_corr_stop = best_ampl_corr + ampl_corr_step;
+ ampl_corr_step = (ampl_corr_stop - ampl_corr_start)/(num_search_steps+1);
}
- if (best_suppression > 30){ //most likely valid, keep result
+ if (best_suppression > initial_suppression) //keep result
+ {
result_t result;
result.freq = tx_lo;
- result.real_corr = best_correction.real();
- result.imag_corr = best_correction.imag();
+ result.real_corr = best_ampl_corr;
+ result.imag_corr = best_phase_corr;
result.best = best_suppression;
result.delta = best_suppression - initial_suppression;
results.push_back(result);
- if (vm.count("verbose")){
+ if (vm.count("verbose"))
std::cout << boost::format("TX IQ: %f MHz: best suppression %f dB, corrected %f dB") % (tx_lo/1e6) % result.best % result.delta << std::endl;
- }
- else std::cout << "." << std::flush;
+ else
+ std::cout << "." << std::flush;
}
-
}
std::cout << std::endl;
//stop the transmitter
threads.interrupt_all();
+ boost::this_thread::sleep(boost::posix_time::milliseconds(500)); //wait for threads to finish
threads.join_all();
store_results(results, "TX", "tx", "iq", serial);
diff --git a/host/utils/usrp_cal_utils.hpp b/host/utils/usrp_cal_utils.hpp
index 9e7f4c469..6673b6329 100644
--- a/host/utils/usrp_cal_utils.hpp
+++ b/host/utils/usrp_cal_utils.hpp
@@ -1,5 +1,5 @@
//
-// Copyright 2011-2012 Ettus Research LLC
+// Copyright 2011-2012,2014 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@@ -41,82 +41,77 @@ typedef std::complex<float> samp_type;
* Constants
**********************************************************************/
static const double tau = 6.28318531;
-static const size_t wave_table_len = 8192;
+static const size_t wave_table_len = 65536;
static const size_t num_search_steps = 5;
-static const size_t num_search_iters = 7;
+static const double default_precision = 0.0001;
static const double default_freq_step = 7.3e6;
-static const size_t default_num_samps = 10000;
+static const size_t default_fft_bin_size = 1000;
/***********************************************************************
* Set standard defaults for devices
**********************************************************************/
-static inline void set_optimum_defaults(uhd::usrp::multi_usrp::sptr usrp){
+static inline void set_optimum_defaults(uhd::usrp::multi_usrp::sptr usrp)
+{
uhd::property_tree::sptr tree = usrp->get_device()->get_tree();
// Will work on 1st subdev, top-level must make sure it's the right one
uhd::usrp::subdev_spec_t subdev_spec = usrp->get_rx_subdev_spec();
const uhd::fs_path mb_path = "/mboards/0";
const std::string mb_name = tree->access<std::string>(mb_path / "name").get();
- if (mb_name.find("USRP2") != std::string::npos or mb_name.find("N200") != std::string::npos or mb_name.find("N210") != std::string::npos or mb_name.find("X300") != std::string::npos or mb_name.find("X310") != std::string::npos){
+ if (mb_name.find("USRP2") != std::string::npos or
+ mb_name.find("N200") != std::string::npos or
+ mb_name.find("N210") != std::string::npos or
+ mb_name.find("X300") != std::string::npos or
+ mb_name.find("X310") != std::string::npos)
+ {
usrp->set_tx_rate(12.5e6);
usrp->set_rx_rate(12.5e6);
}
- else if (mb_name.find("B100") != std::string::npos){
+ else if (mb_name.find("B100") != std::string::npos)
+ {
usrp->set_tx_rate(4e6);
usrp->set_rx_rate(4e6);
}
- else if (mb_name.find("E100") != std::string::npos or mb_name.find("E110") != std::string::npos){
+ else if (mb_name.find("E100") != std::string::npos or mb_name.find("E110") != std::string::npos)
+ {
usrp->set_tx_rate(4e6);
usrp->set_rx_rate(8e6);
}
- else{
- throw std::runtime_error("self-calibration is not supported for this hardware");
+ else
+ {
+ throw std::runtime_error("self-calibration is not supported for this device");
}
const uhd::fs_path tx_fe_path = "/mboards/0/dboards/" + subdev_spec[0].db_name + "/tx_frontends/0";
const std::string tx_name = tree->access<std::string>(tx_fe_path / "name").get();
- if (tx_name.find("WBX") != std::string::npos){
- usrp->set_tx_gain(0);
- }
- else if (tx_name.find("SBX") != std::string::npos){
- usrp->set_tx_gain(0);
- }
- else if (tx_name.find("CBX") != std::string::npos){
- usrp->set_tx_gain(0);
- }
- else if (tx_name.find("RFX") != std::string::npos){
- usrp->set_tx_gain(0);
- }
- else{
- throw std::runtime_error("self-calibration is not supported for this hardware");
+ if (tx_name.find("WBX") == std::string::npos and
+ tx_name.find("SBX") == std::string::npos and
+ tx_name.find("CBX") == std::string::npos and
+ tx_name.find("RFX") == std::string::npos
+ )
+ {
+ throw std::runtime_error("self-calibration is not supported for this TX dboard");
}
+ usrp->set_tx_gain(0);
const uhd::fs_path rx_fe_path = "/mboards/0/dboards/" + subdev_spec[0].db_name + "/rx_frontends/0";
const std::string rx_name = tree->access<std::string>(rx_fe_path / "name").get();
- if (rx_name.find("WBX") != std::string::npos){
- usrp->set_rx_gain(25);
- }
- else if (rx_name.find("SBX") != std::string::npos){
- usrp->set_rx_gain(25);
+ if (rx_name.find("WBX") == std::string::npos and
+ rx_name.find("SBX") == std::string::npos and
+ rx_name.find("CBX") == std::string::npos and
+ rx_name.find("RFX") == std::string::npos
+ )
+ {
+ throw std::runtime_error("self-calibration is not supported for this RX dboard");
}
- else if (rx_name.find("CBX") != std::string::npos){
- usrp->set_rx_gain(25);
- }
- else if (rx_name.find("RFX") != std::string::npos){
- usrp->set_rx_gain(25);
- }
- else{
- throw std::runtime_error("self-calibration is not supported for this hardware");
- }
-
+ usrp->set_rx_gain(0);
}
/***********************************************************************
* Check for empty serial
**********************************************************************/
-void check_for_empty_serial(
- uhd::usrp::multi_usrp::sptr usrp
-){
+void check_for_empty_serial(uhd::usrp::multi_usrp::sptr usrp)
+{
// Will work on 1st subdev, top-level must make sure it's the right one
uhd::usrp::subdev_spec_t subdev_spec = usrp->get_rx_subdev_spec();
@@ -127,22 +122,25 @@ void check_for_empty_serial(
const uhd::usrp::dboard_eeprom_t db_eeprom = tree->access<uhd::usrp::dboard_eeprom_t>(db_path).get();
std::string error_string = "This dboard has no serial!\n\nPlease see the Calibration documentation for details on how to fix this.";
- if (db_eeprom.serial.empty()) throw std::runtime_error(error_string);
+ if (db_eeprom.serial.empty())
+ throw std::runtime_error(error_string);
}
/***********************************************************************
* Sinusoid wave table
**********************************************************************/
-class wave_table{
+class wave_table
+{
public:
- wave_table(const double ampl){
+ wave_table(const double ampl)
+ {
_table.resize(wave_table_len);
- for (size_t i = 0; i < wave_table_len; i++){
+ for (size_t i = 0; i < wave_table_len; i++)
_table[i] = samp_type(std::polar(ampl, (tau*i)/wave_table_len));
- }
}
- inline samp_type operator()(const size_t index) const{
+ inline samp_type operator()(const size_t index) const
+ {
return _table[index % wave_table_len];
}
@@ -154,15 +152,14 @@ private:
* Compute power of a tone
**********************************************************************/
static inline double compute_tone_dbrms(
- const std::vector<samp_type > &samples,
- const double freq //freq is fractional
-){
+ const std::vector<samp_type> &samples,
+ const double freq) //freq is fractional
+{
//shift the samples so the tone at freq is down at DC
//and average the samples to measure the DC component
samp_type average = 0;
- for (size_t i = 0; i < samples.size(); i++){
+ for (size_t i = 0; i < samples.size(); i++)
average += samp_type(std::polar(1.0, -freq*tau*i)) * samples[i];
- }
return 20*std::log10(std::abs(average/float(samples.size())));
}
@@ -171,8 +168,8 @@ static inline double compute_tone_dbrms(
* Write a dat file
**********************************************************************/
static inline void write_samples_to_file(
- const std::vector<samp_type > &samples, const std::string &file
-){
+ const std::vector<samp_type > &samples, const std::string &file)
+{
std::ofstream outfile(file.c_str(), std::ofstream::binary);
outfile.write((const char*)&samples.front(), samples.size()*sizeof(samp_type));
outfile.close();
@@ -184,8 +181,8 @@ static inline void write_samples_to_file(
**********************************************************************/
static std::string get_serial(
uhd::usrp::multi_usrp::sptr usrp,
- const std::string &tx_rx
-){
+ const std::string &tx_rx)
+{
uhd::property_tree::sptr tree = usrp->get_device()->get_tree();
// Will work on 1st subdev, top-level must make sure it's the right one
uhd::usrp::subdev_spec_t subdev_spec = usrp->get_rx_subdev_spec();
@@ -202,17 +199,16 @@ static void store_results(
const std::string &XX, // "TX" or "RX"
const std::string &xx, // "tx" or "rx"
const std::string &what, // Type of test, e.g. "iq",
- const std::string &serial
-){
+ const std::string &serial)
+{
//make the calibration file path
fs::path cal_data_path = fs::path(uhd::get_app_path()) / ".uhd";
fs::create_directory(cal_data_path);
cal_data_path = cal_data_path / "cal";
fs::create_directory(cal_data_path);
cal_data_path = cal_data_path / str(boost::format("%s_%s_cal_v0.2_%s.csv") % xx % what % serial);
- if (fs::exists(cal_data_path)){
+ if (fs::exists(cal_data_path))
fs::rename(cal_data_path, cal_data_path.string() + str(boost::format(".%d") % time(NULL)));
- }
//fill the calibration file
std::ofstream cal_data(cal_data_path.string().c_str());
@@ -223,7 +219,8 @@ static void store_results(
cal_data << boost::format("DATA STARTS HERE\n");
cal_data << "lo_frequency, correction_real, correction_imag, measured, delta\n";
- for (size_t i = 0; i < results.size(); i++){
+ for (size_t i = 0; i < results.size(); i++)
+ {
cal_data
<< results[i].freq << ", "
<< results[i].real_corr << ", "
@@ -243,31 +240,50 @@ static void capture_samples(
uhd::usrp::multi_usrp::sptr usrp,
uhd::rx_streamer::sptr rx_stream,
std::vector<samp_type > &buff,
- const size_t nsamps_requested
-){
+ const size_t nsamps_requested)
+{
buff.resize(nsamps_requested);
uhd::rx_metadata_t md;
+ // Right after the stream is started, there will be transient data.
+ // That transient data is discarded and only "good" samples are returned.
+ size_t nsamps_to_discard = usrp->get_rx_rate() * 0.001; // 1ms to be discarded
+ std::vector<samp_type> discard_buff(nsamps_to_discard);
+
uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
- stream_cmd.num_samps = buff.size();
+ stream_cmd.num_samps = buff.size() + nsamps_to_discard;
stream_cmd.stream_now = true;
usrp->issue_stream_cmd(stream_cmd);
- const size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md);
+ size_t num_rx_samps = 0;
+
+ // Discard the transient samples.
+ rx_stream->recv(&discard_buff.front(), discard_buff.size(), md);
+ if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE)
+ {
+ throw std::runtime_error(str(boost::format(
+ "Receiver error: %s"
+ ) % md.strerror()));
+ }
+
+ // Now capture the data we want
+ num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md);
//validate the received data
- if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){
+ if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE)
+ {
throw std::runtime_error(str(boost::format(
"Receiver error: %s"
) % md.strerror()));
}
+
//we can live if all the data didnt come in
- if (num_rx_samps > buff.size()/2){
+ if (num_rx_samps > buff.size()/2)
+ {
buff.resize(num_rx_samps);
return;
}
- if (num_rx_samps != buff.size()){
+ if (num_rx_samps != buff.size())
throw std::runtime_error("did not get all the samples requested");
- }
}
/***********************************************************************
@@ -280,7 +296,8 @@ static uhd::usrp::multi_usrp::sptr setup_usrp_for_cal(std::string &args, std::st
uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);
// Configure subdev
- if (!subdev.empty()) {
+ if (!subdev.empty())
+ {
usrp->set_tx_subdev_spec(subdev);
usrp->set_rx_subdev_spec(subdev);
}
@@ -289,9 +306,8 @@ static uhd::usrp::multi_usrp::sptr setup_usrp_for_cal(std::string &args, std::st
UHD_MSG(status) << "Daughterboard serial: " << serial << std::endl;
//set the antennas to cal
- if (not uhd::has(usrp->get_rx_antennas(), "CAL") or not uhd::has(usrp->get_tx_antennas(), "CAL")){
+ if (not uhd::has(usrp->get_rx_antennas(), "CAL") or not uhd::has(usrp->get_tx_antennas(), "CAL"))
throw std::runtime_error("This board does not have the CAL antenna option, cannot self-calibrate.");
- }
usrp->set_rx_antenna("CAL");
usrp->set_tx_antenna("CAL");
@@ -304,3 +320,72 @@ static uhd::usrp::multi_usrp::sptr setup_usrp_for_cal(std::string &args, std::st
return usrp;
}
+/***********************************************************************
+ * Function to find optimal RX gain setting (for the current frequency)
+ **********************************************************************/
+UHD_INLINE void set_optimal_rx_gain(
+ uhd::usrp::multi_usrp::sptr usrp,
+ uhd::rx_streamer::sptr rx_stream,
+ double wave_freq = 0.0)
+{
+ const double gain_step = 3.0;
+ const double gain_compression_threshold = gain_step * 0.5;
+ const double actual_rx_rate = usrp->get_rx_rate();
+ const double actual_tx_freq = usrp->get_tx_freq();
+ const double actual_rx_freq = usrp->get_rx_freq();
+ const double bb_tone_freq = actual_tx_freq - actual_rx_freq + wave_freq;
+ const size_t nsamps = size_t(actual_rx_rate / default_fft_bin_size);
+
+ std::vector<samp_type> buff(nsamps);
+ uhd::gain_range_t rx_gain_range = usrp->get_rx_gain_range();
+ double rx_gain = rx_gain_range.start() + gain_step;
+ double curr_dbrms = 0.0;
+ double prev_dbrms = 0.0;
+ double delta = 0.0;
+
+ // No sense in setting the gain where this is no gain range
+ if (rx_gain_range.stop() - rx_gain_range.start() < gain_step)
+ return;
+
+ // The algorithm below cycles through the RX gain range
+ // looking for the point where the signal begins to get
+ // clipped and the gain begins to be compressed. It does
+ // this by looking for the gain setting where the increase
+ // in the tone is less than the gain step by more than the
+ // gain compression threshold (curr - prev < gain - threshold).
+
+ // Initialize prev_dbrms value
+ usrp->set_rx_gain(rx_gain);
+ capture_samples(usrp, rx_stream, buff, nsamps);
+ prev_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate);
+ rx_gain += gain_step;
+
+ // Find RX gain where signal begins to clip
+ while (rx_gain <= rx_gain_range.stop())
+ {
+ usrp->set_rx_gain(rx_gain);
+ capture_samples(usrp, rx_stream, buff, nsamps);
+ curr_dbrms = compute_tone_dbrms(buff, bb_tone_freq/actual_rx_rate);
+ delta = curr_dbrms - prev_dbrms;
+
+ // check if the gain is compressed beyone the threshold
+ if (delta < gain_step - gain_compression_threshold)
+ break; // if so, we are done
+
+ prev_dbrms = curr_dbrms;
+ rx_gain += gain_step;
+ }
+
+ // The rx_gain value at this point is the gain setting where clipping
+ // occurs or the gain setting that is just beyond the gain range.
+ // The gain is reduced by 2 steps to make sure it is within the range and
+ // under the point where it is clipped with enough room to make adjustments.
+ rx_gain -= 2 * gain_step;
+
+ // Make sure the gain is within the range.
+ rx_gain = rx_gain_range.clip(rx_gain);
+
+ // Finally, set the gain.
+ usrp->set_rx_gain(rx_gain);
+}
+