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 | Protected Member Functions | Private Attributes | List of all members
AFSquelch Class Reference

#include <afsquelch.h>

Public Member Functions

 AFSquelch ()
 
virtual ~AFSquelch ()
 
void setCoefficients (unsigned int N, unsigned int nbAvg, unsigned int sampleRate, unsigned int samplesAttack, unsigned int samplesDecay, const double *tones)
 center frequency of tones tested More...
 
void setThreshold (double _threshold)
 
bool analyze (double sample)
 
bool evaluate ()
 
const double * getToneSet () const
 
bool open () const
 
void reset ()
 

Protected Member Functions

void feedback (double sample)
 
void feedForward ()
 

Private Attributes

unsigned int m_nbAvg
 number of power samples taken for moving average More...
 
unsigned int m_N
 
unsigned int m_sampleRate
 
unsigned int m_samplesProcessed
 
unsigned int m_samplesAvgProcessed
 
unsigned int m_maxPowerIndex
 
unsigned int m_nTones
 
unsigned int m_samplesAttack
 
unsigned int m_attackCount
 
unsigned int m_samplesDecay
 
unsigned int m_decayCount
 
unsigned int m_squelchCount
 
bool m_isOpen
 
double m_threshold
 
double * m_k
 
double * m_coef
 
double * m_toneSet
 
double * m_u0
 
double * m_u1
 
double * m_power
 
std::vector< MovingAverage< double > > m_movingAverages
 

Detailed Description

AFSquelch: AF squelch class based on the Modified Goertzel algorithm.

Definition at line 28 of file afsquelch.h.

Constructor & Destructor Documentation

◆ AFSquelch()

AFSquelch::AFSquelch ( )

Definition at line 24 of file afsquelch.cpp.

References cos(), m_coef, m_k, m_movingAverages, m_N, m_nbAvg, m_nTones, M_PI, m_power, m_sampleRate, m_toneSet, m_u0, and m_u1.

24  :
25  m_nbAvg(128),
26  m_N(24),
27  m_sampleRate(48000),
30  m_maxPowerIndex(0),
31  m_nTones(2),
32  m_samplesAttack(0),
33  m_attackCount(0),
34  m_samplesDecay(0),
35  m_decayCount(0),
36  m_squelchCount(0),
37  m_isOpen(false),
38  m_threshold(0.0)
39 {
40  m_k = new double[m_nTones];
41  m_coef = new double[m_nTones];
42  m_toneSet = new double[m_nTones];
43  m_u0 = new double[m_nTones];
44  m_u1 = new double[m_nTones];
45  m_power = new double[m_nTones];
47 
48  for (unsigned int j = 0; j < m_nTones; ++j)
49  {
50  m_toneSet[j] = j == 0 ? 1000.0 : 6000.0;
51  m_k[j] = ((double)m_N * m_toneSet[j]) / (double) m_sampleRate;
52  m_coef[j] = 2.0 * cos((2.0 * M_PI * m_toneSet[j])/(double) m_sampleRate);
53  m_u0[j] = 0.0;
54  m_u1[j] = 0.0;
55  m_power[j] = 0.0;
56  m_movingAverages[j].fill(0.0);
57  }
58 }
double * m_k
Definition: afsquelch.h:82
unsigned int m_samplesAvgProcessed
Definition: afsquelch.h:72
#define M_PI
Definition: afsquelch.cpp:22
Fixed< IntType, IntBits > cos(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2271
double * m_u0
Definition: afsquelch.h:85
double * m_coef
Definition: afsquelch.h:83
double * m_u1
Definition: afsquelch.h:86
double * m_power
Definition: afsquelch.h:87
unsigned int m_N
Definition: afsquelch.h:69
unsigned int m_maxPowerIndex
Definition: afsquelch.h:73
unsigned int m_squelchCount
Definition: afsquelch.h:79
double m_threshold
Definition: afsquelch.h:81
std::vector< MovingAverage< double > > m_movingAverages
Definition: afsquelch.h:88
bool m_isOpen
Definition: afsquelch.h:80
unsigned int m_samplesProcessed
Definition: afsquelch.h:71
unsigned int m_samplesAttack
Definition: afsquelch.h:75
unsigned int m_samplesDecay
Definition: afsquelch.h:77
unsigned int m_nbAvg
number of power samples taken for moving average
Definition: afsquelch.h:68
unsigned int m_attackCount
Definition: afsquelch.h:76
unsigned int m_nTones
Definition: afsquelch.h:74
unsigned int m_decayCount
Definition: afsquelch.h:78
double * m_toneSet
Definition: afsquelch.h:84
unsigned int m_sampleRate
Definition: afsquelch.h:70
+ Here is the call graph for this function:

◆ ~AFSquelch()

AFSquelch::~AFSquelch ( )
virtual

Definition at line 61 of file afsquelch.cpp.

References m_coef, m_k, m_power, m_toneSet, m_u0, and m_u1.

62 {
63  delete[] m_k;
64  delete[] m_coef;
65  delete[] m_toneSet;
66  delete[] m_u0;
67  delete[] m_u1;
68  delete[] m_power;
69 }
double * m_k
Definition: afsquelch.h:82
double * m_u0
Definition: afsquelch.h:85
double * m_coef
Definition: afsquelch.h:83
double * m_u1
Definition: afsquelch.h:86
double * m_power
Definition: afsquelch.h:87
double * m_toneSet
Definition: afsquelch.h:84

Member Function Documentation

◆ analyze()

bool AFSquelch::analyze ( double  sample)

Definition at line 116 of file afsquelch.cpp.

References feedback(), feedForward(), m_N, m_nbAvg, m_samplesAvgProcessed, and m_samplesProcessed.

Referenced by NFMDemod::processOneSample().

117 {
118 
119  feedback(sample); // Goertzel feedback
120 
121  if (m_samplesProcessed < m_N) // completed a block of N
122  {
124  return false;
125  }
126  else
127  {
128  feedForward(); // calculate the power at each tone
129  m_samplesProcessed = 0;
130 
132  {
134  return false;
135  }
136  else
137  {
138  return true; // have a result
139  }
140  }
141 }
unsigned int m_samplesAvgProcessed
Definition: afsquelch.h:72
void feedback(double sample)
Definition: afsquelch.cpp:144
unsigned int m_N
Definition: afsquelch.h:69
unsigned int m_samplesProcessed
Definition: afsquelch.h:71
unsigned int m_nbAvg
number of power samples taken for moving average
Definition: afsquelch.h:68
void feedForward()
Definition: afsquelch.cpp:158
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ evaluate()

bool AFSquelch::evaluate ( )

Definition at line 188 of file afsquelch.cpp.

References m_isOpen, m_movingAverages, m_nTones, m_samplesAttack, m_samplesDecay, m_squelchCount, and m_threshold.

Referenced by feedForward(), and NFMDemod::processOneSample().

189 {
190  double maxPower = 0.0;
191  double minPower;
192  int minIndex = 0, maxIndex = 0;
193 
194  for (unsigned int j = 0; j < m_nTones; ++j)
195  {
196  if (m_movingAverages[j].sum() > maxPower)
197  {
198  maxPower = m_movingAverages[j].sum();
199  maxIndex = j;
200  }
201  }
202 
203  if (maxPower == 0.0)
204  {
205  return m_isOpen;
206  }
207 
208  minPower = maxPower;
209 
210  for (unsigned int j = 0; j < m_nTones; ++j)
211  {
212  if (m_movingAverages[j].sum() < minPower) {
213  minPower = m_movingAverages[j].sum();
214  minIndex = j;
215  }
216  }
217 
218 // m_isOpen = ((minPower/maxPower < m_threshold) && (minIndex > maxIndex));
219 
220  if ((minPower/maxPower < m_threshold) && (minIndex > maxIndex)) // open condition
221  {
223  {
224  m_squelchCount++;
225  }
226  }
227  else
228  {
230  {
231  m_squelchCount--;
232  }
233  else
234  {
235  m_squelchCount = 0;
236  }
237  }
238 
240 
241 // if ((minPower/maxPower < m_threshold) && (minIndex > maxIndex)) // open condition
242 // {
243 // if ((m_samplesAttack > 0) && (m_attackCount < m_samplesAttack))
244 // {
245 // m_isOpen = false;
246 // m_attackCount++;
247 // }
248 // else
249 // {
250 // m_isOpen = true;
251 // m_decayCount = 0;
252 // }
253 // }
254 // else
255 // {
256 // if ((m_samplesDecay > 0) && (m_decayCount < m_samplesDecay))
257 // {
258 // m_isOpen = true;
259 // m_decayCount++;
260 // }
261 // else
262 // {
263 // m_isOpen = false;
264 // m_attackCount = 0;
265 // }
266 // }
267 
268  return m_isOpen;
269 }
unsigned int m_squelchCount
Definition: afsquelch.h:79
double m_threshold
Definition: afsquelch.h:81
std::vector< MovingAverage< double > > m_movingAverages
Definition: afsquelch.h:88
bool m_isOpen
Definition: afsquelch.h:80
unsigned int m_samplesAttack
Definition: afsquelch.h:75
unsigned int m_samplesDecay
Definition: afsquelch.h:77
unsigned int m_nTones
Definition: afsquelch.h:74
+ Here is the caller graph for this function:

◆ feedback()

void AFSquelch::feedback ( double  sample)
protected

Definition at line 144 of file afsquelch.cpp.

References m_coef, m_nTones, m_u0, and m_u1.

Referenced by analyze().

145 {
146  double t;
147 
148  // feedback for each tone
149  for (unsigned int j = 0; j < m_nTones; ++j)
150  {
151  t = m_u0[j];
152  m_u0[j] = in + (m_coef[j] * m_u0[j]) - m_u1[j];
153  m_u1[j] = t;
154  }
155 }
double * m_u0
Definition: afsquelch.h:85
double * m_coef
Definition: afsquelch.h:83
double * m_u1
Definition: afsquelch.h:86
unsigned int m_nTones
Definition: afsquelch.h:74
+ Here is the caller graph for this function:

◆ feedForward()

void AFSquelch::feedForward ( )
protected

Definition at line 158 of file afsquelch.cpp.

References evaluate(), m_coef, m_movingAverages, m_nTones, m_power, m_u0, and m_u1.

Referenced by analyze().

159 {
160  for (unsigned int j = 0; j < m_nTones; ++j)
161  {
162  m_power[j] = (m_u0[j] * m_u0[j]) + (m_u1[j] * m_u1[j]) - (m_coef[j] * m_u0[j] * m_u1[j]);
163  m_movingAverages[j].feed(m_power[j]);
164  m_u0[j] = 0.0;
165  m_u1[j] = 0.0; // reset for next block.
166  }
167 
168  evaluate();
169 }
double * m_u0
Definition: afsquelch.h:85
double * m_coef
Definition: afsquelch.h:83
double * m_u1
Definition: afsquelch.h:86
double * m_power
Definition: afsquelch.h:87
bool evaluate()
Definition: afsquelch.cpp:188
std::vector< MovingAverage< double > > m_movingAverages
Definition: afsquelch.h:88
unsigned int m_nTones
Definition: afsquelch.h:74
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getToneSet()

const double* AFSquelch::getToneSet ( ) const
inline

Definition at line 52 of file afsquelch.h.

53  {
54  return m_toneSet;
55  }
double * m_toneSet
Definition: afsquelch.h:84

◆ open()

bool AFSquelch::open ( ) const
inline

Definition at line 57 of file afsquelch.h.

57  {
58  return m_isOpen;
59  }
bool m_isOpen
Definition: afsquelch.h:80

◆ reset()

void AFSquelch::reset ( )

Definition at line 172 of file afsquelch.cpp.

References m_isOpen, m_maxPowerIndex, m_movingAverages, m_nTones, m_power, m_samplesProcessed, m_u0, and m_u1.

Referenced by NFMDemod::applySettings(), and setThreshold().

173 {
174  for (unsigned int j = 0; j < m_nTones; ++j)
175  {
176  m_u0[j] = 0.0;
177  m_u1[j] = 0.0;
178  m_power[j] = 0.0;
179  m_movingAverages[j].fill(0.0);
180  }
181 
182  m_samplesProcessed = 0;
183  m_maxPowerIndex = 0;
184  m_isOpen = false;
185 }
double * m_u0
Definition: afsquelch.h:85
double * m_u1
Definition: afsquelch.h:86
double * m_power
Definition: afsquelch.h:87
unsigned int m_maxPowerIndex
Definition: afsquelch.h:73
std::vector< MovingAverage< double > > m_movingAverages
Definition: afsquelch.h:88
bool m_isOpen
Definition: afsquelch.h:80
unsigned int m_samplesProcessed
Definition: afsquelch.h:71
unsigned int m_nTones
Definition: afsquelch.h:74
+ Here is the caller graph for this function:

◆ setCoefficients()

void AFSquelch::setCoefficients ( unsigned int  N,
unsigned int  nbAvg,
unsigned int  sampleRate,
unsigned int  samplesAttack,
unsigned int  samplesDecay,
const double *  tones 
)

center frequency of tones tested

Parameters
Nthe algorithm "block" size
nbAvgaveraging size
sampleRateinput signal sample rate
samplesAttacknumber of results before squelch opens
samplesDecaynumber of results keeping squelch open

Definition at line 71 of file afsquelch.cpp.

References cos(), m_attackCount, m_coef, m_decayCount, m_isOpen, m_k, m_maxPowerIndex, m_movingAverages, m_N, m_nbAvg, m_nTones, M_PI, m_power, m_sampleRate, m_samplesAttack, m_samplesAvgProcessed, m_samplesDecay, m_samplesProcessed, m_squelchCount, m_threshold, m_toneSet, m_u0, and m_u1.

Referenced by NFMDemod::applyAudioSampleRate(), and NFMDemod::NFMDemod().

78 {
79  m_N = N; // save the basic parameters for use during analysis
80  m_nbAvg = nbAvg;
81  m_sampleRate = sampleRate;
82  m_samplesAttack = samplesAttack;
83  m_samplesDecay = samplesDecay;
87  m_maxPowerIndex = 0;
88  m_attackCount = 0;
89  m_decayCount = 0;
90  m_squelchCount = 0;
91  m_isOpen = false;
92  m_threshold = 0.0;
93 
94  // for each of the frequencies (tones) of interest calculate
95  // k and the associated filter coefficient as per the Goertzel
96  // algorithm. Note: we are using a real value (as opposed to
97  // an integer as described in some references. k is retained
98  // for later display. The tone set is specified in the
99  // constructor. Notice that the resulting coefficients are
100  // independent of N.
101 
102  for (unsigned int j = 0; j < m_nTones; ++j)
103  {
104  m_toneSet[j] = tones[j] < ((double) m_sampleRate) * 0.4 ? tones[j] : ((double) m_sampleRate) * 0.4; // guarantee 80% Nyquist rate
105  m_k[j] = ((double)m_N * m_toneSet[j]) / (double)m_sampleRate;
106  m_coef[j] = 2.0 * cos((2.0 * M_PI * m_toneSet[j])/(double)m_sampleRate);
107  m_u0[j] = 0.0;
108  m_u1[j] = 0.0;
109  m_power[j] = 0.0;
110  m_movingAverages[j].fill(0.0);
111  }
112 }
double * m_k
Definition: afsquelch.h:82
unsigned int m_samplesAvgProcessed
Definition: afsquelch.h:72
#define M_PI
Definition: afsquelch.cpp:22
Fixed< IntType, IntBits > cos(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2271
double * m_u0
Definition: afsquelch.h:85
double * m_coef
Definition: afsquelch.h:83
double * m_u1
Definition: afsquelch.h:86
double * m_power
Definition: afsquelch.h:87
unsigned int m_N
Definition: afsquelch.h:69
unsigned int m_maxPowerIndex
Definition: afsquelch.h:73
unsigned int m_squelchCount
Definition: afsquelch.h:79
double m_threshold
Definition: afsquelch.h:81
std::vector< MovingAverage< double > > m_movingAverages
Definition: afsquelch.h:88
bool m_isOpen
Definition: afsquelch.h:80
unsigned int m_samplesProcessed
Definition: afsquelch.h:71
unsigned int m_samplesAttack
Definition: afsquelch.h:75
unsigned int m_samplesDecay
Definition: afsquelch.h:77
unsigned int m_nbAvg
number of power samples taken for moving average
Definition: afsquelch.h:68
unsigned int m_attackCount
Definition: afsquelch.h:76
unsigned int m_nTones
Definition: afsquelch.h:74
unsigned int m_decayCount
Definition: afsquelch.h:78
double * m_toneSet
Definition: afsquelch.h:84
unsigned int m_sampleRate
Definition: afsquelch.h:70
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setThreshold()

void AFSquelch::setThreshold ( double  _threshold)

Definition at line 271 of file afsquelch.cpp.

References m_threshold, and reset().

Referenced by NFMDemod::applySettings().

272 {
273  qDebug("AFSquelch::setThreshold: threshold: %f", threshold);
274  m_threshold = threshold;
275  reset();
276 }
double m_threshold
Definition: afsquelch.h:81
void reset()
Definition: afsquelch.cpp:172
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_attackCount

unsigned int AFSquelch::m_attackCount
private

Definition at line 76 of file afsquelch.h.

Referenced by setCoefficients().

◆ m_coef

double* AFSquelch::m_coef
private

Definition at line 83 of file afsquelch.h.

Referenced by AFSquelch(), feedback(), feedForward(), setCoefficients(), and ~AFSquelch().

◆ m_decayCount

unsigned int AFSquelch::m_decayCount
private

Definition at line 78 of file afsquelch.h.

Referenced by setCoefficients().

◆ m_isOpen

bool AFSquelch::m_isOpen
private

Definition at line 80 of file afsquelch.h.

Referenced by evaluate(), reset(), and setCoefficients().

◆ m_k

double* AFSquelch::m_k
private

Definition at line 82 of file afsquelch.h.

Referenced by AFSquelch(), setCoefficients(), and ~AFSquelch().

◆ m_maxPowerIndex

unsigned int AFSquelch::m_maxPowerIndex
private

Definition at line 73 of file afsquelch.h.

Referenced by reset(), and setCoefficients().

◆ m_movingAverages

std::vector<MovingAverage<double> > AFSquelch::m_movingAverages
private

Definition at line 88 of file afsquelch.h.

Referenced by AFSquelch(), evaluate(), feedForward(), reset(), and setCoefficients().

◆ m_N

unsigned int AFSquelch::m_N
private

Definition at line 69 of file afsquelch.h.

Referenced by AFSquelch(), analyze(), and setCoefficients().

◆ m_nbAvg

unsigned int AFSquelch::m_nbAvg
private

number of power samples taken for moving average

Definition at line 68 of file afsquelch.h.

Referenced by AFSquelch(), analyze(), and setCoefficients().

◆ m_nTones

unsigned int AFSquelch::m_nTones
private

Definition at line 74 of file afsquelch.h.

Referenced by AFSquelch(), evaluate(), feedback(), feedForward(), reset(), and setCoefficients().

◆ m_power

double* AFSquelch::m_power
private

Definition at line 87 of file afsquelch.h.

Referenced by AFSquelch(), feedForward(), reset(), setCoefficients(), and ~AFSquelch().

◆ m_sampleRate

unsigned int AFSquelch::m_sampleRate
private

Definition at line 70 of file afsquelch.h.

Referenced by AFSquelch(), and setCoefficients().

◆ m_samplesAttack

unsigned int AFSquelch::m_samplesAttack
private

Definition at line 75 of file afsquelch.h.

Referenced by evaluate(), and setCoefficients().

◆ m_samplesAvgProcessed

unsigned int AFSquelch::m_samplesAvgProcessed
private

Definition at line 72 of file afsquelch.h.

Referenced by analyze(), and setCoefficients().

◆ m_samplesDecay

unsigned int AFSquelch::m_samplesDecay
private

Definition at line 77 of file afsquelch.h.

Referenced by evaluate(), and setCoefficients().

◆ m_samplesProcessed

unsigned int AFSquelch::m_samplesProcessed
private

Definition at line 71 of file afsquelch.h.

Referenced by analyze(), reset(), and setCoefficients().

◆ m_squelchCount

unsigned int AFSquelch::m_squelchCount
private

Definition at line 79 of file afsquelch.h.

Referenced by evaluate(), and setCoefficients().

◆ m_threshold

double AFSquelch::m_threshold
private

Definition at line 81 of file afsquelch.h.

Referenced by evaluate(), setCoefficients(), and setThreshold().

◆ m_toneSet

double* AFSquelch::m_toneSet
private

Definition at line 84 of file afsquelch.h.

Referenced by AFSquelch(), setCoefficients(), and ~AFSquelch().

◆ m_u0

double* AFSquelch::m_u0
private

Definition at line 85 of file afsquelch.h.

Referenced by AFSquelch(), feedback(), feedForward(), reset(), setCoefficients(), and ~AFSquelch().

◆ m_u1

double* AFSquelch::m_u1
private

Definition at line 86 of file afsquelch.h.

Referenced by AFSquelch(), feedback(), feedForward(), reset(), setCoefficients(), and ~AFSquelch().


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