GNU Radio Manual and C++ API Reference  3.8.1.0
The Free & Open Software Radio Ecosystem
agc.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_AGC_H
24 #define INCLUDED_ANALOG_AGC_H
25 
26 #include <gnuradio/analog/api.h>
27 #include <gnuradio/gr_complex.h>
28 #include <cmath>
29 
30 namespace gr {
31 namespace analog {
32 namespace kernel {
33 
34 /*!
35  * \brief high performance Automatic Gain Control class for complex signals.
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 complex value AGC loop implementation object.
46  *
47  * \param rate the update rate of the loop.
48  * \param reference reference value to adjust signal power to.
49  * \param gain initial gain value.
50  * \param max_gain maximum gain value (0 for unlimited).
51  */
52  agc_cc(float rate = 1e-4,
53  float reference = 1.0,
54  float gain = 1.0,
55  float max_gain = 0.0)
56  : _rate(rate), _reference(reference), _gain(gain), _max_gain(max_gain){};
57 
58  virtual ~agc_cc(){};
59 
60  float rate() const { return _rate; }
61  float reference() const { return _reference; }
62  float gain() const { return _gain; }
63  float max_gain() const { return _max_gain; }
64 
65  void set_rate(float rate) { _rate = rate; }
66  void set_reference(float reference) { _reference = reference; }
67  void set_gain(float gain) { _gain = gain; }
68  void set_max_gain(float max_gain) { _max_gain = max_gain; }
69 
71  {
72  gr_complex output = input * _gain;
73 
74  _gain += _rate * (_reference - std::sqrt(output.real() * output.real() +
75  output.imag() * output.imag()));
76  if (_max_gain > 0.0 && _gain > _max_gain) {
77  _gain = _max_gain;
78  }
79  return output;
80  }
81 
82  void scaleN(gr_complex output[], const gr_complex input[], unsigned n)
83  {
84  for (unsigned i = 0; i < n; i++) {
85  output[i] = scale(input[i]);
86  }
87  }
88 
89 protected:
90  float _rate; // adjustment rate
91  float _reference; // reference value
92  float _gain; // current gain
93  float _max_gain; // max allowable gain
94 };
95 
96 /*!
97  * \brief high performance Automatic Gain Control class for float signals.
98  *
99  * Power is approximated by absolute value
100  */
102 {
103 public:
104  /*!
105  * Construct a floating point value AGC loop implementation object.
106  *
107  * \param rate the update rate of the loop.
108  * \param reference reference value to adjust signal power to.
109  * \param gain initial gain value.
110  * \param max_gain maximum gain value (0 for unlimited).
111  */
112  agc_ff(float rate = 1e-4,
113  float reference = 1.0,
114  float gain = 1.0,
115  float max_gain = 0.0)
116  : _rate(rate), _reference(reference), _gain(gain), _max_gain(max_gain){};
117 
118  ~agc_ff(){};
119 
120  float rate() const { return _rate; }
121  float reference() const { return _reference; }
122  float gain() const { return _gain; }
123  float max_gain() const { return _max_gain; }
124 
125  void set_rate(float rate) { _rate = rate; }
126  void set_reference(float reference) { _reference = reference; }
127  void set_gain(float gain) { _gain = gain; }
128  void set_max_gain(float max_gain) { _max_gain = max_gain; }
129 
130  float scale(float input)
131  {
132  float output = input * _gain;
133  _gain += (_reference - fabsf(output)) * _rate;
134  if (_max_gain > 0.0 && _gain > _max_gain)
135  _gain = _max_gain;
136  return output;
137  }
138 
139  void scaleN(float output[], const float input[], unsigned n)
140  {
141  for (unsigned i = 0; i < n; i++)
142  output[i] = scale(input[i]);
143  }
144 
145 protected:
146  float _rate; // adjustment rate
147  float _reference; // reference value
148  float _gain; // current gain
149  float _max_gain; // maximum gain
150 };
151 
152 } /* namespace kernel */
153 } /* namespace analog */
154 } /* namespace gr */
155 
156 #endif /* INCLUDED_ANALOG_AGC_H */
void set_rate(float rate)
Definition: agc.h:125
float _gain
Definition: agc.h:92
agc_cc(float rate=1e-4, float reference=1.0, float gain=1.0, float max_gain=0.0)
Definition: agc.h:52
float max_gain() const
Definition: agc.h:63
void scaleN(float output[], const float input[], unsigned n)
Definition: agc.h:139
float gain() const
Definition: agc.h:62
float _max_gain
Definition: agc.h:93
void set_reference(float reference)
Definition: agc.h:66
float scale(float input)
Definition: agc.h:130
float _max_gain
Definition: agc.h:149
std::complex< float > gr_complex
Definition: gr_complex.h:27
void set_reference(float reference)
Definition: agc.h:126
high performance Automatic Gain Control class for complex signals.
Definition: agc.h:41
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:43
high performance Automatic Gain Control class for float signals.
Definition: agc.h:101
~agc_ff()
Definition: agc.h:118
gr_complex scale(gr_complex input)
Definition: agc.h:70
float rate() const
Definition: agc.h:120
void set_max_gain(float max_gain)
Definition: agc.h:128
float reference() const
Definition: agc.h:121
void set_gain(float gain)
Definition: agc.h:127
float rate() const
Definition: agc.h:60
agc_ff(float rate=1e-4, float reference=1.0, float gain=1.0, float max_gain=0.0)
Definition: agc.h:112
float _gain
Definition: agc.h:148
float _reference
Definition: agc.h:147
float _rate
Definition: agc.h:146
#define ANALOG_API
Definition: gr-analog/include/gnuradio/analog/api.h:30
float max_gain() const
Definition: agc.h:123
float gain() const
Definition: agc.h:122
float _rate
Definition: agc.h:90
float _reference
Definition: agc.h:91
float reference() const
Definition: agc.h:61
virtual ~agc_cc()
Definition: agc.h:58
void set_rate(float rate)
Definition: agc.h:65
void set_max_gain(float max_gain)
Definition: agc.h:68
void scaleN(gr_complex output[], const gr_complex input[], unsigned n)
Definition: agc.h:82
void set_gain(float gain)
Definition: agc.h:67