39 if (info.sample_rate ==
cfg.sample_rate && info.channels ==
cfg.channels &&
40 info.bits_per_sample ==
cfg.bits_per_sample)
42 cfg.sample_rate = info.sample_rate;
43 cfg.channels = info.channels;
44 cfg.bits_per_sample = info.bits_per_sample;
50 using audio_tools::AudioDecoder::begin;
60 if (!
cfg.isValid())
return false;
102 size_t write(
const uint8_t* data,
size_t len)
override {
104 input.write(data, len,
185 return ((
noise_seed >> 8) & 0xFFFF) / 65535.0f - 0.5f;
192 if (add_noise &&
cfg.rx_noise != 0.0f)
211 uint32_t d = t - 2 *
half + 1;
221 static bool isReached(uint32_t a, uint32_t b) {
return (int32_t)(a - b) >= 0; }
225 for (
int j =
half - 1; j >= 0; j--) {
235 uint32_t keep =
now - 2 *
half - 1;
239 int32_t n = keep -
base;
250 if (metric >
cfg.sync_threshold) {
276 for (
int k = j - w / 2; k <= j + w / 2; k++) {
284 int first = -1, last = -1;
287 if (first < 0) first = j;
291 if (first < 0 || last - first <
prefix_size / 4)
return;
303 float re = 0, im = 0;
305 for (
int n = 0; n <
fft_size; n++) {
307 re += x *
table.cosine(idx);
308 im -= x *
table.sine(idx);
317 static float wrap(
float angle) {
318 while (angle > (
float)M_PI) angle -= 2.0f * (float)M_PI;
319 while (angle <= -(
float)M_PI) angle += 2.0f * (float)M_PI;
324 static void fit(
const float* angle,
const float* weight,
int n,
325 float& common,
float& slope) {
326 float sw = 0, sx = 0, sy = 0, sxx = 0, sxy = 0;
327 for (
int i = 0; i < n; i++) {
329 if (w <= 0)
continue;
334 sxy += w * i * angle[i];
336 float det = sw * sxx - sx * sx;
337 if (sw <= 0 || fabsf(det) < 1e-12f) {
338 common = sw > 0 ? sy / sw : 0;
342 slope = (sw * sxy - sx * sy) / det;
343 common = (sy - slope * sx) / sw;
368 }
else if (
drift <= -1.0f) {
378 float angle[128], weight[128];
390 angle[i] =
wrap(atan2f(di, dr) - known * (
float)M_PI / 2);
391 weight[i] = sqrtf(dr * dr + di * di);
411 float angle[128], weight[128];
412 float symbol_power = 0;
416 float step = is_header ? (float)M_PI : (float)M_PI / 2;
423 int q = lroundf(a / step);
424 angle[i] = a - q * step;
425 weight[i] = sqrtf(dr * dr + di * di);
461 static const int dibit[4] = {0, 1, 3, 2};
467 for (
int b = 0; b < 32; b++) header = (header << 1) | (
header_soft[b] < 0);
468 uint16_t len = header >> 16;
469 uint16_t check = header & 0xFFFF;
470 if ((uint16_t)~len != check || len == 0) {
478 (
double)
cfg.carrierFrequency(0),
485 if (p_print !=
nullptr) p_print->write(
current);
499 status(
"\n### NOCARRIER ndata=%d phase_error=%.1f deg%s ###\n",
ndata,
500 (
double)(error * 180.0f / (
float)M_PI),
501 complete ?
"" :
" (incomplete)");
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
OFDMConfig defaultConfig()
Provides a default configuration: ofdm32.
Definition OFDMDecoder.h:31
void processSymbol()
Definition OFDMDecoder.h:346
void recalculate(uint32_t t)
Definition OFDMDecoder.h:223
int error_count
Definition OFDMDecoder.h:169
void compact()
Drops the samples which are not needed any more.
Definition OFDMDecoder.h:234
static constexpr float TRACKING_GAIN
Definition OFDMDecoder.h:125
int first_bin
Definition OFDMDecoder.h:136
int frame_len
Definition OFDMDecoder.h:164
OFDMTable table
Definition OFDMDecoder.h:132
int prefix_size
Definition OFDMDecoder.h:135
uint32_t filled
Definition OFDMDecoder.h:144
audio_tools::Vector< float > buffer
Definition OFDMDecoder.h:140
void endFrame(bool complete)
Definition OFDMDecoder.h:493
float header_soft[32]
Definition OFDMDecoder.h:162
void addSample(float x, bool add_noise)
Definition OFDMDecoder.h:190
static int phaseToGray(int phase)
0 -> 00, 90 -> 01, 180 -> 11, 270 -> 10 degrees
Definition OFDMDecoder.h:460
State state
Definition OFDMDecoder.h:155
int ncarriers
Definition OFDMDecoder.h:136
PcmInput input
Definition OFDMDecoder.h:131
State
Definition OFDMDecoder.h:124
@ SEARCH
Definition OFDMDecoder.h:124
@ FRAME
Definition OFDMDecoder.h:124
float sample(uint32_t idx)
Definition OFDMDecoder.h:188
int ndata
Definition OFDMDecoder.h:165
bool begin(OFDMConfig config)
Starts the processing with the indicated configuration.
Definition OFDMDecoder.h:53
audio_tools::Vector< float > z_re
Definition OFDMDecoder.h:158
OFDMConfig cfg
Definition OFDMDecoder.h:128
int symbol
Definition OFDMDecoder.h:157
void checkHeader()
Definition OFDMDecoder.h:465
static constexpr float MIN_POWER
Definition OFDMDecoder.h:126
float power
Definition OFDMDecoder.h:147
bool isFinished() override
true if the decoding was stopped (rx_one)
Definition OFDMDecoder.h:113
audio_tools::Vector< float > smoothed
Definition OFDMDecoder.h:151
float noise()
uniform random value in the range -0.5 to 0.5
Definition OFDMDecoder.h:183
float slope_adjust
Definition OFDMDecoder.h:159
audio_tools::Vector< float > prev_re
Definition OFDMDecoder.h:158
int current_dibits
Definition OFDMDecoder.h:167
void referenceSymbol()
Definition OFDMDecoder.h:377
audio_tools::Vector< float > prev_im
Definition OFDMDecoder.h:158
bool is_finished
Definition OFDMDecoder.h:130
OFDMDecoder(Print &out)
Definition OFDMDecoder.h:28
uint8_t current
Definition OFDMDecoder.h:166
size_t frameCount()
Number of received frames.
Definition OFDMDecoder.h:116
static bool isReached(uint32_t a, uint32_t b)
true if the global index a is at or after b (with wrap around)
Definition OFDMDecoder.h:221
static void fit(const float *angle, const float *weight, int n, float &common, float &slope)
Weighted linear fit: angle = common + slope * carrier.
Definition OFDMDecoder.h:324
int half
Definition OFDMDecoder.h:135
int symbol_size
Definition OFDMDecoder.h:135
size_t nvalid
Definition OFDMDecoder.h:141
int plateau_len
Definition OFDMDecoder.h:152
bool in_plateau
Definition OFDMDecoder.h:148
OFDMConfig & config()
The configuration which was provided in begin().
Definition OFDMDecoder.h:119
uint32_t now
Definition OFDMDecoder.h:143
audio_tools::Vector< float > plateau
Definition OFDMDecoder.h:150
float reference_power
Definition OFDMDecoder.h:161
void dataSymbol()
Definition OFDMDecoder.h:410
uint32_t base
Definition OFDMDecoder.h:142
uint32_t window
Definition OFDMDecoder.h:156
bool isCarrier() override
true while a frame is received
Definition OFDMDecoder.h:110
void search(uint32_t d)
Definition OFDMDecoder.h:248
float pred_common
Definition OFDMDecoder.h:159
void setAudioInfo(audio_tools::AudioInfo info) override
Defines the format of the input audio data.
Definition OFDMDecoder.h:38
uint32_t plateau_start
Definition OFDMDecoder.h:149
void dataBits(int dibit)
Definition OFDMDecoder.h:482
int fft_size
Definition OFDMDecoder.h:135
float pred_slope
Definition OFDMDecoder.h:159
void flush() override
Processes the remaining samples (e.g. at the end of the input).
Definition OFDMDecoder.h:90
float corr
Definition OFDMDecoder.h:147
audio_tools::AudioInfo audioInfo() override
Definition OFDMDecoder.h:48
static float wrap(float angle)
Definition OFDMDecoder.h:317
audio_tools::Vector< float > z_im
Definition OFDMDecoder.h:158
uint32_t noise_seed
Definition OFDMDecoder.h:133
void dft()
DFT of the window at the carrier frequencies.
Definition OFDMDecoder.h:300
bool is_active
Definition OFDMDecoder.h:129
float drift
Definition OFDMDecoder.h:160
float error_total
Definition OFDMDecoder.h:168
size_t frames
Definition OFDMDecoder.h:134
bool begin() override
Definition OFDMDecoder.h:58
int header_symbol
Definition OFDMDecoder.h:163
size_t write(const uint8_t *data, size_t len) override
Provide PCM data to the decoder.
Definition OFDMDecoder.h:102
void resetState()
Definition OFDMDecoder.h:171
void end() override
Processes the remaining samples.
Definition OFDMDecoder.h:83
Sine and cosine of 2 * PI * i / size.
Definition OFDM.h:140
TinyModem: software audio modems and packets with error correction.
Definition AX25.h:11
Configuration of the OFDM modem (OFDMEncoder, OFDMDecoder): the data is transmitted in parallel on ca...
Definition OFDM.h:23
static int knownPhase(int symbol, int carrier)
Definition OFDM.h:120
static int headerSymbols(int carriers)
Number of symbols of the header (32 bits, one bit per carrier).
Definition OFDM.h:116
bool setMode(const char *mode)
Selects "ofdm16", "ofdm32" or "ofdm64": returns false if invalid.
Definition OFDM.h:52