2023-08-31 02:05:49 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1996 Thomas Sailer (sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu)
|
|
|
|
* Copyright (C) 2012-2014 Elias Oenal (multimon-ng@eliasoenal.com)
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
|
|
|
* Copyright (C) 2023 Kyle Reed
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __PROC_POCSAG2_H__
|
|
|
|
#define __PROC_POCSAG2_H__
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* https://www.aaroncake.net/schoolpage/pocsag.htm */
|
|
|
|
|
2023-09-04 00:49:44 -04:00
|
|
|
#include "audio_output.hpp"
|
2023-08-31 02:05:49 -04:00
|
|
|
#include "baseband_processor.hpp"
|
|
|
|
#include "baseband_thread.hpp"
|
|
|
|
#include "dsp_decimate.hpp"
|
|
|
|
#include "dsp_demodulate.hpp"
|
2023-09-04 00:49:44 -04:00
|
|
|
#include "dsp_iir_config.hpp"
|
2023-08-31 02:05:49 -04:00
|
|
|
#include "message.hpp"
|
2023-09-04 00:49:44 -04:00
|
|
|
#include "pocsag.hpp"
|
|
|
|
#include "pocsag_packet.hpp"
|
2023-08-31 02:05:49 -04:00
|
|
|
#include "portapack_shared_memory.hpp"
|
2023-09-04 00:49:44 -04:00
|
|
|
#include "rssi_thread.hpp"
|
2023-08-31 02:05:49 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
#include <array>
|
2023-08-31 02:05:49 -04:00
|
|
|
#include <cstdint>
|
2023-09-08 13:41:09 -04:00
|
|
|
#include <functional>
|
2023-08-31 02:05:49 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Normalizes audio stream to +/-1.0f */
|
2023-09-04 00:49:44 -04:00
|
|
|
class AudioNormalizer {
|
2023-08-31 02:05:49 -04:00
|
|
|
public:
|
2023-09-08 13:41:09 -04:00
|
|
|
void execute_in_place(const buffer_f32_t& audio);
|
2023-08-31 02:05:49 -04:00
|
|
|
|
2023-09-04 00:49:44 -04:00
|
|
|
private:
|
2023-09-08 13:41:09 -04:00
|
|
|
void calculate_thresholds();
|
2023-09-04 00:49:44 -04:00
|
|
|
|
|
|
|
uint32_t counter_ = 0;
|
|
|
|
float min_ = 99.0f;
|
|
|
|
float max_ = -99.0f;
|
|
|
|
float t_hi_ = 1.0;
|
|
|
|
float t_lo_ = 1.0;
|
2023-08-31 02:05:49 -04:00
|
|
|
};
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t data_ = 0;
|
|
|
|
uint8_t count_ = 0;
|
|
|
|
|
|
|
|
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:
|
2023-09-09 09:18:42 -04:00
|
|
|
/* Clock signal detection magic number. */
|
|
|
|
static constexpr uint32_t clock_magic_number = 0xAAAAAAAA;
|
2023-09-08 13:41:09 -04:00
|
|
|
|
2023-09-09 09:18:42 -04:00
|
|
|
struct RateInfo {
|
2023-09-09 11:49:22 -04:00
|
|
|
enum class State : uint8_t {
|
|
|
|
WaitForSample,
|
|
|
|
ReadyToSend
|
|
|
|
};
|
|
|
|
|
2023-09-09 09:18:42 -04:00
|
|
|
const int16_t baud_rate = 0;
|
|
|
|
float sample_interval = 0.0;
|
2023-09-08 13:41:09 -04:00
|
|
|
|
2023-09-09 11:49:22 -04:00
|
|
|
State state = State::WaitForSample;
|
2023-09-09 09:18:42 -04:00
|
|
|
float samples_until_next = 0.0;
|
2023-09-09 11:49:22 -04:00
|
|
|
bool prev_value = false;
|
|
|
|
bool is_stable = false;
|
2023-09-09 09:18:42 -04:00
|
|
|
BitQueue bits{};
|
2023-09-08 13:41:09 -04:00
|
|
|
|
2023-09-09 11:49:22 -04:00
|
|
|
/* Updates a rate info with the given sample.
|
|
|
|
* Returns true if the rate info has a new bit in its queue. */
|
|
|
|
bool handle_sample(float sample);
|
|
|
|
void reset();
|
|
|
|
};
|
2023-09-08 13:41:09 -04:00
|
|
|
|
2023-09-09 09:18:42 -04:00
|
|
|
std::array<RateInfo, 3> known_rates_{
|
|
|
|
RateInfo{512},
|
|
|
|
RateInfo{1200},
|
|
|
|
RateInfo{2400}};
|
2023-09-08 13:41:09 -04:00
|
|
|
|
|
|
|
BitQueue& bits_;
|
|
|
|
|
|
|
|
uint32_t sample_rate_ = 0;
|
2023-09-09 09:18:42 -04:00
|
|
|
RateInfo* current_rate_ = nullptr;
|
2023-09-08 13:41:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Extracts codeword batches from the BitQueue. */
|
|
|
|
class CodewordExtractor {
|
|
|
|
public:
|
|
|
|
using batch_t = pocsag::batch_t;
|
|
|
|
using batch_handler_t = std::function<void(CodewordExtractor&)>;
|
2023-09-04 00:49:44 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
CodewordExtractor(BitQueue& bits, batch_handler_t on_batch)
|
|
|
|
: bits_{bits}, on_batch_{on_batch} {}
|
2023-09-04 00:49:44 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* 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;
|
2023-09-04 00:49:44 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* 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. */
|
2023-08-31 02:05:49 -04:00
|
|
|
class POCSAGProcessor : public BasebandProcessor {
|
|
|
|
public:
|
|
|
|
void execute(const buffer_c8_t& buffer) override;
|
|
|
|
void on_message(const Message* const message) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr size_t baseband_fs = 3072000;
|
2023-09-04 00:49:44 -04:00
|
|
|
static constexpr uint8_t stat_update_interval = 10;
|
|
|
|
static constexpr uint32_t stat_update_threshold =
|
|
|
|
baseband_fs / stat_update_interval;
|
2023-08-31 02:05:49 -04:00
|
|
|
|
2023-09-04 00:49:44 -04:00
|
|
|
void configure();
|
2023-09-08 13:41:09 -04:00
|
|
|
void flush();
|
|
|
|
void reset();
|
2023-09-04 00:49:44 -04:00
|
|
|
void send_stats() const;
|
2023-09-08 13:41:09 -04:00
|
|
|
void send_packet();
|
2023-09-04 00:49:44 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Set once app is ready to receive messages. */
|
2023-09-04 00:49:44 -04:00
|
|
|
bool configured = false;
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Buffer for decimated IQ data. */
|
|
|
|
std::array<complex16_t, 256> dst{};
|
2023-09-04 00:49:44 -04:00
|
|
|
const buffer_c16_t dst_buffer{dst.data(), dst.size()};
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Buffer for demodulated audio. */
|
|
|
|
std::array<float, 16> audio{};
|
2023-09-04 00:49:44 -04:00
|
|
|
const buffer_f32_t audio_buffer{audio.data(), audio.size()};
|
2023-08-31 02:05:49 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Decimate to 48kHz. */
|
2023-08-31 02:05:49 -04:00
|
|
|
dsp::decimate::FIRC8xR16x24FS4Decim8 decim_0{};
|
|
|
|
dsp::decimate::FIRC16xR16x32Decim8 decim_1{};
|
2023-09-04 00:49:44 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Filter to 24kHz and demodulate. */
|
2023-08-31 02:05:49 -04:00
|
|
|
dsp::decimate::FIRAndDecimateComplex channel_filter{};
|
|
|
|
dsp::demodulate::FM demod{};
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Squelch to ignore noise. */
|
2023-09-04 00:49:44 -04:00
|
|
|
FMSquelch squelch{};
|
|
|
|
uint64_t squelch_history = 0;
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* 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. */
|
2023-09-04 00:49:44 -04:00
|
|
|
AudioNormalizer normalizer{};
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Handles writing audio stream to hardware. */
|
2023-08-31 02:05:49 -04:00
|
|
|
AudioOutput audio_output{};
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Holds the data sent to the app. */
|
2023-08-31 02:05:49 -04:00
|
|
|
pocsag::POCSAGPacket packet{};
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Used to keep track of how many samples were processed
|
|
|
|
* between status update messages. */
|
2023-09-04 00:49:44 -04:00
|
|
|
uint32_t samples_processed = 0;
|
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
BitQueue bits{};
|
2023-08-31 02:05:49 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Processes audio into bits. */
|
|
|
|
BitExtractor bit_extractor{bits};
|
2023-08-31 02:05:49 -04:00
|
|
|
|
2023-09-08 13:41:09 -04:00
|
|
|
/* Processes bits into codewords. */
|
|
|
|
CodewordExtractor word_extractor{
|
|
|
|
bits, [this](CodewordExtractor&) {
|
|
|
|
send_packet();
|
|
|
|
}};
|
2023-08-31 02:05:49 -04:00
|
|
|
|
|
|
|
/* NB: Threads should be the last members in the class definition. */
|
|
|
|
BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive};
|
|
|
|
RSSIThread rssi_thread{};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /*__PROC_POCSAG2_H__*/
|