diff options
author | Christian Daniel <cd@maintech.de> | 2013-03-31 22:48:01 +0200 |
---|---|---|
committer | Christian Daniel <cd@maintech.de> | 2013-03-31 22:48:01 +0200 |
commit | ffe8034275774ca7e8d25798515ae92ccf7ddda2 (patch) | |
tree | 9a0f05790fab053470af6def7a169589b3b5752d | |
parent | 20a7a0ebe5ea70665399a6e18c5ff65eb571d72c (diff) |
add moving average to NFM squelch
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | include-gpl/dsp/movingaverage.h | 53 | ||||
-rw-r--r-- | plugins/demod/nfm/nfmdemod.cpp | 6 | ||||
-rw-r--r-- | plugins/demod/nfm/nfmdemod.h | 2 | ||||
-rw-r--r-- | sdrbase/dsp/movingaverage.cpp | 1 |
5 files changed, 63 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 941082a..209504e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ set(sdrbase_SOURCES sdrbase/dsp/interpolator.cpp sdrbase/dsp/inthalfbandfilter.cpp sdrbase/dsp/lowpass.cpp + sdrbase/dsp/movingaverage.cpp sdrbase/dsp/nco.cpp sdrbase/dsp/pidcontroller.cpp sdrbase/dsp/samplefifo.cpp @@ -97,6 +98,7 @@ set(sdrbase_HEADERS include-gpl/dsp/interpolator.h include-gpl/dsp/inthalfbandfilter.h include-gpl/dsp/lowpass.h + include-gpl/dsp/movingaverage.h include-gpl/dsp/nco.h sdrbase/dsp/pidcontroller.h include/dsp/samplefifo.h diff --git a/include-gpl/dsp/movingaverage.h b/include-gpl/dsp/movingaverage.h new file mode 100644 index 0000000..c08058b --- /dev/null +++ b/include-gpl/dsp/movingaverage.h @@ -0,0 +1,53 @@ +#ifndef INCLUDE_MOVINGAVERAGE_H +#define INCLUDE_MOVINGAVERAGE_H + +#include <vector> +#include "dsp/dsptypes.h" + +class MovingAverage { +public: + MovingAverage() : + m_history(), + m_sum(0), + m_ptr(0) + { + } + + MovingAverage(int historySize, Real initial) : + m_history(historySize, initial), + m_sum(historySize * initial), + m_ptr(0) + { + } + + void resize(int historySize, Real initial) + { + m_history.resize(historySize); + for(size_t i = 0; i < m_history.size(); i++) + m_history[i] = initial; + m_sum = m_history.size() * initial; + m_ptr = 0; + } + + void feed(Real value) + { + m_sum -= m_history[m_ptr]; + m_history[m_ptr] = value; + m_sum += value; + m_ptr++; + if(m_ptr >= m_history.size()) + m_ptr = 0; + } + + Real average() const + { + return m_sum / (Real)m_history.size(); + } + +protected: + std::vector<Real> m_history; + Real m_sum; + int m_ptr; +}; + +#endif // INCLUDE_MOVINGAVERAGE_H diff --git a/plugins/demod/nfm/nfmdemod.cpp b/plugins/demod/nfm/nfmdemod.cpp index 7324852..c6179ab 100644 --- a/plugins/demod/nfm/nfmdemod.cpp +++ b/plugins/demod/nfm/nfmdemod.cpp @@ -42,6 +42,8 @@ NFMDemod::NFMDemod(AudioFifo* audioFifo, SampleSink* sampleSink) : m_audioBuffer.resize(256); m_audioBufferFill = 0; + + m_movingAverage.resize(16, 0); } NFMDemod::~NFMDemod() @@ -69,7 +71,9 @@ void NFMDemod::feed(SampleVector::const_iterator begin, SampleVector::const_iter if(m_interpolator.interpolate(&m_sampleDistanceRemain, c, &consumed, &ci)) { m_sampleBuffer.push_back(Sample(ci.real() * 32768.0, ci.imag() * 32768.0)); - if((ci.real() * ci.real() + ci.imag() * ci.imag()) >= m_squelchLevel) + m_movingAverage.feed(ci.real() * ci.real() + ci.imag() * ci.imag()); + + if(m_movingAverage.average() >= m_squelchLevel) m_squelchState = m_sampleRate / 100; if(m_squelchState > 0) { diff --git a/plugins/demod/nfm/nfmdemod.h b/plugins/demod/nfm/nfmdemod.h index c3e1e6e..4826f33 100644 --- a/plugins/demod/nfm/nfmdemod.h +++ b/plugins/demod/nfm/nfmdemod.h @@ -23,6 +23,7 @@ #include "dsp/nco.h" #include "dsp/interpolator.h" #include "dsp/lowpass.h" +#include "dsp/movingaverage.h" #include "audio/audiofifo.h" #include "util/message.h" @@ -90,6 +91,7 @@ private: int m_squelchState; Complex m_lastSample; + MovingAverage m_movingAverage; AudioVector m_audioBuffer; uint m_audioBufferFill; diff --git a/sdrbase/dsp/movingaverage.cpp b/sdrbase/dsp/movingaverage.cpp new file mode 100644 index 0000000..dcf8d55 --- /dev/null +++ b/sdrbase/dsp/movingaverage.cpp @@ -0,0 +1 @@ +#include "dsp/movingaverage.h" |