forked from dolske/modem.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
afsk-decoder.js
357 lines (310 loc) · 11.1 KB
/
afsk-decoder.js
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// from Packet.java
//
// Only methods used here are bytesWithoutCRC, addByte, terminate
function Packet() {
this.data = new Uint8Array(this.MAX_SIZE);
this.dataSize = 0;
}
Packet.prototype = {
data: null,
dataSize: 0,
MAX_SIZE: 16384, // XXX I'm lazy, this should just realloc.
addByte: function(val) {
console.log("Packet: addByte[" + this.dataSize + "] = " + String.fromCharCode(val) + " / " + val.toString(16));
this.data[this.dataSize++] = val;
// XXX skipped some CRC stuff
return true;
},
terminate: function() {
console.log("Packet: terminate!");
// nop
// XXX skipped some CRC stuff
return true;
},
bytesWithoutCRC: function() {
console.log("Packet: bytesWithoutCRC");
// TODO
return this.data.subarray(0, this.dataSize);
},
};
// Not implementing: 8000 -> 16000 supersampling.
//
// Orig code takes a "filter_length" argument. Not sure what this was being
// used for. The each filter type has two sets of coefficients (each set
// being broken down into data for specific sample rates). Seems the
// difference between the two sets is how many terms there are in the
// rate-specific data, with the second set having twice (?) as many terms
// as the first. TODO: read the paper again to see what this is for.
// The sample code's test construct with filter_length == 1, which matches
// nothing, and forces the fallback code to use the last (longer) set. So
// we'll just bake that in here.
function AfskDecoder(sampleRate, baud, onStatus) {
this.sampleRate = sampleRate;
this.baud = baud;
this.emphasis = true; // TODO
this.onStatus = onStatus;
this.samplesPerBit = sampleRate / baud; // Not rounded! Floating point!
this.td_filter = AfskFilters.getBandpassFilter(sampleRate, this.emphasis);
this.cd_filter = AfskFilters.getCorrelationFilter(sampleRate);
this.state.x = new Float32Array(this.td_filter.length);
this.state.u1 = new Float32Array(this.td_filter.length);
var spb = Math.ceil(this.samplesPerBit);
this.state.c0_real = new Float32Array(spb);
this.state.c0_imag = new Float32Array(spb);
this.state.c1_real = new Float32Array(spb);
this.state.c1_imag = new Float32Array(spb);
this.state.diff = new Float32Array(this.cd_filter.length);
// Explain this?
this.phaseIncrementFreqHi = 2 * Math.PI * this.freqHi / sampleRate;
this.phaseIncrementFreqLo = 2 * Math.PI * this.freqLo / sampleRate;
}
AfskDecoder.prototype = {
sampleRate: 0,
baud: 0,
onStatus: null, // callback
emphasis: false,
freqHi: 2200,
freqLo: 1200,
phaseIncrementFreqHi: 0,
phaseIncrementFreqLo: 0,
samplesPerBit: 0.0,
state: {
current: 0,
WAITING: 0,
JUST_SEEN_FLAG: 1,
DECODING: 2,
data: 0,
bitcount: 0,
x: null,
u1: null,
c0_real: null,
c0_imag: null,
c1_real: null,
c1_imag: null,
diff: null,
j_td: 0, // "time domain index"
j_cd: 0, // "time domain index" (?)
j_corr: 0, // correlation index
t: 0, // running sample counter
last_transition: 0,
phase_f0: 0.0,
phase_f1: 0.0,
previous_fdiff: 0.0,
flag_count: 0,
flag_separator_seen: false,
},
packet: null,
_haveCarrier: false,
get data_carrier() {
return this._haveCarrier;
},
set data_carrier(val) {
var change = val != this._haveCarrier;
this._haveCarrier = val;
if (change)
this.onStatus("carrier", this._haveCarrier);
},
dataAvailable: function(data) {
var blob = new Blob([data]);
var fileReader = new FileReader();
fileReader.onload = function(e) {
console.log("fileReader onload");
this.onStatus("data", e.target.result);
}.bind(this);
// This handles the utf8 conversion too.
fileReader.readAsText(blob);
},
correlation: function(x , y, j) { // (float[] x, float[] y, int j)
var c = 0.0;
for (var i = 0; i < x.length; i++) {
c += x[j] * y[j];
j--;
if (j == -1)
j = x.length - 1;
}
return c;
},
sum: function(x, j) { //(float[] x, int j)
c = 0.0;
for (var i = 0; i < x.length; i++) {
c += x[j];
j--;
if (j == -1)
j = x.length - 1;
}
return c;
},
// filter a signal x stored in a cyclic buffer with a FIR filter f
// The length of x must be larger than the length of the filter.
filter: function(x, j, f) {
// from Filter.filter()
var c = 0.0;
for (var i = 0; i < f.length; i++) {
c += x[j] * f[i];
j--;
if (j == -1)
j = x.length - 1;
}
return c;
},
addSamplesPrivate: function(s, n) { //(float[] s, int n)
var state = this.state;
var i = 0;
while (i < n) {
//if (i > 5000) throw "temp limit";
var sample = s[i++];
state.u1[state.j_td] = sample;
state.x[state.j_td] = this.filter(state.u1, state.j_td, this.td_filter);
// compute correlation running value
state.c0_real[state.j_corr] = state.x[state.j_td] * Math.cos(state.phase_f0);
state.c0_imag[state.j_corr] = state.x[state.j_td] * Math.sin(state.phase_f0);
state.c1_real[state.j_corr] = state.x[state.j_td] * Math.cos(state.phase_f1);
state.c1_imag[state.j_corr] = state.x[state.j_td] * Math.sin(state.phase_f1);
state.phase_f0 += this.phaseIncrementFreqLo;
if (state.phase_f0 > 2 * Math.PI)
state.phase_f0 -= 2 * Math.PI;
state.phase_f1 += this.phaseIncrementFreqHi;
if (state.phase_f1 > 2 * Math.PI)
state.phase_f1 -= 2 * Math.PI;
var cr, ci, c0, c1;
cr = this.sum(state.c0_real, state.j_corr);
ci = this.sum(state.c0_imag, state.j_corr);
c0 = Math.sqrt(cr * cr + ci * ci);
cr = this.sum(state.c1_real, state.j_corr);
ci = this.sum(state.c1_imag, state.j_corr);
c1 = Math.sqrt(cr * cr + ci * ci);
state.diff[state.j_cd] = c0 - c1;
var fdiff = this.filter(state.diff, state.j_cd, this.cd_filter);
if (state.previous_fdiff * fdiff < 0 || state.previous_fdiff == 0) {
//console.log("transition at sample " + i);
// we found a transition
var p = state.t - state.last_transition;
state.last_transition = state.t;
var bits = Math.round(p / this.samplesPerBit);
// collect statistics
/*
if (fdiff < 0) { // last period was high, meaning f0
f0_period_count++;
f0_max += f0_current_max;
double err = Math.abs(bits - ((double) p / (double)samples_per_bit));
//System.out.printf(")) %.02f %d %.02f\n",(double) p / (double)samples_per_bit,bits,err);
if (err > max_period_error) max_period_error = (float) err;
// prepare for the period just starting now
f1_current_min = fdiff;
} else {
f1_period_count++;
f1_min += f1_current_min;
double err = Math.abs(bits - ((double) p / (double)samples_per_bit));
//System.out.printf(")) %.02f %d %.02f\n",(double) p / (double)samples_per_bit,bits,err);
if (err > max_period_error) max_period_error = (float) err;
ii// prepare for the period just starting now
f0_current_max = fdiff;
}
*/
if (bits == 0 || bits > 7) {
state.current = state.WAITING;
this.data_carrier = false;
state.flag_count = 0;
} else {
//console.log("bits="+bits);
if (bits == 7) {
state.flag_count++;
console.log("FLAG FOUND (count = " + state.flag_count + ") in state " + state.current);
state.flag_separator_seen = false;
state.data = 0;
state.bitcount = 0;
switch (state.current) {
case state.WAITING:
state.current = state.JUST_SEEN_FLAG;
this.data_carrier = true;
// statisticsInit(); // start measuring a new packet
break;
case state.JUST_SEEN_FLAG:
break;
case state.DECODING:
if (this.packet && this.packet.terminate()) {
//statisticsFinalize();
//packet.statistics(new float[] {emphasis,f0_max/-f1_min,max_period_error});
//System.out.print(String.format("%ddB:%.02f:%.02f\n",
// emphasis,f0_max/-f1_min,max_period_error));
//handler.handlePacket(packet.bytesWithoutCRC());
this.dataAvailable(this.packet.bytesWithoutCRC());
//System.out.println(""+(++decode_count)+": "+packet);
}
this.packet = null;
state.current = state.JUST_SEEN_FLAG;
break;
}
} else {
//console.log("ok state is " + state.current);
switch (state.current) {
case state.WAITING:
break;
case state.JUST_SEEN_FLAG:
state.current = state.DECODING;
break;
case state.DECODING:
break;
}
if (state.current == state.DECODING) {
// If this is the 0-bit after a flag, set seperator_seen
if (bits != 1) {
state.flag_count = 0;
} else {
if (state.flag_count > 0 && !state.flag_separator_seen)
state.flag_separator_seen = true;
else
state.flag_count = 0;
}
for (var k = 0; k < bits - 1; k++) {
state.bitcount++;
state.data >>>= 1;
state.data += 128;
if (state.bitcount == 8) {
if (!this.packet)
this.packet = new Packet();
if (!this.packet.addByte(state.data)) {
state.current = state.WAITING;
this.data_carrier = false;
}
//System.out.printf(">>> %02x %c %c\n", data, (char)data, (char)(data>>1));
state.data = 0;
state.bitcount = 0;
}
}
if (bits - 1 != 5) { // the zero after the ones is not a stuffing
state.bitcount++;
state.data >>= 1;
if (state.bitcount == 8) {
if (!this.packet)
this.packet = new Packet();
if (!this.packet.addByte(state.data)) {
state.current = state.WAITING;
this.data_carrier = false;
}
//System.out.printf(">>> %02x %c %c\n", data, (char)data, (char)(data>>1));
state.data = 0;
state.bitcount = 0;
}
}
}
}
}
}
state.previous_fdiff = fdiff;
state.t++;
state.j_td++;
if (state.j_td == this.td_filter.length)
state.j_td = 0;
state.j_cd++;
if (state.j_cd == this.cd_filter.length)
state.j_cd = 0;
state.j_corr++;
if (state.j_corr == state.c0_real.length) // samples_per_bit
state.j_corr=0;
} // main while loop
},
demodulate: function(samples) {
this.addSamplesPrivate(samples, samples.length);
},
};