| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2018 F4EXB                                                      //
 | 
					
						
							|  |  |  | // written by Edouard Griffiths                                                  //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // See: http://liquidsdr.org/blog/pll-howto/                                     //
 | 
					
						
							|  |  |  | // Fixed filter registers saturation                                             //
 | 
					
						
							|  |  |  | // Added order for PSK locking. This brilliant idea actually comes from this     //
 | 
					
						
							|  |  |  | // post: https://www.dsprelated.com/showthread/comp.dsp/36356-1.php              //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:32:15 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "freqlockcomplex.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-03 13:52:12 +01:00
										 |  |  | #include <cmath>
 | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | FreqLockComplex::FreqLockComplex() : | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  |     m_a0(0.998), | 
					
						
							|  |  |  |     m_a1(0.002), | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  |     m_y(1.0, 0.0), | 
					
						
							|  |  |  |     m_yRe(1.0), | 
					
						
							|  |  |  |     m_yIm(0.0), | 
					
						
							|  |  |  |     m_freq(0.0), | 
					
						
							|  |  |  |     m_phi(0.0), | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  |     m_phiX0(0.0), | 
					
						
							|  |  |  |     m_phiX1(0.0), | 
					
						
							|  |  |  |     m_y1(0.0f) | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FreqLockComplex::~FreqLockComplex() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FreqLockComplex::reset() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_y.real(1.0); | 
					
						
							|  |  |  |     m_y.imag(0.0); | 
					
						
							|  |  |  |     m_yRe = 1.0f; | 
					
						
							|  |  |  |     m_yIm = 0.0f; | 
					
						
							|  |  |  |     m_freq = 0.0f; | 
					
						
							|  |  |  |     m_phi = 0.0f; | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  |     m_phiX0 = 0.0f; | 
					
						
							|  |  |  |     m_phiX1 = 0.0f; | 
					
						
							|  |  |  |     m_y1 = 0.0f; | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  | void FreqLockComplex::setSampleRate(unsigned int sampleRate) | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  |     m_a1 = 10.0f / sampleRate; // 1 - alpha
 | 
					
						
							|  |  |  |     m_a0 = 1.0f - m_a1; // alpha
 | 
					
						
							|  |  |  |     reset(); | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FreqLockComplex::feed(float re, float im) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_yRe = cos(m_phi); | 
					
						
							|  |  |  |     m_yIm = sin(m_phi); | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  |     m_y.real(m_yRe); | 
					
						
							|  |  |  |     m_y.imag(m_yIm); | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  |     std::complex<float> x(re, im); | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  |     m_phiX0 = std::arg(x); | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  |     float eF = normalizeAngle(m_phiX0 - m_phiX1); | 
					
						
							|  |  |  |     float fHat = m_a1*eF + m_a0*m_y1; | 
					
						
							|  |  |  |     m_y1 = fHat; | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  |     m_freq = fHat; // correct instantaneous frequency
 | 
					
						
							|  |  |  |     m_phi += fHat; // advance phase with instantaneous frequency
 | 
					
						
							|  |  |  |     m_phiX1 = m_phiX0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-18 02:36:29 +02:00
										 |  |  | float FreqLockComplex::normalizeAngle(float angle) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     while (angle <= -M_PI) { | 
					
						
							|  |  |  |         angle += 2.0*M_PI; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     while (angle > M_PI) { | 
					
						
							|  |  |  |         angle -= 2.0*M_PI; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return angle; | 
					
						
							| 
									
										
										
										
											2018-05-17 01:05:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 |