GNU Radio 3.4.0 C++ API
gri_agc2_ff.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2002,2006 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 _GRI_AGC2_FF_H_
00024 #define _GRI_AGC2_FF_H_
00025 
00026 #include <math.h>
00027 
00028 /*!
00029  * \brief high performance Automatic Gain Control class with attack and decay rate
00030  *
00031  * Power is approximated by absolute value
00032  */
00033 
00034 class gri_agc2_ff {
00035 
00036  public:
00037   gri_agc2_ff (float attack_rate = 1e-1, float decay_rate = 1e-2, float reference = 1.0, 
00038                float gain = 1.0, float max_gain = 0.0)
00039     : _attack_rate(attack_rate), _decay_rate(decay_rate), _reference(reference), 
00040       _gain(gain), _max_gain(max_gain) {};
00041 
00042   float attack_rate () const { return _attack_rate; }
00043   float decay_rate () const  { return _decay_rate; }
00044   float reference () const   { return _reference; }
00045   float gain () const        { return _gain;  }
00046   float max_gain () const    { return _max_gain; }
00047   
00048   void set_attack_rate (float rate) { _attack_rate = rate; }
00049   void set_decay_rate (float rate) { _decay_rate = rate; }
00050   void set_reference (float reference) { _reference = reference; }
00051   void set_gain (float gain) { _gain = gain; }
00052   void set_max_gain (float max_gain) { _max_gain = max_gain; }
00053   
00054   float scale (float input){
00055     float output = input * _gain;
00056     
00057     float tmp = (fabsf(output)) - _reference;
00058     float rate = _decay_rate;
00059     if(fabsf(tmp) > _gain)
00060         rate = _attack_rate;
00061     _gain -= tmp*rate;
00062 
00063 #if 0
00064     fprintf(stdout, "rate = %f\ttmp = %f\t gain = %f\n", rate, tmp, _gain);
00065 #endif
00066 
00067     // Not sure about this
00068     if (_gain < 0.0)
00069         _gain = 10e-5;
00070 
00071     if (_max_gain > 0.0 && _gain > _max_gain)
00072        _gain = _max_gain;
00073     return output;
00074   }
00075 
00076   void scaleN (float output[], const float input[], unsigned n){
00077     for (unsigned i = 0; i < n; i++)
00078       output[i] = scale (input[i]);
00079   }
00080   
00081  protected:
00082   float _attack_rate;           // attack_rate for fast changing signals
00083   float _decay_rate;            // decay rate for slow changing signals
00084   float _reference;             // reference value
00085   float _gain;                  // current gain
00086   float _max_gain;              // maximum gain
00087 };
00088 
00089 #endif /* _GRI_AGC2_FF_H_ */