1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-25 01:18:38 -05:00
sdrangel/sdrbase/dsp/downchannelizer.cpp

337 lines
12 KiB
C++
Raw Normal View History

2019-11-23 01:39:57 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel //
// Copyright (C) 2015-2020, 2023 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2023 Jon Beniston, M7RCE <jon@beniston.com> //
2019-11-23 01:39:57 -05:00
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
2023-10-17 06:36:27 -04:00
#include <array>
2019-11-23 01:39:57 -05:00
#include <QString>
#include <QDebug>
#include "dsp/hbfilterchainconverter.h"
#include "downchannelizer.h"
2019-11-23 01:39:57 -05:00
DownChannelizer::DownChannelizer(ChannelSampleSink* sampleSink) :
2019-11-23 01:39:57 -05:00
m_filterChainSetMode(false),
m_sampleSink(sampleSink),
m_basebandSampleRate(0),
m_requestedOutputSampleRate(0),
m_requestedCenterFrequency(0),
m_channelSampleRate(0),
m_channelFrequencyOffset(0),
m_log2Decim(0),
m_filterChainHash(0)
{
}
DownChannelizer::~DownChannelizer()
2019-11-23 01:39:57 -05:00
{
freeFilterChain();
}
void DownChannelizer::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end)
2019-11-23 01:39:57 -05:00
{
if (m_sampleSink == 0)
{
m_sampleBuffer.clear();
return;
}
if (m_filterStages.size() == 0) // optimization when no downsampling is done anyway
{
m_sampleSink->feed(begin, end);
}
else
{
for (SampleVector::const_iterator sample = begin; sample != end; ++sample)
{
Sample s(*sample);
FilterStages::iterator stage = m_filterStages.begin();
for (; stage != m_filterStages.end(); ++stage)
{
#ifndef SDR_RX_SAMPLE_24BIT
s.m_real /= 2; // avoid saturation on 16 bit samples
s.m_imag /= 2;
#endif
if (!(*stage)->work(&s)) {
break;
}
}
if(stage == m_filterStages.end())
{
#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
s.m_imag /= (1<<(m_filterStages.size()));
#endif
m_sampleBuffer.push_back(s);
}
}
m_sampleSink->feed(m_sampleBuffer.begin(), m_sampleBuffer.end());
m_sampleBuffer.clear();
}
}
void DownChannelizer::setChannelization(int requestedSampleRate, qint64 requestedCenterFrequency)
2019-11-23 01:39:57 -05:00
{
if (requestedSampleRate < 0)
{
qWarning("DownChannelizer::setChannelization: wrong sample rate requested: %d", requestedSampleRate);
return;
}
2019-11-23 01:39:57 -05:00
m_requestedOutputSampleRate = requestedSampleRate;
m_requestedCenterFrequency = requestedCenterFrequency;
applyChannelization();
}
void DownChannelizer::setBasebandSampleRate(int basebandSampleRate, bool decim)
2019-11-23 01:39:57 -05:00
{
m_basebandSampleRate = basebandSampleRate;
if (decim) {
applyDecimation();
} else {
applyChannelization();
}
}
void DownChannelizer::applyChannelization()
2019-11-23 01:39:57 -05:00
{
m_filterChainSetMode = false;
if (m_basebandSampleRate == 0)
{
qDebug() << "DownChannelizer::applyChannelization: aborting (in=0)"
2019-11-23 01:39:57 -05:00
<< " in (baseband):" << m_basebandSampleRate
<< " req:" << m_requestedOutputSampleRate
<< " out (channel):" << m_channelSampleRate
<< " fc:" << m_channelFrequencyOffset;
return;
}
freeFilterChain();
m_channelFrequencyOffset = createFilterChain(
m_basebandSampleRate / -2, m_basebandSampleRate / 2,
m_requestedCenterFrequency - m_requestedOutputSampleRate / 2, m_requestedCenterFrequency + m_requestedOutputSampleRate / 2);
m_channelSampleRate = m_basebandSampleRate / (1 << m_filterStages.size());
qDebug() << "DownChannelizer::applyChannelization done:"
<< " nb stages:" << m_filterStages.size()
2019-11-23 01:39:57 -05:00
<< " in (baseband):" << m_basebandSampleRate
<< " req:" << m_requestedOutputSampleRate
<< " out (channel):" << m_channelSampleRate
<< " fc:" << m_channelFrequencyOffset;
}
void DownChannelizer::setDecimation(unsigned int log2Decim, unsigned int filterChainHash)
2019-11-23 01:39:57 -05:00
{
m_log2Decim = log2Decim;
m_filterChainHash = filterChainHash;
applyDecimation();
}
void DownChannelizer::applyDecimation()
2019-11-23 01:39:57 -05:00
{
m_filterChainSetMode = true;
std::vector<unsigned int> stageIndexes;
m_channelFrequencyOffset = m_basebandSampleRate * HBFilterChainConverter::convertToIndexes(m_log2Decim, m_filterChainHash, stageIndexes);
m_requestedCenterFrequency = m_channelFrequencyOffset;
freeFilterChain();
m_channelFrequencyOffset = m_basebandSampleRate * setFilterChain(stageIndexes);
m_channelSampleRate = m_basebandSampleRate / (1 << m_filterStages.size());
m_requestedOutputSampleRate = m_channelSampleRate;
2020-06-24 19:14:39 -04:00
qDebug() << "DownChannelizer::applyDecimation:"
<< " m_log2Decim:" << m_log2Decim
2019-11-23 01:39:57 -05:00
<< " m_filterChainHash:" << m_filterChainHash
<< " out:" << m_basebandSampleRate
<< " in:" << m_channelSampleRate
<< " fc:" << m_channelFrequencyOffset;
}
#ifdef SDR_RX_SAMPLE_24BIT
DownChannelizer::FilterStage::FilterStage(Mode mode) :
m_filter(new IntHalfbandFilterEO<qint64, qint64, DOWNCHANNELIZER_HB_FILTER_ORDER, true>),
2019-11-23 01:39:57 -05:00
m_workFunction(0),
m_mode(mode),
m_sse(true)
{
switch(mode) {
case ModeCenter:
m_workFunction = &IntHalfbandFilterEO<qint64, qint64, DOWNCHANNELIZER_HB_FILTER_ORDER, true>::workDecimateCenter;
2019-11-23 01:39:57 -05:00
break;
case ModeLowerHalf:
m_workFunction = &IntHalfbandFilterEO<qint64, qint64, DOWNCHANNELIZER_HB_FILTER_ORDER, true>::workDecimateLowerHalf;
2019-11-23 01:39:57 -05:00
break;
case ModeUpperHalf:
m_workFunction = &IntHalfbandFilterEO<qint64, qint64, DOWNCHANNELIZER_HB_FILTER_ORDER, true>::workDecimateUpperHalf;
2019-11-23 01:39:57 -05:00
break;
}
}
#else
DownChannelizer::FilterStage::FilterStage(Mode mode) :
m_filter(new IntHalfbandFilterEO<qint32, qint32, DOWNCHANNELIZER_HB_FILTER_ORDER, true>),
2019-11-23 01:39:57 -05:00
m_workFunction(0),
m_mode(mode),
m_sse(true)
{
switch(mode) {
case ModeCenter:
m_workFunction = &IntHalfbandFilterEO<qint32, qint32, DOWNCHANNELIZER_HB_FILTER_ORDER, true>::workDecimateCenter;
2019-11-23 01:39:57 -05:00
break;
case ModeLowerHalf:
m_workFunction = &IntHalfbandFilterEO<qint32, qint32, DOWNCHANNELIZER_HB_FILTER_ORDER, true>::workDecimateLowerHalf;
2019-11-23 01:39:57 -05:00
break;
case ModeUpperHalf:
m_workFunction = &IntHalfbandFilterEO<qint32, qint32, DOWNCHANNELIZER_HB_FILTER_ORDER, true>::workDecimateUpperHalf;
2019-11-23 01:39:57 -05:00
break;
}
}
#endif
DownChannelizer::FilterStage::~FilterStage()
2019-11-23 01:39:57 -05:00
{
delete m_filter;
}
Real DownChannelizer::channelMinSpace(Real sigStart, Real sigEnd, Real chanStart, Real chanEnd)
2019-11-23 01:39:57 -05:00
{
Real leftSpace = chanStart - sigStart;
Real rightSpace = sigEnd - chanEnd;
return std::min(leftSpace, rightSpace);
2019-11-23 01:39:57 -05:00
}
Real DownChannelizer::createFilterChain(Real sigStart, Real sigEnd, Real chanStart, Real chanEnd)
2019-11-23 01:39:57 -05:00
{
Real sigBw = sigEnd - sigStart;
Real chanBw = chanEnd - chanStart;
2019-11-23 01:39:57 -05:00
Real rot = sigBw / 4;
std::array<Real, 3> filterMinSpaces; // Array of left, center and right filter min spaces respectively
filterMinSpaces[0] = channelMinSpace(sigStart, sigStart + sigBw / 2.0, chanStart, chanEnd);
filterMinSpaces[1] = channelMinSpace(sigStart + rot, sigEnd - rot, chanStart, chanEnd);
filterMinSpaces[2] = channelMinSpace(sigEnd - sigBw / 2.0f, sigEnd, chanStart, chanEnd);
auto maxIt = std::max_element(filterMinSpaces.begin(), filterMinSpaces.end());
int maxIndex = maxIt - filterMinSpaces.begin();
Real maxValue = *maxIt;
2019-11-23 01:39:57 -05:00
qDebug("DownChannelizer::createFilterChain: Signal [%.1f, %.1f] (BW %.1f) Channel [%.1f, %.1f] (BW %.1f) Selected: %d (fit %.1f)",
sigStart, sigEnd, sigBw, chanStart, chanEnd, chanBw, maxIndex, maxValue);
2019-11-23 01:39:57 -05:00
if ((sigStart < sigEnd) && (chanStart < chanEnd) && (maxValue >= chanBw/8.0))
2019-11-23 01:39:57 -05:00
{
if (maxIndex == 0)
{
m_filterStages.push_back(new FilterStage(FilterStage::ModeLowerHalf));
return createFilterChain(sigStart, sigStart + sigBw / 2.0, chanStart, chanEnd);
}
if (maxIndex == 1)
{
m_filterStages.push_back(new FilterStage(FilterStage::ModeCenter));
return createFilterChain(sigStart + rot, sigEnd - rot, chanStart, chanEnd);
}
if (maxIndex == 2)
{
m_filterStages.push_back(new FilterStage(FilterStage::ModeUpperHalf));
return createFilterChain(sigEnd - sigBw / 2.0f, sigEnd, chanStart, chanEnd);
}
}
2019-11-23 01:39:57 -05:00
Real ofs = ((chanEnd - chanStart) / 2.0 + chanStart) - ((sigEnd - sigStart) / 2.0 + sigStart);
qDebug("DownChannelizer::createFilterChain: -> complete (final BW %.1f, frequency offset %.1f)", sigBw, ofs);
2019-11-23 01:39:57 -05:00
return ofs;
}
double DownChannelizer::setFilterChain(const std::vector<unsigned int>& stageIndexes)
2019-11-23 01:39:57 -05: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();
double ofs = 0.0, ofs_stage = 0.25;
// 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));
ofs -= ofs_stage;
qDebug("DownChannelizer::setFilterChain: lower half: ofs: %f", ofs);
2019-11-23 01:39:57 -05:00
}
else if (*rit == 1)
{
m_filterStages.push_back(new FilterStage(FilterStage::ModeCenter));
qDebug("DownChannelizer::setFilterChain: center: ofs: %f", ofs);
2019-11-23 01:39:57 -05:00
}
else if (*rit == 2)
{
m_filterStages.push_back(new FilterStage(FilterStage::ModeUpperHalf));
ofs += ofs_stage;
qDebug("DownChannelizer::setFilterChain: upper half: ofs: %f", ofs);
2019-11-23 01:39:57 -05:00
}
}
return ofs;
}
void DownChannelizer::freeFilterChain()
2019-11-23 01:39:57 -05:00
{
for(FilterStages::iterator it = m_filterStages.begin(); it != m_filterStages.end(); ++it)
delete *it;
m_filterStages.clear();
}
void DownChannelizer::debugFilterChain()
2019-11-23 01:39:57 -05:00
{
qDebug("DownChannelizer::debugFilterChain: %lu stages", m_filterStages.size());
2019-11-23 01:39:57 -05: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");
2019-11-23 01:39:57 -05:00
break;
case FilterStage::ModeLowerHalf:
qDebug("DownChannelizer::debugFilterChain: lower %s", (*it)->m_sse ? "sse" : "no_sse");
2019-11-23 01:39:57 -05:00
break;
case FilterStage::ModeUpperHalf:
qDebug("DownChannelizer::debugFilterChain: upper %s", (*it)->m_sse ? "sse" : "no_sse");
2019-11-23 01:39:57 -05:00
break;
default:
qDebug("DownChannelizer::debugFilterChain: none %s", (*it)->m_sse ? "sse" : "no_sse");
2019-11-23 01:39:57 -05:00
break;
}
}
}