| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  | //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2015-2020 Edouard Griffiths, F4EXB                                                         //
 | 
					
						
							|  |  |  | //                                                                                                          //
 | 
					
						
							|  |  |  | // See: http://www.embedded.com/design/connectivity/4025660/Detecting-CTCSS-tones-with-Goertzel-s-algorithm //
 | 
					
						
							|  |  |  | //                                                                                                          //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify                                     //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by                                     //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                                             //
 | 
					
						
							|  |  |  | // (at your option) any later version.                                                                      //
 | 
					
						
							|  |  |  | //                                                                                                          //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,                                          //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                                           //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                                             //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                                                          //
 | 
					
						
							|  |  |  | //                                                                                                          //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License                                        //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.                                     //
 | 
					
						
							|  |  |  | //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifndef INCLUDE_GPL_DSP_CTCSSDETECTOR_H_
 | 
					
						
							|  |  |  | #define INCLUDE_GPL_DSP_CTCSSDETECTOR_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "dsp/dsptypes.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-20 13:49:21 +01:00
										 |  |  | #include "export.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  | #include "ctcssfrequencies.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** CTCSSDetector: Continuous Tone Coded Squelch System
 | 
					
						
							|  |  |  |  * tone detector class based on the Modified Goertzel | 
					
						
							|  |  |  |  * algorithm. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-03-03 20:23:38 +01:00
										 |  |  | class SDRBASE_API CTCSSDetector { | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     CTCSSDetector(); | 
					
						
							|  |  |  |     virtual ~CTCSSDetector(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // setup the basic parameters and coefficients
 | 
					
						
							|  |  |  |     void setCoefficients( | 
					
						
							| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  |     		int N,          // the algorithm "block"  size
 | 
					
						
							|  |  |  | 			int sampleRate  // input signal sample rate
 | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // set the detection threshold
 | 
					
						
							|  |  |  |     void setThreshold(double thold); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  |     // analyze a sample set and optionally filter the tone frequencies.
 | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  |     bool analyze(Real *sample); // input signal sample
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // get the number of defined tones.
 | 
					
						
							|  |  |  |     int getNTones() const { | 
					
						
							| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  |     	return CTCSSFrequencies::m_nbFreqs; | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // get the tone set
 | 
					
						
							| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  |     const Real *getToneSet() const { | 
					
						
							|  |  |  |     	return CTCSSFrequencies::m_Freqs; | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // get the currently detected tone, if any
 | 
					
						
							|  |  |  |     bool getDetectedTone(int &maxTone) const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  |     	maxTone = m_maxPowerIndex; | 
					
						
							|  |  |  |     	return m_toneDetected; | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  |     // Get the max m_power at the detected tone.
 | 
					
						
							|  |  |  |     Real getMaxPower() const { | 
					
						
							|  |  |  |     	return m_maxPower; | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  |     void reset(); // reset the analysis algorithm
 | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     // Override these to change behavior of the detector
 | 
					
						
							|  |  |  |     virtual void initializePower(); | 
					
						
							|  |  |  |     virtual void evaluatePower(); | 
					
						
							|  |  |  |     void feedback(Real sample); | 
					
						
							|  |  |  |     void feedForward(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-11-01 08:23:50 +01:00
										 |  |  |     int m_N; | 
					
						
							|  |  |  |     int m_sampleRate; | 
					
						
							|  |  |  |     int m_samplesProcessed; | 
					
						
							|  |  |  |     int m_maxPowerIndex; | 
					
						
							|  |  |  |     bool m_toneDetected; | 
					
						
							|  |  |  |     Real m_maxPower; | 
					
						
							|  |  |  |     Real *m_k; | 
					
						
							|  |  |  |     Real *m_coef; | 
					
						
							|  |  |  |     Real *m_u0; | 
					
						
							|  |  |  |     Real *m_u1; | 
					
						
							|  |  |  |     Real *m_power; | 
					
						
							| 
									
										
										
										
											2015-06-16 04:42:37 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* INCLUDE_GPL_DSP_CTCSSDETECTOR_H_ */
 |