-
Notifications
You must be signed in to change notification settings - Fork 7
/
MorseDecoder.cpp
307 lines (282 loc) · 8.78 KB
/
MorseDecoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "MorseDecoder.h"
#include "gui_bar.h"
#include "SharedQueue.h"
MorseDecoder::MorseDecoder(float ifrate)
{
unsigned int order = 4; // filter order
float fc = 0.00208333333333333333333333333333f; // cutoff frequency 100Hz / 48000Hz
float f0 = 0.01041666666666666666666666666667f; // center frequency 500Hz / 48000Hz
float Ap = 1.0f; // pass-band ripple
float As = 40.0f; // stop-band attenuation
q = iirfilt_crcf_create_prototype(LIQUID_IIRDES_BUTTER, LIQUID_IIRDES_BANDPASS, LIQUID_IIRDES_TF, order=1, fc, f0, Ap, As);
iirfilt_crcf_print(q);
magnitudelimit = 0.01;
}
MorseDecoder::~MorseDecoder()
{
iirfilt_crcf_destroy(q);
}
void MorseDecoder::decode(const IQSampleVector &samples_in)
{
float y{0.0}, magnitude;
int i = 0;
IQSampleVector filter_out;
for (auto con : samples_in)
{
complex<float> v;
// run filter
iirfilt_crcf_execute(q, con, &v);
filter_out.insert(filter_out.end(), v);
}
for (auto &out : filter_out)
{
y += std::real(out * std::conj(out));
}
magnitude = y / samples_in.size();
//printf("%f\n", magnitude);
const float g = 0.0006667;
if (magnitude > magnitudelimit_low)
{
//magnitudelimit = (magnitudelimit + ((magnitude - magnitudelimit) / 6));
magnitudelimit = (1.0 - g) * magnitudelimit + g * magnitude;
} /// moving average filter
if (magnitudelimit < magnitudelimit_low)
magnitudelimit = magnitudelimit_low;
//printf("limit %f mag %f %f %s\n", magnitudelimit, magnitude, 6 * magnitudelimit, magnitude > magnitudelimit * 0.3 ? "y" : "n");
// Now check the magnitude //
if (magnitude > magnitudelimit * 0.6) // just to have some space up
{
realstate = 1;
}
else
{
realstate = 0;
}
if (realstate != realstatebefore)
{
laststarttime = std::chrono::high_resolution_clock::now();
}
auto now = std::chrono::high_resolution_clock::now();
const auto timePassed = std::chrono::duration_cast<std::chrono::microseconds>(now - laststarttime);
//if ((millis() - laststarttime) > nbtime)
if (timePassed.count() > nbtime)
{
if (realstate != filteredstate)
{
filteredstate = realstate;
}
}
if (filteredstate)
{
guiQueue.push_back(GuiMessage(GuiMessage::action::setled, 1));
}
else
{
guiQueue.push_back(GuiMessage(GuiMessage::action::setled, 0));
}
if (filteredstate != filteredstatebefore)
{
if (filteredstate == HIGH)
{
starttimehigh = std::chrono::high_resolution_clock::now(); //millis();
lowduration = std::chrono::duration_cast<std::chrono::microseconds>(now - startttimelow);
}
if (filteredstate == LOW)
{
startttimelow = std::chrono::high_resolution_clock::now(); //millis();
auto now = std::chrono::high_resolution_clock::now();
highduration = std::chrono::duration_cast<std::chrono::microseconds>(now - starttimehigh);
//highduration = (millis() - starttimehigh);
if (highduration.count() < (2 * hightimesavg.count()) || hightimesavg.count() == 0)
{
hightimesavg = (highduration + hightimesavg + hightimesavg) / 3; // now we know avg dit time ( rolling 3 avg)
}
if (highduration > (5 * hightimesavg))
{
hightimesavg = highduration + hightimesavg; // if speed decrease fast ..
}
}
}
// Now check the baud rate based on dit or dah duration either 1, 3 or 7 pauses
if (filteredstate != filteredstatebefore)
{
stop = LOW;
if (filteredstate == LOW)
{ // we did end on a HIGH
if (highduration < (hightimesavg * 2) && highduration > (hightimesavg * 0.6))
{ /// 0.6 filter out false dits
strcat(CodeBuffer, ".");
//printf(". ");
}
if (highduration > (hightimesavg * 2) && highduration < (hightimesavg * 6))
{
strcat(CodeBuffer, "-");
//printf("- ");
wpm = (wpm + (1200000 / ((highduration.count()) / 3))) / 2; //// the most precise we can do ;o)
guiQueue.push_back(GuiMessage(GuiMessage::action::setwpm, wpm));
}
}
if (filteredstate == HIGH)
{ //// we did end a LOW
float lacktime = 1;
if (wpm > 25)
lacktime = 1.0; /// when high speeds we have to have a little more pause before new letter or new word
if (wpm > 30)
lacktime = 1.2;
if (wpm > 35)
lacktime = 1.5;
if (lowduration.count() > (hightimesavg.count() * (2 * lacktime)) && lowduration.count() < hightimesavg.count() * (5 * lacktime))
{ // letter space
CodeToChar();
CodeBuffer[0] = '\0';
//AddCharacter('/');
//Serial.print("/");
}
if (lowduration.count() >= hightimesavg.count() * (5 * lacktime))
{ // word space
CodeToChar();
CodeBuffer[0] = '\0';
AddCharacter(' ');
}
}
}
now = std::chrono::high_resolution_clock::now();
auto timePassed1 = std::chrono::duration_cast<std::chrono::microseconds>(now - startttimelow);
if (timePassed1.count() > (highduration.count() * 6) && stop == LOW)
{
CodeToChar();
CodeBuffer[0] = '\0';
stop = HIGH;
}
// the end of main loop clean up//
realstatebefore = realstate;
lasthighduration = highduration;
filteredstatebefore = filteredstate;
}
void MorseDecoder::read(std::string &message)
{
message = DisplayLine;
}
void MorseDecoder::CodeToChar()
{ // translate cw code to ascii character//
char decode_char = '{';
if (strcmp(CodeBuffer, ".-") == 0)
decode_char = char('a');
if (strcmp(CodeBuffer, "-...") == 0)
decode_char = char('b');
if (strcmp(CodeBuffer, "-.-.") == 0)
decode_char = char('c');
if (strcmp(CodeBuffer, "-..") == 0)
decode_char = char('d');
if (strcmp(CodeBuffer, ".") == 0)
decode_char = char('e');
if (strcmp(CodeBuffer, "..-.") == 0)
decode_char = char('f');
if (strcmp(CodeBuffer, "--.") == 0)
decode_char = char('g');
if (strcmp(CodeBuffer, "....") == 0)
decode_char = char('h');
if (strcmp(CodeBuffer, "..") == 0)
decode_char = char('i');
if (strcmp(CodeBuffer, ".---") == 0)
decode_char = char('j');
if (strcmp(CodeBuffer, "-.-") == 0)
decode_char = char('k');
if (strcmp(CodeBuffer, ".-..") == 0)
decode_char = char('l');
if (strcmp(CodeBuffer, "--") == 0)
decode_char = char('m');
if (strcmp(CodeBuffer, "-.") == 0)
decode_char = char('n');
if (strcmp(CodeBuffer, "---") == 0)
decode_char = char('o');
if (strcmp(CodeBuffer, ".--.") == 0)
decode_char = char('p');
if (strcmp(CodeBuffer, "--.-") == 0)
decode_char = char('q');
if (strcmp(CodeBuffer, ".-.") == 0)
decode_char = char('r');
if (strcmp(CodeBuffer, "...") == 0)
decode_char = char('s');
if (strcmp(CodeBuffer, "-") == 0)
decode_char = char('t');
if (strcmp(CodeBuffer, "..-") == 0)
decode_char = char('u');
if (strcmp(CodeBuffer, "...-") == 0)
decode_char = char('v');
if (strcmp(CodeBuffer, ".--") == 0)
decode_char = char('w');
if (strcmp(CodeBuffer, "-..-") == 0)
decode_char = char('x');
if (strcmp(CodeBuffer, "-.--") == 0)
decode_char = char('y');
if (strcmp(CodeBuffer, "--..") == 0)
decode_char = char('z');
if (strcmp(CodeBuffer, ".----") == 0)
decode_char = char('1');
if (strcmp(CodeBuffer, "..---") == 0)
decode_char = char('2');
if (strcmp(CodeBuffer, "...--") == 0)
decode_char = char('3');
if (strcmp(CodeBuffer, "....-") == 0)
decode_char = char('4');
if (strcmp(CodeBuffer, ".....") == 0)
decode_char = char('5');
if (strcmp(CodeBuffer, "-....") == 0)
decode_char = char('6');
if (strcmp(CodeBuffer, "--...") == 0)
decode_char = char('7');
if (strcmp(CodeBuffer, "---..") == 0)
decode_char = char('8');
if (strcmp(CodeBuffer, "----.") == 0)
decode_char = char('9');
if (strcmp(CodeBuffer, "-----") == 0)
decode_char = char('0');
if (strcmp(CodeBuffer, "..--..") == 0)
decode_char = char('?');
if (strcmp(CodeBuffer, ".-.-.-") == 0)
decode_char = char('.');
if (strcmp(CodeBuffer, "--..--") == 0)
decode_char = char(',');
if (strcmp(CodeBuffer, "-.-.--") == 0)
decode_char = char('!');
if (strcmp(CodeBuffer, ".--.-.") == 0)
decode_char = char('@');
if (strcmp(CodeBuffer, "---...") == 0)
decode_char = char(':');
if (strcmp(CodeBuffer, "-....-") == 0)
decode_char = char('-');
if (strcmp(CodeBuffer, "-..-.") == 0)
decode_char = char('/');
if (strcmp(CodeBuffer, "-.--.") == 0)
decode_char = char('(');
if (strcmp(CodeBuffer, "-.--.-") == 0)
decode_char = char(')');
if (strcmp(CodeBuffer, ".-...") == 0)
decode_char = char('_');
if (strcmp(CodeBuffer, "...-..-") == 0)
decode_char = char('$');
if (strcmp(CodeBuffer, "...-.-") == 0)
decode_char = char('>');
if (strcmp(CodeBuffer, ".-.-.") == 0)
decode_char = char('<');
if (strcmp(CodeBuffer, "...-.") == 0)
decode_char = char('~');
if (strcmp(CodeBuffer, ".-.-") == 0)
decode_char = char('a'); // a umlaut
if (strcmp(CodeBuffer, "---.") == 0)
decode_char = char('o'); // o accent
if (strcmp(CodeBuffer, ".--.-") == 0)
decode_char = char('a'); // a accent
if (decode_char != '{')
{
AddCharacter(decode_char);
}
}
void MorseDecoder::AddCharacter(char newchar)
{
DisplayLine.push_back(newchar) ;
if (DisplayLine.size() > 40)
DisplayLine.erase(0,1);
guiQueue.push_back(GuiMessage(GuiMessage::action::displayline, DisplayLine));
}