| 
									
										
										
										
											2016-10-17 08:58:49 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | // Copyright (C) 2016-2019 F4EXB                                                 //
 | 
					
						
							| 
									
										
										
										
											2016-10-17 08:58:49 +02:00
										 |  |  | // 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.                                           //
 | 
					
						
							| 
									
										
										
										
											2016-10-17 08:58:49 +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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | #include <dsp/downchannelizer.h>
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | #include "dsp/inthalfbandfilter.h"
 | 
					
						
							|  |  |  | #include "dsp/dspcommands.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  | #include "dsp/hbfilterchainconverter.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-21 08:54:28 +02:00
										 |  |  | #include <QString>
 | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2015-05-15 03:12:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | MESSAGE_CLASS_DEFINITION(DownChannelizer::MsgChannelizerNotification, Message) | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | MESSAGE_CLASS_DEFINITION(DownChannelizer::MsgSetChannelizer, Message) | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 22:29:04 +02:00
										 |  |  | DownChannelizer::DownChannelizer(BasebandSampleSink* sampleSink) : | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  |     m_filterChainSetMode(false), | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_sampleSink(sampleSink), | 
					
						
							| 
									
										
										
										
											2015-08-24 02:06:11 +02:00
										 |  |  | 	m_inputSampleRate(0), | 
					
						
							|  |  |  | 	m_requestedOutputSampleRate(0), | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_requestedCenterFrequency(0), | 
					
						
							| 
									
										
										
										
											2015-08-10 00:09:10 +02:00
										 |  |  | 	m_currentOutputSampleRate(0), | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_currentCenterFrequency(0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 	QString name = "DownChannelizer(" + m_sampleSink->objectName() + ")"; | 
					
						
							| 
									
										
										
										
											2015-08-21 08:54:28 +02:00
										 |  |  | 	setObjectName(name); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | DownChannelizer::~DownChannelizer() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	freeFilterChain(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | void DownChannelizer::configure(MessageQueue* messageQueue, int sampleRate, int centerFrequency) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 	Message* cmd = new DSPConfigureChannelizer(sampleRate, centerFrequency); | 
					
						
							|  |  |  | 	messageQueue->push(cmd); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  | void DownChannelizer::set(MessageQueue* messageQueue, unsigned int log2Decim, unsigned int filterChainHash) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Message* cmd = new MsgSetChannelizer(log2Decim, filterChainHash); | 
					
						
							|  |  |  |     messageQueue->push(cmd); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | void DownChannelizer::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-24 00:51:27 +02:00
										 |  |  | 	if(m_sampleSink == 0) { | 
					
						
							| 
									
										
										
										
											2014-06-15 09:32:25 +01:00
										 |  |  | 		m_sampleBuffer.clear(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 16:31:54 +02:00
										 |  |  | 	if (m_filterStages.size() == 0) // optimization when no downsampling is done anyway
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_sampleSink->feed(begin, end, positiveOnly); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2015-09-06 10:21:06 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2016-10-04 16:31:54 +02:00
										 |  |  | 		m_mutex.lock(); | 
					
						
							| 
									
										
										
										
											2015-09-06 10:21:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 16:31:54 +02:00
										 |  |  | 		for(SampleVector::const_iterator sample = begin; sample != end; ++sample) | 
					
						
							| 
									
										
										
										
											2015-09-06 10:21:06 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2016-10-04 16:31:54 +02:00
										 |  |  | 			Sample s(*sample); | 
					
						
							|  |  |  | 			FilterStages::iterator stage = m_filterStages.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for (; stage != m_filterStages.end(); ++stage) | 
					
						
							| 
									
										
										
										
											2015-09-06 10:21:06 +02:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-05-03 00:23:38 +02:00
										 |  |  | #ifndef SDR_RX_SAMPLE_24BIT
 | 
					
						
							|  |  |  |                 s.m_real /= 2; // avoid saturation on 16 bit samples
 | 
					
						
							|  |  |  |                 s.m_imag /= 2; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-04 16:31:54 +02:00
										 |  |  | 				if(!(*stage)->work(&s)) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-09-06 10:21:06 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 16:31:54 +02:00
										 |  |  | 			if(stage == m_filterStages.end()) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-05-03 00:23:38 +02:00
										 |  |  | #ifdef SDR_RX_SAMPLE_24BIT
 | 
					
						
							|  |  |  | 			    s.m_real /= (1<<(m_filterStages.size())); // on 32 bit samples there is enough headroom to just divide the final result
 | 
					
						
							| 
									
										
										
										
											2017-05-12 02:41:51 +02:00
										 |  |  | 			    s.m_imag /= (1<<(m_filterStages.size())); | 
					
						
							| 
									
										
										
										
											2019-05-03 00:23:38 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-04 16:31:54 +02:00
										 |  |  | 				m_sampleBuffer.push_back(s); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-09-06 10:21:06 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 16:31:54 +02:00
										 |  |  | 		m_mutex.unlock(); | 
					
						
							| 
									
										
										
										
											2015-09-06 10:21:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 16:31:54 +02:00
										 |  |  | 		m_sampleSink->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), positiveOnly); | 
					
						
							|  |  |  | 		m_sampleBuffer.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | void DownChannelizer::start() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-24 02:06:11 +02:00
										 |  |  | 	if (m_sampleSink != 0) | 
					
						
							| 
									
										
										
										
											2015-08-13 01:14:21 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 		qDebug() << "DownChannelizer::start: thread: " << thread() | 
					
						
							| 
									
										
										
										
											2015-08-24 00:51:27 +02:00
										 |  |  | 				<< " m_inputSampleRate: " << m_inputSampleRate | 
					
						
							|  |  |  | 				<< " m_requestedOutputSampleRate: " << m_requestedOutputSampleRate | 
					
						
							|  |  |  | 				<< " m_requestedCenterFrequency: " << m_requestedCenterFrequency; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		m_sampleSink->start(); | 
					
						
							| 
									
										
										
										
											2015-08-13 01:14:21 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | void DownChannelizer::stop() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-24 00:51:27 +02:00
										 |  |  | 	if(m_sampleSink != 0) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		m_sampleSink->stop(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | bool DownChannelizer::handleMessage(const Message& cmd) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-07 01:15:55 +02:00
										 |  |  | 	// TODO: apply changes only if input sample rate or requested output sample rate change. Change of center frequency has no impact.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if (DSPSignalNotification::match(cmd)) | 
					
						
							| 
									
										
										
										
											2015-08-10 00:09:10 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		DSPSignalNotification& notif = (DSPSignalNotification&) cmd; | 
					
						
							|  |  |  | 		m_inputSampleRate = notif.getSampleRate(); | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 		qDebug() << "DownChannelizer::handleMessage: DSPSignalNotification: m_inputSampleRate: " << m_inputSampleRate; | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!m_filterChainSetMode) { | 
					
						
							|  |  |  | 		    applyConfiguration(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-24 00:51:27 +02:00
										 |  |  | 		if (m_sampleSink != 0) | 
					
						
							| 
									
										
										
										
											2015-08-10 00:09:10 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-12-29 05:48:54 +01:00
										 |  |  | 		    DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy
 | 
					
						
							|  |  |  | 			m_sampleSink->getInputMessageQueue()->push(rep); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-08-10 00:09:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-12 09:32:54 +02:00
										 |  |  | 		emit inputSampleRateChanged(); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2015-08-10 00:09:10 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:52 +02:00
										 |  |  | 	else if (DSPConfigureChannelizer::match(cmd)) | 
					
						
							| 
									
										
										
										
											2015-08-10 00:09:10 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		DSPConfigureChannelizer& chan = (DSPConfigureChannelizer&) cmd; | 
					
						
							|  |  |  | 		m_requestedOutputSampleRate = chan.getSampleRate(); | 
					
						
							|  |  |  | 		m_requestedCenterFrequency = chan.getCenterFrequency(); | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 22:00:24 +02:00
										 |  |  |             // qDebug() << "DownChannelizer::handleMessage: DSPConfigureChannelizer:"
 | 
					
						
							|  |  |  |             // 		<< " m_requestedOutputSampleRate: " << m_requestedOutputSampleRate
 | 
					
						
							|  |  |  |             // 		<< " m_requestedCenterFrequency: " << m_requestedCenterFrequency;
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		applyConfiguration(); | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2015-08-10 00:09:10 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  |     else if (MsgSetChannelizer::match(cmd)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         MsgSetChannelizer& chan = (MsgSetChannelizer&) cmd; | 
					
						
							|  |  |  |         qDebug() << "DownChannelizer::handleMessage: MsgSetChannelizer"; | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  |         applySetting(chan.getLog2Decim(), chan.getFilterChainHash()); | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     else if (BasebandSampleSink::MsgThreadedSink::match(cmd)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-29 10:03:52 +02:00
										 |  |  |         qDebug() << "DownChannelizer::handleMessage: MsgThreadedSink: forwarded to demod"; | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |         return m_sampleSink->handleMessage(cmd); // this message is passed to the demod
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-10 00:09:10 +02:00
										 |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-03-29 10:03:52 +02:00
										 |  |  |         qDebug() << "DownChannelizer::handleMessage: " << cmd.getIdentifier() << " unhandled"; | 
					
						
							| 
									
										
										
										
											2017-12-29 05:48:54 +01:00
										 |  |  | 	    return false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | void DownChannelizer::applyConfiguration() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  |     m_filterChainSetMode = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-24 02:06:11 +02:00
										 |  |  | 	if (m_inputSampleRate == 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 		qDebug() << "DownChannelizer::applyConfiguration: m_inputSampleRate=0 aborting"; | 
					
						
							| 
									
										
										
										
											2015-08-24 02:06:11 +02:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-06 10:21:06 +02:00
										 |  |  | 	m_mutex.lock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	freeFilterChain(); | 
					
						
							| 
									
										
										
										
											2015-08-24 02:06:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_currentCenterFrequency = createFilterChain( | 
					
						
							|  |  |  | 		m_inputSampleRate / -2, m_inputSampleRate / 2, | 
					
						
							|  |  |  | 		m_requestedCenterFrequency - m_requestedOutputSampleRate / 2, m_requestedCenterFrequency + m_requestedOutputSampleRate / 2); | 
					
						
							| 
									
										
										
										
											2015-08-24 02:06:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-06 10:21:06 +02:00
										 |  |  | 	m_mutex.unlock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 02:41:51 +02:00
										 |  |  | 	//debugFilterChain();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_currentOutputSampleRate = m_inputSampleRate / (1 << m_filterStages.size()); | 
					
						
							| 
									
										
										
										
											2015-07-12 09:32:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 	qDebug() << "DownChannelizer::applyConfiguration in=" << m_inputSampleRate | 
					
						
							| 
									
										
										
										
											2015-05-15 11:29:41 +02:00
										 |  |  | 			<< ", req=" << m_requestedOutputSampleRate | 
					
						
							|  |  |  | 			<< ", out=" << m_currentOutputSampleRate | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  | 			<< ", fc=" << m_currentCenterFrequency; | 
					
						
							| 
									
										
										
										
											2015-08-24 00:51:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (m_sampleSink != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-12-29 05:14:40 +01:00
										 |  |  | 		MsgChannelizerNotification *notif = MsgChannelizerNotification::create(m_currentOutputSampleRate, m_currentCenterFrequency); | 
					
						
							|  |  |  | 		m_sampleSink->getInputMessageQueue()->push(notif); | 
					
						
							| 
									
										
										
										
											2015-08-24 00:51:27 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  | void DownChannelizer::applySetting(unsigned int log2Decim, unsigned int filterChainHash) | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  |     m_filterChainSetMode = true; | 
					
						
							|  |  |  |     std::vector<unsigned int> stageIndexes; | 
					
						
							|  |  |  |     m_currentCenterFrequency = m_inputSampleRate * HBFilterChainConverter::convertToIndexes(log2Decim, filterChainHash, stageIndexes); | 
					
						
							|  |  |  |     m_requestedCenterFrequency = m_currentCenterFrequency; | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  |     m_mutex.lock(); | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  |     freeFilterChain(); | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  |     setFilterChain(stageIndexes); | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  |     m_mutex.unlock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_currentOutputSampleRate = m_inputSampleRate / (1 << m_filterStages.size()); | 
					
						
							|  |  |  |     m_requestedOutputSampleRate = m_currentOutputSampleRate; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  | 	qDebug() << "DownChannelizer::applySetting inputSampleRate:" << m_inputSampleRate | 
					
						
							|  |  |  | 			<< " currentOutputSampleRate: " << m_currentOutputSampleRate | 
					
						
							|  |  |  | 			<< " currentCenterFrequency: " << m_currentCenterFrequency | 
					
						
							|  |  |  |             << " nb_filters: " << stageIndexes.size() | 
					
						
							|  |  |  |             << " nb_stages: " << m_filterStages.size(); | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (m_sampleSink != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		MsgChannelizerNotification *notif = MsgChannelizerNotification::create(m_currentOutputSampleRate, m_currentCenterFrequency); | 
					
						
							|  |  |  | 		m_sampleSink->getInputMessageQueue()->push(notif); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-22 08:46:05 +01:00
										 |  |  | #ifdef SDR_RX_SAMPLE_24BIT
 | 
					
						
							| 
									
										
										
										
											2018-01-21 21:48:36 +01:00
										 |  |  | DownChannelizer::FilterStage::FilterStage(Mode mode) : | 
					
						
							| 
									
										
										
										
											2018-05-08 01:35:08 +02:00
										 |  |  |     m_filter(new IntHalfbandFilterEO<qint64, qint64, DOWNCHANNELIZER_HB_FILTER_ORDER>), | 
					
						
							| 
									
										
										
										
											2018-01-21 21:48:36 +01:00
										 |  |  |     m_workFunction(0), | 
					
						
							|  |  |  |     m_mode(mode), | 
					
						
							| 
									
										
										
										
											2018-05-01 22:02:30 +02:00
										 |  |  |     m_sse(true) | 
					
						
							| 
									
										
										
										
											2018-01-21 21:48:36 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     switch(mode) { | 
					
						
							|  |  |  |         case ModeCenter: | 
					
						
							| 
									
										
										
										
											2018-05-08 01:35:08 +02:00
										 |  |  |             m_workFunction = &IntHalfbandFilterEO<qint64, qint64, DOWNCHANNELIZER_HB_FILTER_ORDER>::workDecimateCenter; | 
					
						
							| 
									
										
										
										
											2018-01-21 21:48:36 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case ModeLowerHalf: | 
					
						
							| 
									
										
										
										
											2018-05-08 01:35:08 +02:00
										 |  |  |             m_workFunction = &IntHalfbandFilterEO<qint64, qint64, DOWNCHANNELIZER_HB_FILTER_ORDER>::workDecimateLowerHalf; | 
					
						
							| 
									
										
										
										
											2018-01-21 21:48:36 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case ModeUpperHalf: | 
					
						
							| 
									
										
										
										
											2018-05-08 01:35:08 +02:00
										 |  |  |             m_workFunction = &IntHalfbandFilterEO<qint64, qint64, DOWNCHANNELIZER_HB_FILTER_ORDER>::workDecimateUpperHalf; | 
					
						
							| 
									
										
										
										
											2018-01-21 21:48:36 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2016-11-07 18:51:32 +01:00
										 |  |  | DownChannelizer::FilterStage::FilterStage(Mode mode) : | 
					
						
							| 
									
										
										
										
											2018-05-08 01:35:08 +02:00
										 |  |  |     m_filter(new IntHalfbandFilterEO<qint32, qint32, DOWNCHANNELIZER_HB_FILTER_ORDER>), | 
					
						
							| 
									
										
										
										
											2018-05-01 22:02:30 +02:00
										 |  |  |     m_workFunction(0), | 
					
						
							|  |  |  |     m_mode(mode), | 
					
						
							|  |  |  |     m_sse(true) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-01 22:02:30 +02:00
										 |  |  |     switch(mode) { | 
					
						
							|  |  |  |         case ModeCenter: | 
					
						
							| 
									
										
										
										
											2018-05-08 01:35:08 +02:00
										 |  |  |             m_workFunction = &IntHalfbandFilterEO<qint32, qint32, DOWNCHANNELIZER_HB_FILTER_ORDER>::workDecimateCenter; | 
					
						
							| 
									
										
										
										
											2018-05-01 22:02:30 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case ModeLowerHalf: | 
					
						
							| 
									
										
										
										
											2018-05-08 01:35:08 +02:00
										 |  |  |             m_workFunction = &IntHalfbandFilterEO<qint32, qint32, DOWNCHANNELIZER_HB_FILTER_ORDER>::workDecimateLowerHalf; | 
					
						
							| 
									
										
										
										
											2018-05-01 22:02:30 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case ModeUpperHalf: | 
					
						
							| 
									
										
										
										
											2018-05-08 01:35:08 +02:00
										 |  |  |             m_workFunction = &IntHalfbandFilterEO<qint32, qint32, DOWNCHANNELIZER_HB_FILTER_ORDER>::workDecimateUpperHalf; | 
					
						
							| 
									
										
										
										
											2018-05-01 22:02:30 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-11-07 18:51:32 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-05-01 22:02:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | DownChannelizer::FilterStage::~FilterStage() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	delete m_filter; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | bool DownChannelizer::signalContainsChannel(Real sigStart, Real sigEnd, Real chanStart, Real chanEnd) const | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	//qDebug("   testing signal [%f, %f], channel [%f, %f]", sigStart, sigEnd, chanStart, chanEnd);
 | 
					
						
							|  |  |  | 	if(sigEnd <= sigStart) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	if(chanEnd <= chanStart) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	return (sigStart <= chanStart) && (sigEnd >= chanEnd); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | Real DownChannelizer::createFilterChain(Real sigStart, Real sigEnd, Real chanStart, Real chanEnd) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	Real sigBw = sigEnd - sigStart; | 
					
						
							|  |  |  | 	Real rot = sigBw / 4; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 22:00:24 +02:00
										 |  |  | 	//qDebug("DownChannelizer::createFilterChain: Signal [%.1f, %.1f] (BW %.1f), Channel [%.1f, %.1f], Rot %.1f", sigStart, sigEnd, sigBw, chanStart, chanEnd, rot);
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// check if it fits into the left half
 | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | 	if(signalContainsChannel(sigStart, sigStart + sigBw / 2.0, chanStart, chanEnd)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-04 22:00:24 +02:00
										 |  |  | 		//qDebug("DownChannelizer::createFilterChain: -> take left half (rotate by +1/4 and decimate by 2)");
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		m_filterStages.push_back(new FilterStage(FilterStage::ModeLowerHalf)); | 
					
						
							|  |  |  | 		return createFilterChain(sigStart, sigStart + sigBw / 2.0, chanStart, chanEnd); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check if it fits into the right half
 | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | 	if(signalContainsChannel(sigEnd - sigBw / 2.0f, sigEnd, chanStart, chanEnd)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-04 22:00:24 +02:00
										 |  |  | 		//qDebug("DownChannelizer::createFilterChain: -> take right half (rotate by -1/4 and decimate by 2)");
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		m_filterStages.push_back(new FilterStage(FilterStage::ModeUpperHalf)); | 
					
						
							|  |  |  | 		return createFilterChain(sigEnd - sigBw / 2.0f, sigEnd, chanStart, chanEnd); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check if it fits into the center
 | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | 	if(signalContainsChannel(sigStart + rot, sigEnd - rot, chanStart, chanEnd)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-04 22:00:24 +02:00
										 |  |  | 		//qDebug("DownChannelizer::createFilterChain: -> take center half (decimate by 2)");
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		m_filterStages.push_back(new FilterStage(FilterStage::ModeCenter)); | 
					
						
							| 
									
										
										
										
											2015-05-15 03:12:27 +02:00
										 |  |  | 		return createFilterChain(sigStart + rot, sigEnd - rot, chanStart, chanEnd); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	Real ofs = ((chanEnd - chanStart) / 2.0 + chanStart) - ((sigEnd - sigStart) / 2.0 + sigStart); | 
					
						
							| 
									
										
										
										
											2019-05-04 22:00:24 +02:00
										 |  |  | 	//qDebug("DownChannelizer::createFilterChain: -> complete (final BW %.1f, frequency offset %.1f)", sigBw, ofs);
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	return ofs; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  | void DownChannelizer::setFilterChain(const std::vector<unsigned int>& stageIndexes) | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // filters are described from lower to upper level but the chain is constructed the other way round
 | 
					
						
							|  |  |  |     std::vector<unsigned int>::const_reverse_iterator rit = stageIndexes.rbegin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Each index is a base 3 number with 0 = low, 1 = center, 2 = high
 | 
					
						
							|  |  |  |     // Functions at upper level will convert a number to base 3 to describe the filter chain. Common converting
 | 
					
						
							|  |  |  |     // algorithms will go from LSD to MSD. This explains the reverse order.
 | 
					
						
							|  |  |  |     for (; rit != stageIndexes.rend(); ++rit) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  |         if (*rit == 0) { | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  |             m_filterStages.push_back(new FilterStage(FilterStage::ModeLowerHalf)); | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  |         } else if (*rit == 1) { | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  |             m_filterStages.push_back(new FilterStage(FilterStage::ModeCenter)); | 
					
						
							| 
									
										
										
										
											2019-04-26 01:27:36 +02:00
										 |  |  |         } else if (*rit == 2) { | 
					
						
							| 
									
										
										
										
											2019-04-25 09:14:17 +02:00
										 |  |  |             m_filterStages.push_back(new FilterStage(FilterStage::ModeUpperHalf)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 21:52:39 +02:00
										 |  |  | void DownChannelizer::freeFilterChain() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	for(FilterStages::iterator it = m_filterStages.begin(); it != m_filterStages.end(); ++it) | 
					
						
							|  |  |  | 		delete *it; | 
					
						
							|  |  |  | 	m_filterStages.clear(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-05-12 02:41:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void DownChannelizer::debugFilterChain() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-02 03:06:04 +01:00
										 |  |  |     qDebug("DownChannelizer::debugFilterChain: %lu stages", m_filterStages.size()); | 
					
						
							| 
									
										
										
										
											2017-05-12 02:41:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for(FilterStages::iterator it = m_filterStages.begin(); it != m_filterStages.end(); ++it) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         switch ((*it)->m_mode) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         case FilterStage::ModeCenter: | 
					
						
							|  |  |  |             qDebug("DownChannelizer::debugFilterChain: center %s", (*it)->m_sse ? "sse" : "no_sse"); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case FilterStage::ModeLowerHalf: | 
					
						
							|  |  |  |             qDebug("DownChannelizer::debugFilterChain: lower %s", (*it)->m_sse ? "sse" : "no_sse"); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case FilterStage::ModeUpperHalf: | 
					
						
							|  |  |  |             qDebug("DownChannelizer::debugFilterChain: upper %s", (*it)->m_sse ? "sse" : "no_sse"); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             qDebug("DownChannelizer::debugFilterChain: none %s", (*it)->m_sse ? "sse" : "no_sse"); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |