GNU Radio 3.6.5.1 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 <analog/api.h>
27 #include <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  *
37  * \details
38  * For Power the absolute value of the complex number is used.
39  */
41  {
42  public:
43  agc_cc(float rate = 1e-4, float reference = 1.0,
44  float gain = 1.0, float max_gain = 0.0)
45  : _rate(rate), _reference(reference),
46  _gain(gain), _max_gain(max_gain) {};
47 
48  virtual ~agc_cc() {};
49 
50  float rate() const { return _rate; }
51  float reference() const { return _reference; }
52  float gain() const { return _gain; }
53  float max_gain() const { return _max_gain; }
54 
55  void set_rate(float rate) { _rate = rate; }
56  void set_reference(float reference) { _reference = reference; }
57  void set_gain(float gain) { _gain = gain; }
58  void set_max_gain(float max_gain) { _max_gain = max_gain; }
59 
60  gr_complex scale(gr_complex input)
61  {
62  gr_complex output = input * _gain;
63 
64  _gain += _rate * (_reference - sqrt(output.real()*output.real() +
65  output.imag()*output.imag()));
66  if(_max_gain > 0.0 && _gain > _max_gain) {
67  _gain = _max_gain;
68  }
69  return output;
70  }
71 
72  void scaleN(gr_complex output[], const gr_complex input[], unsigned n)
73  {
74  for(unsigned i = 0; i < n; i++) {
75  output[i] = scale (input[i]);
76  }
77  }
78 
79  protected:
80  float _rate; // adjustment rate
81  float _reference; // reference value
82  float _gain; // current gain
83  float _max_gain; // max allowable gain
84  };
85 
86  /*!
87  * \brief high performance Automatic Gain Control class for float signals.
88  *
89  * Power is approximated by absolute value
90  */
92  {
93  public:
94  agc_ff(float rate = 1e-4, float reference = 1.0,
95  float gain = 1.0, float max_gain = 0.0)
96  : _rate(rate), _reference(reference), _gain(gain),
97  _max_gain(max_gain) {};
98 
99  ~agc_ff() {};
100 
101  float rate () const { return _rate; }
102  float reference () const { return _reference; }
103  float gain () const { return _gain; }
104  float max_gain () const { return _max_gain; }
105 
106  void set_rate (float rate) { _rate = rate; }
107  void set_reference (float reference) { _reference = reference; }
108  void set_gain (float gain) { _gain = gain; }
109  void set_max_gain (float max_gain) { _max_gain = max_gain; }
110 
111  float scale (float input)
112  {
113  float output = input * _gain;
114  _gain += (_reference - fabsf (output)) * _rate;
115  if(_max_gain > 0.0 && _gain > _max_gain)
116  _gain = _max_gain;
117  return output;
118  }
119 
120  void scaleN(float output[], const float input[], unsigned n)
121  {
122  for(unsigned i = 0; i < n; i++)
123  output[i] = scale (input[i]);
124  }
125 
126  protected:
127  float _rate; // adjustment rate
128  float _reference; // reference value
129  float _gain; // current gain
130  float _max_gain; // maximum gain
131  };
132 
133  } /* namespace kernel */
134  } /* namespace analog */
135 } /* namespace gr */
136 
137 #endif /* INCLUDED_ANALOG_AGC_H */