TinyModem
Header-only software audio modem library
Loading...
Searching...
No Matches
tinymodem::CSSDecoder Class Reference

Chirp spread spectrum demodulator (LoRa-like): the PCM audio data is written to the decoder and the payload of the received frames with a correct CRC is written to the output. The input can be 8, 16 or 32 bit signed samples with any number of channels (only CSSConfig::channel is evaluated). More...

#include <CSSDecoder.h>

Inheritance diagram for tinymodem::CSSDecoder:
Collaboration diagram for tinymodem::CSSDecoder:

Classes

struct  Peak
 Peak of a spectrum. More...

Public Member Functions

 CSSDecoder ()=default
 CSSDecoder (Print &out)
CSSConfig defaultConfig ()
 Provides a default configuration: css7 (SF 7, 1000 Hz).
void setAudioInfo (audio_tools::AudioInfo info) override
 Defines the format of the input audio data.
audio_tools::AudioInfo audioInfo () override
bool begin (CSSConfig config)
 Starts the processing with the indicated configuration.
bool begin () override
void end () override
 Processes the remaining samples and reports the statistics.
void flush () override
 Processes the remaining samples (e.g. at the end of the input).
size_t write (const uint8_t *data, size_t len) override
 Provide PCM data to the decoder.
bool isCarrier () override
 true while a frame is received
bool isFinished () override
 true if the decoding was stopped (rx_one)
size_t frameCount ()
 Number of received frames with a correct CRC.
size_t errorCount ()
 Number of frames which could not be decoded.
float frequencyOffset ()
 Frequency offset of the last frame in Hz.
CSSConfig & config ()
 The configuration which was provided in begin().
 operator bool () override
Public Member Functions inherited from tinymodem::ModemDecoder
void setStatusOutput (Print &out)
 Defines the output for the ### CARRIER / ### NOCARRIER messages.
void setQuiet (bool quiet)
 Suppresses the status messages.
const char * mime () override

Protected Types

enum  State { SEARCH , SYNC , HEADER , PAYLOAD }

Protected Member Functions

void resetState ()
float noise ()
 uniform random value in the range -0.5 to 0.5
void addSample (float x)
 Mixes a sample to the baseband and resamples it to the bandwidth.
void addBaseband (float re, float im)
Peak spectrum (bool up, float offset)
float signedBin (float pos)
 signed value of a bin (-n/2 to n/2)
void processWindow ()
void search ()
 Preamble: the same bin in consecutive windows.
void sync ()
float estimateDrift ()
 Change of the preamble peaks per symbol (least squares fit).
void data ()
 Header and payload symbols.
void endOfCarrier (bool ok, int corrected=0)
Protected Member Functions inherited from tinymodem::ModemDecoder
void status (const char *fmt,...)

Protected Attributes

CSSConfig cfg
bool is_active = false
bool is_finished = false
PcmInput input
uint32_t noise_seed = 1
int n = 128
int m = 256
SimpleFFT fft
SampleRateConverter converter
float mix_re = 1
float mix_im = 0
float mix_step_re = 1
float mix_step_im = 0
uint32_t mix_count = 0
audio_tools::Vector< float > ring_re
audio_tools::Vector< float > ring_im
audio_tools::Vector< float > fft_re
audio_tools::Vector< float > fft_im
audio_tools::Vector< float > power
audio_tools::Vector< float > chirp_re
audio_tools::Vector< float > chirp_im
int64_t count = 0
int64_t next_start = 0
State state = SEARCH
int hits = 0
int candidate = 0
int sync_windows = 0
float last_up = 0
float ups [MAX_UPS]
int nups = 0
float drift = 0
float rate_factor = 1
float sync_offset = 0
float freq_offset = 0
audio_tools::Vector< uint16_t > symbols
int nsymbols = 0
int needed = 0
int frame_len = 0
float snr_total = 0
size_t frames = 0
size_t errors = 0
Protected Attributes inherited from tinymodem::ModemDecoder
Print * p_status = nullptr
bool is_quiet = false

Static Protected Attributes

static constexpr int RING = 4
static constexpr int OVERSAMPLING = 2
static constexpr int PREAMBLE_HITS = 3
static constexpr float TRACKING_GAIN = 0.3f
 tracking of the remaining offset per symbol
static constexpr float DRIFT_GAIN = 0.02f
 tracking of the remaining drift per symbol
static constexpr float MIN_DRIFT = 0.08f
 min drift in bins per symbol for the correction of the sample rate
static constexpr int MAX_UPS = 16
 max preamble windows for the drift estimation

Detailed Description

Chirp spread spectrum demodulator (LoRa-like): the PCM audio data is written to the decoder and the payload of the received frames with a correct CRC is written to the output. The input can be 8, 16 or 32 bit signed samples with any number of channels (only CSSConfig::channel is evaluated).

The signal is mixed to the complex baseband and resampled to the bandwidth, so that a symbol has N = 2^SF samples. A symbol is multiplied with the conjugate chirp (dechirped): the result is a tone, whose FFT bin is the symbol value. The preamble is detected by the same bin in consecutive windows; the bins of the up and down chirps provide the time and frequency offset. During the frame the timing follows a different clock of the sender.

The receiver must use the same ecc_bytes as the transmitter.

Member Enumeration Documentation

◆ State

Enumerator
SEARCH 
SYNC 
HEADER 
PAYLOAD 

Constructor & Destructor Documentation

◆ CSSDecoder() [1/2]

tinymodem::CSSDecoder::CSSDecoder ( )
default

◆ CSSDecoder() [2/2]

tinymodem::CSSDecoder::CSSDecoder ( Print & out)
inline

Member Function Documentation

◆ addBaseband()

void tinymodem::CSSDecoder::addBaseband ( float re,
float im )
inlineprotected

◆ addSample()

void tinymodem::CSSDecoder::addSample ( float x)
inlineprotected

Mixes a sample to the baseband and resamples it to the bandwidth.

◆ audioInfo()

audio_tools::AudioInfo tinymodem::CSSDecoder::audioInfo ( )
inlineoverride

◆ begin() [1/2]

bool tinymodem::CSSDecoder::begin ( )
inlineoverride

◆ begin() [2/2]

bool tinymodem::CSSDecoder::begin ( CSSConfig config)
inline

Starts the processing with the indicated configuration.

◆ config()

CSSConfig & tinymodem::CSSDecoder::config ( )
inline

The configuration which was provided in begin().

◆ data()

void tinymodem::CSSDecoder::data ( )
inlineprotected

Header and payload symbols.

◆ defaultConfig()

CSSConfig tinymodem::CSSDecoder::defaultConfig ( )
inline

Provides a default configuration: css7 (SF 7, 1000 Hz).

◆ end()

void tinymodem::CSSDecoder::end ( )
inlineoverride

Processes the remaining samples and reports the statistics.

◆ endOfCarrier()

void tinymodem::CSSDecoder::endOfCarrier ( bool ok,
int corrected = 0 )
inlineprotected

◆ errorCount()

size_t tinymodem::CSSDecoder::errorCount ( )
inline

Number of frames which could not be decoded.

◆ estimateDrift()

float tinymodem::CSSDecoder::estimateDrift ( )
inlineprotected

Change of the preamble peaks per symbol (least squares fit).

◆ flush()

void tinymodem::CSSDecoder::flush ( )
inlineoverridevirtual

Processes the remaining samples (e.g. at the end of the input).

Implements tinymodem::ModemDecoder.

◆ frameCount()

size_t tinymodem::CSSDecoder::frameCount ( )
inline

Number of received frames with a correct CRC.

◆ frequencyOffset()

float tinymodem::CSSDecoder::frequencyOffset ( )
inline

Frequency offset of the last frame in Hz.

◆ isCarrier()

bool tinymodem::CSSDecoder::isCarrier ( )
inlineoverridevirtual

true while a frame is received

Implements tinymodem::ModemDecoder.

◆ isFinished()

bool tinymodem::CSSDecoder::isFinished ( )
inlineoverridevirtual

true if the decoding was stopped (rx_one)

Implements tinymodem::ModemDecoder.

◆ noise()

float tinymodem::CSSDecoder::noise ( )
inlineprotected

uniform random value in the range -0.5 to 0.5

◆ operator bool()

tinymodem::CSSDecoder::operator bool ( )
inlineoverride

◆ processWindow()

void tinymodem::CSSDecoder::processWindow ( )
inlineprotected

◆ resetState()

void tinymodem::CSSDecoder::resetState ( )
inlineprotected

◆ search()

void tinymodem::CSSDecoder::search ( )
inlineprotected

Preamble: the same bin in consecutive windows.

◆ setAudioInfo()

void tinymodem::CSSDecoder::setAudioInfo ( audio_tools::AudioInfo info)
inlineoverride

Defines the format of the input audio data.

◆ signedBin()

float tinymodem::CSSDecoder::signedBin ( float pos)
inlineprotected

signed value of a bin (-n/2 to n/2)

◆ spectrum()

Peak tinymodem::CSSDecoder::spectrum ( bool up,
float offset )
inlineprotected

Dechirped spectrum of the window at next_start: up or down chirp, shifted by the frequency offset (in bins). The power of both parts of the chirp is added, which is robust against any time offset.

◆ sync()

void tinymodem::CSSDecoder::sync ( )
inlineprotected

Waits for the down chirps: up peak = tau + offset, down peak = -tau + offset

◆ write()

size_t tinymodem::CSSDecoder::write ( const uint8_t * data,
size_t len )
inlineoverride

Provide PCM data to the decoder.

Member Data Documentation

◆ candidate

int tinymodem::CSSDecoder::candidate = 0
protected

◆ cfg

CSSConfig tinymodem::CSSDecoder::cfg
protected

◆ chirp_im

audio_tools::Vector<float> tinymodem::CSSDecoder::chirp_im
protected

◆ chirp_re

audio_tools::Vector<float> tinymodem::CSSDecoder::chirp_re
protected

◆ converter

SampleRateConverter tinymodem::CSSDecoder::converter
protected

◆ count

int64_t tinymodem::CSSDecoder::count = 0
protected

◆ drift

float tinymodem::CSSDecoder::drift = 0
protected

◆ DRIFT_GAIN

float tinymodem::CSSDecoder::DRIFT_GAIN = 0.02f
staticconstexprprotected

tracking of the remaining drift per symbol

◆ errors

size_t tinymodem::CSSDecoder::errors = 0
protected

◆ fft

SimpleFFT tinymodem::CSSDecoder::fft
protected

◆ fft_im

audio_tools::Vector<float> tinymodem::CSSDecoder::fft_im
protected

◆ fft_re

audio_tools::Vector<float> tinymodem::CSSDecoder::fft_re
protected

◆ frame_len

int tinymodem::CSSDecoder::frame_len = 0
protected

◆ frames

size_t tinymodem::CSSDecoder::frames = 0
protected

◆ freq_offset

float tinymodem::CSSDecoder::freq_offset = 0
protected

◆ hits

int tinymodem::CSSDecoder::hits = 0
protected

◆ input

PcmInput tinymodem::CSSDecoder::input
protected

◆ is_active

bool tinymodem::CSSDecoder::is_active = false
protected

◆ is_finished

bool tinymodem::CSSDecoder::is_finished = false
protected

◆ last_up

float tinymodem::CSSDecoder::last_up = 0
protected

◆ m

int tinymodem::CSSDecoder::m = 256
protected

◆ MAX_UPS

int tinymodem::CSSDecoder::MAX_UPS = 16
staticconstexprprotected

max preamble windows for the drift estimation

◆ MIN_DRIFT

float tinymodem::CSSDecoder::MIN_DRIFT = 0.08f
staticconstexprprotected

min drift in bins per symbol for the correction of the sample rate

◆ mix_count

uint32_t tinymodem::CSSDecoder::mix_count = 0
protected

◆ mix_im

float tinymodem::CSSDecoder::mix_im = 0
protected

◆ mix_re

float tinymodem::CSSDecoder::mix_re = 1
protected

◆ mix_step_im

float tinymodem::CSSDecoder::mix_step_im = 0
protected

◆ mix_step_re

float tinymodem::CSSDecoder::mix_step_re = 1
protected

◆ n

int tinymodem::CSSDecoder::n = 128
protected

◆ needed

int tinymodem::CSSDecoder::needed = 0
protected

◆ next_start

int64_t tinymodem::CSSDecoder::next_start = 0
protected

◆ noise_seed

uint32_t tinymodem::CSSDecoder::noise_seed = 1
protected

◆ nsymbols

int tinymodem::CSSDecoder::nsymbols = 0
protected

◆ nups

int tinymodem::CSSDecoder::nups = 0
protected

◆ OVERSAMPLING

int tinymodem::CSSDecoder::OVERSAMPLING = 2
staticconstexprprotected

baseband samples per chip: the two parts of a chirp (before and after the wrap around) are separate tones, which makes the detection robust against a fractional time offset

◆ power

audio_tools::Vector<float> tinymodem::CSSDecoder::power
protected

◆ PREAMBLE_HITS

int tinymodem::CSSDecoder::PREAMBLE_HITS = 3
staticconstexprprotected

◆ rate_factor

float tinymodem::CSSDecoder::rate_factor = 1
protected

◆ RING

int tinymodem::CSSDecoder::RING = 4
staticconstexprprotected

◆ ring_im

audio_tools::Vector<float> tinymodem::CSSDecoder::ring_im
protected

◆ ring_re

audio_tools::Vector<float> tinymodem::CSSDecoder::ring_re
protected

◆ snr_total

float tinymodem::CSSDecoder::snr_total = 0
protected

◆ state

State tinymodem::CSSDecoder::state = SEARCH
protected

◆ symbols

audio_tools::Vector<uint16_t> tinymodem::CSSDecoder::symbols
protected

◆ sync_offset

float tinymodem::CSSDecoder::sync_offset = 0
protected

◆ sync_windows

int tinymodem::CSSDecoder::sync_windows = 0
protected

◆ TRACKING_GAIN

float tinymodem::CSSDecoder::TRACKING_GAIN = 0.3f
staticconstexprprotected

tracking of the remaining offset per symbol

◆ ups

float tinymodem::CSSDecoder::ups[MAX_UPS]
protected

The documentation for this class was generated from the following file: