mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-08-01 11:06:30 -04:00
POCSAG Processor Rewrite (#1437)
* WIP Refactoring * WordExtractor building * Fix buffer sizes and squelch execute * Move impls to cpp file * Baud indicator * WIP new bit extractor * New approach for bit extraction. * Code fit and finish * Fix case on button * Cleanup * Adjust rate miss threshold * Fix count bits error calculation.
This commit is contained in:
parent
9525738118
commit
31e8019642
13 changed files with 648 additions and 534 deletions
|
@ -26,6 +26,8 @@
|
|||
#ifndef __PROC_POCSAG2_H__
|
||||
#define __PROC_POCSAG2_H__
|
||||
|
||||
/* https://www.aaroncake.net/schoolpage/pocsag.htm */
|
||||
|
||||
#include "audio_output.hpp"
|
||||
#include "baseband_processor.hpp"
|
||||
#include "baseband_thread.hpp"
|
||||
|
@ -38,57 +40,17 @@
|
|||
#include "portapack_shared_memory.hpp"
|
||||
#include "rssi_thread.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
/* Takes audio stream and automatically normalizes it to +/-1.0f */
|
||||
/* Normalizes audio stream to +/-1.0f */
|
||||
class AudioNormalizer {
|
||||
public:
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if (val >= t_hi_)
|
||||
val = 1.0f;
|
||||
else if (val <= t_lo_)
|
||||
val = -1.0f;
|
||||
else
|
||||
val = 0.0;
|
||||
}
|
||||
}
|
||||
void execute_in_place(const buffer_f32_t& audio);
|
||||
|
||||
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;
|
||||
}
|
||||
void calculate_thresholds();
|
||||
|
||||
uint32_t counter_ = 0;
|
||||
float min_ = 99.0f;
|
||||
|
@ -97,24 +59,158 @@ class AudioNormalizer {
|
|||
float t_lo_ = 1.0;
|
||||
};
|
||||
|
||||
// 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?
|
||||
/* FIFO wrapper over a uint32_t's bits. */
|
||||
class BitQueue {
|
||||
public:
|
||||
void push(bool bit);
|
||||
bool pop();
|
||||
void reset();
|
||||
uint8_t size() const;
|
||||
uint32_t data() const;
|
||||
|
||||
class BitExtractor {};
|
||||
private:
|
||||
uint32_t data_ = 0;
|
||||
uint8_t count_ = 0;
|
||||
|
||||
class WordExtractor {};
|
||||
static constexpr uint8_t max_size_ = sizeof(data_) * 8;
|
||||
};
|
||||
|
||||
/* Extracts bits and bitrate from audio stream. */
|
||||
class BitExtractor {
|
||||
public:
|
||||
BitExtractor(BitQueue& bits)
|
||||
: bits_{bits} {}
|
||||
|
||||
void extract_bits(const buffer_f32_t& audio);
|
||||
void configure(uint32_t sample_rate);
|
||||
void reset();
|
||||
|
||||
uint16_t baud_rate() const;
|
||||
|
||||
private:
|
||||
/* Number of rate misses that would cause a rate update. */
|
||||
static constexpr uint8_t rate_miss_reset_threshold = 5;
|
||||
|
||||
/* Number of rate misses that would cause a rate update. */
|
||||
static constexpr uint8_t bad_transition_reset_threshold = 10;
|
||||
|
||||
struct BaudInfo {
|
||||
uint16_t baud_rate = 0;
|
||||
float bit_length = 0.0;
|
||||
float min_bit_length = 0.0;
|
||||
float max_bit_length = 0.0;
|
||||
};
|
||||
|
||||
/* Handle a transition, returns true if "good". */
|
||||
bool handle_transition();
|
||||
|
||||
/* Count the number of bits the length represents.
|
||||
* Returns true if valid given the current baud rate. */
|
||||
bool count_bits(uint32_t length, uint16_t& bit_count);
|
||||
|
||||
/* Gets the best baud info associated with the specified bit length. */
|
||||
const BaudInfo* get_baud_info(float bit_length) const;
|
||||
|
||||
std::array<BaudInfo, 3> known_rates_{
|
||||
BaudInfo{512},
|
||||
BaudInfo{1200},
|
||||
BaudInfo{2400}};
|
||||
|
||||
BitQueue& bits_;
|
||||
|
||||
uint32_t sample_rate_ = 0;
|
||||
uint16_t min_valid_length_ = 0;
|
||||
const BaudInfo* current_rate_ = nullptr;
|
||||
uint8_t rate_misses_ = 0;
|
||||
|
||||
float sample_ = 0.0;
|
||||
float last_sample_ = 0.0;
|
||||
float next_bit_center_ = 0.0;
|
||||
|
||||
uint32_t sample_index_ = 0;
|
||||
uint32_t last_transition_index_ = 0;
|
||||
uint32_t bad_transitions_ = 0;
|
||||
};
|
||||
|
||||
/* Extracts codeword batches from the BitQueue. */
|
||||
class CodewordExtractor {
|
||||
public:
|
||||
using batch_t = pocsag::batch_t;
|
||||
using batch_handler_t = std::function<void(CodewordExtractor&)>;
|
||||
|
||||
CodewordExtractor(BitQueue& bits, batch_handler_t on_batch)
|
||||
: bits_{bits}, on_batch_{on_batch} {}
|
||||
|
||||
/* Process the BitQueue to extract codeword batches. */
|
||||
void process_bits();
|
||||
|
||||
/* Pad then send any pending frames. */
|
||||
void flush();
|
||||
|
||||
/* Completely reset to prepare for a new message. */
|
||||
void reset();
|
||||
|
||||
/* Gets the underlying batch array. */
|
||||
const batch_t& batch() const { return batch_; }
|
||||
|
||||
/* Gets in-progress codeword. */
|
||||
uint32_t current() const { return data_; }
|
||||
|
||||
/* Gets the count of completed codewords. */
|
||||
uint8_t count() const { return word_count_; }
|
||||
|
||||
/* Returns true if the batch has as sync frame. */
|
||||
bool has_sync() const { return has_sync_; }
|
||||
|
||||
private:
|
||||
/* Sync frame codeword. */
|
||||
static constexpr uint32_t sync_codeword = 0x7cd215d8;
|
||||
|
||||
/* Idle codeword used to pad a 16 codeword "batch". */
|
||||
static constexpr uint32_t idle_codeword = 0x7a89c197;
|
||||
|
||||
/* Number of bits in 'data_' member. */
|
||||
static constexpr uint8_t data_bit_count = sizeof(uint32_t) * 8;
|
||||
|
||||
/* Clears data_ and bit_count_ to prepare for next codeword. */
|
||||
void clear_data_bits();
|
||||
|
||||
/* Pop a bit off the queue and add it to data_. */
|
||||
void take_one_bit();
|
||||
|
||||
/* Handles receiving the sync frame codeword, start of batch. */
|
||||
void handle_sync(bool inverted);
|
||||
|
||||
/* Saves the current codeword in data_ to the batch. */
|
||||
void save_current_codeword();
|
||||
|
||||
/* Sends the batch to the handler, resets for next batch. */
|
||||
void handle_batch_complete();
|
||||
|
||||
/* Fill the rest of the batch with 'idle' codewords. */
|
||||
void pad_idle();
|
||||
|
||||
BitQueue& bits_;
|
||||
batch_handler_t on_batch_{};
|
||||
|
||||
/* When true, sync frame has been received. */
|
||||
bool has_sync_ = false;
|
||||
|
||||
/* When true, bit vales are flipped in the codewords. */
|
||||
bool inverted_ = false;
|
||||
|
||||
uint32_t data_ = 0;
|
||||
uint8_t bit_count_ = 0;
|
||||
uint8_t word_count_ = 0;
|
||||
batch_t batch_{};
|
||||
};
|
||||
|
||||
/* Processes POCSAG signal into codeword batches. */
|
||||
class POCSAGProcessor : public BasebandProcessor {
|
||||
public:
|
||||
void execute(const buffer_c8_t& buffer) override;
|
||||
void on_message(const Message* const message) override;
|
||||
|
||||
int OnDataFrame(int len, int baud);
|
||||
int OnDataWord(uint32_t word, int pos);
|
||||
|
||||
private:
|
||||
static constexpr size_t baseband_fs = 3072000;
|
||||
static constexpr uint8_t stat_update_interval = 10;
|
||||
|
@ -122,106 +218,63 @@ class POCSAGProcessor : public BasebandProcessor {
|
|||
baseband_fs / stat_update_interval;
|
||||
|
||||
void configure();
|
||||
void flush();
|
||||
void reset();
|
||||
void send_stats() const;
|
||||
void send_packet();
|
||||
|
||||
// Set once app is ready to receive messages.
|
||||
/* Set once app is ready to receive messages. */
|
||||
bool configured = false;
|
||||
|
||||
// Buffer for decimated IQ data.
|
||||
std::array<complex16_t, 512> dst{};
|
||||
/* Buffer for decimated IQ data. */
|
||||
std::array<complex16_t, 256> dst{};
|
||||
const buffer_c16_t dst_buffer{dst.data(), dst.size()};
|
||||
|
||||
// Buffer for demodulated audio.
|
||||
std::array<float, 32> audio{};
|
||||
/* Buffer for demodulated audio. */
|
||||
std::array<float, 16> audio{};
|
||||
const buffer_f32_t audio_buffer{audio.data(), audio.size()};
|
||||
|
||||
// Decimate to 48kHz.
|
||||
/* Decimate to 48kHz. */
|
||||
dsp::decimate::FIRC8xR16x24FS4Decim8 decim_0{};
|
||||
dsp::decimate::FIRC16xR16x32Decim8 decim_1{};
|
||||
|
||||
// Filter to 24kHz and demodulate.
|
||||
/* 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.
|
||||
/* Squelch to ignore noise. */
|
||||
FMSquelch squelch{};
|
||||
uint64_t squelch_history = 0;
|
||||
|
||||
// Attempts to de-noise signal and normalize to +/- 1.0f.
|
||||
/* LPF to reduce noise. POCSAG supports 2400 baud, but that falls
|
||||
* nicely into the transition band of this 1800Hz filter.
|
||||
* scipy.signal.butter(2, 1800, "lowpass", fs=24000, analog=False) */
|
||||
IIRBiquadFilter lpf{{{0.04125354f, 0.082507070f, 0.04125354f},
|
||||
{1.00000000f, -1.34896775f, 0.51398189f}}};
|
||||
|
||||
/* Attempts to de-noise and normalize signal. */
|
||||
AudioNormalizer normalizer{};
|
||||
|
||||
// Handles writing audio stream to hardware.
|
||||
/* Handles writing audio stream to hardware. */
|
||||
AudioOutput audio_output{};
|
||||
|
||||
// Holds the data sent to the app.
|
||||
/* Holds the data sent to the app. */
|
||||
pocsag::POCSAGPacket packet{};
|
||||
|
||||
bool has_been_reset = true;
|
||||
/* Used to keep track of how many samples were processed
|
||||
* between status update messages. */
|
||||
uint32_t samples_processed = 0;
|
||||
|
||||
//--------------------------------------------------
|
||||
BitQueue bits{};
|
||||
|
||||
// ----------------------------------------
|
||||
// Frame extractraction methods and members
|
||||
// ----------------------------------------
|
||||
void initFrameExtraction();
|
||||
struct FIFOStruct {
|
||||
unsigned long codeword;
|
||||
int numBits;
|
||||
};
|
||||
/* Processes audio into bits. */
|
||||
BitExtractor bit_extractor{bits};
|
||||
|
||||
void resetVals();
|
||||
void setFrameExtractParams(long a_samplesPerSec, long a_maxBaud = 8000, long a_minBaud = 200, long maxRunOfSameValue = 32);
|
||||
|
||||
int processDemodulatedSamples(float* sampleBuff, int noOfSamples);
|
||||
int extractFrames();
|
||||
|
||||
void storeBit();
|
||||
short getBit();
|
||||
|
||||
int getNoOfBits();
|
||||
uint32_t getRate();
|
||||
|
||||
uint32_t m_averageSymbolLen_1024{0};
|
||||
uint32_t m_lastStableSymbolLen_1024{0};
|
||||
|
||||
uint32_t m_samplesPerSec{0};
|
||||
uint32_t m_goodTransitions{0};
|
||||
uint32_t m_badTransitions{0};
|
||||
|
||||
uint32_t m_sampleNo{0};
|
||||
float m_sample{0};
|
||||
float m_valMid{0.0f};
|
||||
float m_lastSample{0.0f};
|
||||
|
||||
uint32_t m_lastTransPos_1024{0};
|
||||
uint32_t m_lastSingleBitPos_1024{0};
|
||||
|
||||
uint32_t m_nextBitPosInt{0}; // Integer rounded up version to save on ops
|
||||
uint32_t m_nextBitPos_1024{0};
|
||||
uint32_t m_lastBitPos_1024{0};
|
||||
|
||||
uint32_t m_shortestGoodTrans_1024{0};
|
||||
uint32_t m_minSymSamples_1024{0};
|
||||
uint32_t m_maxSymSamples_1024{0};
|
||||
uint32_t m_maxRunOfSameValue{0};
|
||||
|
||||
static constexpr long BIT_BUF_SIZE = 64;
|
||||
std::bitset<64> m_bits{0};
|
||||
long m_bitsStart{0};
|
||||
long m_bitsEnd{0};
|
||||
|
||||
FIFOStruct m_fifo{0, 0};
|
||||
bool m_gotSync{false};
|
||||
int m_numCode{0};
|
||||
bool m_inverted{false};
|
||||
|
||||
//--------------------------------------------------
|
||||
/* Processes bits into codewords. */
|
||||
CodewordExtractor word_extractor{
|
||||
bits, [this](CodewordExtractor&) {
|
||||
send_packet();
|
||||
}};
|
||||
|
||||
/* NB: Threads should be the last members in the class definition. */
|
||||
BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue