GNU Radio 3.7.0 C++ API
agc.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2006,2012 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_ANALOG_AGC_H
00024 #define INCLUDED_ANALOG_AGC_H
00025 
00026 #include <gnuradio/analog/api.h>
00027 #include <gnuradio/gr_complex.h>
00028 #include <math.h>
00029 
00030 namespace gr {
00031   namespace analog {
00032     namespace kernel {
00033 
00034       /*!
00035        * \brief high performance Automatic Gain Control class for complex signals.
00036        * \ingroup level_controllers_blk
00037        *
00038        * \details
00039        * For Power the absolute value of the complex number is used.
00040        */
00041       class ANALOG_API agc_cc
00042       {
00043       public:
00044         /*!
00045          * Construct a complex value AGC loop implementation object.
00046          *
00047          * \param rate the update rate of the loop.
00048          * \param reference reference value to adjust signal power to.
00049          * \param gain initial gain value.
00050          * \param max_gain maximum gain value (0 for unlimited).
00051          */
00052         agc_cc(float rate = 1e-4, float reference = 1.0,
00053                float gain = 1.0, float max_gain = 0.0)
00054           : _rate(rate), _reference(reference),
00055           _gain(gain), _max_gain(max_gain) {};
00056 
00057         virtual ~agc_cc() {};
00058 
00059         float rate() const      { return _rate; }
00060         float reference() const { return _reference; }
00061         float gain() const      { return _gain;  }
00062         float max_gain() const   { return _max_gain; }
00063 
00064         void set_rate(float rate) { _rate = rate; }
00065         void set_reference(float reference) { _reference = reference; }
00066         void set_gain(float gain) { _gain = gain; }
00067         void set_max_gain(float max_gain) { _max_gain = max_gain; }
00068 
00069         gr_complex scale(gr_complex input)
00070         {
00071           gr_complex output = input * _gain;
00072 
00073           _gain +=  _rate * (_reference - sqrt(output.real()*output.real() +
00074                                                output.imag()*output.imag()));
00075           if(_max_gain > 0.0 && _gain > _max_gain) {
00076             _gain = _max_gain;
00077           }
00078           return output;
00079         }
00080 
00081         void scaleN(gr_complex output[], const gr_complex input[], unsigned n)
00082         {
00083           for(unsigned i = 0; i < n; i++) {
00084             output[i] = scale (input[i]);
00085           }
00086         }
00087 
00088       protected:
00089         float _rate;            // adjustment rate
00090         float _reference;       // reference value
00091         float _gain;            // current gain
00092         float _max_gain;        // max allowable gain
00093       };
00094 
00095       /*!
00096        * \brief high performance Automatic Gain Control class for float signals.
00097        *
00098        * Power is approximated by absolute value
00099        */
00100       class ANALOG_API agc_ff 
00101       {
00102       public:
00103         /*!
00104          * Construct a floating point value AGC loop implementation object.
00105          *
00106          * \param rate the update rate of the loop.
00107          * \param reference reference value to adjust signal power to.
00108          * \param gain initial gain value.
00109          * \param max_gain maximum gain value (0 for unlimited).
00110          */
00111         agc_ff(float rate = 1e-4, float reference = 1.0,
00112                float gain = 1.0, float max_gain = 0.0)
00113           : _rate(rate), _reference(reference), _gain(gain),
00114           _max_gain(max_gain) {};
00115 
00116         ~agc_ff() {};
00117 
00118         float rate () const      { return _rate; }
00119         float reference () const { return _reference; }
00120         float gain () const      { return _gain;  }
00121         float max_gain () const  { return _max_gain; }
00122 
00123         void set_rate (float rate) { _rate = rate; }
00124         void set_reference (float reference) { _reference = reference; }
00125         void set_gain (float gain) { _gain = gain; }
00126         void set_max_gain (float max_gain) { _max_gain = max_gain; }
00127 
00128         float scale (float input)
00129         {
00130           float output = input * _gain;
00131           _gain += (_reference - fabsf (output)) * _rate;
00132           if(_max_gain > 0.0 && _gain > _max_gain)
00133             _gain = _max_gain;
00134           return output;
00135         }
00136 
00137         void scaleN(float output[], const float input[], unsigned n)
00138         {
00139           for(unsigned i = 0; i < n; i++)
00140             output[i] = scale (input[i]);
00141         }
00142 
00143       protected:
00144         float _rate;            // adjustment rate
00145         float _reference;       // reference value
00146         float _gain;            // current gain
00147         float _max_gain;        // maximum gain
00148       };
00149 
00150     } /* namespace kernel */
00151   } /* namespace analog */
00152 } /* namespace gr */
00153 
00154 #endif /* INCLUDED_ANALOG_AGC_H */