GNU Radio 3.6.4 C++ API
agc2.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2012 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 INCLUDED_ANALOG_AGC2_H
24 #define INCLUDED_ANALOG_AGC2_H
25 
26 #include <analog/api.h>
27 #include <gr_complex.h>
28 #include <math.h>
29 
30 namespace gr {
31  namespace analog {
32  namespace kernel {
33 
34  /*!
35  * \brief high performance Automatic Gain Control class
36  *
37  * For Power the absolute value of the complex number is used.
38  */
40  {
41  public:
42  agc2_cc(float attack_rate = 1e-1, float decay_rate = 1e-2,
43  float reference = 1.0,
44  float gain = 1.0, float max_gain = 0.0)
45  : _attack_rate(attack_rate), _decay_rate(decay_rate),
46  _reference(reference),
47  _gain(gain), _max_gain(max_gain) {};
48 
49  float decay_rate() const { return _decay_rate; }
50  float attack_rate() const { return _attack_rate; }
51  float reference() const { return _reference; }
52  float gain() const { return _gain; }
53  float max_gain() const { return _max_gain; }
54 
55  void set_decay_rate(float rate) { _decay_rate = rate; }
56  void set_attack_rate(float rate) { _attack_rate = rate; }
57  void set_reference(float reference) { _reference = reference; }
58  void set_gain(float gain) { _gain = gain; }
59  void set_max_gain(float max_gain) { _max_gain = max_gain; }
60 
61  gr_complex scale(gr_complex input)
62  {
63  gr_complex output = input * _gain;
64 
65  float tmp = -_reference + sqrt(output.real()*output.real() +
66  output.imag()*output.imag());
67  float rate = _decay_rate;
68  if((tmp) > _gain) {
69  rate = _attack_rate;
70  }
71  _gain -= tmp*rate;
72 
73  // Not sure about this; will blow up if _gain < 0 (happens
74  // when rates are too high), but is this the solution?
75  if(_gain < 0.0)
76  _gain = 10e-5;
77 
78  if(_max_gain > 0.0 && _gain > _max_gain) {
79  _gain = _max_gain;
80  }
81  return output;
82  }
83 
84  void scaleN(gr_complex output[], const gr_complex input[], unsigned n)
85  {
86  for(unsigned i = 0; i < n; i++)
87  output[i] = scale (input[i]);
88  }
89 
90  protected:
91  float _attack_rate; // attack rate for fast changing signals
92  float _decay_rate; // decay rate for slow changing signals
93  float _reference; // reference value
94  float _gain; // current gain
95  float _max_gain; // max allowable gain
96  };
97 
98 
100  {
101  public:
102  agc2_ff(float attack_rate = 1e-1, float decay_rate = 1e-2,
103  float reference = 1.0,
104  float gain = 1.0, float max_gain = 0.0)
105  : _attack_rate(attack_rate), _decay_rate(decay_rate),
106  _reference(reference),
107  _gain(gain), _max_gain(max_gain) {};
108 
109  float attack_rate() const { return _attack_rate; }
110  float decay_rate() const { return _decay_rate; }
111  float reference() const { return _reference; }
112  float gain() const { return _gain; }
113  float max_gain() const { return _max_gain; }
114 
115  void set_attack_rate(float rate) { _attack_rate = rate; }
116  void set_decay_rate(float rate) { _decay_rate = rate; }
117  void set_reference(float reference) { _reference = reference; }
118  void set_gain(float gain) { _gain = gain; }
119  void set_max_gain(float max_gain) { _max_gain = max_gain; }
120 
121  float scale(float input)
122  {
123  float output = input * _gain;
124 
125  float tmp = (fabsf(output)) - _reference;
126  float rate = _decay_rate;
127  if(fabsf(tmp) > _gain) {
128  rate = _attack_rate;
129  }
130  _gain -= tmp*rate;
131 
132  // Not sure about this
133  if(_gain < 0.0)
134  _gain = 10e-5;
135 
136  if(_max_gain > 0.0 && _gain > _max_gain) {
137  _gain = _max_gain;
138  }
139  return output;
140  }
141 
142  void scaleN(float output[], const float input[], unsigned n)
143  {
144  for(unsigned i = 0; i < n; i++)
145  output[i] = scale (input[i]);
146  }
147 
148  protected:
149  float _attack_rate; // attack_rate for fast changing signals
150  float _decay_rate; // decay rate for slow changing signals
151  float _reference; // reference value
152  float _gain; // current gain
153  float _max_gain; // maximum gain
154  };
155 
156  } /* namespace kernel */
157  } /* namespace analog */
158 } /* namespace gr */
159 
160 #endif /* INCLUDED_ANALOG_AGC2_H */