-
Notifications
You must be signed in to change notification settings - Fork 5
/
eas_demod.cpp
331 lines (319 loc) · 8.94 KB
/
eas_demod.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <assert.h>
#include <math.h>
#include "eas_demod.h"
namespace eas {
inline double corr9(const float *p, const float *q)
{
double f;
asm volatile ("flds (%1);\n\t"
"fmuls (%2);\n\t"
"flds 4(%1);\n\t"
"fmuls 4(%2);\n\t"
"flds 8(%1);\n\t"
"fmuls 8(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 12(%1);\n\t"
"fmuls 12(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 16(%1);\n\t"
"fmuls 16(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 20(%1);\n\t"
"fmuls 20(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 24(%1);\n\t"
"fmuls 24(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 28(%1);\n\t"
"fmuls 28(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 32(%1);\n\t"
"fmuls 32(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"faddp;\n\t" :
"=t" (f) :
"r" (p),
"r" (q) : "memory");
return f;
}
inline double corr18(const float *p, const float *q)
{
double f;
asm volatile ("flds (%1);\n\t"
"fmuls (%2);\n\t"
"flds 4(%1);\n\t"
"fmuls 4(%2);\n\t"
"flds 8(%1);\n\t"
"fmuls 8(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 12(%1);\n\t"
"fmuls 12(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 16(%1);\n\t"
"fmuls 16(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 20(%1);\n\t"
"fmuls 20(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 24(%1);\n\t"
"fmuls 24(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 28(%1);\n\t"
"fmuls 28(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 32(%1);\n\t"
"fmuls 32(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 36(%1);\n\t"
"fmuls 36(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 40(%1);\n\t"
"fmuls 40(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 44(%1);\n\t"
"fmuls 44(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 48(%1);\n\t"
"fmuls 48(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 52(%1);\n\t"
"fmuls 52(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 56(%1);\n\t"
"fmuls 56(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 60(%1);\n\t"
"fmuls 60(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 64(%1);\n\t"
"fmuls 64(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"flds 68(%1);\n\t"
"fmuls 68(%2);\n\t"
"fxch %%st(2);\n\t"
"faddp;\n\t"
"faddp;\n\t" :
"=t" (f) :
"r" (p),
"r" (q) : "memory");
return f;
}
inline double corr(const float *p, const float *q, int n)
{
if (n == 18) {
return corr18(p, q);
}
if (n == 9) {
return corr9(p, q);
}
double s = 0;
while (n--) {
s += (*p++) * (*q++);
}
return s;
}
Demodulator::Demodulator()
{
double f0 = 2*M_PI*(3/1920e-6);
double f1 = 2*M_PI*(4/1920e-6);
for (int i = 0; i < CORRLEN; i++) {
ref[0][0][i] = sin(i/11025.0*f0);
ref[0][1][i] = cos(i/11025.0*f0);
ref[1][0][i] = sin(i/11025.0*f1);
ref[1][1][i] = cos(i/11025.0*f1);
}
overlap = 0;
bphase = 0;
bitcount = 0;
lastbit = 0;
bits = 0;
samebytes = 0;
hindex = -5;
}
void Demodulator::demod(const float *buf, int n)
{
if (overlap > 0) {
assert(overlap < CORRLEN);
if (n >= CORRLEN) {
memcpy(&overlapbuf[overlap], buf, (CORRLEN-1)*sizeof(float));
int r = gotsamples(overlapbuf, overlap+CORRLEN-1);
assert(r == overlap);
overlap = 0;
} else {
memcpy(&overlapbuf[overlap], buf, n*sizeof(float));
overlap += n;
if (overlap >= CORRLEN) {
int r = gotsamples(overlapbuf, overlap);
memmove(overlapbuf, &overlapbuf[r], (overlap-r)*sizeof(float));
overlap -= r;
}
return;
}
}
int r = gotsamples(buf, n);
buf += r;
n -= r;
if (n > 0) {
memcpy(overlapbuf, buf, n*sizeof(float));
overlap = n;
}
}
int Demodulator::gotsamples(const float *buf, int n)
{
int r = 0;
while (n >= CORRLEN) {
double c00 = corr(buf, ref[0][0], CORRLEN);
double c01 = corr(buf, ref[0][1], CORRLEN);
double c10 = corr(buf, ref[1][0], CORRLEN);
double c11 = corr(buf, ref[1][1], CORRLEN);
double d = (c10*c10 + c11*c11) - (c00*c00 + c01*c01);
int bit = d > 0;
//printf("%d", bit);
if (bit) {
if (bitcount < 6) {
bitcount++;
}
} else {
if (bitcount > -6) {
bitcount--;
}
}
if (bit != lastbit) {
if (bphase < 0x6000) {
bphase += BPHASESTEP/8;
} else {
bphase -= BPHASESTEP/8;
}
}
lastbit = bit;
bphase += BPHASESTEP;
if (bphase >= 0x10000) {
bphase &= 0xffff;
//printf("(%d)", bitcount);
gotbit(bitcount > 0);
}
buf++;
n--;
r++;
}
return r;
}
void Demodulator::gotbit(int b)
{
//printf(" %d ", b);
byte >>= 1;
if (b) {
byte |= 0x80;
}
bits++;
if (bits >= 8) {
//printf("[%02x]", byte);
bits = 0;
if (byte & 0x80) {
if (byte == 0xab) {
//printf("(sync)");
gotbyte(byte);
return;
} else if (byte == 0xae) {
byte >>= 2;
bits = 6;
} else if (byte == 0xba) {
byte >>= 4;
bits = 4;
} else if (byte == 0xea) {
byte >>= 6;
bits = 2;
} else if (byte == 0xd5) {
byte >>= 7;
bits = 1;
}
gotbyte(0);
} else if (byte == 0x57 && samebytes >= 4) {
byte >>= 1;
bits = 7;
} else if (byte == 0x5d && samebytes >= 4) {
byte >>= 3;
bits = 5;
} else if (byte == 0x75 && samebytes >= 4) {
byte >>= 5;
bits = 3;
} else {
if (byte == lastbyte) {
samebytes++;
} else {
samebytes = 0;
}
gotbyte(byte);
lastbyte = byte;
}
}
}
void Demodulator::gotbyte(unsigned char c)
{
//printf("\n%c\n", c);
//printf("%d.%d ", hindex, dashstate);
if (c == 0) {
if (hindex > 0) {
header[hindex] = 0;
gotheader(header);
}
hindex = -5;
} else if (c == 0xab) {
if (hindex < 0) {
hindex++;
}
dashstate = 0;
} else if (hindex >= 0) {
if (hindex+1 >= (int)sizeof(header)) {
hindex = -5;
} else {
header[hindex++] = c;
if (c == '+') {
dashstate = 1;
} else if (c == '-' && dashstate > 0) {
dashstate++;
if (dashstate >= 4) {
header[hindex] = 0;
gotheader(header);
hindex = -5;
}
}
}
}
}
void Demodulator::gotheader(const char *s)
{
//printf("gotheader: %s\n", s);
if (s == lastheader) {
return;
}
if (strncmp(s, "ZCZC-", 5) == 0) {
activate(s);
} else if (strncmp(s, "NN", 2) == 0) {
deactivate();
}
lastheader = s;
}
} // namespace eas