| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2015 F4EXB                                                      //
 | 
					
						
							|  |  |  | // written by Edouard Griffiths                                                  //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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.                                           //
 | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef INCLUDE_DSPDEVICEENGINE_H
 | 
					
						
							|  |  |  | #define INCLUDE_DSPDEVICEENGINE_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QThread>
 | 
					
						
							|  |  |  | #include <QTimer>
 | 
					
						
							|  |  |  | #include <QMutex>
 | 
					
						
							|  |  |  | #include <QWaitCondition>
 | 
					
						
							|  |  |  | #include "dsp/dsptypes.h"
 | 
					
						
							|  |  |  | #include "dsp/fftwindow.h"
 | 
					
						
							|  |  |  | #include "util/messagequeue.h"
 | 
					
						
							|  |  |  | #include "util/syncmessenger.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-20 13:49:21 +01:00
										 |  |  | #include "export.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-01 02:45:55 +01:00
										 |  |  | #include "util/movingaverage.h"
 | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 23:16:40 +02:00
										 |  |  | class DeviceSampleSource; | 
					
						
							| 
									
										
										
										
											2016-10-02 22:29:04 +02:00
										 |  |  | class BasebandSampleSink; | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-03 20:23:38 +01:00
										 |  |  | class SDRBASE_API DSPDeviceSourceEngine : public QThread { | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 	Q_OBJECT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	enum State { | 
					
						
							|  |  |  | 		StNotStarted,  //!< engine is before initialization
 | 
					
						
							|  |  |  | 		StIdle,        //!< engine is idle
 | 
					
						
							|  |  |  | 		StReady,       //!< engine is ready to run
 | 
					
						
							|  |  |  | 		StRunning,     //!< engine is running
 | 
					
						
							|  |  |  | 		StError        //!< engine is in error
 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 18:29:05 +02:00
										 |  |  | 	DSPDeviceSourceEngine(uint uid, QObject* parent = NULL); | 
					
						
							|  |  |  | 	~DSPDeviceSourceEngine(); | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-11 19:14:23 +02:00
										 |  |  | 	uint getUID() const { return m_uid; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 	MessageQueue* getInputMessageQueue() { return &m_inputMessageQueue; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void start(); //!< This thread start
 | 
					
						
							|  |  |  | 	void stop();  //!< This thread stop
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool initAcquisition(); //!< Initialize acquisition sequence
 | 
					
						
							|  |  |  | 	bool startAcquisition(); //!< Start acquisition sequence
 | 
					
						
							|  |  |  | 	void stopAcquistion();   //!< Stop acquisition sequence
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 23:16:40 +02:00
										 |  |  | 	void setSource(DeviceSampleSource* source); //!< Set the sample source type
 | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 	void setSourceSequence(int sequence); //!< Set the sample source sequence in type
 | 
					
						
							| 
									
										
										
										
											2017-09-05 13:57:49 +02:00
										 |  |  | 	DeviceSampleSource *getSource() { return m_deviceSampleSource; } | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 22:29:04 +02:00
										 |  |  | 	void addSink(BasebandSampleSink* sink); //!< Add a sample sink
 | 
					
						
							|  |  |  | 	void removeSink(BasebandSampleSink* sink); //!< Remove a sample sink
 | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void configureCorrections(bool dcOffsetCorrection, bool iqImbalanceCorrection); //!< Configure DSP corrections
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	State state() const { return m_state; } //!< Return DSP engine current state
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QString errorMessage(); //!< Return the current error message
 | 
					
						
							|  |  |  | 	QString sourceDeviceDescription(); //!< Return the source device description
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2016-05-11 19:14:23 +02:00
										 |  |  | 	uint m_uid; //!< unique ID
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 	MessageQueue m_inputMessageQueue;  //<! Input message queue. Post here.
 | 
					
						
							|  |  |  | 	SyncMessenger m_syncMessenger;     //!< Used to process messages synchronously with the thread
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	State m_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QString m_errorMessage; | 
					
						
							|  |  |  | 	QString m_deviceDescription; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 01:49:28 +02:00
										 |  |  | 	DeviceSampleSource* m_deviceSampleSource; | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 	int m_sampleSourceSequence; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 01:49:28 +02:00
										 |  |  | 	typedef std::list<BasebandSampleSink*> BasebandSampleSinks; | 
					
						
							|  |  |  | 	BasebandSampleSinks m_basebandSampleSinks; //!< sample sinks within main thread (usually spectrum, file output)
 | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	uint m_sampleRate; | 
					
						
							|  |  |  | 	quint64 m_centerFrequency; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool m_dcOffsetCorrection; | 
					
						
							|  |  |  | 	bool m_iqImbalanceCorrection; | 
					
						
							|  |  |  | 	double m_iOffset, m_qOffset; | 
					
						
							| 
									
										
										
										
											2018-02-04 18:20:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-01 02:45:55 +01:00
										 |  |  | 	MovingAverageUtil<int32_t, int64_t, 1024> m_iBeta; | 
					
						
							|  |  |  |     MovingAverageUtil<int32_t, int64_t, 1024> m_qBeta; | 
					
						
							| 
									
										
										
										
											2018-02-04 18:20:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if IMBALANCE_INT
 | 
					
						
							|  |  |  |     // Fixed point DC + IQ corrections
 | 
					
						
							|  |  |  |     MovingAverageUtil<int64_t, int64_t, 128> m_avgII; | 
					
						
							|  |  |  |     MovingAverageUtil<int64_t, int64_t, 128> m_avgIQ; | 
					
						
							|  |  |  |     MovingAverageUtil<int64_t, int64_t, 128> m_avgPhi; | 
					
						
							|  |  |  |     MovingAverageUtil<int64_t, int64_t, 128> m_avgII2; | 
					
						
							|  |  |  |     MovingAverageUtil<int64_t, int64_t, 128> m_avgQQ2; | 
					
						
							|  |  |  |     MovingAverageUtil<int64_t, int64_t, 128> m_avgAmp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     // Floating point DC + IQ corrections
 | 
					
						
							| 
									
										
										
										
											2018-02-04 10:49:13 +01:00
										 |  |  |     MovingAverageUtil<float, double, 128> m_avgII; | 
					
						
							|  |  |  |     MovingAverageUtil<float, double, 128> m_avgIQ; | 
					
						
							|  |  |  |     MovingAverageUtil<float, double, 128> m_avgII2; | 
					
						
							|  |  |  |     MovingAverageUtil<float, double, 128> m_avgQQ2; | 
					
						
							|  |  |  |     MovingAverageUtil<double, double, 128> m_avgPhi; | 
					
						
							|  |  |  |     MovingAverageUtil<double, double, 128> m_avgAmp; | 
					
						
							| 
									
										
										
										
											2018-02-04 18:20:16 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     qint32 m_iRange; | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 	qint32 m_qRange; | 
					
						
							|  |  |  | 	qint32 m_imbalance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void run(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-01 02:45:55 +01:00
										 |  |  | 	void iqCorrections(SampleVector::iterator begin, SampleVector::iterator end, bool imbalanceCorrection); | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 	void dcOffset(SampleVector::iterator begin, SampleVector::iterator end); | 
					
						
							|  |  |  | 	void imbalance(SampleVector::iterator begin, SampleVector::iterator end); | 
					
						
							|  |  |  | 	void work(); //!< transfer samples from source to sinks if in running state
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	State gotoIdle();     //!< Go to the idle state
 | 
					
						
							|  |  |  | 	State gotoInit();     //!< Go to the acquisition init state from idle
 | 
					
						
							|  |  |  | 	State gotoRunning();  //!< Go to the running state from ready state
 | 
					
						
							|  |  |  | 	State gotoError(const QString& errorMsg); //!< Go to an error state
 | 
					
						
							| 
									
										
										
										
											2022-08-27 10:18:17 +01:00
										 |  |  | 	void setState(State state); | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 23:16:40 +02:00
										 |  |  | 	void handleSetSource(DeviceSampleSource* source); //!< Manage source setting
 | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private slots: | 
					
						
							|  |  |  | 	void handleData(); //!< Handle data when samples from source FIFO are ready to be processed
 | 
					
						
							|  |  |  | 	void handleInputMessages(); //!< Handle input message queue
 | 
					
						
							|  |  |  | 	void handleSynchronousMessages(); //!< Handle synchronous messages with the thread
 | 
					
						
							| 
									
										
										
										
											2022-08-27 10:18:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | signals: | 
					
						
							|  |  |  | 	void stateChanged(); | 
					
						
							| 
									
										
										
										
											2015-10-15 04:05:19 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // INCLUDE_DSPDEVICEENGINE_H
 |