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