SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Public Member Functions | Private Member Functions | Private Attributes | List of all members
MagAGC Class Reference

#include <agc.h>

+ Inheritance diagram for MagAGC:
+ Collaboration diagram for MagAGC:

Public Member Functions

 MagAGC (int historySize, double R, double threshold)
 
virtual ~MagAGC ()
 
void setSquared (bool squared)
 
void resize (int historySize, int stepLength, Real R)
 
void setOrder (double R)
 
virtual void feed (Complex &ci)
 
double feedAndGetValue (const Complex &ci)
 
double getMagSq () const
 
void setThreshold (double threshold)
 
void setThresholdEnable (bool enable)
 
void setGate (int gate)
 
void setStepDownDelay (int stepDownDelay)
 
void setClamping (bool clamping)
 
void setClampMax (double clampMax)
 
int getStepDownDelay () const
 
float getStepValue () const
 
void setHardLimiting (bool hardLimiting)
 
- Public Member Functions inherited from AGC
 AGC (int historySize, double R)
 
virtual ~AGC ()
 
void resize (int historySize, double R)
 
void setOrder (double R)
 
Real getValue ()
 
Real getAverage ()
 

Private Member Functions

double hardLimiter (double multiplier, double magsq)
 

Private Attributes

bool m_squared
 use squared magnitude (power) to compute AGC value More...
 
double m_magsq
 current squared magnitude (power) More...
 
double m_threshold
 squelch on magsq average More...
 
bool m_thresholdEnable
 enable squelch on power threshold More...
 
int m_gate
 power threshold gate in number of samples More...
 
int m_stepLength
 transition step length in number of samples More...
 
double m_stepDelta
 transition step unit by sample More...
 
int m_stepUpCounter
 step up transition samples counter More...
 
int m_stepDownCounter
 step down transition samples counter More...
 
int m_gateCounter
 threshold gate samples counter More...
 
int m_stepDownDelay
 delay in samples before cutoff (release) More...
 
bool m_clamping
 clamping active More...
 
double m_R2
 square of ordered magnitude More...
 
double m_clampMax
 maximum to clamp to as power value More...
 
bool m_hardLimiting
 hard limit multiplier so that resulting sample magnitude does not exceed 1.0 More...
 

Additional Inherited Members

- Protected Attributes inherited from AGC
double m_u0
 AGC factor. More...
 
double m_R
 ordered magnitude More...
 
MovingAverage< double > m_moving_average
 Averaging engine. The stack length conditions the smoothness of AGC. More...
 
int m_historySize
 Averaging length (attack) More...
 
int m_count
 Samples counter. More...
 

Detailed Description

Definition at line 36 of file agc.h.

Constructor & Destructor Documentation

◆ MagAGC()

MagAGC::MagAGC ( int  historySize,
double  R,
double  threshold 
)

Definition at line 43 of file agc.cpp.

43  :
44  AGC(historySize, R),
45  m_squared(false),
46  m_magsq(0.0),
47  m_threshold(threshold),
48  m_thresholdEnable(true),
49  m_gate(0),
50  m_stepLength(std::min(2400, historySize/2)), // max 50 ms (at 48 kHz)
52  m_stepUpCounter(0),
54  m_gateCounter(0),
55  m_stepDownDelay(historySize),
56  m_clamping(false),
57  m_R2(R*R),
58  m_clampMax(1.0),
59  m_hardLimiting(false)
60 {}
int m_gate
power threshold gate in number of samples
Definition: agc.h:62
int m_stepDownCounter
step down transition samples counter
Definition: agc.h:66
double m_threshold
squelch on magsq average
Definition: agc.h:60
double m_R2
square of ordered magnitude
Definition: agc.h:70
double m_clampMax
maximum to clamp to as power value
Definition: agc.h:71
bool m_clamping
clamping active
Definition: agc.h:69
int m_gateCounter
threshold gate samples counter
Definition: agc.h:67
int m_stepLength
transition step length in number of samples
Definition: agc.h:63
AGC(int historySize, double R)
Definition: agc.cpp:13
bool m_squared
use squared magnitude (power) to compute AGC value
Definition: agc.h:58
double m_magsq
current squared magnitude (power)
Definition: agc.h:59
double m_stepDelta
transition step unit by sample
Definition: agc.h:64
int m_stepDownDelay
delay in samples before cutoff (release)
Definition: agc.h:68
int m_stepUpCounter
step up transition samples counter
Definition: agc.h:65
bool m_thresholdEnable
enable squelch on power threshold
Definition: agc.h:61
bool m_hardLimiting
hard limit multiplier so that resulting sample magnitude does not exceed 1.0
Definition: agc.h:72
T min(const T &x, const T &y)
Definition: framework.h:440

◆ ~MagAGC()

MagAGC::~MagAGC ( )
virtual

Definition at line 62 of file agc.cpp.

63 {}

Member Function Documentation

◆ feed()

void MagAGC::feed ( Complex ci)
virtual

Implements AGC.

Definition at line 94 of file agc.cpp.

References feedAndGetValue().

95 {
96  ci *= feedAndGetValue(ci);
97 }
double feedAndGetValue(const Complex &ci)
Definition: agc.cpp:108
+ Here is the call graph for this function:

◆ feedAndGetValue()

double MagAGC::feedAndGetValue ( const Complex ci)

Definition at line 108 of file agc.cpp.

References MovingAverage< Type >::average(), MovingAverage< Type >::feed(), hardLimiter(), m_clamping, m_clampMax, AGC::m_count, m_gate, m_gateCounter, m_magsq, AGC::m_moving_average, AGC::m_R, m_squared, m_stepDelta, m_stepDownCounter, m_stepDownDelay, m_stepLength, m_stepUpCounter, m_threshold, m_thresholdEnable, AGC::m_u0, StepFunctions::smootherstep(), and sqrt().

Referenced by feed(), UDPSink::feed(), AMDemod::processOneSample(), and SSBDemod::processOneSample().

109 {
110  m_magsq = ci.real()*ci.real() + ci.imag()*ci.imag();
112 
113  if (m_clamping)
114  {
115  if (m_squared)
116  {
117  if (m_magsq > m_clampMax) {
118  m_u0 = m_clampMax / m_magsq;
119  } else {
121  }
122  }
123  else
124  {
125  if (sqrt(m_magsq) > m_clampMax) {
127  } else {
129  }
130  }
131  }
132  else
133  {
135  }
136 
137  if (m_thresholdEnable)
138  {
139  bool open = false;
140 
141  if (m_magsq > m_threshold)
142  {
143  if (m_gateCounter < m_gate) {
144  m_gateCounter++;
145  } else {
146  open = true;
147  }
148  }
149  else
150  {
151  m_gateCounter = 0;
152  }
153 
154  if (open)
155  {
156  m_count = m_stepDownDelay; // delay before step down (grace delay)
157  }
158  else
159  {
160  m_count--;
161  m_gateCounter = m_gate; // keep gate open during grace
162  }
163 
164  if (m_count > 0) // up phase
165  {
166  m_stepDownCounter = m_stepUpCounter; // prepare for step down
167 
168  if (m_stepUpCounter < m_stepLength) // step up
169  {
170  m_stepUpCounter++;
172  }
173  else // steady open
174  {
175  return hardLimiter(m_u0, m_magsq);
176  }
177  }
178  else // down phase
179  {
180  m_stepUpCounter = m_stepDownCounter; // prepare for step up
181 
182  if (m_stepDownCounter > 0) // step down
183  {
186  }
187  else // steady closed
188  {
189  return 0.0;
190  }
191  }
192  }
193  else
194  {
195  return hardLimiter(m_u0, m_magsq);
196  }
197 }
int m_gate
power threshold gate in number of samples
Definition: agc.h:62
int m_stepDownCounter
step down transition samples counter
Definition: agc.h:66
void feed(Type value)
Definition: movingaverage.h:24
double m_u0
AGC factor.
Definition: agc.h:28
double m_threshold
squelch on magsq average
Definition: agc.h:60
static float smootherstep(float x)
Definition: stepfunctions.h:24
Type average() const
Definition: movingaverage.h:43
int m_count
Samples counter.
Definition: agc.h:32
double hardLimiter(double multiplier, double magsq)
Definition: agc.cpp:99
double m_clampMax
maximum to clamp to as power value
Definition: agc.h:71
bool m_clamping
clamping active
Definition: agc.h:69
MovingAverage< double > m_moving_average
Averaging engine. The stack length conditions the smoothness of AGC.
Definition: agc.h:30
double m_R
ordered magnitude
Definition: agc.h:29
int m_gateCounter
threshold gate samples counter
Definition: agc.h:67
int m_stepLength
transition step length in number of samples
Definition: agc.h:63
Fixed< IntType, IntBits > sqrt(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2283
bool m_squared
use squared magnitude (power) to compute AGC value
Definition: agc.h:58
double m_magsq
current squared magnitude (power)
Definition: agc.h:59
double m_stepDelta
transition step unit by sample
Definition: agc.h:64
int m_stepDownDelay
delay in samples before cutoff (release)
Definition: agc.h:68
int m_stepUpCounter
step up transition samples counter
Definition: agc.h:65
bool m_thresholdEnable
enable squelch on power threshold
Definition: agc.h:61
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMagSq()

double MagAGC::getMagSq ( ) const
inline

Definition at line 46 of file agc.h.

Referenced by UDPSink::feed().

46 { return m_magsq; }
double m_magsq
current squared magnitude (power)
Definition: agc.h:59
+ Here is the caller graph for this function:

◆ getStepDownDelay()

int MagAGC::getStepDownDelay ( ) const
inline

Definition at line 53 of file agc.h.

Referenced by SSBDemod::processOneSample().

53 { return m_stepDownDelay; }
int m_stepDownDelay
delay in samples before cutoff (release)
Definition: agc.h:68
+ Here is the caller graph for this function:

◆ getStepValue()

float MagAGC::getStepValue ( ) const

Definition at line 199 of file agc.cpp.

References AGC::m_count, m_stepDelta, m_stepDownCounter, m_stepUpCounter, and StepFunctions::smootherstep().

Referenced by SSBDemod::processOneSample().

200 {
201  if (m_count > 0) // up phase
202  {
204  }
205  else // down phase
206  {
208  }
209 }
int m_stepDownCounter
step down transition samples counter
Definition: agc.h:66
static float smootherstep(float x)
Definition: stepfunctions.h:24
int m_count
Samples counter.
Definition: agc.h:32
double m_stepDelta
transition step unit by sample
Definition: agc.h:64
int m_stepUpCounter
step up transition samples counter
Definition: agc.h:65
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hardLimiter()

double MagAGC::hardLimiter ( double  multiplier,
double  magsq 
)
private

Definition at line 99 of file agc.cpp.

References m_hardLimiting, and sqrt().

Referenced by feedAndGetValue().

100 {
101  if ((m_hardLimiting) && (multiplier*multiplier*magsq > 1.0)) {
102  return 1.0 / (multiplier*sqrt(magsq));
103  } else {
104  return multiplier;
105  }
106 }
Fixed< IntType, IntBits > sqrt(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2283
bool m_hardLimiting
hard limit multiplier so that resulting sample magnitude does not exceed 1.0
Definition: agc.h:72
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ resize()

void MagAGC::resize ( int  historySize,
int  stepLength,
Real  R 
)

Definition at line 65 of file agc.cpp.

References MovingAverage< Type >::fill(), AGC::m_moving_average, m_R2, m_stepDelta, m_stepDownCounter, m_stepLength, m_stepUpCounter, and AGC::resize().

Referenced by AMDemod::AMDemod(), AMDemod::applyAudioSampleRate(), SSBDemod::applyAudioSampleRate(), UDPSink::applySettings(), and SSBDemod::applySettings().

66 {
67  m_R2 = R*R;
68  m_stepLength = stepLength;
69  m_stepDelta = 1.0 / m_stepLength;
70  m_stepUpCounter = 0;
72  AGC::resize(historySize, R);
74 }
int m_stepDownCounter
step down transition samples counter
Definition: agc.h:66
void fill(Type value)
Definition: movingaverage.h:37
double m_R2
square of ordered magnitude
Definition: agc.h:70
void resize(int historySize, double R)
Definition: agc.cpp:24
MovingAverage< double > m_moving_average
Averaging engine. The stack length conditions the smoothness of AGC.
Definition: agc.h:30
int m_stepLength
transition step length in number of samples
Definition: agc.h:63
double m_stepDelta
transition step unit by sample
Definition: agc.h:64
int m_stepUpCounter
step up transition samples counter
Definition: agc.h:65
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setClamping()

void MagAGC::setClamping ( bool  clamping)
inline

Definition at line 51 of file agc.h.

Referenced by SSBDemod::applySettings(), SSBDemod::SSBDemod(), and UDPSink::UDPSink().

51 { m_clamping = clamping; }
bool m_clamping
clamping active
Definition: agc.h:69
+ Here is the caller graph for this function:

◆ setClampMax()

void MagAGC::setClampMax ( double  clampMax)
inline

Definition at line 52 of file agc.h.

Referenced by SSBDemod::SSBDemod(), and UDPSink::UDPSink().

52 { m_clampMax = clampMax; }
double m_clampMax
maximum to clamp to as power value
Definition: agc.h:71
+ Here is the caller graph for this function:

◆ setGate()

void MagAGC::setGate ( int  gate)
inline

Definition at line 49 of file agc.h.

Referenced by SSBDemod::applyAudioSampleRate(), UDPSink::applySettings(), and SSBDemod::applySettings().

49 { m_gate = gate; m_gateCounter = 0; m_count = 0; }
int m_gate
power threshold gate in number of samples
Definition: agc.h:62
int m_count
Samples counter.
Definition: agc.h:32
int m_gateCounter
threshold gate samples counter
Definition: agc.h:67
+ Here is the caller graph for this function:

◆ setHardLimiting()

void MagAGC::setHardLimiting ( bool  hardLimiting)
inline

Definition at line 55 of file agc.h.

55 { m_hardLimiting = hardLimiting; }
bool m_hardLimiting
hard limit multiplier so that resulting sample magnitude does not exceed 1.0
Definition: agc.h:72

◆ setOrder()

void MagAGC::setOrder ( double  R)

Definition at line 76 of file agc.cpp.

References MovingAverage< Type >::fill(), AGC::m_moving_average, m_R2, and AGC::setOrder().

77 {
78  m_R2 = R*R;
79  AGC::setOrder(R);
81 }
void fill(Type value)
Definition: movingaverage.h:37
double m_R2
square of ordered magnitude
Definition: agc.h:70
MovingAverage< double > m_moving_average
Averaging engine. The stack length conditions the smoothness of AGC.
Definition: agc.h:30
void setOrder(double R)
Definition: agc.h:22
+ Here is the call graph for this function:

◆ setSquared()

void MagAGC::setSquared ( bool  squared)
inline

Definition at line 41 of file agc.h.

References AGC::feed(), AGC::resize(), and AGC::setOrder().

41 { m_squared = squared; }
bool m_squared
use squared magnitude (power) to compute AGC value
Definition: agc.h:58
+ Here is the call graph for this function:

◆ setStepDownDelay()

void MagAGC::setStepDownDelay ( int  stepDownDelay)
inline

Definition at line 50 of file agc.h.

Referenced by SSBDemod::applyAudioSampleRate(), UDPSink::applySettings(), and SSBDemod::applySettings().

50 { m_stepDownDelay = stepDownDelay; m_gateCounter = 0; m_count = 0; }
int m_count
Samples counter.
Definition: agc.h:32
int m_gateCounter
threshold gate samples counter
Definition: agc.h:67
int m_stepDownDelay
delay in samples before cutoff (release)
Definition: agc.h:68
+ Here is the caller graph for this function:

◆ setThreshold()

void MagAGC::setThreshold ( double  threshold)
inline

Definition at line 47 of file agc.h.

Referenced by UDPSink::applySettings(), and SSBDemod::applySettings().

47 { m_threshold = threshold; }
double m_threshold
squelch on magsq average
Definition: agc.h:60
+ Here is the caller graph for this function:

◆ setThresholdEnable()

void MagAGC::setThresholdEnable ( bool  enable)

Definition at line 83 of file agc.cpp.

References m_stepDownCounter, m_stepUpCounter, and m_thresholdEnable.

Referenced by AMDemod::AMDemod(), and SSBDemod::applySettings().

84 {
85  if (m_thresholdEnable != enable)
86  {
87  m_stepUpCounter = 0;
89  }
90 
91  m_thresholdEnable = enable;
92 }
int m_stepDownCounter
step down transition samples counter
Definition: agc.h:66
int m_stepUpCounter
step up transition samples counter
Definition: agc.h:65
bool m_thresholdEnable
enable squelch on power threshold
Definition: agc.h:61
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_clamping

bool MagAGC::m_clamping
private

clamping active

Definition at line 69 of file agc.h.

Referenced by feedAndGetValue().

◆ m_clampMax

double MagAGC::m_clampMax
private

maximum to clamp to as power value

Definition at line 71 of file agc.h.

Referenced by feedAndGetValue().

◆ m_gate

int MagAGC::m_gate
private

power threshold gate in number of samples

Definition at line 62 of file agc.h.

Referenced by feedAndGetValue().

◆ m_gateCounter

int MagAGC::m_gateCounter
private

threshold gate samples counter

Definition at line 67 of file agc.h.

Referenced by feedAndGetValue().

◆ m_hardLimiting

bool MagAGC::m_hardLimiting
private

hard limit multiplier so that resulting sample magnitude does not exceed 1.0

Definition at line 72 of file agc.h.

Referenced by hardLimiter().

◆ m_magsq

double MagAGC::m_magsq
private

current squared magnitude (power)

Definition at line 59 of file agc.h.

Referenced by feedAndGetValue().

◆ m_R2

double MagAGC::m_R2
private

square of ordered magnitude

Definition at line 70 of file agc.h.

Referenced by resize(), and setOrder().

◆ m_squared

bool MagAGC::m_squared
private

use squared magnitude (power) to compute AGC value

Definition at line 58 of file agc.h.

Referenced by feedAndGetValue().

◆ m_stepDelta

double MagAGC::m_stepDelta
private

transition step unit by sample

Definition at line 64 of file agc.h.

Referenced by feedAndGetValue(), getStepValue(), and resize().

◆ m_stepDownCounter

int MagAGC::m_stepDownCounter
private

step down transition samples counter

Definition at line 66 of file agc.h.

Referenced by feedAndGetValue(), getStepValue(), resize(), and setThresholdEnable().

◆ m_stepDownDelay

int MagAGC::m_stepDownDelay
private

delay in samples before cutoff (release)

Definition at line 68 of file agc.h.

Referenced by feedAndGetValue().

◆ m_stepLength

int MagAGC::m_stepLength
private

transition step length in number of samples

Definition at line 63 of file agc.h.

Referenced by feedAndGetValue(), and resize().

◆ m_stepUpCounter

int MagAGC::m_stepUpCounter
private

step up transition samples counter

Definition at line 65 of file agc.h.

Referenced by feedAndGetValue(), getStepValue(), resize(), and setThresholdEnable().

◆ m_threshold

double MagAGC::m_threshold
private

squelch on magsq average

Definition at line 60 of file agc.h.

Referenced by feedAndGetValue().

◆ m_thresholdEnable

bool MagAGC::m_thresholdEnable
private

enable squelch on power threshold

Definition at line 61 of file agc.h.

Referenced by feedAndGetValue(), and setThresholdEnable().


The documentation for this class was generated from the following files: