diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index 359a6cfc4..177827d6b 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -64,8 +64,10 @@ set(sdrbase_SOURCES dsp/agc.cpp dsp/downchannelizer.cpp dsp/upchannelizer.cpp + dsp/upsamplechannelizer.cpp dsp/channelmarker.cpp dsp/ctcssdetector.cpp + dsp/channelsamplesource.cpp dsp/cwkeyer.cpp dsp/cwkeyersettings.cpp dsp/decimatorsif.cpp @@ -179,7 +181,9 @@ set(sdrbase_HEADERS dsp/autocorrector.h dsp/downchannelizer.h dsp/upchannelizer.h + dsp/upsamplechannelizer.h dsp/channelmarker.h + dsp/channelsamplesource.h dsp/complex.h dsp/cwkeyer.h dsp/cwkeyersettings.h diff --git a/sdrbase/dsp/channelsamplesource.cpp b/sdrbase/dsp/channelsamplesource.cpp new file mode 100644 index 000000000..d06b9789f --- /dev/null +++ b/sdrbase/dsp/channelsamplesource.cpp @@ -0,0 +1,25 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2019 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 // +// (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 . // +/////////////////////////////////////////////////////////////////////////////////// + +#include "channelsamplesource.h" + +ChannelSampleSource::ChannelSampleSource() +{} + +ChannelSampleSource::~ChannelSampleSource() +{} \ No newline at end of file diff --git a/sdrbase/dsp/channelsamplesource.h b/sdrbase/dsp/channelsamplesource.h new file mode 100644 index 000000000..68c373d6a --- /dev/null +++ b/sdrbase/dsp/channelsamplesource.h @@ -0,0 +1,38 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2019 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 // +// (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 . // +/////////////////////////////////////////////////////////////////////////////////// + +// This is a lightweight channel sample source interface + +#ifndef SDRBASE_DSP_CHANNELSAMPLESOURCE_H_ +#define SDRBASE_DSP_CHANNELSAMPLESOURCE_H_ + +#include "export.h" +#include "dsptypes.h" + +class Message; + +class SDRBASE_API ChannelSampleSource { +public: + ChannelSampleSource(); + virtual ~ChannelSampleSource(); + + virtual void pull(SampleVector::iterator begin, unsigned int nbSamples) = 0; //!< pull nbSamples from the source and write them starting at begin + virtual void pullOne(Sample& sample) = 0; //!< pull a single sample from the source +}; + +#endif // SDRBASE_DSP_CHANNELSAMPLESOURCE_H_ diff --git a/sdrbase/dsp/upsamplechannelizer.cpp b/sdrbase/dsp/upsamplechannelizer.cpp new file mode 100644 index 000000000..7a9f6ec04 --- /dev/null +++ b/sdrbase/dsp/upsamplechannelizer.cpp @@ -0,0 +1,319 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2019 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 // +// (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 . // +/////////////////////////////////////////////////////////////////////////////////// + + +#include +#include +#include + +#include "inthalfbandfilter.h" +#include "dspcommands.h" +#include "hbfilterchainconverter.h" +#include "upsamplechannelizer.h" + +UpSampleChannelizer::UpSampleChannelizer(ChannelSampleSource* sampleSource) : + m_filterChainSetMode(false), + m_sampleSource(sampleSource), + m_outputSampleRate(0), + m_requestedInputSampleRate(0), + m_requestedCenterFrequency(0), + m_currentInputSampleRate(0), + m_currentCenterFrequency(0) +{ +} + +UpSampleChannelizer::~UpSampleChannelizer() +{ + freeFilterChain(); +} + +void UpSampleChannelizer::pullOne(Sample& sample) +{ + if (m_sampleSource == nullptr) + { + m_sampleBuffer.clear(); + return; + } + + unsigned int log2Interp = m_filterStages.size(); + + if (log2Interp == 0) // optimization when no downsampling is done anyway + { + m_sampleSource->pullOne(sample); + } + else + { + FilterStages::iterator stage = m_filterStages.begin(); + std::vector::iterator stageSample = m_stageSamples.begin(); + + for (; stage != m_filterStages.end(); ++stage, ++stageSample) + { + if(stage == m_filterStages.end() - 1) + { + if ((*stage)->work(&m_sampleIn, &(*stageSample))) + { + m_sampleSource->pullOne(m_sampleIn); // get new input sample + } + } + else + { + if (!(*stage)->work(&(*(stageSample+1)), &(*stageSample))) + { + break; + } + } + } + + sample = *m_stageSamples.begin(); + } +} + +void UpSampleChannelizer::pull(SampleVector::iterator begin, unsigned int nbSamples) +{ + if (m_sampleSource == nullptr) + { + m_sampleBuffer.clear(); + return; + } + + unsigned int log2Interp = m_filterStages.size(); + + if (log2Interp == 0) // optimization when no downsampling is done anyway + { + m_sampleSource->pull(begin, nbSamples); + } + else + { + std::for_each( + begin, + begin + nbSamples, + [this](Sample& s) { + pullOne(s); + } + ); + } +} + +void UpSampleChannelizer::applyConfiguration() +{ + m_filterChainSetMode = false; + + if (m_outputSampleRate == 0) + { + qDebug() << "UpChannelizer::applyConfiguration: aborting (out=0):" + << " out =" << m_outputSampleRate + << ", req =" << m_requestedInputSampleRate + << ", in =" << m_currentInputSampleRate + << ", fc =" << m_currentCenterFrequency; + return; + } + + freeFilterChain(); + + m_currentCenterFrequency = createFilterChain( + m_outputSampleRate / -2, m_outputSampleRate / 2, + m_requestedCenterFrequency - m_requestedInputSampleRate / 2, m_requestedCenterFrequency + m_requestedInputSampleRate / 2); + + m_currentInputSampleRate = m_outputSampleRate / (1 << m_filterStages.size()); + + qDebug() << "UpChannelizer::applyConfiguration:" + << " out=" << m_outputSampleRate + << ", req=" << m_requestedInputSampleRate + << ", in=" << m_currentInputSampleRate + << ", fc=" << m_currentCenterFrequency; +} + +void UpSampleChannelizer::applySetting(unsigned int log2Interp, unsigned int filterChainHash) +{ + m_filterChainSetMode = true; + std::vector stageIndexes; + m_currentCenterFrequency = m_outputSampleRate * HBFilterChainConverter::convertToIndexes(log2Interp, filterChainHash, stageIndexes); + m_requestedCenterFrequency = m_currentCenterFrequency; + + m_mutex.lock(); + freeFilterChain(); + m_currentCenterFrequency = m_outputSampleRate * setFilterChain(stageIndexes); + m_mutex.unlock(); + + m_currentInputSampleRate = m_outputSampleRate / (1 << m_filterStages.size()); + m_requestedInputSampleRate = m_currentInputSampleRate; + + qDebug() << "UpChannelizer::applySetting in=" << m_outputSampleRate + << ", out=" << m_currentInputSampleRate + << ", fc=" << m_currentCenterFrequency; +} + +#ifdef USE_SSE4_1 +UpSampleChannelizer::FilterStage::FilterStage(Mode mode) : + m_filter(new IntHalfbandFilterEO1), + m_workFunction(0) +{ + switch(mode) { + case ModeCenter: + m_workFunction = &IntHalfbandFilterEO1::workInterpolateCenter; + break; + + case ModeLowerHalf: + m_workFunction = &IntHalfbandFilterEO1::workInterpolateLowerHalf; + break; + + case ModeUpperHalf: + m_workFunction = &IntHalfbandFilterEO1::workInterpolateUpperHalf; + break; + } +} +#else +UpSampleChannelizer::FilterStage::FilterStage(Mode mode) : + m_filter(new IntHalfbandFilterDB), + m_workFunction(0) +{ + switch(mode) { + case ModeCenter: + m_workFunction = &IntHalfbandFilterDB::workInterpolateCenter; + break; + + case ModeLowerHalf: + m_workFunction = &IntHalfbandFilterDB::workInterpolateLowerHalf; + break; + + case ModeUpperHalf: + m_workFunction = &IntHalfbandFilterDB::workInterpolateUpperHalf; + break; + } +} +#endif + +UpSampleChannelizer::FilterStage::~FilterStage() +{ + delete m_filter; +} + +bool UpSampleChannelizer::signalContainsChannel(Real sigStart, Real sigEnd, Real chanStart, Real chanEnd) const +{ + //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); +} + +Real UpSampleChannelizer::createFilterChain(Real sigStart, Real sigEnd, Real chanStart, Real chanEnd) +{ + Real sigBw = sigEnd - sigStart; + Real rot = sigBw / 4; + Sample s; + + qDebug() << "UpSampleChannelizer::createFilterChain: start:" + << " sig: [" << sigStart << ":" << sigEnd << "]" + << " BW: " << sigBw + << " chan: [" << chanStart << ":" << chanEnd << "]" + << " rot: " << rot; + + // check if it fits into the left half + if(signalContainsChannel(sigStart, sigStart + sigBw / 2.0, chanStart, chanEnd)) + { + qDebug() << "UpSampleChannelizer::createFilterChain: take left half (rotate by +1/4 and decimate by 2):" + << " [" << m_filterStages.size() << "]" + << " sig: [" << sigStart << ":" << sigStart + sigBw / 2.0 << "]"; + m_filterStages.push_back(new FilterStage(FilterStage::ModeLowerHalf)); + m_stageSamples.push_back(s); + return createFilterChain(sigStart, sigStart + sigBw / 2.0, chanStart, chanEnd); + } + + // check if it fits into the right half + if(signalContainsChannel(sigEnd - sigBw / 2.0f, sigEnd, chanStart, chanEnd)) + { + qDebug() << "UpSampleChannelizer::createFilterChain: take right half (rotate by -1/4 and decimate by 2):" + << " [" << m_filterStages.size() << "]" + << " sig: [" << sigEnd - sigBw / 2.0f << ":" << sigEnd << "]"; + m_filterStages.push_back(new FilterStage(FilterStage::ModeUpperHalf)); + m_stageSamples.push_back(s); + return createFilterChain(sigEnd - sigBw / 2.0f, sigEnd, chanStart, chanEnd); + } + + // check if it fits into the center + // Was: if(signalContainsChannel(sigStart + rot + safetyMargin, sigStart + rot + sigBw / 2.0f - safetyMargin, chanStart, chanEnd)) { + if(signalContainsChannel(sigStart + rot, sigEnd - rot, chanStart, chanEnd)) + { + qDebug() << "UpSampleChannelizer::createFilterChain: take center half (decimate by 2):" + << " [" << m_filterStages.size() << "]" + << " sig: [" << sigStart + rot << ":" << sigEnd - rot << "]"; + m_filterStages.push_back(new FilterStage(FilterStage::ModeCenter)); + m_stageSamples.push_back(s); + // Was: return createFilterChain(sigStart + rot, sigStart + sigBw / 2.0f + rot, chanStart, chanEnd); + return createFilterChain(sigStart + rot, sigEnd - rot, chanStart, chanEnd); + } + + Real ofs = ((chanEnd - chanStart) / 2.0 + chanStart) - ((sigEnd - sigStart) / 2.0 + sigStart); + + qDebug() << "UpSampleChannelizer::createFilterChain: complete:" + << " #stages: " << m_filterStages.size() + << " BW: " << sigBw + << " ofs: " << ofs; + + return ofs; +} + +double UpSampleChannelizer::setFilterChain(const std::vector& stageIndexes) +{ + // filters are described from lower to upper level but the chain is constructed the other way round + std::vector::const_reverse_iterator rit = stageIndexes.rbegin(); + double ofs = 0.0, ofs_stage = 0.25; + Sample s; + + // 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) + { + if (*rit == 0) + { + m_filterStages.push_back(new FilterStage(FilterStage::ModeLowerHalf)); + m_stageSamples.push_back(s); + ofs -= ofs_stage; + } + else if (*rit == 1) + { + m_filterStages.push_back(new FilterStage(FilterStage::ModeCenter)); + m_stageSamples.push_back(s); + } + else if (*rit == 2) + { + m_filterStages.push_back(new FilterStage(FilterStage::ModeUpperHalf)); + m_stageSamples.push_back(s); + ofs += ofs_stage; + } + + ofs_stage /= 2; + } + + return ofs; +} + +void UpSampleChannelizer::freeFilterChain() +{ + for(FilterStages::iterator it = m_filterStages.begin(); it != m_filterStages.end(); ++it) + delete *it; + m_filterStages.clear(); + m_stageSamples.clear(); +} + + + + diff --git a/sdrbase/dsp/upsamplechannelizer.h b/sdrbase/dsp/upsamplechannelizer.h new file mode 100644 index 000000000..d5c57261f --- /dev/null +++ b/sdrbase/dsp/upsamplechannelizer.h @@ -0,0 +1,98 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2019 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 // +// (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 . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef SDRBASE_DSP_UPSAMPLECHANNELIZER_H_ +#define SDRBASE_DSP_UPSAMPLECHANNELIZER_H_ + +#include +#include +#include + +#include "export.h" +#include "channelsamplesource.h" + +#ifdef USE_SSE4_1 +#include "dsp/inthalfbandfiltereo1.h" +#else +#include "dsp/inthalfbandfilterdb.h" +#endif + +#define UPSAMPLECHANNELIZER_HB_FILTER_ORDER 96 + +class SDRBASE_API UpSampleChannelizer : public ChannelSampleSource { +public: + UpSampleChannelizer(ChannelSampleSource* sampleSource); + virtual ~UpSampleChannelizer(); + + virtual void pull(SampleVector::iterator begin, unsigned int nbSamples); + virtual void pullOne(Sample& sample); + +protected: + struct FilterStage { + enum Mode { + ModeCenter, + ModeLowerHalf, + ModeUpperHalf + }; + +#ifdef USE_SSE4_1 + typedef bool (IntHalfbandFilterEO1::*WorkFunction)(Sample* sIn, Sample *sOut); + IntHalfbandFilterEO1* m_filter; +#else + typedef bool (IntHalfbandFilterDB::*WorkFunction)(Sample* sIn, Sample *sOut); + IntHalfbandFilterDB* m_filter; +#endif + WorkFunction m_workFunction; + + FilterStage(Mode mode); + ~FilterStage(); + + bool work(Sample* sampleIn, Sample *sampleOut) { + return (m_filter->*m_workFunction)(sampleIn, sampleOut); + } + }; + + typedef std::vector FilterStages; + FilterStages m_filterStages; + bool m_filterChainSetMode; + std::vector m_stageSamples; + ChannelSampleSource* m_sampleSource; //!< Modulator + int m_outputSampleRate; + int m_requestedInputSampleRate; + int m_requestedCenterFrequency; + int m_currentInputSampleRate; + int m_currentCenterFrequency; + SampleVector m_sampleBuffer; + Sample m_sampleIn; + QMutex m_mutex; + + void applyConfiguration(); + void applySetting(unsigned int log2Interp, unsigned int filterChainHash); + bool signalContainsChannel(Real sigStart, Real sigEnd, Real chanStart, Real chanEnd) const; + Real createFilterChain(Real sigStart, Real sigEnd, Real chanStart, Real chanEnd); + double setFilterChain(const std::vector& stageIndexes); //!< returns offset in ratio of sample rate + void freeFilterChain(); + +signals: + void outputSampleRateChanged(); +}; + + + + +#endif // SDRBASE_DSP_UPSAMPLECHANNELIZER_H_