Decode status widget (#1431)

* Initial cleanup of pocsag beta, using DSP filters

* Better filter params

* Better filter

* Add signal diagnostics widgets

* POCSAG procs sends stats messages

* Only draw 32 bits

* Add AudioNormalizer filter
This commit is contained in:
Kyle Reed 2023-09-03 21:49:44 -07:00 committed by GitHub
parent 2435ee780f
commit 4819a2f4e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 305 additions and 230 deletions

View file

@ -26,107 +26,87 @@
#ifndef __PROC_POCSAG2_H__
#define __PROC_POCSAG2_H__
#include "audio_output.hpp"
#include "baseband_processor.hpp"
#include "baseband_thread.hpp"
#include "rssi_thread.hpp"
#include "dsp_decimate.hpp"
#include "dsp_demodulate.hpp"
#include "pocsag_packet.hpp"
#include "pocsag.hpp"
#include "dsp_iir_config.hpp"
#include "message.hpp"
#include "audio_output.hpp"
#include "pocsag.hpp"
#include "pocsag_packet.hpp"
#include "portapack_shared_memory.hpp"
#include "rssi_thread.hpp"
#include <cstdint>
#include <bitset>
using namespace std;
// Class used to smooth demodulated waveform prior to decoding
// -----------------------------------------------------------
template <class ValType, class CalcType>
class SmoothVals {
protected:
ValType* m_lastVals; // Previous N values
int m_size; // The size N
CalcType m_sumVal; // Running sum of lastVals
int m_pos; // Current position in last vals ring buffer
int m_count; //
/* Takes audio stream and automatically normalizes it to +/-1.0f */
class AudioNormalizer {
public:
SmoothVals()
: m_lastVals(NULL), m_size(1), m_sumVal(0), m_pos(0), m_count(0) {
m_lastVals = new ValType[m_size];
}
// --------------------------------------------------
// --------------------------------------------------
virtual ~SmoothVals() {
delete[] m_lastVals;
}
SmoothVals(const SmoothVals<float, float>&) {
}
SmoothVals& operator=(const SmoothVals<float, float>&) {
return *this;
}
// --------------------------------------------------
// Set size of smoothing
// --------------------------------------------------
void SetSize(int size) {
m_size = std::max(size, 1);
m_pos = 0;
delete[] m_lastVals;
m_lastVals = new ValType[m_size];
m_sumVal = 0;
}
// --------------------------------------------------
// Get size of smoothing
// --------------------------------------------------
int Size() { return m_size; }
// --------------------------------------------------
// In place processing
// --------------------------------------------------
void Process(ValType* valBuff, int numVals) {
ValType tmpVal;
if (m_count > (1024 * 10)) {
// Recalculate the sum value occasionaly, stops accumulated errors when using float
m_count = 0;
m_sumVal = 0;
for (int i = 0; i < m_size; ++i) {
m_sumVal += (CalcType)m_lastVals[i];
}
void execute_in_place(const buffer_f32_t& audio) {
// Decay min/max every second (@24kHz).
if (counter_ >= 24'000) {
// 90% decay factor seems to work well.
// This keeps large transients from wrecking the filter.
max_ *= 0.9f;
min_ *= 0.9f;
counter_ = 0;
calculate_thresholds();
}
// Use a rolling smoothed value while processing the buffer
for (int buffPos = 0; buffPos < numVals; ++buffPos) {
m_pos++;
if (m_pos >= m_size) {
m_pos = 0;
counter_ += audio.count;
for (size_t i = 0; i < audio.count; ++i) {
auto& val = audio.p[i];
if (val > max_) {
max_ = val;
calculate_thresholds();
}
if (val < min_) {
min_ = val;
calculate_thresholds();
}
m_sumVal -= (CalcType)m_lastVals[m_pos]; // Subtract the oldest value
m_lastVals[m_pos] = valBuff[buffPos]; // Store the new value
m_sumVal += (CalcType)m_lastVals[m_pos]; // Add on the new value
tmpVal = (ValType)(m_sumVal / m_size); // Scale by number of values smoothed
valBuff[buffPos] = tmpVal;
if (val >= t_hi_)
val = 1.0f;
else if (val <= t_lo_)
val = -1.0f;
else
val = 0.0;
}
m_count += numVals;
}
private:
void calculate_thresholds() {
auto center = (max_ + min_) / 2.0f;
auto range = (max_ - min_) / 2.0f;
// 10% off center force either +/-1.0f.
// Higher == larger dead zone.
// Lower == more false positives.
auto threshold = range * 0.1;
t_hi_ = center + threshold;
t_lo_ = center - threshold;
}
uint32_t counter_ = 0;
float min_ = 99.0f;
float max_ = -99.0f;
float t_hi_ = 1.0;
float t_lo_ = 1.0;
};
// --------------------------------------------------
// Class to process base band data to pocsag frames
// --------------------------------------------------
// How to detect clock signal across baud rates?
// Maybe have a bit extraction state machine that reset
// then watches for the clocks, but there are multiple
// clock and the last one is the right one.
// So keep updating clock until a sync?
class BitExtractor {};
class WordExtractor {};
class POCSAGProcessor : public BasebandProcessor {
public:
void execute(const buffer_c8_t& buffer) override;
@ -137,28 +117,54 @@ class POCSAGProcessor : public BasebandProcessor {
private:
static constexpr size_t baseband_fs = 3072000;
std::array<complex16_t, 512> dst{};
const buffer_c16_t dst_buffer{
dst.data(),
dst.size()};
std::array<float, 32> audio{};
const buffer_f32_t audio_buffer{
audio.data(),
audio.size()};
dsp::decimate::FIRC8xR16x24FS4Decim8 decim_0{};
dsp::decimate::FIRC16xR16x32Decim8 decim_1{};
dsp::decimate::FIRAndDecimateComplex channel_filter{};
dsp::demodulate::FM demod{};
SmoothVals<float, float> smooth = {};
AudioOutput audio_output{};
bool configured = false;
pocsag::POCSAGPacket packet{};
static constexpr uint8_t stat_update_interval = 10;
static constexpr uint32_t stat_update_threshold =
baseband_fs / stat_update_interval;
void configure();
void send_stats() const;
// Set once app is ready to receive messages.
bool configured = false;
// Buffer for decimated IQ data.
std::array<complex16_t, 512> dst{};
const buffer_c16_t dst_buffer{dst.data(), dst.size()};
// Buffer for demodulated audio.
std::array<float, 32> audio{};
const buffer_f32_t audio_buffer{audio.data(), audio.size()};
// Decimate to 48kHz.
dsp::decimate::FIRC8xR16x24FS4Decim8 decim_0{};
dsp::decimate::FIRC16xR16x32Decim8 decim_1{};
// Filter to 24kHz and demodulate.
dsp::decimate::FIRAndDecimateComplex channel_filter{};
dsp::demodulate::FM demod{};
// LPF to reduce noise.
// scipy.signal.butter(2, 1800, "lowpass", fs=24000, analog=False)
IIRBiquadFilter lpf{{{0.04125354f, 0.082507070f, 0.04125354f},
{1.00000000f, -1.34896775f, 0.51398189f}}};
// Squelch to ignore noise.
FMSquelch squelch{};
uint64_t squelch_history = 0;
// Attempts to de-noise signal and normalize to +/- 1.0f.
AudioNormalizer normalizer{};
// Handles writing audio stream to hardware.
AudioOutput audio_output{};
// Holds the data sent to the app.
pocsag::POCSAGPacket packet{};
bool has_been_reset = true;
uint32_t samples_processed = 0;
//--------------------------------------------------
// ----------------------------------------
// Frame extractraction methods and members
@ -169,8 +175,6 @@ class POCSAGProcessor : public BasebandProcessor {
int numBits;
};
#define BIT_BUF_SIZE (64)
void resetVals();
void setFrameExtractParams(long a_samplesPerSec, long a_maxBaud = 8000, long a_minBaud = 200, long maxRunOfSameValue = 32);
@ -207,7 +211,8 @@ class POCSAGProcessor : public BasebandProcessor {
uint32_t m_maxSymSamples_1024{0};
uint32_t m_maxRunOfSameValue{0};
bitset<(size_t)BIT_BUF_SIZE> m_bits{0};
static constexpr long BIT_BUF_SIZE = 64;
std::bitset<64> m_bits{0};
long m_bitsStart{0};
long m_bitsEnd{0};
@ -216,8 +221,7 @@ class POCSAGProcessor : public BasebandProcessor {
int m_numCode{0};
bool m_inverted{false};
FMSquelch squelch_{};
uint64_t squelch_history = 0;
//--------------------------------------------------
/* NB: Threads should be the last members in the class definition. */
BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive};