43 if (info.sample_rate ==
cfg.sample_rate && info.channels ==
cfg.channels &&
44 info.bits_per_sample ==
cfg.bits_per_sample)
46 cfg.sample_rate = info.sample_rate;
47 cfg.channels = info.channels;
48 cfg.bits_per_sample = info.bits_per_sample;
54 using audio_tools::AudioDecoder::begin;
64 if (!
cfg.isValid())
return false;
75 int first =
cfg.firstCarrier();
122 size_t write(
const uint8_t* data,
size_t len)
override {
124 input.write(data, len, [
this](
float x) {
125 if (
cfg.rx_noise != 0.0f) x +=
noise() *
cfg.rx_noise * 2.0f;
171 audio_tools::Vector<int8_t>
soft;
190 for (
size_t j = 0; j <
soft.size(); j++)
soft[j] = 0;
209 for (
int j = 0; j <
phases; j++) {
220 return ((
noise_seed >> 8) & 0xFFFF) / 65535.0f - 0.5f;
242 for (
int j = 0; j <
width; j++) {
247 int8_t block_bits[64 * 8];
248 float slice_best = 0;
249 int slice_best_offset = 0;
250 uint64_t slice_block = 0;
251 float slice_noise = 0, slice_snr = 0;
252 for (
int o = 0; o <
offsets; o++) {
257 float signal = 0, noise_energy = 0;
259 for (
int b = 0; b <
bits; b++) {
261 uint8_t ch =
codec.decodeChar(block_bits, b, peak,
noise);
262 block |= (uint64_t)ch << (8 * b);
264 noise_energy +=
noise;
267 noise_energy /=
bits;
268 float weight = 1.0f /
cfg.sync_integration;
271 s += weight * (signal - s);
272 n += weight * (noise_energy - n);
273 if (s > slice_best) {
275 slice_best_offset = o;
278 slice_snr = signal / sqrtf(noise_energy > 0 ? noise_energy : 1e-9f);
282 int8_t* best_buffer =
297 if (dist < 0) dist +=
phases;
311 for (
int k = 0; k <
tones; k++) {
313 float e = tone_energy[2 * k];
316 for (
int b = 0; b <
bits; b++) sum[b] += (symbol >> b) & 1 ? -e : e;
318 for (
int b = 0; b <
bits; b++)
319 out[b] = total > 0 ? (int8_t)lroundf(127.0f * sum[b] / total) : 0;
328 for (
int b = 0; b <
bits; b++)
329 if (block[t *
bits + b] < 0) symbol |= 1 << b;
330 if (symbol == prev) result++;
341 memcpy(block, buffer + start *
bits, n1);
342 memcpy(block + n1, buffer, start *
bits);
352 for (
int d = -1; d <= 1; d += 2) {
358 if (
snr <
cfg.sync_threshold ||
366 uint64_t block =
blocks[use];
367 for (
int b = 0; b <
bits; b++)
outputChar((block >> (8 * b)) & 0xFF);
371 if (!
cfg.contestia) {
375 }
else if (ch == 127) {
382 if (p_print !=
nullptr) p_print->write(ch);
391 status(
"### CARRIER %s %d/%d @ %.1f Hz ###\n",
392 cfg.contestia ?
"contestia" :
"olivia",
cfg.tones,
cfg.bandwidth,
399 status(
"\n### NOCARRIER ndata=%u blocks=%u snr=%.1f freq=%.1f Hz ###\n",
Common API of the demodulators (FSKDecoder, MFSKDecoder): PCM audio data is written to the decoder an...
Definition ModemBase.h:344
void status(const char *fmt,...)
Definition ModemBase.h:367
The forward error correction of Olivia and Contestia (as in fldigi): each character of a block of bit...
Definition Olivia.h:213
static uint8_t binaryCode(uint8_t gray)
Definition Olivia.h:289
audio_tools::Vector< float > block_snr
Definition OliviaDecoder.h:176
audio_tools::AudioInfo audioInfo() override
Definition OliviaDecoder.h:52
void linearize(const int8_t *buffer, int8_t *block)
Copies the circular soft bit buffer: the oldest symbol first.
Definition OliviaDecoder.h:337
int slice
Definition OliviaDecoder.h:169
audio_tools::Vector< float > sync_signal
Definition OliviaDecoder.h:174
float snr_total
Definition OliviaDecoder.h:186
bool begin(OliviaConfig config)
Starts the processing with the indicated configuration.
Definition OliviaDecoder.h:57
void resetState()
Definition OliviaDecoder.h:188
audio_tools::Vector< float > energy
Definition OliviaDecoder.h:166
audio_tools::Vector< float > fft_im
Definition OliviaDecoder.h:166
audio_tools::Vector< uint64_t > blocks
Definition OliviaDecoder.h:175
void startOfCarrier()
Definition OliviaDecoder.h:385
void flush() override
Processes the remaining samples (e.g. at the end of the input).
Definition OliviaDecoder.h:110
OliviaConfig cfg
Definition OliviaDecoder.h:154
int phases
Definition OliviaDecoder.h:163
uint32_t noise_seed
Definition OliviaDecoder.h:161
SimpleFFT fft
Definition OliviaDecoder.h:160
int sample_pos
Definition OliviaDecoder.h:167
int best_offset
Definition OliviaDecoder.h:179
int soft_pos
Definition OliviaDecoder.h:172
int repeatedSymbols(const int8_t *block)
Number of symbols which are equal to the previous symbol (hard decision).
Definition OliviaDecoder.h:323
bool is_finished
Definition OliviaDecoder.h:156
audio_tools::Vector< float > sync_noise
Definition OliviaDecoder.h:174
int first_bin
Definition OliviaDecoder.h:163
void softDecode(const float *tone_energy, int8_t *out)
Soft bits of a symbol from the tone energies (like fldigi).
Definition OliviaDecoder.h:308
float frequencyOffset()
Frequency offset of the received signal in Hz.
Definition OliviaDecoder.h:141
int tones
Definition OliviaDecoder.h:162
OliviaDecoder(Print &out)
Definition OliviaDecoder.h:36
float best_signal
Definition OliviaDecoder.h:178
int block_len
Definition OliviaDecoder.h:162
float noise()
uniform random value in the range -0.5 to 0.5
Definition OliviaDecoder.h:218
void endOfCarrier()
Definition OliviaDecoder.h:396
int symbol_len
Definition OliviaDecoder.h:162
float signalToNoise()
Signal to noise ratio of the synchronizer.
Definition OliviaDecoder.h:138
void setAudioInfo(audio_tools::AudioInfo info) override
Defines the format of the input audio data.
Definition OliviaDecoder.h:42
void outputChar(uint8_t ch)
Definition OliviaDecoder.h:370
bool begin() override
Definition OliviaDecoder.h:62
unsigned nblocks
Definition OliviaDecoder.h:185
audio_tools::Vector< float > samples
Definition OliviaDecoder.h:166
audio_tools::Vector< float > fft_re
Definition OliviaDecoder.h:166
static constexpr int SLICES
Definition OliviaDecoder.h:151
audio_tools::Vector< int8_t > soft
Definition OliviaDecoder.h:171
int phase
Definition OliviaDecoder.h:177
int offsets
Definition OliviaDecoder.h:163
int margin
Definition OliviaDecoder.h:163
void resetSync()
Forgets the timing and frequency of the last transmission.
Definition OliviaDecoder.h:206
bool isCarrier() override
true while a transmission is received
Definition OliviaDecoder.h:132
void evaluateBlock()
Provides the best block if the signal to noise ratio is sufficient.
Definition OliviaDecoder.h:346
int slice_countdown
Definition OliviaDecoder.h:168
float snr
Definition OliviaDecoder.h:180
OliviaCodec codec
Definition OliviaDecoder.h:159
int best_phase
Definition OliviaDecoder.h:179
PcmInput input
Definition OliviaDecoder.h:157
bool carrier
Definition OliviaDecoder.h:182
unsigned ndata
Definition OliviaDecoder.h:184
void end() override
Processes the remaining samples and reports the end of the carrier.
Definition OliviaDecoder.h:103
int bits
Definition OliviaDecoder.h:162
float best_noise
Definition OliviaDecoder.h:178
int width
Definition OliviaDecoder.h:163
bool escape
Definition OliviaDecoder.h:183
OliviaConfig & config()
The configuration which was provided in begin().
Definition OliviaDecoder.h:146
void processSlice()
FFT over the last symbol_len samples and decoding of all offsets.
Definition OliviaDecoder.h:234
audio_tools::Vector< float > window
Definition OliviaDecoder.h:166
void addSample8k(float x)
Definition OliviaDecoder.h:223
SampleRateConverter converter
Definition OliviaDecoder.h:158
size_t write(const uint8_t *data, size_t len) override
Provide PCM data to the decoder.
Definition OliviaDecoder.h:122
static constexpr float BLOCK_THRESHOLD
minimum signal to noise ratio of a block relative to the sync threshold
Definition OliviaDecoder.h:153
bool isFinished() override
true if the decoding was stopped (rx_one)
Definition OliviaDecoder.h:135
bool is_active
Definition OliviaDecoder.h:155
OliviaConfig defaultConfig()
Provides a default configuration: olivia-32-1000.
Definition OliviaDecoder.h:39
Converts the sample rate of a signal with a windowed sinc low pass filter (by default the cut off is ...
Definition DSPUtils.h:15
Radix 2 complex FFT with a precomputed twiddle table.
Definition DSPUtils.h:131
TinyModem: software audio modems and packets with error correction.
Definition AX25.h:11
Configuration of the Olivia and Contestia modems (OliviaEncoder, OliviaDecoder): MFSK with 2 to 256 t...
Definition Olivia.h:31
static constexpr float BASE_RATE
internal sample rate which defines the signal
Definition Olivia.h:65