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