GNU Radio C++ API Reference: agc2.h Source File

| GNU Radio C++ API Reference 3.10.12.0 The Free & Open Software Radio Ecosystem |

Loading...
Searching...
No Matches
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
18namespace gr {
19namespace analog {
20namespace 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 */
29 class ANALOG_API agc2_cc
30{
31public:
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
64 gr_complex scale(gr_complex input)
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
93protected:
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
102 class ANALOG_API agc2_ff
103{
104public:
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
164protected:
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 */
gr::analog::kernel::agc2_cc::gain
float gain() const
Definition agc2.h:55
gr::analog::kernel::agc2_cc::set_gain
void set_gain(float gain)
Definition agc2.h:61
gr::analog::kernel::agc2_cc::_attack_rate
float _attack_rate
Definition agc2.h:94
gr::analog::kernel::agc2_cc::set_attack_rate
void set_attack_rate(float rate)
Definition agc2.h:59
gr::analog::kernel::agc2_cc::set_decay_rate
void set_decay_rate(float rate)
Definition agc2.h:58
gr::analog::kernel::agc2_cc::_reference
float _reference
Definition agc2.h:96
gr::analog::kernel::agc2_cc::agc2_cc
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::analog::kernel::agc2_cc::scale
gr_complex scale(gr_complex input)
Definition agc2.h:64
gr::analog::kernel::agc2_cc::scaleN
void scaleN(gr_complex output[], const gr_complex input[], unsigned n)
Definition agc2.h:87
gr::analog::kernel::agc2_cc::_gain
float _gain
Definition agc2.h:97
gr::analog::kernel::agc2_cc::reference
float reference() const
Definition agc2.h:54
gr::analog::kernel::agc2_cc::set_max_gain
void set_max_gain(float max_gain)
Definition agc2.h:62
gr::analog::kernel::agc2_cc::_decay_rate
float _decay_rate
Definition agc2.h:95
gr::analog::kernel::agc2_cc::set_reference
void set_reference(float reference)
Definition agc2.h:60
gr::analog::kernel::agc2_cc::max_gain
float max_gain() const
Definition agc2.h:56
gr::analog::kernel::agc2_cc::decay_rate
float decay_rate() const
Definition agc2.h:52
gr::analog::kernel::agc2_cc::_max_gain
float _max_gain
Definition agc2.h:98
gr::analog::kernel::agc2_cc::attack_rate
float attack_rate() const
Definition agc2.h:53
gr::analog::kernel::agc2_ff::_attack_rate
float _attack_rate
Definition agc2.h:165
gr::analog::kernel::agc2_ff::set_decay_rate
void set_decay_rate(float rate)
Definition agc2.h:132
gr::analog::kernel::agc2_ff::_decay_rate
float _decay_rate
Definition agc2.h:166
gr::analog::kernel::agc2_ff::scale
float scale(float input)
Definition agc2.h:137
gr::analog::kernel::agc2_ff::set_reference
void set_reference(float reference)
Definition agc2.h:133
gr::analog::kernel::agc2_ff::scaleN
void scaleN(float output[], const float input[], unsigned n)
Definition agc2.h:158
gr::analog::kernel::agc2_ff::_gain
float _gain
Definition agc2.h:168
gr::analog::kernel::agc2_ff::agc2_ff
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
gr::analog::kernel::agc2_ff::set_attack_rate
void set_attack_rate(float rate)
Definition agc2.h:131
gr::analog::kernel::agc2_ff::_reference
float _reference
Definition agc2.h:167
gr::analog::kernel::agc2_ff::reference
float reference() const
Definition agc2.h:127
gr::analog::kernel::agc2_ff::set_max_gain
void set_max_gain(float max_gain)
Definition agc2.h:135
gr::analog::kernel::agc2_ff::set_gain
void set_gain(float gain)
Definition agc2.h:134
gr::analog::kernel::agc2_ff::attack_rate
float attack_rate() const
Definition agc2.h:125
gr::analog::kernel::agc2_ff::max_gain
float max_gain() const
Definition agc2.h:129
gr::analog::kernel::agc2_ff::_max_gain
float _max_gain
Definition agc2.h:169
gr::analog::kernel::agc2_ff::gain
float gain() const
Definition agc2.h:128
gr::analog::kernel::agc2_ff::decay_rate
float decay_rate() const
Definition agc2.h:126
api.h
ANALOG_API
#define ANALOG_API
Definition gr-analog/include/gnuradio/analog/api.h:18
gr_complex.h
gr_complex
std::complex< float > gr_complex
Definition gr_complex.h:15
gr::analog::kernel
Definition agc.h:20
gr::analog
Definition agc.h:19
gr
GNU Radio logging wrapper.
Definition basic_block.h:29