Applied patch from Dean Armstrong to fix gr_repeat (ticket:246).
[debian/gnuradio] / gnuradio-core / src / lib / general / gri_agc2_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 3, 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., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifndef _GRI_AGC2_CC_H_
24 #define _GRI_AGC2_CC_H_
25
26 #include <math.h>
27
28 /*!
29  * \brief high performance Automatic Gain Control class
30  * \ingroup level
31  *
32  * For Power the absolute value of the complex number is used.
33  */
34
35 class gri_agc2_cc {
36
37  public:
38   gri_agc2_cc (float attack_rate = 1e-1, float decay_rate = 1e-2, float reference = 1.0, 
39                float gain = 1.0, float max_gain = 0.0)
40     : _attack_rate(attack_rate), _decay_rate(decay_rate), _reference(reference),
41       _gain(gain), _max_gain(max_gain) {};
42
43   float decay_rate () const  { return _decay_rate; }
44   float attack_rate () const { return _attack_rate; }
45   float reference () const   { return _reference; }
46   float gain () const        { return _gain;  }
47   float max_gain() const     { return _max_gain; }
48
49   void set_decay_rate (float rate) { _decay_rate = rate; }
50   void set_attack_rate (float rate) { _attack_rate = rate; }
51   void set_reference (float reference) { _reference = reference; }
52   void set_gain (float gain) { _gain = gain; }
53   void set_max_gain(float max_gain) { _max_gain = max_gain; }
54
55   gr_complex scale (gr_complex input){
56     gr_complex output = input * _gain;
57     
58     float tmp = -_reference + sqrt(output.real()*output.real() + 
59                                    output.imag()*output.imag());
60     float rate = _decay_rate;
61     if((tmp) > _gain)
62         rate = _attack_rate;
63     _gain -= tmp*rate;
64     
65 #if 0
66     fprintf(stdout, "rate = %f\ttmp = %f\t gain = %f\n", rate, tmp, _gain);
67 #endif
68
69     // Not sure about this; will blow up if _gain < 0 (happens when rates are too high),
70     // but is this the solution?
71     if (_gain < 0.0)
72         _gain = 10e-5;
73
74     if (_max_gain > 0.0 && _gain > _max_gain)
75        _gain = _max_gain;                                                    
76     return output;
77   }
78
79   void scaleN (gr_complex output[], const gr_complex input[], unsigned n){
80     for (unsigned i = 0; i < n; i++)
81       output[i] = scale (input[i]);
82   }
83   
84  protected:
85   float _attack_rate;           // attack rate for fast changing signals
86   float _decay_rate;            // decay rate for slow changing signals
87   float _reference;             // reference value
88   float _gain;                  // current gain
89   float _max_gain;              // max allowable gain
90 };
91
92 #endif /* _GRI_AGC2_CC_H_ */