1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-23 16:38:37 -05:00
sdrangel/plugins/channelrx/chanalyzerng/chanalyzerng.cpp
2018-05-20 01:10:08 +02:00

396 lines
13 KiB
C++

///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017 Edouard Griffiths, F4EXB //
// //
// 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 //
// //
// 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 "chanalyzerng.h"
#include <QTime>
#include <QDebug>
#include <stdio.h>
#include "device/devicesourceapi.h"
#include "audio/audiooutput.h"
#include "dsp/threadedbasebandsamplesink.h"
#include "dsp/downchannelizer.h"
MESSAGE_CLASS_DEFINITION(ChannelAnalyzerNG::MsgConfigureChannelAnalyzer, Message)
MESSAGE_CLASS_DEFINITION(ChannelAnalyzerNG::MsgConfigureChannelAnalyzerOld, Message)
MESSAGE_CLASS_DEFINITION(ChannelAnalyzerNG::MsgConfigureChannelizer, Message)
MESSAGE_CLASS_DEFINITION(ChannelAnalyzerNG::MsgReportChannelSampleRateChanged, Message)
const QString ChannelAnalyzerNG::m_channelIdURI = "sdrangel.channel.chanalyzerng";
const QString ChannelAnalyzerNG::m_channelId = "ChannelAnalyzerNG";
ChannelAnalyzerNG::ChannelAnalyzerNG(DeviceSourceAPI *deviceAPI) :
ChannelSinkAPI(m_channelIdURI),
m_deviceAPI(deviceAPI),
m_sampleSink(0),
m_settingsMutex(QMutex::Recursive)
{
setObjectName(m_channelId);
m_undersampleCount = 0;
m_sum = 0;
m_usb = true;
m_magsq = 0;
m_useInterpolator = false;
m_interpolatorDistance = 1.0f;
m_interpolatorDistanceRemain = 0.0f;
m_inputSampleRate = 48000;
m_inputFrequencyOffset = 0;
SSBFilter = new fftfilt(m_settings.m_lowCutoff / m_inputSampleRate, m_settings.m_bandwidth / m_inputSampleRate, ssbFftLen);
DSBFilter = new fftfilt(m_settings.m_bandwidth / m_inputSampleRate, 2*ssbFftLen);
//m_pll.computeCoefficients(0.05f, 0.707f, 1000.0f); // bandwidth, damping factor, loop gain
m_pll.computeCoefficients(0.002f, 0.5f, 10.0f); // bandwidth, damping factor, loop gain
applyChannelSettings(m_inputSampleRate, m_inputFrequencyOffset, true);
applySettings(m_settings, true);
m_channelizer = new DownChannelizer(this);
m_threadedChannelizer = new ThreadedBasebandSampleSink(m_channelizer, this);
m_deviceAPI->addThreadedSink(m_threadedChannelizer);
m_deviceAPI->addChannelAPI(this);
}
ChannelAnalyzerNG::~ChannelAnalyzerNG()
{
m_deviceAPI->removeChannelAPI(this);
m_deviceAPI->removeThreadedSink(m_threadedChannelizer);
delete m_threadedChannelizer;
delete m_channelizer;
delete SSBFilter;
delete DSBFilter;
}
void ChannelAnalyzerNG::configure(MessageQueue* messageQueue,
int channelSampleRate,
Real Bandwidth,
Real LowCutoff,
int spanLog2,
bool ssb,
bool pll,
bool fll,
unsigned int pllPskOrder)
{
Message* cmd = MsgConfigureChannelAnalyzerOld::create(channelSampleRate, Bandwidth, LowCutoff, spanLog2, ssb, pll, fll, pllPskOrder);
messageQueue->push(cmd);
}
void ChannelAnalyzerNG::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly __attribute__((unused)))
{
fftfilt::cmplx *sideband = 0;
Complex ci;
m_settingsMutex.lock();
for(SampleVector::const_iterator it = begin; it < end; ++it)
{
Complex c(it->real(), it->imag());
c *= m_nco.nextIQ();
if (m_useInterpolator)
{
if (m_interpolator.decimate(&m_interpolatorDistanceRemain, c, &ci))
{
processOneSample(ci, sideband);
m_interpolatorDistanceRemain += m_interpolatorDistance;
}
}
else
{
processOneSample(c, sideband);
}
}
if(m_sampleSink != 0)
{
m_sampleSink->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), m_settings.m_ssb); // m_ssb = positive only
}
m_sampleBuffer.clear();
m_settingsMutex.unlock();
}
void ChannelAnalyzerNG::processOneSample(Complex& c, fftfilt::cmplx *sideband)
{
int n_out;
int decim = 1<<m_settings.m_spanLog2;
if (m_settings.m_ssb) {
n_out = SSBFilter->runSSB(c, &sideband, m_usb);
} else {
n_out = DSBFilter->runDSB(c, &sideband);
}
for (int i = 0; i < n_out; i++)
{
// Downsample by 2^(m_scaleLog2 - 1) for SSB band spectrum display
// smart decimation with bit gain using float arithmetic (23 bits significand)
m_sum += sideband[i];
if (!(m_undersampleCount++ & (decim - 1))) // counter LSB bit mask for decimation by 2^(m_scaleLog2 - 1)
{
m_sum /= decim;
Real re = m_sum.real() / SDR_RX_SCALED;
Real im = m_sum.imag() / SDR_RX_SCALED;
m_magsq = re*re + im*im;
Real mixI = 1.0f;
Real mixQ = 0.0f;
if (m_settings.m_pll)
{
if (m_settings.m_fll)
{
m_fll.feed(re, im);
// Use -fPLL to mix (exchange PLL real and image in the complex multiplication)
mixI = m_sum.real() * m_fll.getImag() - m_sum.imag() * m_fll.getReal();
mixQ = m_sum.real() * m_fll.getReal() + m_sum.imag() * m_fll.getImag();
// mixI = m_fll.getReal() * SDR_RX_SCALED;
// mixQ = m_fll.getImag() * SDR_RX_SCALED;
}
else
{
m_pll.feed(re, im);
// Use -fPLL to mix (exchange PLL real and image in the complex multiplication)
mixI = m_sum.real() * m_pll.getImag() - m_sum.imag() * m_pll.getReal();
mixQ = m_sum.real() * m_pll.getReal() + m_sum.imag() * m_pll.getImag();
}
if (m_settings.m_ssb & !m_usb)
{ // invert spectrum for LSB
m_sampleBuffer.push_back(Sample(mixQ, mixI));
}
else
{
m_sampleBuffer.push_back(Sample(mixI, mixQ));
}
}
else
{
if (m_settings.m_ssb & !m_usb)
{ // invert spectrum for LSB
m_sampleBuffer.push_back(Sample(m_sum.imag(), m_sum.real()));
}
else
{
m_sampleBuffer.push_back(Sample(m_sum.real(), m_sum.imag()));
}
}
m_sum = 0;
}
}
}
void ChannelAnalyzerNG::start()
{
applyChannelSettings(m_inputSampleRate, m_inputFrequencyOffset, true);
}
void ChannelAnalyzerNG::stop()
{
}
bool ChannelAnalyzerNG::handleMessage(const Message& cmd)
{
if (DownChannelizer::MsgChannelizerNotification::match(cmd))
{
DownChannelizer::MsgChannelizerNotification& notif = (DownChannelizer::MsgChannelizerNotification&) cmd;
qDebug() << "ChannelAnalyzerNG::handleMessage: DownChannelizer::MsgChannelizerNotification:"
<< " sampleRate: " << notif.getSampleRate()
<< " frequencyOffset: " << notif.getFrequencyOffset();
applyChannelSettings(notif.getSampleRate(), notif.getFrequencyOffset());
if (getMessageQueueToGUI())
{
MsgReportChannelSampleRateChanged *msg = MsgReportChannelSampleRateChanged::create();
getMessageQueueToGUI()->push(msg);
}
return true;
}
else if (MsgConfigureChannelizer::match(cmd))
{
MsgConfigureChannelizer& cfg = (MsgConfigureChannelizer&) cmd;
qDebug() << "ChannelAnalyzerNG::handleMessage: MsgConfigureChannelizer:"
<< " sampleRate: " << cfg.getSampleRate()
<< " centerFrequency: " << cfg.getCenterFrequency();
m_channelizer->configure(m_channelizer->getInputMessageQueue(),
cfg.getSampleRate(),
cfg.getCenterFrequency());
return true;
}
else if (MsgConfigureChannelAnalyzer::match(cmd))
{
qDebug("ChannelAnalyzerNG::handleMessage: MsgConfigureChannelAnalyzer");
MsgConfigureChannelAnalyzer& cfg = (MsgConfigureChannelAnalyzer&) cmd;
applySettings(cfg.getSettings(), cfg.getForce());
return true;
}
else
{
if (m_sampleSink != 0)
{
return m_sampleSink->handleMessage(cmd);
}
else
{
return false;
}
}
}
void ChannelAnalyzerNG::applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force)
{
qDebug() << "ChannelAnalyzerNG::applyChannelSettings:"
<< " inputSampleRate: " << inputSampleRate
<< " inputFrequencyOffset: " << inputFrequencyOffset;
if ((m_inputFrequencyOffset != inputFrequencyOffset) ||
(m_inputSampleRate != inputSampleRate) || force)
{
m_nco.setFreq(-inputFrequencyOffset, inputSampleRate);
}
if ((m_inputSampleRate != inputSampleRate) || force)
{
m_settingsMutex.lock();
m_interpolator.create(16, inputSampleRate, inputSampleRate / 2.2f);
m_interpolatorDistanceRemain = 0;
m_interpolatorDistance = (Real) inputSampleRate / (Real) m_settings.m_downSampleRate;
if (!m_settings.m_downSample)
{
setFilters(inputSampleRate, m_settings.m_bandwidth, m_settings.m_lowCutoff);
m_pll.setSampleRate(inputSampleRate / (1<<m_settings.m_spanLog2));
m_fll.setSampleRate(inputSampleRate / (1<<m_settings.m_spanLog2));
}
m_settingsMutex.unlock();
}
m_inputSampleRate = inputSampleRate;
m_inputFrequencyOffset = inputFrequencyOffset;
}
void ChannelAnalyzerNG::setFilters(int sampleRate, float bandwidth, float lowCutoff)
{
qDebug("ChannelAnalyzerNG::setFilters: sampleRate: %d bandwidth: %f lowCutoff: %f",
sampleRate, bandwidth, lowCutoff);
if (bandwidth < 0)
{
bandwidth = -bandwidth;
lowCutoff = -lowCutoff;
m_usb = false;
}
else
{
m_usb = true;
}
if (bandwidth < 100.0f)
{
bandwidth = 100.0f;
lowCutoff = 0;
}
SSBFilter->create_filter(lowCutoff / sampleRate, bandwidth / sampleRate);
DSBFilter->create_dsb_filter(bandwidth / sampleRate);
}
void ChannelAnalyzerNG::applySettings(const ChannelAnalyzerNGSettings& settings, bool force)
{
qDebug() << "ChannelAnalyzerNG::applySettings:"
<< " m_downSample: " << settings.m_downSample
<< " m_downSampleRate: " << settings.m_downSampleRate
<< " m_bandwidth: " << settings.m_bandwidth
<< " m_lowCutoff: " << settings.m_lowCutoff
<< " m_spanLog2: " << settings.m_spanLog2
<< " m_ssb: " << settings.m_ssb
<< " m_pll: " << settings.m_pll
<< " m_fll: " << settings.m_fll
<< " m_pllPskOrder: " << settings.m_pllPskOrder;
if ((settings.m_downSampleRate != m_settings.m_downSampleRate) || force)
{
m_settingsMutex.lock();
m_interpolator.create(16, m_inputSampleRate, m_inputSampleRate / 2.2);
m_interpolatorDistanceRemain = 0.0f;
m_interpolatorDistance = (Real) m_inputSampleRate / (Real) settings.m_downSampleRate;
m_settingsMutex.unlock();
}
if ((settings.m_downSample != m_settings.m_downSample) || force)
{
int sampleRate = settings.m_downSample ? settings.m_downSampleRate : m_inputSampleRate;
m_settingsMutex.lock();
m_useInterpolator = settings.m_downSample;
setFilters(sampleRate, settings.m_bandwidth, settings.m_lowCutoff);
m_pll.setSampleRate(sampleRate / (1<<settings.m_spanLog2));
m_fll.setSampleRate(sampleRate / (1<<settings.m_spanLog2));
m_settingsMutex.unlock();
}
if ((settings.m_bandwidth != m_settings.m_bandwidth) ||
(settings.m_lowCutoff != m_settings.m_lowCutoff)|| force)
{
m_settingsMutex.lock();
setFilters(settings.m_downSample ? settings.m_downSampleRate : m_inputSampleRate, settings.m_bandwidth, settings.m_lowCutoff);
m_settingsMutex.unlock();
}
if ((settings.m_spanLog2 != m_settings.m_spanLog2) || force)
{
int sampleRate = (settings.m_downSample ? settings.m_downSampleRate : m_inputSampleRate) / (1<<m_settings.m_spanLog2);
m_pll.setSampleRate(sampleRate);
m_fll.setSampleRate(sampleRate);
}
if (settings.m_pll != m_settings.m_pll || force)
{
if (settings.m_pll)
{
m_pll.reset();
m_fll.reset();
}
}
if (settings.m_fll != m_settings.m_fll || force)
{
if (settings.m_fll) {
m_fll.reset();
}
}
if (settings.m_pllPskOrder != m_settings.m_pllPskOrder || force)
{
if (settings.m_pllPskOrder < 32) {
m_pll.setPskOrder(settings.m_pllPskOrder);
}
}
m_settings = settings;
}