-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathdsp.c
372 lines (292 loc) · 9.03 KB
/
dsp.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
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011, 2012, 2013, 2015, 2019 John Langner, WB2OSZ
//
// 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, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Name: dsp.c
*
* Purpose: Generate the filters used by the demodulators.
*
*----------------------------------------------------------------*/
#include "direwolf.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "audio.h"
#include "fsk_demod_state.h"
#include "fsk_gen_filter.h"
#include "textcolor.h"
#include "dsp.h"
//#include "fsk_demod_agc.h" /* for M_FILTER_SIZE, etc. */
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
// Don't remove this. It serves as a reminder that an experiment is underway.
#if defined(TUNE_MS_FILTER_SIZE) || defined(TUNE_MS2_FILTER_SIZE) || defined(TUNE_AGC_FAST) || defined(TUNE_LPF_BAUD) || defined(TUNE_PLL_LOCKED) || defined(TUNE_PROFILE)
#define DEBUG1 1 // Don't remove this.
#endif
/*------------------------------------------------------------------
*
* Name: window
*
* Purpose: Filter window shape functions.
*
* Inputs: type - BP_WINDOW_HAMMING, etc.
* size - Number of filter taps.
* j - Index in range of 0 to size-1.
*
* Returns: Multiplier for the window shape.
*
*----------------------------------------------------------------*/
float window (bp_window_t type, int size, int j)
{
float center;
float w;
center = 0.5 * (size - 1);
switch (type) {
case BP_WINDOW_COSINE:
w = cos((j - center) / size * M_PI);
//w = sin(j * M_PI / (size - 1));
break;
case BP_WINDOW_HAMMING:
w = 0.53836 - 0.46164 * cos((j * 2 * M_PI) / (size - 1));
break;
case BP_WINDOW_BLACKMAN:
w = 0.42659 - 0.49656 * cos((j * 2 * M_PI) / (size - 1))
+ 0.076849 * cos((j * 4 * M_PI) / (size - 1));
break;
case BP_WINDOW_FLATTOP:
w = 1.0 - 1.93 * cos((j * 2 * M_PI) / (size - 1))
+ 1.29 * cos((j * 4 * M_PI) / (size - 1))
- 0.388 * cos((j * 6 * M_PI) / (size - 1))
+ 0.028 * cos((j * 8 * M_PI) / (size - 1));
break;
case BP_WINDOW_TRUNCATED:
default:
w = 1.0;
break;
}
return (w);
}
/*------------------------------------------------------------------
*
* Name: gen_lowpass
*
* Purpose: Generate low pass filter kernel.
*
* Inputs: fc - Cutoff frequency as fraction of sampling frequency.
* filter_size - Number of filter taps.
* wtype - Window type, BP_WINDOW_HAMMING, etc.
* lp_delay_fract - Fudge factor for the delay value.
*
* Outputs: lp_filter
*
* Returns: Signal delay thru the filter in number of audio samples.
*
*----------------------------------------------------------------*/
int gen_lowpass (float fc, float *lp_filter, int filter_size, bp_window_t wtype, float lp_delay_fract)
{
int j;
float G;
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Lowpass, size=%d, fc=%.2f\n", filter_size, fc);
dw_printf (" j shape sinc final\n");
#endif
assert (filter_size >= 3 && filter_size <= MAX_FILTER_SIZE);
for (j=0; j<filter_size; j++) {
float center;
float sinc;
float shape;
center = 0.5 * (filter_size - 1);
if (j - center == 0) {
sinc = 2 * fc;
}
else {
sinc = sin(2 * M_PI * fc * (j-center)) / (M_PI*(j-center));
}
shape = window (wtype, filter_size, j);
lp_filter[j] = sinc * shape;
#if DEBUG1
dw_printf ("%6d %6.2f %6.3f %6.3f\n", j, shape, sinc, lp_filter[j] ) ;
#endif
}
/*
* Normalize lowpass for unity gain at DC.
*/
G = 0;
for (j=0; j<filter_size; j++) {
G += lp_filter[j];
}
for (j=0; j<filter_size; j++) {
lp_filter[j] = lp_filter[j] / G;
}
// Calculate the signal delay.
// If a signal at level 0 steps to level 1, this is the time that it would
// take for the output to reach 0.5.
//
// Examples:
//
// Filter has one tap with value of 1.0.
// Output is immediate so I would call this delay of 0.
//
// Filter coefficients: 0.2, 0.2, 0.2, 0.2, 0.2
// "1" inputs Out
// 1 0.2
// 2 0.4
// 3 0.6
//
// In this case, the output does not change immediately.
// It takes two more samples to reach the half way point
// so it has a delay of 2.
float sum = 0;
int delay = 0;
if (lp_delay_fract == 0) lp_delay_fract = 0.5;
for (j=0; j<filter_size; j++) {
sum += lp_filter[j];
#if DEBUG1
dw_printf ("lp_filter[%d] = %.3f sum = %.3f lp_delay_fract = %.3f\n", j, lp_filter[j], sum, lp_delay_fract);
#endif
if (sum > lp_delay_fract) {
delay = j;
break;
}
}
#if DEBUG1
dw_printf ("Low Pass Delay = %d samples\n", delay) ;
#endif
// Hmmm. This might have been wasted effort. The result is always half the number of taps.
if (delay < 2 || delay > filter_size - 2) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Internal error, %s %d, delay %d for size %d\n", __func__, __LINE__, delay, filter_size);
}
return (delay);
} /* end gen_lowpass */
#undef DEBUG1
/*------------------------------------------------------------------
*
* Name: gen_bandpass
*
* Purpose: Generate band pass filter kernel for the prefilter.
* This is NOT for the mark/space filters.
*
* Inputs: f1 - Lower cutoff frequency as fraction of sampling frequency.
* f2 - Upper cutoff frequency...
* filter_size - Number of filter taps.
* wtype - Window type, BP_WINDOW_HAMMING, etc.
*
* Outputs: bp_filter
*
* Reference: http://www.labbookpages.co.uk/audio/firWindowing.html
*
* Does it need to be an odd length?
*
*----------------------------------------------------------------*/
void gen_bandpass (float f1, float f2, float *bp_filter, int filter_size, bp_window_t wtype)
{
int j;
float w;
float G;
float center = 0.5 * (filter_size - 1);
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Bandpass, size=%d\n", filter_size);
dw_printf (" j shape sinc final\n");
#endif
assert (filter_size >= 3 && filter_size <= MAX_FILTER_SIZE);
for (j=0; j<filter_size; j++) {
float sinc;
float shape;
if (j - center == 0) {
sinc = 2 * (f2 - f1);
}
else {
sinc = sin(2 * M_PI * f2 * (j-center)) / (M_PI*(j-center))
- sin(2 * M_PI * f1 * (j-center)) / (M_PI*(j-center));
}
shape = window (wtype, filter_size, j);
bp_filter[j] = sinc * shape;
#if DEBUG1
dw_printf ("%6d %6.2f %6.3f %6.3f\n", j, shape, sinc, bp_filter[j] ) ;
#endif
}
/*
* Normalize bandpass for unity gain in middle of passband.
* Can't use same technique as for lowpass.
* Instead compute gain in middle of passband.
* See http://dsp.stackexchange.com/questions/4693/fir-filter-gain
*/
w = 2 * M_PI * (f1 + f2) / 2;
G = 0;
for (j=0; j<filter_size; j++) {
G += 2 * bp_filter[j] * cos((j-center)*w); // is this correct?
}
#if DEBUG1
dw_printf ("Before normalizing, G=%.3f\n", G);
#endif
for (j=0; j<filter_size; j++) {
bp_filter[j] = bp_filter[j] / G;
}
} /* end gen_bandpass */
/*------------------------------------------------------------------
*
* Name: gen_ms
*
* Purpose: Generate mark and space filters.
*
* Inputs: fc - Tone frequency, i.e. mark or space.
* sps - Samples per second.
* filter_size - Number of filter taps.
* wtype - Window type, BP_WINDOW_HAMMING, etc.
*
* Outputs: bp_filter
*
* Reference: http://www.labbookpages.co.uk/audio/firWindowing.html
*
* Does it need to be an odd length?
*
*----------------------------------------------------------------*/
void gen_ms (int fc, int sps, float *sin_table, float *cos_table, int filter_size, int wtype)
{
int j;
float Gs = 0, Gc = 0;;
for (j=0; j<filter_size; j++) {
float center = 0.5f * (filter_size - 1);
float am = ((float)(j - center) / (float)sps) * ((float)fc) * (2.0f * (float)M_PI);
float shape = window (wtype, filter_size, j);
sin_table[j] = sinf(am) * shape;
cos_table[j] = cosf(am) * shape;
Gs += sin_table[j] * sinf(am);
Gc += cos_table[j] * cosf(am);
#if DEBUG1
dw_printf ("%6d %6.2f %6.2f %6.2f\n", j, shape, sin_table[j], cos_table[j]) ;
#endif
}
/* Normalize for unity gain */
#if DEBUG1
dw_printf ("Before normalizing, Gs = %.2f, Gc = %.2f\n", Gs, Gc) ;
#endif
for (j=0; j<filter_size; j++) {
sin_table[j] = sin_table[j] / Gs;
cos_table[j] = cos_table[j] / Gc;
}
} /* end gen_ms */
/* end dsp.c */