Add proper DC-bias removal, tuned EMAs

This commit is contained in:
Niccolò Izzo 2022-04-12 19:27:23 +02:00 committed by Silvano Seva
parent da5c453852
commit d1651a702c
2 changed files with 19 additions and 15 deletions

View File

@ -32,6 +32,7 @@
#include <cstddef> #include <cstddef>
#include <interfaces/audio_stream.h> #include <interfaces/audio_stream.h>
#include <hwconfig.h> #include <hwconfig.h>
#include <dsp.h>
#include <deque> #include <deque>
#include "M17Datatypes.h" #include "M17Datatypes.h"
@ -122,9 +123,8 @@ private:
static constexpr size_t M17_SAMPLE_BUF_SIZE = M17_FRAME_SAMPLES / 2; static constexpr size_t M17_SAMPLE_BUF_SIZE = M17_FRAME_SAMPLES / 2;
static constexpr size_t M17_BRIDGE_SIZE = M17_SAMPLES_PER_SYMBOL * M17_SYNCWORD_SYMBOLS; static constexpr size_t M17_BRIDGE_SIZE = M17_SAMPLES_PER_SYMBOL * M17_SYNCWORD_SYMBOLS;
static constexpr size_t M17_CONV_THRESHOLD = 50000; static constexpr float CONV_STATS_ALPHA = 0.01f;
static constexpr float CONV_STATS_ALPHA = 0.001f; static constexpr float QNT_STATS_ALPHA = 0.995f;
static constexpr float QNT_STATS_ALPHA = 0.9f;
static constexpr float CONV_THRESHOLD_FACTOR = 3.40; static constexpr float CONV_THRESHOLD_FACTOR = 3.40;
/** /**
@ -159,7 +159,6 @@ private:
/* /*
* Convolution statistics computation * Convolution statistics computation
*/ */
float conv_ema = 0.0f; /// Exponential moving average from all the correlation values.
float conv_emvar = 0.0f; float conv_emvar = 0.0f;
/* /*
@ -169,6 +168,11 @@ private:
float qnt_max = 0.0f; ///< Max hold of the sliced samples float qnt_max = 0.0f; ///< Max hold of the sliced samples
float qnt_min = 0.0f; ///< Min hold of the sliced samples. float qnt_min = 0.0f; ///< Min hold of the sliced samples.
/*
* DSP filter state
*/
filter_state_t dsp_state;
/** /**
* Resets the exponential mean and variance/stddev computation. * Resets the exponential mean and variance/stddev computation.
*/ */

View File

@ -93,6 +93,8 @@ void M17Demodulator::startBasebandSampling()
M17_RX_SAMPLE_RATE); M17_RX_SAMPLE_RATE);
// Clean start of the demodulation statistics // Clean start of the demodulation statistics
resetCorrelationStats(); resetCorrelationStats();
// DC removal filter reset
dsp_resetFilterState(&dsp_state);
} }
void M17Demodulator::stopBasebandSampling() void M17Demodulator::stopBasebandSampling()
@ -102,8 +104,7 @@ void M17Demodulator::stopBasebandSampling()
void M17Demodulator::resetCorrelationStats() void M17Demodulator::resetCorrelationStats()
{ {
conv_ema = 0.0f; conv_emvar = 80000000.0f;
conv_emvar = 800000000.0f;
} }
/** /**
@ -112,10 +113,8 @@ void M17Demodulator::resetCorrelationStats()
*/ */
void M17Demodulator::updateCorrelationStats(int32_t value) void M17Demodulator::updateCorrelationStats(int32_t value)
{ {
float delta = (float) value - conv_ema; float incr = CONV_STATS_ALPHA * static_cast<float>(value);
float incr = CONV_STATS_ALPHA * delta; conv_emvar = (1.0f - CONV_STATS_ALPHA) * (conv_emvar + static_cast<float>(value) * incr);
conv_ema += incr;
conv_emvar = (1.0f - CONV_STATS_ALPHA) * (conv_emvar + delta * incr);
} }
float M17Demodulator::getCorrelationStddev() float M17Demodulator::getCorrelationStddev()
@ -201,21 +200,19 @@ sync_t M17Demodulator::nextFrameSync(int32_t offset)
fprintf(csv_log, "%" PRId16 ",%d,%f,%d\n", fprintf(csv_log, "%" PRId16 ",%d,%f,%d\n",
sample, sample,
conv - static_cast< int32_t >(conv_ema), conv,
CONV_THRESHOLD_FACTOR * getCorrelationStddev(), CONV_THRESHOLD_FACTOR * getCorrelationStddev(),
i); i);
#endif #endif
// Positive correlation peak -> frame syncword // Positive correlation peak -> frame syncword
if ((conv - static_cast< int32_t >(conv_ema)) > if (conv > (getCorrelationStddev() * CONV_THRESHOLD_FACTOR))
(getCorrelationStddev() * CONV_THRESHOLD_FACTOR))
{ {
syncword.lsf = false; syncword.lsf = false;
syncword.index = i; syncword.index = i;
} }
// Negative correlation peak -> LSF syncword // Negative correlation peak -> LSF syncword
else if ((conv - static_cast< int32_t >(conv_ema)) < else if (conv < -(getCorrelationStddev() * CONV_THRESHOLD_FACTOR))
-(getCorrelationStddev() * CONV_THRESHOLD_FACTOR))
{ {
syncword.lsf = true; syncword.lsf = true;
syncword.index = i; syncword.index = i;
@ -278,6 +275,9 @@ bool M17Demodulator::update()
// Read samples from the ADC // Read samples from the ADC
baseband = inputStream_getData(basebandId); baseband = inputStream_getData(basebandId);
// Apply DC removal filter
dsp_dcRemoval(&dsp_state, baseband.data, baseband.len);
#ifdef PLATFORM_LINUX #ifdef PLATFORM_LINUX
FILE *csv_log = fopen("demod_log_2.csv", "a"); FILE *csv_log = fopen("demod_log_2.csv", "a");
#endif #endif