TinyModem
Header-only software audio modem library
Loading...
Searching...
No Matches
MFSKConfig.h
Go to the documentation of this file.
1#pragma once
2
3#include <ctype.h>
4#include <math.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include "AudioTools.h"
9
10namespace tinymodem {
11
23struct MFSKConfig : public audio_tools::AudioInfo {
25 sample_rate = 48000;
26 channels = 1;
27 bits_per_sample = 16;
28 }
29
31 int tones = 16;
33 float symbol_rate = 125.0f;
35 float base_freq = 1000.0f;
37 float tone_spacing = 0.0f;
39 float volume = 1.0f;
43 float min_amplitude = 0.005f;
45 bool rx_one = false;
47 uint8_t channel = 0;
49 float rx_noise = 0.0f;
50
53 bool setMode(const char* mode) {
54 if (mode == nullptr) return false;
55 if (!isMode(mode)) return false;
56 int n = atoi(mode + 4);
57 switch (n) {
58 case 4:
59 symbol_rate = 500.0f;
60 break;
61 case 8:
62 symbol_rate = 250.0f;
63 break;
64 case 16:
65 symbol_rate = 125.0f;
66 break;
67 case 32:
68 symbol_rate = 62.5f;
69 break;
70 case 64:
71 symbol_rate = 31.25f;
72 break;
73 default:
74 return false;
75 }
76 tones = n;
77 base_freq = 1000.0f;
78 tone_spacing = 0.0f;
79 return true;
80 }
81
83 static bool isMode(const char* mode) {
84 if (mode == nullptr) return false;
85 const char* prefix = "mfsk";
86 for (int j = 0; j < 4; j++)
87 if (tolower(mode[j]) != prefix[j]) return false;
88 return true;
89 }
90
92 int bitsPerSymbol() const {
93 int bits = 0;
94 while ((1 << bits) < tones) bits++;
95 return bits;
96 }
97
99 float spacing() const { return tone_spacing > 0 ? tone_spacing : symbol_rate; }
100
102 float toneFrequency(int idx) const { return base_freq + idx * spacing(); }
103
105 int syncLength() const {
106 int n = (8 + bitsPerSymbol() - 1) / bitsPerSymbol();
107 return n < 2 ? 2 : n;
108 }
109
111 int syncSymbol(int idx) const {
112 static const int pattern[4] = {2, 4, 4, 2}; // tones / 2, tones / 4
113 return tones / pattern[idx % 4];
114 }
115
117 float bitRate() const { return symbol_rate * bitsPerSymbol(); }
118
120 float samplesPerSymbol() const { return sample_rate / symbol_rate; }
121
123 bool isValid() const {
124 if (tones < 4 || tones > 64 || (tones & (tones - 1)) != 0) {
125 LOGE("MFSK: tones must be 4, 8, 16, 32 or 64, not %d", tones);
126 return false;
127 }
128 if (symbol_rate <= 0 || base_freq <= 0 || tone_spacing < 0) {
129 LOGE("MFSK: invalid symbol_rate, base_freq or tone_spacing");
130 return false;
131 }
132 if (toneFrequency(tones - 1) >= sample_rate / 2.0f) {
133 LOGE("MFSK: highest tone %.1f Hz must be below %.1f Hz",
134 (double)toneFrequency(tones - 1), (double)(sample_rate / 2.0f));
135 return false;
136 }
137 if (samplesPerSymbol() < 8) {
138 LOGE("MFSK: at least 8 samples per symbol are needed");
139 return false;
140 }
141 if (preamble_symbols < 4) {
142 LOGE("MFSK: at least 4 preamble symbols are needed");
143 return false;
144 }
145 return true;
146 }
147};
148
149} // namespace tinymodem
TinyModem: software audio modems and packets with error correction.
Definition AX25.h:11
uint8_t channel
receiver: input channel which is used for the decoding
Definition MFSKConfig.h:47
float spacing() const
distance between the tones in Hz
Definition MFSKConfig.h:99
float toneFrequency(int idx) const
frequency of the tone with the indicated index
Definition MFSKConfig.h:102
MFSKConfig()
Definition MFSKConfig.h:24
float rx_noise
receiver: (testing only) amplitude of added uniform noise
Definition MFSKConfig.h:49
float bitRate() const
data bits per second
Definition MFSKConfig.h:117
float base_freq
frequency of the lowest tone in Hz
Definition MFSKConfig.h:35
float volume
transmit amplitude (0.0 - 1.0)
Definition MFSKConfig.h:39
float samplesPerSymbol() const
number of samples per symbol
Definition MFSKConfig.h:120
float symbol_rate
symbols per second
Definition MFSKConfig.h:33
int tones
number of tones: 4, 8, 16, 32 or 64
Definition MFSKConfig.h:31
bool setMode(const char *mode)
Definition MFSKConfig.h:53
int bitsPerSymbol() const
bits per symbol: log2(tones)
Definition MFSKConfig.h:92
float tone_spacing
distance between the tones in Hz (0 = symbol_rate)
Definition MFSKConfig.h:37
int preamble_symbols
number of preamble symbols (alternating lowest and highest tone)
Definition MFSKConfig.h:41
bool isValid() const
Checks the configuration: returns false (and logs the reason) if invalid.
Definition MFSKConfig.h:123
int syncSymbol(int idx) const
tone of the sync symbol with the indicated index
Definition MFSKConfig.h:111
float min_amplitude
receiver: minimum amplitude of a tone (1.0 = full scale)
Definition MFSKConfig.h:43
bool rx_one
receiver: stop after the first carrier was lost
Definition MFSKConfig.h:45
int syncLength() const
number of sync symbols after the preamble: they carry at least 8 bits
Definition MFSKConfig.h:105
static bool isMode(const char *mode)
true if the mode name is a MFSK mode
Definition MFSKConfig.h:83