summaryrefslogtreecommitdiffstats
path: root/host/lib/utils
diff options
context:
space:
mode:
authorJosh Blum <josh@joshknows.com>2010-08-06 15:18:02 -0700
committerJosh Blum <josh@joshknows.com>2010-08-06 15:18:02 -0700
commit03ccbeb6411c0c13adf54bde4c19372ffa141aa0 (patch)
treeb3b1c1b801495775db74b0c66f273166131b68a0 /host/lib/utils
parent5fe5ff0655a410aac686d130daf9a77e39fd01b5 (diff)
parentd1711722dd432ba63b54f93d92270d4050465204 (diff)
downloaduhd-03ccbeb6411c0c13adf54bde4c19372ffa141aa0.tar.gz
uhd-03ccbeb6411c0c13adf54bde4c19372ffa141aa0.tar.bz2
uhd-03ccbeb6411c0c13adf54bde4c19372ffa141aa0.zip
Merge branch 'codec_gains'
Conflicts: host/lib/usrp/mimo_usrp.cpp host/lib/usrp/simple_usrp.cpp host/test/CMakeLists.txt
Diffstat (limited to 'host/lib/utils')
-rw-r--r--host/lib/utils/CMakeLists.txt2
-rw-r--r--host/lib/utils/gain_group.cpp149
-rw-r--r--host/lib/utils/gain_handler.cpp177
3 files changed, 150 insertions, 178 deletions
diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt
index 07e3b71b3..d6ec44193 100644
--- a/host/lib/utils/CMakeLists.txt
+++ b/host/lib/utils/CMakeLists.txt
@@ -19,7 +19,7 @@
LIBUHD_APPEND_SOURCES(
${CMAKE_SOURCE_DIR}/lib/utils/assert.cpp
- ${CMAKE_SOURCE_DIR}/lib/utils/gain_handler.cpp
+ ${CMAKE_SOURCE_DIR}/lib/utils/gain_group.cpp
${CMAKE_SOURCE_DIR}/lib/utils/load_modules.cpp
${CMAKE_SOURCE_DIR}/lib/utils/props.cpp
${CMAKE_SOURCE_DIR}/lib/utils/thread_priority.cpp
diff --git a/host/lib/utils/gain_group.cpp b/host/lib/utils/gain_group.cpp
new file mode 100644
index 000000000..1be09dee2
--- /dev/null
+++ b/host/lib/utils/gain_group.cpp
@@ -0,0 +1,149 @@
+//
+// Copyright 2010 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
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+
+#include <uhd/utils/gain_group.hpp>
+#include <uhd/types/dict.hpp>
+#include <uhd/utils/algorithm.hpp>
+#include <uhd/utils/assert.hpp>
+#include <boost/foreach.hpp>
+#include <boost/bind.hpp>
+#include <algorithm>
+#include <vector>
+#include <iostream>
+
+using namespace uhd;
+
+static const bool verbose = false;
+
+static bool compare_by_step_size(
+ const size_t &rhs, const size_t &lhs, std::vector<gain_fcns_t> &fcns
+){
+ return fcns.at(rhs).get_range().step > fcns.at(lhs).get_range().step;
+}
+
+/***********************************************************************
+ * gain group implementation
+ **********************************************************************/
+class gain_group_impl : public gain_group{
+public:
+ gain_group_impl(void){
+ /*NOP*/
+ }
+
+ gain_range_t get_range(void){
+ float overall_min = 0, overall_max = 0, overall_step = 0;
+ BOOST_FOREACH(const gain_fcns_t &fcns, get_all_fcns()){
+ const gain_range_t range = fcns.get_range();
+ overall_min += range.min;
+ overall_max += range.max;
+ //the overall step is the min (zero is invalid, first run)
+ if (overall_step == 0) overall_step = range.step;
+ overall_step = std::min(overall_step, range.step);
+ }
+ return gain_range_t(overall_min, overall_max, overall_step);
+ }
+
+ float get_value(void){
+ float overall_gain = 0;
+ BOOST_FOREACH(const gain_fcns_t &fcns, get_all_fcns()){
+ overall_gain += fcns.get_value();
+ }
+ return overall_gain;
+ }
+
+ void set_value(float gain){
+ std::vector<gain_fcns_t> all_fcns = get_all_fcns();
+ if (all_fcns.size() == 0) return; //nothing to set!
+
+ //get the max step size among the gains
+ float max_step = 0;
+ BOOST_FOREACH(const gain_fcns_t &fcns, all_fcns){
+ max_step = std::max(max_step, fcns.get_range().step);
+ }
+
+ //create gain bucket to distribute power
+ std::vector<float> gain_bucket;
+
+ //distribute power according to priority (round to max step)
+ float gain_left_to_distribute = gain;
+ BOOST_FOREACH(const gain_fcns_t &fcns, all_fcns){
+ const gain_range_t range = fcns.get_range();
+ gain_bucket.push_back(
+ max_step*int(std::clip(gain_left_to_distribute, range.min, range.max)/max_step)
+ );
+ gain_left_to_distribute -= gain_bucket.back();
+ }
+
+ //get a list of indexes sorted by step size large to small
+ std::vector<size_t> indexes_step_size_dec;
+ for (size_t i = 0; i < all_fcns.size(); i++){
+ indexes_step_size_dec.push_back(i);
+ }
+ std::sort(
+ indexes_step_size_dec.begin(), indexes_step_size_dec.end(),
+ boost::bind(&compare_by_step_size, _1, _2, all_fcns)
+ );
+ UHD_ASSERT_THROW(
+ all_fcns.at(indexes_step_size_dec.front()).get_range().step >=
+ all_fcns.at(indexes_step_size_dec.back()).get_range().step
+ );
+
+ //distribute the remainder (less than max step)
+ //fill in the largest step sizes first that are less than the remainder
+ BOOST_FOREACH(size_t i, indexes_step_size_dec){
+ const gain_range_t range = all_fcns.at(i).get_range();
+ float additional_gain = range.step*int(
+ std::clip(gain_bucket.at(i) + gain_left_to_distribute, range.min, range.max
+ )/range.step) - gain_bucket.at(i);
+ gain_bucket.at(i) += additional_gain;
+ gain_left_to_distribute -= additional_gain;
+ }
+ if (verbose) std::cout << "gain_left_to_distribute " << gain_left_to_distribute << std::endl;
+
+ //now write the bucket out to the individual gain values
+ for (size_t i = 0; i < gain_bucket.size(); i++){
+ if (verbose) std::cout << gain_bucket.at(i) << std::endl;
+ all_fcns.at(i).set_value(gain_bucket.at(i));
+ }
+ }
+
+ void register_fcns(
+ const gain_fcns_t &gain_fcns, size_t priority
+ ){
+ _registry[priority].push_back(gain_fcns);
+ }
+
+private:
+ //! get the gain function sets in order (highest priority first)
+ std::vector<gain_fcns_t> get_all_fcns(void){
+ std::vector<gain_fcns_t> all_fcns;
+ BOOST_FOREACH(ssize_t key, std::sorted(_registry.keys())){
+ const std::vector<gain_fcns_t> &fcns = _registry[key];
+ all_fcns.insert(all_fcns.begin(), fcns.begin(), fcns.end());
+ }
+ return all_fcns;
+ }
+
+ uhd::dict<size_t, std::vector<gain_fcns_t> > _registry;
+};
+
+/***********************************************************************
+ * gain group factory function
+ **********************************************************************/
+gain_group::sptr gain_group::make(void){
+ return sptr(new gain_group_impl());
+}
diff --git a/host/lib/utils/gain_handler.cpp b/host/lib/utils/gain_handler.cpp
deleted file mode 100644
index 36e2e8ed3..000000000
--- a/host/lib/utils/gain_handler.cpp
+++ /dev/null
@@ -1,177 +0,0 @@
-//
-// Copyright 2010 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
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see <http://www.gnu.org/licenses/>.
-//
-
-#include <uhd/utils/gain_handler.hpp>
-#include <uhd/utils/assert.hpp>
-#include <uhd/types/ranges.hpp>
-#include <uhd/utils/props.hpp>
-#include <boost/assign/list_of.hpp>
-#include <boost/foreach.hpp>
-#include <boost/format.hpp>
-#include <cmath>
-#include <vector>
-
-using namespace uhd;
-
-/***********************************************************************
- * gain handler implementation interface
- **********************************************************************/
-class gain_handler_impl : public gain_handler{
-public:
- gain_handler_impl(
- const wax::obj &link,
- const props_t &props,
- is_equal_t is_equal
- );
- ~gain_handler_impl(void);
- bool intercept_get(const wax::obj &key, wax::obj &val);
- bool intercept_set(const wax::obj &key, const wax::obj &val);
-
-private:
- wax::obj _link;
- props_t _props;
- is_equal_t _is_equal;
-
- prop_names_t get_gain_names(void);
- float get_overall_gain_val(void);
- gain_range_t get_overall_gain_range(void);
- template <class T> T get_named_prop(const wax::obj &prop, const std::string &name){
- return _link[named_prop_t(prop, name)].as<T>();
- }
-};
-
-/***********************************************************************
- * the make function
- **********************************************************************/
-gain_handler::sptr gain_handler::make(
- const wax::obj &link,
- const props_t &props,
- is_equal_t is_equal
-){
- return sptr(new gain_handler_impl(link, props, is_equal));
-}
-
-/***********************************************************************
- * gain handler implementation methods
- **********************************************************************/
-gain_handler::props_t::props_t(void){
- /* NOP */
-}
-
-gain_handler_impl::gain_handler_impl(
- const wax::obj &link,
- const props_t &props,
- is_equal_t is_equal
-){
- _link = link;
- _props = props;
- _is_equal = is_equal;
-}
-
-gain_handler_impl::~gain_handler_impl(void){
- /* NOP */
-}
-
-prop_names_t gain_handler_impl::get_gain_names(void){
- return _link[_props.names].as<prop_names_t>();
-}
-
-float gain_handler_impl::get_overall_gain_val(void){
- float gain_val = 0;
- BOOST_FOREACH(std::string name, get_gain_names()){
- gain_val += get_named_prop<float>(_props.value, name);
- }
- return gain_val;
-}
-
-gain_range_t gain_handler_impl::get_overall_gain_range(void){
- float gain_min = 0, gain_max = 0, gain_step = 0;
- BOOST_FOREACH(std::string name, get_gain_names()){
- gain_range_t floatmp = get_named_prop<gain_range_t>(_props.range, name);
- gain_min += floatmp.min;
- gain_max += floatmp.max;
- gain_step = std::max(gain_step, floatmp.step);
- }
- return gain_range_t(gain_min, gain_max, gain_step);
-}
-
-/***********************************************************************
- * gain handler implementation get method
- **********************************************************************/
-bool gain_handler_impl::intercept_get(const wax::obj &key_, wax::obj &val){
- wax::obj key; std::string name;
- boost::tie(key, name) = extract_named_prop(key_);
-
- //not a wildcard... dont handle (but check name)
- if (name != ""){
- assert_has(get_gain_names(), name, "gain name");
- return false;
- }
-
- if (_is_equal(key, _props.value)){
- val = get_overall_gain_val();
- return true;
- }
-
- if (_is_equal(key, _props.range)){
- val = get_overall_gain_range();
- return true;
- }
-
- return false; //not handled
-}
-
-/***********************************************************************
- * gain handler implementation set method
- **********************************************************************/
-bool gain_handler_impl::intercept_set(const wax::obj &key_, const wax::obj &val){
- wax::obj key; std::string name;
- boost::tie(key, name) = extract_named_prop(key_);
-
- //not a gain value key... dont handle
- if (not _is_equal(key, _props.value)) return false;
-
- float gain_val = val.as<float>();
-
- //not a wildcard... dont handle (but check name and range)
- if (name != ""){
- assert_has(get_gain_names(), name, "gain name");
- gain_range_t gain = get_named_prop<gain_range_t>(_props.range, name);
- if (gain_val > gain.max or gain_val < gain.min) throw std::range_error(str(
- boost::format("A value of %f for gain %s is out of range of (%f, %f)")
- % gain_val % name % gain.min % gain.max
- ));
- return false;
- }
-
- //set the overall gain
- BOOST_FOREACH(std::string name, get_gain_names()){
- //get the min, max, step for this gain name
- gain_range_t gain = get_named_prop<gain_range_t>(_props.range, name);
-
- //clip g to be within the allowed range
- float g = std::min(std::max(gain_val, gain.min), gain.max);
- //set g to be a multiple of the step size
- g -= std::fmod(g, gain.step);
- //set g to be the new gain
- _link[named_prop_t(_props.value, name)] = g;
- //subtract g out of the total gain left to apply
- gain_val -= g;
- }
-
- return true;
-}