GNU Radio Manual and C++ API Reference  3.7.13.4
The Free & Open Software Radio Ecosystem
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 <gnuradio/analog/api.h>
27 #include <gnuradio/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  * \ingroup level_controllers_blk
37  *
38  * \details
39  * For Power the absolute value of the complex number is used.
40  */
42  {
43  public:
44  /*!
45  * Construct a comple value AGC loop implementation object.
46  *
47  * \param attack_rate the update rate of the loop when in attack mode.
48  * \param decay_rate the update rate of the loop when in decay mode.
49  * \param reference reference value to adjust signal power to.
50  * \param gain initial gain value.
51  * \param max_gain maximum gain value (0 for unlimited).
52  */
53  agc2_cc(float attack_rate = 1e-1, float decay_rate = 1e-2,
54  float reference = 1.0,
55  float gain = 1.0, float max_gain = 0.0)
56  : _attack_rate(attack_rate), _decay_rate(decay_rate),
57  _reference(reference),
58  _gain(gain), _max_gain(max_gain) {};
59 
60  float decay_rate() const { return _decay_rate; }
61  float attack_rate() const { return _attack_rate; }
62  float reference() const { return _reference; }
63  float gain() const { return _gain; }
64  float max_gain() const { return _max_gain; }
65 
66  void set_decay_rate(float rate) { _decay_rate = rate; }
67  void set_attack_rate(float rate) { _attack_rate = rate; }
68  void set_reference(float reference) { _reference = reference; }
69  void set_gain(float gain) { _gain = gain; }
70  void set_max_gain(float max_gain) { _max_gain = max_gain; }
71 
73  {
74  gr_complex output = input * _gain;
75 
76  float tmp = -_reference + sqrt(output.real()*output.real() +
77  output.imag()*output.imag());
78  float rate = _decay_rate;
79  if((tmp) > _gain) {
80  rate = _attack_rate;
81  }
82  _gain -= tmp*rate;
83 
84  // Not sure about this; will blow up if _gain < 0 (happens
85  // when rates are too high), but is this the solution?
86  if(_gain < 0.0)
87  _gain = 10e-5;
88 
89  if(_max_gain > 0.0 && _gain > _max_gain) {
90  _gain = _max_gain;
91  }
92  return output;
93  }
94 
95  void scaleN(gr_complex output[], const gr_complex input[], unsigned n)
96  {
97  for(unsigned i = 0; i < n; i++)
98  output[i] = scale (input[i]);
99  }
100 
101  protected:
102  float _attack_rate; // attack rate for fast changing signals
103  float _decay_rate; // decay rate for slow changing signals
104  float _reference; // reference value
105  float _gain; // current gain
106  float _max_gain; // max allowable gain
107  };
108 
109 
111  {
112  public:
113  /*!
114  * Construct a floating point value AGC loop implementation object.
115  *
116  * \param attack_rate the update rate of the loop when in attack mode.
117  * \param decay_rate the update rate of the loop when in decay mode.
118  * \param reference reference value to adjust signal power to.
119  * \param gain initial gain value.
120  * \param max_gain maximum gain value (0 for unlimited).
121  */
122  agc2_ff(float attack_rate = 1e-1, float decay_rate = 1e-2,
123  float reference = 1.0,
124  float gain = 1.0, float max_gain = 0.0)
125  : _attack_rate(attack_rate), _decay_rate(decay_rate),
126  _reference(reference),
127  _gain(gain), _max_gain(max_gain) {};
128 
129  float attack_rate() const { return _attack_rate; }
130  float decay_rate() const { return _decay_rate; }
131  float reference() const { return _reference; }
132  float gain() const { return _gain; }
133  float max_gain() const { return _max_gain; }
134 
135  void set_attack_rate(float rate) { _attack_rate = rate; }
136  void set_decay_rate(float rate) { _decay_rate = rate; }
137  void set_reference(float reference) { _reference = reference; }
138  void set_gain(float gain) { _gain = gain; }
139  void set_max_gain(float max_gain) { _max_gain = max_gain; }
140 
141  float scale(float input)
142  {
143  float output = input * _gain;
144 
145  float tmp = (fabsf(output)) - _reference;
146  float rate = _decay_rate;
147  if(fabsf(tmp) > _gain) {
148  rate = _attack_rate;
149  }
150  _gain -= tmp*rate;
151 
152  // Not sure about this
153  if(_gain < 0.0)
154  _gain = 10e-5;
155 
156  if(_max_gain > 0.0 && _gain > _max_gain) {
157  _gain = _max_gain;
158  }
159  return output;
160  }
161 
162  void scaleN(float output[], const float input[], unsigned n)
163  {
164  for(unsigned i = 0; i < n; i++)
165  output[i] = scale (input[i]);
166  }
167 
168  protected:
169  float _attack_rate; // attack_rate for fast changing signals
170  float _decay_rate; // decay rate for slow changing signals
171  float _reference; // reference value
172  float _gain; // current gain
173  float _max_gain; // maximum gain
174  };
175 
176  } /* namespace kernel */
177  } /* namespace analog */
178 } /* namespace gr */
179 
180 #endif /* INCLUDED_ANALOG_AGC2_H */
void set_reference(float reference)
Definition: agc2.h:68
float _attack_rate
Definition: agc2.h:102
void set_attack_rate(float rate)
Definition: agc2.h:67
float reference() const
Definition: agc2.h:131
float _reference
Definition: agc2.h:171
high performance Automatic Gain Control class
Definition: agc2.h:41
float attack_rate() const
Definition: agc2.h:129
void set_attack_rate(float rate)
Definition: agc2.h:135
gr_complex scale(gr_complex input)
Definition: agc2.h:72
float max_gain() const
Definition: agc2.h:64
Definition: agc2.h:110
void set_reference(float reference)
Definition: agc2.h:137
void set_gain(float gain)
Definition: agc2.h:69
float reference() const
Definition: agc2.h:62
float _gain
Definition: agc2.h:172
std::complex< float > gr_complex
Definition: gr_complex.h:27
float _decay_rate
Definition: agc2.h:103
float _decay_rate
Definition: agc2.h:170
Include this header to use the message passing features.
Definition: logger.h:695
void set_decay_rate(float rate)
Definition: agc2.h:136
float _max_gain
Definition: agc2.h:106
float gain() const
Definition: agc2.h:63
agc2_ff(float attack_rate=1e-1, float decay_rate=1e-2, float reference=1.0, float gain=1.0, float max_gain=0.0)
Definition: agc2.h:122
float scale(float input)
Definition: agc2.h:141
float _max_gain
Definition: agc2.h:173
float max_gain() const
Definition: agc2.h:133
void scaleN(gr_complex output[], const gr_complex input[], unsigned n)
Definition: agc2.h:95
agc2_cc(float attack_rate=1e-1, float decay_rate=1e-2, float reference=1.0, float gain=1.0, float max_gain=0.0)
Definition: agc2.h:53
void set_max_gain(float max_gain)
Definition: agc2.h:70
float _attack_rate
Definition: agc2.h:169
void set_decay_rate(float rate)
Definition: agc2.h:66
void scaleN(float output[], const float input[], unsigned n)
Definition: agc2.h:162
float _gain
Definition: agc2.h:105
void set_max_gain(float max_gain)
Definition: agc2.h:139
float attack_rate() const
Definition: agc2.h:61
float decay_rate() const
Definition: agc2.h:60
#define ANALOG_API
Definition: gr-analog/include/gnuradio/analog/api.h:30
float _reference
Definition: agc2.h:104
void set_gain(float gain)
Definition: agc2.h:138
float decay_rate() const
Definition: agc2.h:130
float gain() const
Definition: agc2.h:132