forked from windytan/redsea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbits.c
325 lines (249 loc) · 8.26 KB
/
bits.c
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
/* bits.c -- part of redsea RDS decoder (c) OH2-250
*
* decodes physical layer: get_bit()
* data-link layer: main()
*
* stdin: RDS signal PCM at baseband
* raw pcm, 16bit, little-endian, signed-integer, single-channel,
* 6000 samples/sec
*
* stdout: RDS data
* A group is preceded by a one-byte block count header
* One block = 2 bytes
* special codes: 0x00 = sync lost
* 0xFF = sync acquired
*
*
* Page, section and figure numbers refer to IEC 62106, Edition 2
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <math.h>
#include "bits.h"
// Demodulate next PSK bit from PCM stream
bool get_bit() {
int j, p;
bool f=false, g=false;
static double w = 2 * M_PI * (1187.5 / 6000.0); // PSK carrier frequency, rad/sample
static unsigned short bit_phase = 0; /* bit phase */
static unsigned short bpadvance = 12971; // 2^16 / (6000 Hz / 1187.5 bps)
static unsigned int lo_time = 0;
static unsigned char intptr=0;
static short int sample=0, prevsample=0, x=0, x0=0;
static int integral[5] = {0};
static double lo = 0;
static double lo_phase = 0;
static bool bit=0, prevbit=0, diffbit=0;
j = bit_phase;
if (j>0x8000) j -= 0x10000;
for (f=false,p=0; j < 0x10000; j += bpadvance) {
if (fread(&sample, 2, 1, stdin)) {
/* Local oscillator at 1187.5 Hz */
lo = cos(w * lo_time + lo_phase);
lo_time++;
/* Product detector & integrator over 5 samples */
integral[intptr] = sample * lo;
if (++intptr >= 5) intptr = 0;
x = (integral[0] + integral[1] + integral[2] + integral[3] + integral[4]) / 5;
/* PSK carrier recovery PLL */
if (prevsample * sample < 0) { /* Zero crossing */
if (!g) {
if ( fmod(w * lo_time + lo_phase, M_PI) >= M_PI/2 )
lo_phase += 0.04;
else
lo_phase -= 0.04;
lo_phase = fmod(lo_phase, 2*M_PI);
g = true;
}
}
/* Data clock recovery PLL */
if (x * x0 < 0) { /* Data Change */
if (!f) {
if (j < 0x8000) p = 2048; /* Early */
else p = -2048; /* Late */
f = true;
}
}
x0 = x;
prevsample = sample;
} else {
exit(EXIT_FAILURE);
}
}
bit_phase = (j + p) & _16BIT;
bit = (x > 0);
/* Differential coding */
diffbit = bit ^ prevbit;
prevbit = bit;
return(diffbit);
}
// Calculate the syndrome of a 26-bit vector
unsigned short syndrome (unsigned int vector) {
short k;
unsigned short SyndReg=0,l;
bool bit;
// Figure B.4
for (k=25; k>=0; k--) {
bit = (vector & (1 << k));
// Store leftmost bit of register
l = (SyndReg & 0x200);
// Rotate register
SyndReg = (SyndReg << 1) & _10BIT;
// Premultiply input by x^325 mod g(x)
if (bit) SyndReg ^= 0x31B;
// Division mod 2 by generator polynomial
if (l) SyndReg ^= 0x1B9;
}
return(SyndReg);
}
// When a block has uncorrectable errors, dump the group received so far
void blockerror () {
unsigned char datalen=0,buf=0,k=0;
if (rcvd[A]) {
datalen = 1;
if (rcvd[B]) {
datalen = 2;
if (rcvd[C] || rcvd[Ci]) {
datalen = 3;
}
}
fwrite(&datalen, 1, 1, stdout);
for (k = 0; k < datalen; k++) fwrite(&grp_data[k], 1, 2, stdout);
fflush(stdout);
} else if (rcvd[Ci]) {
datalen = 1;
fwrite(&datalen, 1, 1, stdout);
fwrite(&grp_data[2], 1, 2, stdout);
fflush(stdout);
}
errblock[BlkPointer % 50] = true;
erbloks = 0;
for (k = 0; k < 50; k ++) erbloks += errblock[k];
// Sync is lost when >45 out of last 50 blocks are erroneous (C.1.2)
if (insync && erbloks > 45) {
insync = false;
for (k = 0; k < 50; k ++) errblock[k] = false;
buf = 0x00;
fwrite(&buf, 1, 1, stdout);
fflush(stdout);
}
rcvd[A] = rcvd[B] = rcvd[C] = rcvd[Ci] = rcvd[D] = false;
}
int main() {
short k;
unsigned int block = 0, wideblock = 0;
unsigned int bitcount = 0, prevbitcount = 0;
unsigned int dist;
unsigned short message, pi=0, i=0;
unsigned char j, datalen=0, buf=0, prevsync=0, lefttoread=26;
bool syncb[5] = {false};
#ifdef DOCORRECT
unsigned int l;
unsigned short SyndReg, err;
#endif
// Offset words A, B, C, C', D
unsigned short offset[5] = { 0x0FC, 0x198, 0x168, 0x350, 0x1B4 };
// Map offset numbers to block numbers
unsigned char ofs2block[5] = { 0, 1, 2, 2, 3 };
while(1) {
// Compensate for clock slip corrections
bitcount += 26-lefttoread;
// Read from radio
for (i=0; i < (insync ? lefttoread : 1); i++, bitcount++)
wideblock = (wideblock << 1) + get_bit();
lefttoread = 26;
wideblock &= _28BIT; // wideblock is a buffer containing the block + 1 adjacent
// bit on both sides
block = (wideblock >> 1) & _26BIT;
// Find the offsets for which the syndrome is zero
for (j = A; j <= D; j ++) syncb[j] = (syndrome(block ^ offset[j]) == 0);
/* Acquire synchronization */
if (!insync) {
if ( syncb[A] | syncb[B] | syncb[C] | syncb[Ci] | syncb[D] ) {
for (j = A; j <= D; j ++) {
if (syncb[j]) {
dist = bitcount - prevbitcount;
if ( dist % 26 == 0
&& dist <= 156
&& (ofs2block[prevsync] + dist/26) % 4 == ofs2block[j] ) {
insync = true;
expofs = j;
buf = 0xFF;
fwrite(&buf, 1, 1, stdout);
fflush(stdout);
break;
} else {
prevbitcount = bitcount;
prevsync = j;
}
}
}
}
}
/* Synchronous decoding */
if (insync) {
BlkPointer ++;
message = block >> 10;
// If expecting C but we only got a Ci sync pulse, we have a Ci block
if (expofs == C && !syncb[C] && syncb[Ci]) expofs = Ci;
// If this block offset won't give a sync pulse
if (!syncb[expofs]) {
// If it's a correct PI, the error was probably in the check bits and hence is ignored
if (expofs == A && message == pi && pi != 0) syncb[A] = true;
else if (expofs == C && message == pi && pi != 0) syncb[Ci] = true;
// Detect & correct clock slips (C.1.2)
else if (expofs == A && pi != 0 && ( (wideblock >> 12) & _16BIT ) == pi) {
message = pi;
wideblock >>= 1;
syncb[A] = true;
} else if (expofs == A && pi != 0 && ( (wideblock >> 10) & _16BIT ) == pi) {
message = pi;
wideblock = (wideblock << 1) + get_bit();
syncb[A] = true;
lefttoread = 25;
}
#ifdef DOCORRECT
// Detect & correct burst errors (B.2.2)
SyndReg = syndrome(block ^ offset[expofs]);
for (k = 0; k < 15; k ++) {
if (k > 0) {
l = SyndReg & 0x200;
SyndReg = (SyndReg << 1) & _10BIT;
if (l) SyndReg ^= 0x1B9;
}
if ((SyndReg & _5BIT) == 0) {
err = (SyndReg >> k) & _16BIT;
message = (block >> 10) ^ err;
syncb[expofs] = true;
break;
}
}
#endif
// If still no sync pulse
if ( !syncb[expofs] ) blockerror();
}
// Error-free block received
if (syncb[expofs]) {
grp_data[ofs2block[expofs]] = message;
errblock[BlkPointer % 50] = false;
rcvd[expofs] = true;
if (expofs == A) pi = message;
// If a complete group is received
if (rcvd[A] && rcvd[B] && (rcvd[C] || rcvd[Ci]) && rcvd[D]) {
// Print it out
datalen = 4;
if (!fwrite(&datalen, 1, 1, stdout)) return(EXIT_FAILURE);
for (k = 0; k < datalen; k++) fwrite(&grp_data[k], 1, 2, stdout);
fflush(stdout);
}
}
// The block offset we're expecting next
expofs = (expofs == C ? D : (expofs + 1) % 5);
if (expofs == A) rcvd[A] = rcvd[B] = rcvd[C] = rcvd[Ci] = rcvd[D] = false;
}
}
return (EXIT_SUCCESS);
}