From 3e922dbb0ae62ba675c841ca894207a182d5a8c2 Mon Sep 17 00:00:00 2001 From: f4exb Date: Tue, 12 May 2015 12:12:13 +0200 Subject: [PATCH] Added simple AGC for AM demod --- Readme.md | 2 + include-gpl/dsp/agc.h | 71 +++++++++++++++++++++++++++++++++ include-gpl/dsp/movingaverage.h | 7 ++++ plugins/channel/am/amdemod.cpp | 11 ++++- plugins/channel/am/amdemod.h | 2 + sdrbase/gui/glspectrumgui.ui | 51 ++++++++++++++++------- 6 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 include-gpl/dsp/agc.h diff --git a/Readme.md b/Readme.md index f63c97d9a..308ce8fb6 100644 --- a/Readme.md +++ b/Readme.md @@ -84,6 +84,8 @@ To Do ===== - Add the possibility to change the brightness and/or color of the grid. Sometimes it is barely visible yet useful + - Fix and possibly enhance (stereo, RDS?) WFM. Maybe into two plugins one for plain WFM and the other for Broadcast FM + - Make the the SSB filter frequency bounds tunable so that it can be used for CW. Change marker overlay accordingly. - Possibility to completely undock the receiver in a separate window. Useful when there are many receivers - Larger decimation capability for narrowband and very narrowband work (32, 64, ...) - Even more demods ... diff --git a/include-gpl/dsp/agc.h b/include-gpl/dsp/agc.h new file mode 100644 index 000000000..fadebf4eb --- /dev/null +++ b/include-gpl/dsp/agc.h @@ -0,0 +1,71 @@ +/* + * kissagc.h + * + * Created on: May 12, 2015 + * Author: f4exb + */ + +#ifndef INCLUDE_GPL_DSP_AGC_H_ +#define INCLUDE_GPL_DSP_AGC_H_ + +#include "movingaverage.h" + +class SimpleAGC +{ +public: + + SimpleAGC() : + m_squelch(false), + m_fill(0), + m_cutoff(0), + m_moving_average() + {} + + SimpleAGC(int historySize, Real initial, Real cutoff) : + m_squelch(false), + m_fill(initial), + m_cutoff(cutoff), + m_moving_average(historySize, initial) + {} + + void resize(int historySize, Real initial, Real cutoff) + { + m_fill = initial; + m_cutoff = cutoff; + m_moving_average.resize(historySize, initial); + } + + Real getValue() + { + return m_moving_average.average(); + } + + void feed(Real value) + { + if (value > m_cutoff) + { + m_moving_average.feed(value); + } + + m_squelch = true; + } + + void close() + { + if (m_squelch) + { + m_moving_average.fill(m_fill); + m_squelch = false; + } + } + +private: + bool m_squelch; + Real m_fill; + Real m_cutoff; + MovingAverage m_moving_average; +}; + + + +#endif /* INCLUDE_GPL_DSP_AGC_H_ */ diff --git a/include-gpl/dsp/movingaverage.h b/include-gpl/dsp/movingaverage.h index e608efbd6..38ae278a4 100644 --- a/include-gpl/dsp/movingaverage.h +++ b/include-gpl/dsp/movingaverage.h @@ -39,6 +39,13 @@ public: m_ptr = 0; } + void fill(Real value) + { + for(size_t i = 0; i < m_history.size(); i++) + m_history[i] = value; + m_sum = m_history.size() * value; + } + Real average() const { return m_sum / (Real)m_history.size(); diff --git a/plugins/channel/am/amdemod.cpp b/plugins/channel/am/amdemod.cpp index edaaaa0cd..802cce7a8 100644 --- a/plugins/channel/am/amdemod.cpp +++ b/plugins/channel/am/amdemod.cpp @@ -23,6 +23,8 @@ #include "dsp/dspcommands.h" #include "dsp/pidcontroller.h" +#include + MESSAGE_CLASS_DEFINITION(AMDemod::MsgConfigureAMDemod, Message) AMDemod::AMDemod(AudioFifo* audioFifo, SampleSink* sampleSink) : @@ -43,6 +45,7 @@ AMDemod::AMDemod(AudioFifo* audioFifo, SampleSink* sampleSink) : m_audioBufferFill = 0; m_movingAverage.resize(16, 0); + m_volumeAGC.resize(4096, 0.003, 0); } AMDemod::~AMDemod() @@ -55,6 +58,7 @@ void AMDemod::configure(MessageQueue* messageQueue, Real rfBandwidth, Real afBan cmd->submit(messageQueue, this); } +/* float arctan2(Real y, Real x) { Real coeff_1 = M_PI / 4; @@ -84,6 +88,7 @@ Real angleDist(Real a, Real b) return dist; } +*/ void AMDemod::feed(SampleVector::const_iterator begin, SampleVector::const_iterator end, bool firstOfBurst) { @@ -138,10 +143,14 @@ void AMDemod::feed(SampleVector::const_iterator begin, SampleVector::const_itera else if(demod > 1) demod = 1; + m_volumeAGC.feed(demod); + + demod *= (0.003 / m_volumeAGC.getValue()); demod *= m_running.m_volume; - sample = demod * 32700; + sample = demod * 32700 * 16; } else { + m_volumeAGC.close(); sample = 0; } diff --git a/plugins/channel/am/amdemod.h b/plugins/channel/am/amdemod.h index 2557970e4..c1a4663a9 100644 --- a/plugins/channel/am/amdemod.h +++ b/plugins/channel/am/amdemod.h @@ -23,6 +23,7 @@ #include "dsp/interpolator.h" #include "dsp/lowpass.h" #include "dsp/movingaverage.h" +#include "dsp/agc.h" #include "audio/audiofifo.h" #include "util/message.h" @@ -117,6 +118,7 @@ private: Real m_lastArgument; Complex m_lastSample; MovingAverage m_movingAverage; + SimpleAGC m_volumeAGC; AudioVector m_audioBuffer; uint m_audioBufferFill; diff --git a/sdrbase/gui/glspectrumgui.ui b/sdrbase/gui/glspectrumgui.ui index 2a9babe02..9f2033fb4 100644 --- a/sdrbase/gui/glspectrumgui.ui +++ b/sdrbase/gui/glspectrumgui.ui @@ -6,7 +6,7 @@ 0 0 - 215 + 234 94 @@ -14,7 +14,16 @@ Oscilloscope - + + 2 + + + 2 + + + 2 + + 2 @@ -258,19 +267,6 @@ 3 - - - - Qt::Horizontal - - - - 0 - 0 - - - - @@ -446,6 +442,31 @@ + + + + + 24 + 24 + + + + ArrowCursor + + + Grid intensity + + + 255 + + + 16 + + + 64 + + +