forked from DigitalHERMES/LinuxALE
/
modem.c
403 lines (350 loc) · 9.47 KB
/
modem.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* -*- c -*-
*
* Copyright (C) 2000 - 2001
* Charles Brain (chbrain@dircon.co.uk)
* Ilkka Toivanen (pile@aimo.kareltek.fi)
* Glenn Valenta (glenn@coloradostudios.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* File:
* modem.c FFT and decoder functions for LinuxALE
*
* Version:
* $Revision$
*
* Date:
* $Date$
*
* Author:
* Charles Brain
* Ilkka Toivanen
* Glenn Valenta
*
* History:
* $Log$
* Revision 1.3 2001/06/17 19:39:35 pile
* Socket server functionality by GV
*
* Revision 1.2 2001/05/29 18:53:44 pile
* Fixed sync problems and cleaned up garbage printing (decode_word).
*
* Revision 1.1.1.1 2001/05/23 20:19:50 pile
* Initial version for sourceforge.net
*
* Revision 0.3 2001/05/21 13:51:16 pile
* Minor fixes in output.
*
* Revision 0.2 2001/05/20 20:41:10 pile
* Fixed \n and DATA output.
*
* Revision 0.1 2001/05/20 19:42:52 pile
* Added text logging to file.
*
* Revision 0.0.1.1 2001/05/20 06:10:06 pile
* LinuxALE
*
*/
#include "modem.h"
/* consolidate all output to one place */
void output_mesg(char *mesg, FILE *log_file)
{
if(!Command_line_options.silent)
printf ("%s\n", mesg);
if(Command_line_options.write_server)
send_server(mesg);
if(log_file)
{
fprintf(log_file,"%s\n", mesg);
fflush(log_file);
}
}
void decodeCMD (unsigned long word)
{
int firstletter, metacmd;
firstletter = (word>>14)&0x0F;
metacmd = (word>>14)&0x70;
if (metacmd == 6)
{
printf (" %s ", CMD_types[firstletter]);
/* this part is still under development.
fprintf (stdout, " %s", CMD_types[firstletter]);
if (log_file) {
fprintf (log_file, " %s", CMD_types[firstletter]);
}
*/
}
}
void decode_word (unsigned long word, FILE *log_file)
{
unsigned char a, b, c, preamble;
static int started; /* if other than DATA has arrived */
time_t timestamp;
struct tm *UTC_time;
char msg[200];
char tmpBuffer[200];
memset(msg, '\0', 200);
memset(tmpBuffer, '\0', 200);
timestamp = time(NULL);
UTC_time=gmtime(×tamp);
sprintf (msg, "[%02d:%02d:%02d] ", UTC_time->tm_hour,
UTC_time->tm_min, UTC_time->tm_sec);
c = word&0x7F;
b = (word>>7)&0x7F;
a = (word>>14)&0x7F;
preamble = (word>>21)&0x7;
/* if CMD just print the stuff */
if (preamble == 6)
{
sprintf (tmpBuffer, "%s ",preamble_types[preamble]);
strcat (msg, tmpBuffer);
sprintf(tmpBuffer, "%c%c%c",a,b,c);
strcat (msg, tmpBuffer);
output_mesg(msg, log_file);
}
else
/* if other than CMD */
{
/* check if proper charset i.e. proper ale addresses */
if((ASCII_Set[a]==ASCII_38)&&(ASCII_Set[b]==ASCII_38)&&(ASCII_Set[c]==ASCII_38))
{
sprintf (tmpBuffer, "%s ",preamble_types[preamble]);
strcat (msg, tmpBuffer);
sprintf(tmpBuffer, "%c%c%c",a,b,c);
strcat (msg, tmpBuffer);
if(Command_line_options.cs_enable) {
if(preamble>=1&&preamble<=5) {
search_db(msg);
}
}
output_mesg(msg, log_file);
}
}
}
unsigned long modem_de_interleave_and_fec(int *input, int *errors)
{
int i;
unsigned long worda;
unsigned long wordb;
unsigned int error_a, error_b;
worda = wordb = 0;
for(i = 0; i < VOTE_BUFFER_LENGTH; )
{
worda = input[i++] ? (worda<<1)+1 : worda<<1 ;
wordb = input[i++] ? (wordb<<1)+1 : wordb<<1 ;
}
wordb = wordb ^ 0x000FFF;
/*
Now do the FEC
*/
worda = golay_decode(worda,&error_a);
wordb = golay_decode(wordb,&error_b);
/*
Return the number of errors as the word with the most.
*/
if(error_a > error_b)
*errors = error_a;
else
*errors = error_b;
/*
Format into a single 24 bit word
*/
worda = (worda<<12) + wordb;
return (worda);
}
/*
Process a new received symbol
*/
void modem_new_symbol(int sym, FILE *log_file)
{
static int bits[VOTE_ARRAY_LENGTH];
static int input_buffer_pos;
static int word_sync_position;
int majority_vote_array[VOTE_BUFFER_LENGTH];
int bad_votes, sum, errors, i;
unsigned long word;
/*
Update the input buffer
*/
bits[input_buffer_pos] = (sym&4) ? 1 : 0;
input_buffer_pos = ++input_buffer_pos%VOTE_ARRAY_LENGTH;
bits[input_buffer_pos] = (sym&2) ? 1 : 0;
input_buffer_pos = ++input_buffer_pos%VOTE_ARRAY_LENGTH;
bits[input_buffer_pos] = (sym&1) ? 1 : 0;
input_buffer_pos = ++input_buffer_pos%VOTE_ARRAY_LENGTH;
/*
Do the majority vote, ignore stuff bits
*/
bad_votes = 0;
for(i=0; i<VOTE_BUFFER_LENGTH; i++)
{
sum = bits[(i+input_buffer_pos)%VOTE_ARRAY_LENGTH];
sum += bits[(i+input_buffer_pos+SYMBOLS_PER_WORD)%VOTE_ARRAY_LENGTH];
sum += bits[(i+input_buffer_pos+(2*SYMBOLS_PER_WORD))%VOTE_ARRAY_LENGTH];
if(( sum == 1 ) || ( sum == 2)) bad_votes++;
majority_vote_array[i] = vote_lookup[sum];
}
/*
printf("Votes %d\n",bad_votes);
Now do tests to see if it might be time for a word sync
*/
if( word_sync == NOT_WORD_SYNC )
{
if( bad_votes <= BAD_VOTE_THRESHOLD )
{
word = modem_de_interleave_and_fec(majority_vote_array,&errors);
if(errors <= SYNC_ERROR_THRESHOLD)
{
/*
Word sync may have occurred
Make sure BASIC alphabet characters have been received to confirm
*/
decode_word(word, log_file);
/* now we have a proper sync; added after 0.0.1 */
word_sync = WORD_SYNC;
word_sync_position = input_buffer_pos;
}
}
}
else
{
if( input_buffer_pos == word_sync_position )
{
/* Signal new word */
word = modem_de_interleave_and_fec(majority_vote_array,&errors);
decode_word (word, log_file);
}
else
{
/* we don't have a proper sync; added after 0.0.1 */
word_sync = NOT_WORD_SYNC;
}
}
}
void modem_init(void)
{
int i;
/*
Create the twiddles etc for the FFT
*/
for(i=0; i<FFT_SIZE; i++)
{
fft_cs_twiddle[i] = cos(-2.0*PI*i/FFT_SIZE);
fft_ss_twiddle[i] = sin(-2.0*PI*i/FFT_SIZE);
fft_history[i] = 0;
}
fft_history_offset = 0;
word_sync = NOT_WORD_SYNC;
}
/*
Main Signal processing routine
*/
void modem(unsigned short *sample, int length, FILE *log_file)
{
int i,n,max_offset;
double new_sample;
double old_sample;
double temp_real;
double temp_imag;
double max_magnitude;
static int last_symbol;
static int last_sync_position;
static int sample_count;
for(i=0; i<length; i++)
{
if(sample[i]&0x8000)
/*
converting between 2's compliment and floating point maths
a mystical factor is (1/2^15)
*/
new_sample = -(((~sample[i])&0x7FFF)*0.0000305176);
else
new_sample = sample[i]*0.0000305176;
/*
Do the Sliding FFT
*/
old_sample = fft_history[fft_history_offset];
fft_history[fft_history_offset] = new_sample;
/*
Update output, only do first half, imag in = 0;
*/
for(n=0; n<FFT_SIZE/2; n++)
{
temp_real = fft_out[n].real-old_sample+new_sample;
temp_imag = fft_out[n].imag;
fft_out[n].real = (temp_real*fft_cs_twiddle[n]) - (temp_imag*fft_ss_twiddle[n]);
fft_out[n].imag = (temp_real*fft_ss_twiddle[n]) + (temp_imag*fft_cs_twiddle[n]);
fft_mag[n] = sqrt((fft_out[n].real*fft_out[n].real)+(fft_out[n].imag*fft_out[n].imag));
}
/*
Save the max output value in a running average over one ALE word
This is used for bit sync and symbol decoding
*/
max_magnitude = 0;
max_offset = 0;
/* for( n = 6; n <= 22; n += 2)*/
for( n = 6; n <= 22; n++)
{
if( fft_mag[n] > max_magnitude )
{
max_magnitude = fft_mag[n];
max_offset = n;
}
}
/*
Update the magnitude history for sync purposes
*/
mag_sum[sample_count] -= mag_history[sample_count][mag_history_offset];
mag_sum[sample_count] += max_magnitude;
mag_history[sample_count][mag_history_offset] = max_magnitude;
/*
Decide what to do
*/
if(word_sync == NOT_WORD_SYNC)
{
/*
Update bit sync position
*/
max_magnitude = 0;
for( n=0; n < FFT_SIZE; n++)
{
if(mag_sum[n] > max_magnitude)
{
max_magnitude = mag_sum[n];
last_sync_position = n;
}
}
last_symbol = symbol_lookup[max_offset]; // HZ added March 8, 2021
}
if( sample_count == last_sync_position)
{
/*
See what symbol has been received
*/
last_symbol = symbol_lookup[max_offset];
}
/*
Always output at the same position
*/
if( sample_count == 0 )
{
modem_new_symbol(last_symbol, log_file);
}
fft_history_offset = ++fft_history_offset%FFT_SIZE;
sample_count = ++sample_count%MOD_64;
if(sample_count == 0)
mag_history_offset = ++mag_history_offset%SYMBOLS_PER_WORD;
}
}