From 4e2d3a0aba20e99e6da41759212b2e2865c5c616 Mon Sep 17 00:00:00 2001 From: mattprost Date: Tue, 13 Jul 2021 17:32:54 -0500 Subject: utils: Get signal above noise floor when finding optimal gain Ensure that the signal is at least visible above the noise floor before attempting to identify gain compression. Another option that was considered involved starting from the top of the gain range and working down to avoid this issue, however, that option could expose the device to unsafe amounts of incoming signal power. Signed-off-by: mattprost --- host/utils/usrp_cal_utils.hpp | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/host/utils/usrp_cal_utils.hpp b/host/utils/usrp_cal_utils.hpp index 1b9df9d03..3750be9cb 100644 --- a/host/utils/usrp_cal_utils.hpp +++ b/host/utils/usrp_cal_utils.hpp @@ -263,15 +263,15 @@ static uhd::usrp::multi_usrp::sptr setup_usrp_for_cal( **********************************************************************/ UHD_INLINE void set_optimal_rx_gain(uhd::usrp::multi_usrp::sptr usrp, uhd::rx_streamer::sptr rx_stream, - double wave_freq = 0.0) + double wave_freq = 0.0, + double gain_step = 3.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); + const double gain_step_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 buff(nsamps); uhd::gain_range_t rx_gain_range = usrp->get_rx_gain_range(); @@ -290,6 +290,10 @@ UHD_INLINE void set_optimal_rx_gain(uhd::usrp::multi_usrp::sptr usrp, // 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). + // This routine starts searching at the bottom of the gain range + // rather than the top in order to minimize the chances of + // exposing frontend components to a dangerous amount of power + // from the incoming signal. // Initialize prev_dbrms value usrp->set_rx_gain(rx_gain); @@ -297,6 +301,24 @@ UHD_INLINE void set_optimal_rx_gain(uhd::usrp::multi_usrp::sptr usrp, prev_dbrms = compute_tone_dbrms(buff, bb_tone_freq / actual_rx_rate); rx_gain += gain_step; + // First, get the signal above the noise floor + 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 that the signal power is not already high + if (curr_dbrms >= 0) + break; + // Check that the signal power has increased as the gain increases + if (delta >= gain_step - gain_step_threshold) + break; + + prev_dbrms = curr_dbrms; + 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); @@ -305,7 +327,7 @@ UHD_INLINE void set_optimal_rx_gain(uhd::usrp::multi_usrp::sptr usrp, delta = curr_dbrms - prev_dbrms; // check if the gain is compressed beyone the threshold - if (delta < gain_step - gain_compression_threshold) + if (delta < gain_step - gain_step_threshold) break; // if so, we are done prev_dbrms = curr_dbrms; -- cgit v1.2.3-59-g8ed1b