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