GNU Radio 3.6.5 C++ API

agc2.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_AGC2_H
00024 #define INCLUDED_ANALOG_AGC2_H
00025 
00026 #include <analog/api.h>
00027 #include <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
00036        *
00037        * \details
00038        * For Power the absolute value of the complex number is used.
00039        */
00040       class ANALOG_API agc2_cc
00041       {
00042       public:
00043         agc2_cc(float attack_rate = 1e-1, float decay_rate = 1e-2,
00044                 float reference = 1.0,
00045                 float gain = 1.0, float max_gain = 0.0)
00046           : _attack_rate(attack_rate), _decay_rate(decay_rate),
00047           _reference(reference),
00048           _gain(gain), _max_gain(max_gain) {};
00049 
00050         float decay_rate() const  { return _decay_rate; }
00051         float attack_rate() const { return _attack_rate; }
00052         float reference() const   { return _reference; }
00053         float gain() const        { return _gain;  }
00054         float max_gain() const     { return _max_gain; }
00055 
00056         void set_decay_rate(float rate) { _decay_rate = rate; }
00057         void set_attack_rate(float rate) { _attack_rate = rate; }
00058         void set_reference(float reference) { _reference = reference; }
00059         void set_gain(float gain) { _gain = gain; }
00060         void set_max_gain(float max_gain) { _max_gain = max_gain; }
00061 
00062         gr_complex scale(gr_complex input)
00063         {
00064           gr_complex output = input * _gain;
00065 
00066           float tmp = -_reference + sqrt(output.real()*output.real() +
00067                                          output.imag()*output.imag());
00068           float rate = _decay_rate;
00069           if((tmp) > _gain) {
00070             rate = _attack_rate;
00071           }
00072           _gain -= tmp*rate;
00073 
00074           // Not sure about this; will blow up if _gain < 0 (happens
00075           // when rates are too high), but is this the solution?
00076           if(_gain < 0.0)
00077             _gain = 10e-5;
00078 
00079           if(_max_gain > 0.0 && _gain > _max_gain) {
00080             _gain = _max_gain;
00081           }
00082           return output;
00083         }
00084 
00085         void scaleN(gr_complex output[], const gr_complex input[], unsigned n)
00086         {
00087           for(unsigned i = 0; i < n; i++)
00088             output[i] = scale (input[i]);
00089         }
00090 
00091       protected:
00092         float _attack_rate;     // attack rate for fast changing signals
00093         float _decay_rate;      // decay rate for slow changing signals
00094         float _reference;       // reference value
00095         float _gain;            // current gain
00096         float _max_gain;        // max allowable gain
00097       };
00098 
00099 
00100       class ANALOG_API agc2_ff
00101       {
00102       public:
00103         agc2_ff(float attack_rate = 1e-1, float decay_rate = 1e-2,
00104                 float reference = 1.0,
00105                 float gain = 1.0, float max_gain = 0.0)
00106           : _attack_rate(attack_rate), _decay_rate(decay_rate),
00107           _reference(reference),
00108           _gain(gain), _max_gain(max_gain) {};
00109 
00110         float attack_rate() const { return _attack_rate; }
00111         float decay_rate() const  { return _decay_rate; }
00112         float reference() const   { return _reference; }
00113         float gain() const        { return _gain;  }
00114         float max_gain() const    { return _max_gain; }
00115 
00116         void set_attack_rate(float rate) { _attack_rate = rate; }
00117         void set_decay_rate(float rate) { _decay_rate = rate; }
00118         void set_reference(float reference) { _reference = reference; }
00119         void set_gain(float gain) { _gain = gain; }
00120         void set_max_gain(float max_gain) { _max_gain = max_gain; }
00121 
00122         float scale(float input)
00123         {
00124           float output = input * _gain;
00125 
00126           float tmp = (fabsf(output)) - _reference;
00127           float rate = _decay_rate;
00128           if(fabsf(tmp) > _gain) {
00129             rate = _attack_rate;
00130           }
00131           _gain -= tmp*rate;
00132 
00133           // Not sure about this
00134           if(_gain < 0.0)
00135             _gain = 10e-5;
00136 
00137           if(_max_gain > 0.0 && _gain > _max_gain) {
00138             _gain = _max_gain;
00139           }
00140           return output;
00141         }
00142 
00143         void scaleN(float output[], const float input[], unsigned n)
00144         {
00145           for(unsigned i = 0; i < n; i++)
00146             output[i] = scale (input[i]);
00147         }
00148 
00149       protected:
00150         float _attack_rate;     // attack_rate for fast changing signals
00151         float _decay_rate;      // decay rate for slow changing signals
00152         float _reference;       // reference value
00153         float _gain;            // current gain
00154         float _max_gain;        // maximum gain
00155       };
00156       
00157     } /* namespace kernel */
00158   } /* namespace analog */
00159 } /* namespace gr */
00160 
00161 #endif /* INCLUDED_ANALOG_AGC2_H */