Houston, we have a trunk.
[debian/gnuradio] / gnuradio-core / src / lib / general / gri_agc_cc.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifndef _GRI_AGC_CC_H_
24 #define _GRI_AGC_CC_H_
25
26 #include <math.h>
27
28 /*!
29  * \brief high performance Automatic Gain Control class
30  *
31  * For Power the absolute value of the complex number is used.
32  */
33
34 class gri_agc_cc {
35
36  public:
37   gri_agc_cc (float rate = 1e-4, float reference = 1.0, float gain = 1.0, float max_gain = 0.0)
38     : _rate(rate), _reference(reference), _gain(gain), _max_gain(max_gain) {};
39
40   float rate () const      { return _rate; }
41   float reference () const { return _reference; }
42   float gain () const      { return _gain;  }
43   float max_gain() const   { return _max_gain; }
44
45   void set_rate (float rate) { _rate = rate; }
46   void set_reference (float reference) { _reference = reference; }
47   void set_gain (float gain) { _gain = gain; }
48   void set_max_gain(float max_gain) { _max_gain = max_gain; }
49   
50   gr_complex scale (gr_complex input){
51     gr_complex output = input * _gain;
52     _gain += (_reference - sqrt(output.real()*output.real()+output.imag()*output.imag())) * _rate; //use abs or cabs to get approximation by absolute value, 
53                                                      //note that abs is computationally more intensive then norm for a complex number
54     if (_max_gain > 0.0 && _gain > _max_gain)
55        _gain = _max_gain;                                                    
56     return output;
57   }
58
59   void scaleN (gr_complex output[], const gr_complex input[], unsigned n){
60     for (unsigned i = 0; i < n; i++)
61       output[i] = scale (input[i]);
62   }
63   
64  protected:
65   float _rate;                  // adjustment rate
66   float _reference;             // reference value
67   float _gain;                  // current gain
68   float _max_gain;              // max allowable gain
69 };
70
71 #endif /* _GRI_AGC_CC_H_ */