1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-09-04 14:17:50 -04:00

UDP source plugin: implemented AM with DC filtering and AGC

This commit is contained in:
f4exb 2017-08-20 00:32:40 +02:00
parent 4e53552a99
commit 79f53275de
5 changed files with 113 additions and 7 deletions

View File

@ -32,11 +32,13 @@ MESSAGE_CLASS_DEFINITION(UDPSrc::MsgUDPSrcSpectrum, Message)
UDPSrc::UDPSrc(MessageQueue* uiMessageQueue, UDPSrcGUI* udpSrcGUI, BasebandSampleSink* spectrum) : UDPSrc::UDPSrc(MessageQueue* uiMessageQueue, UDPSrcGUI* udpSrcGUI, BasebandSampleSink* spectrum) :
m_outMovingAverage(480, 1e-10), m_outMovingAverage(480, 1e-10),
m_inMovingAverage(480, 1e-10), m_inMovingAverage(480, 1e-10),
m_amMovingAverage(480, 1e-10),
m_audioFifo(4, 24000), m_audioFifo(4, 24000),
m_squelchOpen(false), m_squelchOpen(false),
m_squelchOpenCount(0), m_squelchOpenCount(0),
m_squelchCloseCount(0), m_squelchCloseCount(0),
m_squelchThreshold(4800), m_squelchThreshold(4800),
m_agc(12000, m_agcTarget, 1e-6),
m_settingsMutex(QMutex::Recursive) m_settingsMutex(QMutex::Recursive)
{ {
setObjectName("UDPSrc"); setObjectName("UDPSrc");
@ -78,6 +80,9 @@ UDPSrc::UDPSrc(MessageQueue* uiMessageQueue, UDPSrcGUI* udpSrcGUI, BasebandSampl
qWarning("UDPSrc::UDPSrc: cannot bind audio port"); qWarning("UDPSrc::UDPSrc: cannot bind audio port");
} }
m_agc.setClampMax(32768.0*32768.0);
m_agc.setClamping(true);
//DSPEngine::instance()->addAudioSink(&m_audioFifo); //DSPEngine::instance()->addAudioSink(&m_audioFifo);
} }
@ -122,6 +127,7 @@ void UDPSrc::configureImmediate(MessageQueue* messageQueue,
Real squelchDB, Real squelchDB,
Real squelchGate, Real squelchGate,
bool squelchEnabled, bool squelchEnabled,
bool agc,
bool force) bool force)
{ {
Message* cmd = MsgUDPSrcConfigureImmediate::create( Message* cmd = MsgUDPSrcConfigureImmediate::create(
@ -132,6 +138,7 @@ void UDPSrc::configureImmediate(MessageQueue* messageQueue,
squelchDB, squelchDB,
squelchGate, squelchGate,
squelchEnabled, squelchEnabled,
agc,
force); force);
messageQueue->push(cmd); messageQueue->push(cmd);
} }
@ -158,7 +165,21 @@ void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector:
if(m_interpolator.decimate(&m_sampleDistanceRemain, c, &ci)) if(m_interpolator.decimate(&m_sampleDistanceRemain, c, &ci))
{ {
double inMagSq = ci.real()*ci.real() + ci.imag()*ci.imag(); double inMagSq;
double agcFactor = 1.0;
if ((m_running.m_agc) &&
(m_running.m_sampleFormat != FormatNFM) &&
(m_running.m_sampleFormat != FormatNFMMono) &&
(m_running.m_sampleFormat != FormatS16LE))
{
agcFactor = m_agc.feedAndGetValue(ci);
inMagSq = m_agc.getMagSq();
}
else
{
inMagSq = ci.real()*ci.real() + ci.imag()*ci.imag();
}
m_inMovingAverage.feed(inMagSq / (1<<30)); m_inMovingAverage.feed(inMagSq / (1<<30));
m_inMagsq = m_inMovingAverage.average(); m_inMagsq = m_inMovingAverage.average();
@ -172,6 +193,7 @@ void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector:
if (m_running.m_sampleFormat == FormatLSB) // binaural LSB if (m_running.m_sampleFormat == FormatLSB) // binaural LSB
{ {
ci *= agcFactor;
int n_out = UDPFilter->runSSB(ci, &sideband, false); int n_out = UDPFilter->runSSB(ci, &sideband, false);
if (n_out) if (n_out)
@ -187,6 +209,7 @@ void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector:
} }
if (m_running.m_sampleFormat == FormatUSB) // binaural USB if (m_running.m_sampleFormat == FormatUSB) // binaural USB
{ {
ci *= agcFactor;
int n_out = UDPFilter->runSSB(ci, &sideband, true); int n_out = UDPFilter->runSSB(ci, &sideband, true);
if (n_out) if (n_out)
@ -214,6 +237,7 @@ void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector:
} }
else if (m_running.m_sampleFormat == FormatLSBMono) // Monaural LSB else if (m_running.m_sampleFormat == FormatLSBMono) // Monaural LSB
{ {
ci *= agcFactor;
int n_out = UDPFilter->runSSB(ci, &sideband, false); int n_out = UDPFilter->runSSB(ci, &sideband, false);
if (n_out) if (n_out)
@ -228,6 +252,7 @@ void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector:
} }
else if (m_running.m_sampleFormat == FormatUSBMono) // Monaural USB else if (m_running.m_sampleFormat == FormatUSBMono) // Monaural USB
{ {
ci *= agcFactor;
int n_out = UDPFilter->runSSB(ci, &sideband, true); int n_out = UDPFilter->runSSB(ci, &sideband, true);
if (n_out) if (n_out)
@ -242,10 +267,26 @@ void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector:
} }
else if (m_running.m_sampleFormat == FormatAMMono) else if (m_running.m_sampleFormat == FormatAMMono)
{ {
FixReal demod = m_squelchOpen ? (FixReal) (sqrt(inMagSq) * m_running.m_gain) : 0; FixReal demod = m_squelchOpen ? (FixReal) (sqrt(inMagSq) * agcFactor * m_running.m_gain) : 0;
m_udpBufferMono->write(demod); m_udpBufferMono->write(demod);
m_outMovingAverage.feed((demod * demod) / (1<<30)); m_outMovingAverage.feed((demod * demod) / 1073741824.0);
} }
else if (m_running.m_sampleFormat == FormatAMNoDCMono)
{
m_amMovingAverage.feed(inMagSq);
if (m_squelchOpen)
{
FixReal demod = (FixReal) ((sqrt(inMagSq) - sqrt(m_amMovingAverage.average())) * agcFactor * m_running.m_gain);
m_udpBufferMono->write(demod);
m_outMovingAverage.feed((demod * demod) / 1073741824.0);
}
else
{
m_udpBufferMono->write(0);
m_outMovingAverage.feed(0);
}
}
else // Raw I/Q samples else // Raw I/Q samples
{ {
if (m_squelchOpen) if (m_squelchOpen)
@ -314,6 +355,7 @@ bool UDPSrc::handleMessage(const Message& cmd)
m_config.m_squelch = CalcDb::powerFromdB(cfg.getSquelchDB()); m_config.m_squelch = CalcDb::powerFromdB(cfg.getSquelchDB());
m_config.m_squelchGate = cfg.getSquelchGate(); m_config.m_squelchGate = cfg.getSquelchGate();
m_config.m_squelchEnabled = cfg.getSquelchEnabled(); m_config.m_squelchEnabled = cfg.getSquelchEnabled();
m_config.m_agc = cfg.getAGC();
apply(cfg.getForce()); apply(cfg.getForce());
@ -324,7 +366,8 @@ bool UDPSrc::handleMessage(const Message& cmd)
<< " m_squelchEnabled: " << m_config.m_squelchEnabled << " m_squelchEnabled: " << m_config.m_squelchEnabled
<< " m_squelch: " << m_config.m_squelch << " m_squelch: " << m_config.m_squelch
<< " getSquelchDB: " << cfg.getSquelchDB() << " getSquelchDB: " << cfg.getSquelchDB()
<< " m_squelchGate" << m_config.m_squelchGate; << " m_squelchGate" << m_config.m_squelchGate
<< " m_agc" << m_config.m_agc;
return true; return true;
@ -388,10 +431,16 @@ void UDPSrc::apply(bool force)
m_interpolator.create(16, m_config.m_inputSampleRate, m_config.m_rfBandwidth / 2.0); m_interpolator.create(16, m_config.m_inputSampleRate, m_config.m_rfBandwidth / 2.0);
m_sampleDistanceRemain = m_config.m_inputSampleRate / m_config.m_outputSampleRate; m_sampleDistanceRemain = m_config.m_inputSampleRate / m_config.m_outputSampleRate;
m_squelchThreshold = m_config.m_outputSampleRate * m_config.m_squelchGate; int gateNbSamples = m_config.m_inputSampleRate * m_config.m_squelchGate;
int agcTimeNbSamples = m_config.m_inputSampleRate * 0.2; // Fixed 200 ms
m_squelchThreshold = gateNbSamples;
initSquelch(m_squelchOpen); initSquelch(m_squelchOpen);
m_agc.resize(agcTimeNbSamples, m_agcTarget);
m_agc.setStepDownDelay(gateNbSamples);
m_agc.setGate(gateNbSamples);
m_inMovingAverage.resize(m_config.m_outputSampleRate * 0.01, 1e-10); // 10 ms m_inMovingAverage.resize(m_config.m_inputSampleRate * 0.01, 1e-10); // 10 ms
m_amMovingAverage.resize(m_config.m_inputSampleRate * 0.01, 1e-10); // 10 ms
m_outMovingAverage.resize(m_config.m_outputSampleRate * 0.01, 1e-10); // 10 ms m_outMovingAverage.resize(m_config.m_outputSampleRate * 0.01, 1e-10); // 10 ms
} }
@ -410,8 +459,16 @@ void UDPSrc::apply(bool force)
if ((m_config.m_squelchGate != m_running.m_squelchGate) || force) if ((m_config.m_squelchGate != m_running.m_squelchGate) || force)
{ {
m_squelchThreshold = m_config.m_outputSampleRate * m_config.m_squelchGate; int gateNbSamples = m_config.m_inputSampleRate * m_config.m_squelchGate;
m_squelchThreshold = gateNbSamples;
initSquelch(m_squelchOpen); initSquelch(m_squelchOpen);
m_agc.setStepDownDelay(gateNbSamples); // same delay for up and down
m_agc.setGate(gateNbSamples);
}
if ((m_config.m_squelch != m_running.m_squelch) || force)
{
m_agc.setThreshold(m_config.m_squelch * (1<<30));
} }
if ((m_config.m_udpAddressStr != m_running.m_udpAddressStr) || force) if ((m_config.m_udpAddressStr != m_running.m_udpAddressStr) || force)

View File

@ -26,6 +26,7 @@
#include "dsp/interpolator.h" #include "dsp/interpolator.h"
#include "dsp/phasediscri.h" #include "dsp/phasediscri.h"
#include "dsp/movingaverage.h" #include "dsp/movingaverage.h"
#include "dsp/agc.h"
#include "util/udpsink.h" #include "util/udpsink.h"
#include "util/message.h" #include "util/message.h"
#include "audio/audiofifo.h" #include "audio/audiofifo.h"
@ -47,6 +48,7 @@ public:
FormatLSBMono, FormatLSBMono,
FormatUSBMono, FormatUSBMono,
FormatAMMono, FormatAMMono,
FormatAMNoDCMono,
FormatNone FormatNone
}; };
@ -77,6 +79,7 @@ public:
Real squelchDB, Real squelchDB,
Real squelchGate, Real squelchGate,
bool squelchEnabled, bool squelchEnabled,
bool agc,
bool force); bool force);
void setSpectrum(MessageQueue* messageQueue, bool enabled); void setSpectrum(MessageQueue* messageQueue, bool enabled);
double getMagSq() const { return m_magsq; } double getMagSq() const { return m_magsq; }
@ -168,6 +171,7 @@ protected:
Real getSquelchDB() const { return m_squelchDB; } Real getSquelchDB() const { return m_squelchDB; }
Real getSquelchGate() const { return m_squelchGate; } Real getSquelchGate() const { return m_squelchGate; }
bool getSquelchEnabled() const { return m_squelchEnabled; } bool getSquelchEnabled() const { return m_squelchEnabled; }
bool getAGC() const { return m_agc; }
bool getForce() const { return m_force; } bool getForce() const { return m_force; }
static MsgUDPSrcConfigureImmediate* create( static MsgUDPSrcConfigureImmediate* create(
@ -178,6 +182,7 @@ protected:
Real squelchDB, Real squelchDB,
Real squelchGate, Real squelchGate,
bool squelchEnabled, bool squelchEnabled,
bool agc,
bool force) bool force)
{ {
return new MsgUDPSrcConfigureImmediate( return new MsgUDPSrcConfigureImmediate(
@ -188,6 +193,7 @@ protected:
squelchDB, squelchDB,
squelchGate, squelchGate,
squelchEnabled, squelchEnabled,
agc,
force); force);
} }
@ -199,6 +205,7 @@ protected:
Real m_squelchDB; Real m_squelchDB;
Real m_squelchGate; // seconds Real m_squelchGate; // seconds
bool m_squelchEnabled; bool m_squelchEnabled;
bool m_agc;
bool m_force; bool m_force;
MsgUDPSrcConfigureImmediate( MsgUDPSrcConfigureImmediate(
@ -209,6 +216,7 @@ protected:
Real squelchDB, Real squelchDB,
Real squelchGate, Real squelchGate,
bool squelchEnabled, bool squelchEnabled,
bool agc,
bool force) : bool force) :
Message(), Message(),
m_gain(gain), m_gain(gain),
@ -218,6 +226,7 @@ protected:
m_squelchDB(squelchDB), m_squelchDB(squelchDB),
m_squelchGate(squelchGate), m_squelchGate(squelchGate),
m_squelchEnabled(squelchEnabled), m_squelchEnabled(squelchEnabled),
m_agc(agc),
m_force(force) m_force(force)
{ } { }
}; };
@ -254,6 +263,7 @@ protected:
Real m_squelch; //!< squared magnitude Real m_squelch; //!< squared magnitude
Real m_squelchGate; //!< seconds Real m_squelchGate; //!< seconds
bool m_squelchEnabled; bool m_squelchEnabled;
bool m_agc;
bool m_audioActive; bool m_audioActive;
bool m_audioStereo; bool m_audioStereo;
int m_volume; int m_volume;
@ -274,6 +284,7 @@ protected:
m_squelch(1e-6), m_squelch(1e-6),
m_squelchGate(0.0), m_squelchGate(0.0),
m_squelchEnabled(true), m_squelchEnabled(true),
m_agc(false),
m_audioActive(false), m_audioActive(false),
m_audioStereo(false), m_audioStereo(false),
m_volume(20), m_volume(20),
@ -294,6 +305,7 @@ protected:
double m_inMagsq; double m_inMagsq;
MovingAverage<double> m_outMovingAverage; MovingAverage<double> m_outMovingAverage;
MovingAverage<double> m_inMovingAverage; MovingAverage<double> m_inMovingAverage;
MovingAverage<double> m_amMovingAverage;
Real m_scale; Real m_scale;
Complex m_last, m_this; Complex m_last, m_this;
@ -319,6 +331,7 @@ protected:
char *m_udpAudioBuf; char *m_udpAudioBuf;
static const int m_udpAudioPayloadSize = 8192; //!< UDP audio samples buffer. No UDP block on Earth is larger than this static const int m_udpAudioPayloadSize = 8192; //!< UDP audio samples buffer. No UDP block on Earth is larger than this
static const Real m_agcTarget = 16384.0f;
PhaseDiscriminators m_phaseDiscri; PhaseDiscriminators m_phaseDiscri;
@ -327,6 +340,8 @@ protected:
int m_squelchCloseCount; int m_squelchCloseCount;
int m_squelchThreshold; //!< number of samples computed from given gate int m_squelchThreshold; //!< number of samples computed from given gate
MagAGC m_agc;
QMutex m_settingsMutex; QMutex m_settingsMutex;
void apply(bool force); void apply(bool force);

View File

@ -347,6 +347,7 @@ void UDPSrcGUI::applySettingsImmediate(bool force)
ui->squelch->value() * 1.0f, ui->squelch->value() * 1.0f,
ui->squelchGate->value() * 0.01f, ui->squelchGate->value() * 0.01f,
ui->squelch->value() != -100, ui->squelch->value() != -100,
ui->agc->isChecked(),
force); force);
} }
} }
@ -448,6 +449,10 @@ void UDPSrcGUI::applySettings(bool force)
sampleFormat = UDPSrc::FormatAMMono; sampleFormat = UDPSrc::FormatAMMono;
ui->fmDeviation->setEnabled(false); ui->fmDeviation->setEnabled(false);
break; break;
case 8:
sampleFormat = UDPSrc::FormatAMNoDCMono;
ui->fmDeviation->setEnabled(false);
break;
default: default:
sampleFormat = UDPSrc::FormatS16LE; sampleFormat = UDPSrc::FormatS16LE;
ui->fmDeviation->setEnabled(false); ui->fmDeviation->setEnabled(false);
@ -544,6 +549,11 @@ void UDPSrcGUI::on_audioStereo_toggled(bool stereo __attribute__((unused)))
applySettingsImmediate(); applySettingsImmediate();
} }
void UDPSrcGUI::on_agc_toggled(bool agc __attribute__((unused)))
{
applySettingsImmediate();
}
void UDPSrcGUI::on_gain_valueChanged(int value) void UDPSrcGUI::on_gain_valueChanged(int value)
{ {
ui->gainText->setText(tr("%1").arg(value/10.0, 0, 'f', 1)); ui->gainText->setText(tr("%1").arg(value/10.0, 0, 'f', 1));

View File

@ -76,6 +76,7 @@ private slots:
void on_volume_valueChanged(int value); void on_volume_valueChanged(int value);
void on_squelch_valueChanged(int value); void on_squelch_valueChanged(int value);
void on_squelchGate_valueChanged(int value); void on_squelchGate_valueChanged(int value);
void on_agc_toggled(bool agc);
void tick(); void tick();
private: private:

View File

@ -333,6 +333,19 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="ButtonSwitch" name="agc">
<property name="toolTip">
<string>Toggle AGC (only for AM and SSB)</string>
</property>
<property name="text">
<string>AGC</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item> <item>
<widget class="QToolButton" name="audioActive"> <widget class="QToolButton" name="audioActive">
<property name="toolTip"> <property name="toolTip">
@ -504,6 +517,11 @@
<string>S16LE AM Mono</string> <string>S16LE AM Mono</string>
</property> </property>
</item> </item>
<item>
<property name="text">
<string>S16LE AM !DC Mono</string>
</property>
</item>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -782,6 +800,11 @@
<header>gui/valuedialz.h</header> <header>gui/valuedialz.h</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>ButtonSwitch</class>
<extends>QToolButton</extends>
<header>gui/buttonswitch.h</header>
</customwidget>
</customwidgets> </customwidgets>
<resources> <resources>
<include location="../../../sdrbase/resources/res.qrc"/> <include location="../../../sdrbase/resources/res.qrc"/>