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