From 6322cb49a4de396e51da05162e522b8e53aee465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Izzo?= Date: Thu, 26 May 2022 10:05:32 +0200 Subject: [PATCH] Cleanup demodulator, improved quantization log Unified offset and phase variable to reduce the chance of off-by-ones between buffers. Improved logging on linux and module17 adding quantization indices. TG-81 --- .../include/protocols/M17/M17Demodulator.h | 14 +++--- openrtx/src/protocols/M17/M17Demodulator.cpp | 43 ++++++++++--------- platform/drivers/audio/inputStream_linux.c | 3 +- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/openrtx/include/protocols/M17/M17Demodulator.h b/openrtx/include/protocols/M17/M17Demodulator.h index ac1c44a0..ebb2080e 100644 --- a/openrtx/include/protocols/M17/M17Demodulator.h +++ b/openrtx/include/protocols/M17/M17Demodulator.h @@ -118,13 +118,13 @@ private: static constexpr size_t M17_RX_SAMPLE_RATE = 24000; - static constexpr size_t M17_SAMPLES_PER_SYMBOL = M17_RX_SAMPLE_RATE / M17_SYMBOL_RATE; - static constexpr size_t M17_FRAME_SAMPLES = M17_FRAME_SYMBOLS * M17_SAMPLES_PER_SYMBOL; - static constexpr size_t M17_SAMPLE_BUF_SIZE = M17_FRAME_SAMPLES / 2; - static constexpr size_t M17_SYNCWORD_SAMPLES = M17_SAMPLES_PER_SYMBOL * M17_SYNCWORD_SYMBOLS; - static constexpr int8_t SYNC_SWEEP_WIDTH = 10; - static constexpr int8_t SYNC_SWEEP_OFFSET = ceil(SYNC_SWEEP_WIDTH / M17_SAMPLES_PER_SYMBOL); - static constexpr size_t M17_BRIDGE_SIZE = M17_SYNCWORD_SAMPLES + 2 * SYNC_SWEEP_WIDTH; + static constexpr size_t M17_SAMPLES_PER_SYMBOL = M17_RX_SAMPLE_RATE / M17_SYMBOL_RATE; + static constexpr size_t M17_FRAME_SAMPLES = M17_FRAME_SYMBOLS * M17_SAMPLES_PER_SYMBOL; + static constexpr size_t M17_SAMPLE_BUF_SIZE = M17_FRAME_SAMPLES / 2; + static constexpr size_t M17_SYNCWORD_SAMPLES = M17_SAMPLES_PER_SYMBOL * M17_SYNCWORD_SYMBOLS; + static constexpr int8_t SYNC_SWEEP_WIDTH = 10; + static constexpr int8_t SYNC_SWEEP_OFFSET = ceil(SYNC_SWEEP_WIDTH / M17_SAMPLES_PER_SYMBOL); + static constexpr int16_t M17_BRIDGE_SIZE = M17_SYNCWORD_SAMPLES + 2 * SYNC_SWEEP_WIDTH; static constexpr float CONV_STATS_ALPHA = 0.005f; static constexpr float CONV_THRESHOLD_FACTOR = 3.40; diff --git a/openrtx/src/protocols/M17/M17Demodulator.cpp b/openrtx/src/protocols/M17/M17Demodulator.cpp index 3592f719..2d3f40a6 100644 --- a/openrtx/src/protocols/M17/M17Demodulator.cpp +++ b/openrtx/src/protocols/M17/M17Demodulator.cpp @@ -92,6 +92,9 @@ static void *logFunc(void *arg) { dumpData = false; emptyCtr = 0; + #ifdef PLATFORM_LINUX + logRunning = false; + #endif } #ifdef PLATFORM_LINUX @@ -103,7 +106,7 @@ static void *logFunc(void *arg) entry.qnt_pos_avg, entry.qnt_neg_avg, entry.symbol, - entry.frame_index + entry.frame_index, entry.flags); fflush(csv_log); #else @@ -114,6 +117,7 @@ static void *logFunc(void *arg) #ifdef PLATFORM_LINUX fclose(csv_log); + exit(-1); #endif return NULL; @@ -217,6 +221,9 @@ void M17Demodulator::startBasebandSampling() void M17Demodulator::stopBasebandSampling() { inputStream_stop(basebandId); + phase = 0; + syncDetected = false; + locked = false; } void M17Demodulator::resetCorrelationStats() @@ -320,7 +327,7 @@ sync_t M17Demodulator::nextFrameSync(int32_t offset) log.qnt_neg_avg = 0.0; log.symbol = 0; log.frame_index = 0; - log.flags = 1; + log.flags = 0;//1; pushLog(log); #endif @@ -397,7 +404,7 @@ int32_t M17Demodulator::syncwordSweep(int32_t offset) log.qnt_neg_avg = 0.0; log.symbol = 0; log.frame_index = 0; - log.flags = 2; + log.flags = 0;//2; pushLog(log); #endif @@ -414,7 +421,7 @@ int32_t M17Demodulator::syncwordSweep(int32_t offset) bool M17Demodulator::update() { sync_t syncword = { 0, false }; - int32_t offset = syncDetected ? 0 : -(int32_t) M17_BRIDGE_SIZE; + phase = (syncDetected) ? phase % M17_SAMPLES_PER_SYMBOL : -M17_BRIDGE_SIZE; uint16_t decoded_syms = 0; // Read samples from the ADC @@ -439,12 +446,11 @@ bool M17Demodulator::update() // If we are not demodulating a syncword, search for one if (!syncDetected) { - syncword = nextFrameSync(offset); + syncword = nextFrameSync(phase); if (syncword.index != -1) // Valid syncword found { syncDetected = true; - offset = syncword.index + 1; - phase = 0; + phase = syncword.index + 1; frame_index = 0; decoded_syms = 0; } @@ -453,9 +459,8 @@ bool M17Demodulator::update() else { // Slice the input buffer to extract a frame and quantize - int32_t symbol_index = offset - + phase - + (M17_SAMPLES_PER_SYMBOL * decoded_syms); + int32_t symbol_index = phase + + (M17_SAMPLES_PER_SYMBOL * decoded_syms); if (symbol_index >= static_cast(baseband.len)) break; // Update quantization stats only on syncwords @@ -472,14 +477,15 @@ bool M17Demodulator::update() { log_entry_t log; log.sample = baseband.data[symbol_index + i]; - log.conv = 0; + log.conv = phase; log.conv_th = 0.0; log.sample_index = symbol_index + i; log.qnt_pos_avg = qnt_pos_avg / 1.5f; log.qnt_neg_avg = qnt_neg_avg / 1.5f; log.symbol = symbol; log.frame_index = frame_index; - log.flags = 3; + log.flags = 0; + if(i == 0) log.flags = 1; pushLog(log); } @@ -540,7 +546,7 @@ bool M17Demodulator::update() { // Find index (possibly negative) of the syncword int32_t expected_sync = - offset + phase + + phase + M17_SAMPLES_PER_SYMBOL * decoded_syms - M17_SYNCWORD_SAMPLES - SYNC_SWEEP_OFFSET * M17_SAMPLES_PER_SYMBOL; @@ -557,18 +563,15 @@ bool M17Demodulator::update() } } } - - // We are at the end of the buffer - if (syncDetected) - { - // Compute phase of next buffer - phase = (static_cast (phase) + offset + baseband.len) % M17_SAMPLES_PER_SYMBOL; - } // Copy last N samples to bridge buffer memcpy(basebandBridge, baseband.data + (baseband.len - M17_BRIDGE_SIZE), sizeof(int16_t) * M17_BRIDGE_SIZE); } + #ifdef PLATFORM_LINUX + if (baseband.data == NULL) + dumpData = true; + #endif return newFrame; } diff --git a/platform/drivers/audio/inputStream_linux.c b/platform/drivers/audio/inputStream_linux.c index 9dec0954..d4a55d29 100644 --- a/platform/drivers/audio/inputStream_linux.c +++ b/platform/drivers/audio/inputStream_linux.c @@ -77,7 +77,8 @@ dataBlock_t inputStream_getData(streamId id) if (read_items != block.len) { - exit(-1); + block.data = NULL; + return block; } return block;