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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
/* -*- c++ -*- */
/*
* Copyright 2002,2007 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio 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 3, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include <gr_firdes.h>
#include <stdexcept>
using std::vector;
#define IzeroEPSILON 1E-21 /* Max error acceptable in Izero */
static double Izero(double x)
{
double sum, u, halfx, temp;
int n;
sum = u = n = 1;
halfx = x/2.0;
do {
temp = halfx/(double)n;
n += 1;
temp *= temp;
u *= temp;
sum += u;
} while (u >= IzeroEPSILON*sum);
return(sum);
}
//
// === Low Pass ===
//
vector<float>
gr_firdes::low_pass (double gain,
double sampling_freq,
double cutoff_freq, // Hz center of transition band
double transition_width, // Hz width of transition band
win_type window_type,
double beta) // used only with Kaiser
{
sanity_check_1f (sampling_freq, cutoff_freq, transition_width);
int ntaps = compute_ntaps (sampling_freq, transition_width,
window_type, beta);
// construct the truncated ideal impulse response
// [sin(x)/x for the low pass case]
vector<float> taps(ntaps);
vector<float> w = window (window_type, ntaps, beta);
int M = (ntaps - 1) / 2;
double fwT0 = 2 * M_PI * cutoff_freq / sampling_freq;
for (int n = -M; n <= M; n++){
if (n == 0)
taps[n + M] = fwT0 / M_PI * w[n + M];
else {
// a little algebra gets this into the more familiar sin(x)/x form
taps[n + M] = sin (n * fwT0) / (n * M_PI) * w[n + M];
}
}
// find the factor to normalize the gain, fmax.
// For low-pass, gain @ zero freq = 1.0
double fmax = taps[0 + M];
for (int n = 1; n <= M; n++)
fmax += 2 * taps[n + M];
gain /= fmax; // normalize
for (int i = 0; i < ntaps; i++)
taps[i] *= gain;
return taps;
}
//
// === High Pass ===
//
vector<float>
gr_firdes::high_pass (double gain,
double sampling_freq,
double cutoff_freq, // Hz center of transition band
double transition_width, // Hz width of transition band
win_type window_type,
double beta) // used only with Kaiser
{
sanity_check_1f (sampling_freq, cutoff_freq, transition_width);
int ntaps = compute_ntaps (sampling_freq, transition_width,
window_type, beta);
// construct the truncated ideal impulse response times the window function
vector<float> taps(ntaps);
vector<float> w = window (window_type, ntaps, beta);
int M = (ntaps - 1) / 2;
double fwT0 = 2 * M_PI * cutoff_freq / sampling_freq;
for (int n = -M; n <= M; n++){
if (n == 0)
taps[n + M] = (1 - (fwT0 / M_PI)) * w[n + M];
else {
// a little algebra gets this into the more familiar sin(x)/x form
taps[n + M] = -sin (n * fwT0) / (n * M_PI) * w[n + M];
}
}
// find the factor to normalize the gain, fmax.
// For high-pass, gain @ fs/2 freq = 1.0
double fmax = taps[0 + M];
for (int n = 1; n <= M; n++)
fmax += 2 * taps[n + M] * cos (n * M_PI);
gain /= fmax; // normalize
for (int i = 0; i < ntaps; i++)
taps[i] *= gain;
return taps;
}
//
// === Band Pass ===
//
vector<float>
gr_firdes::band_pass (double gain,
double sampling_freq,
double low_cutoff_freq, // Hz center of transition band
double high_cutoff_freq, // Hz center of transition band
double transition_width, // Hz width of transition band
win_type window_type,
double beta) // used only with Kaiser
{
sanity_check_2f (sampling_freq,
low_cutoff_freq,
high_cutoff_freq, transition_width);
int ntaps = compute_ntaps (sampling_freq, transition_width,
window_type, beta);
// construct the truncated ideal impulse response times the window function
vector<float> taps(ntaps);
vector<float> w = window (window_type, ntaps, beta);
int M = (ntaps - 1) / 2;
double fwT0 = 2 * M_PI * low_cutoff_freq / sampling_freq;
double fwT1 = 2 * M_PI * high_cutoff_freq / sampling_freq;
for (int n = -M; n <= M; n++){
if (n == 0)
taps[n + M] = (fwT1 - fwT0) / M_PI * w[n + M];
else {
taps[n + M] = (sin (n * fwT1) - sin (n * fwT0)) / (n * M_PI) * w[n + M];
}
}
// find the factor to normalize the gain, fmax.
// For band-pass, gain @ center freq = 1.0
double fmax = taps[0 + M];
for (int n = 1; n <= M; n++)
fmax += 2 * taps[n + M] * cos (n * (fwT0 + fwT1) * 0.5);
gain /= fmax; // normalize
for (int i = 0; i < ntaps; i++)
taps[i] *= gain;
return taps;
}
//
// === Complex Band Pass ===
//
vector<gr_complex>
gr_firdes::complex_band_pass (double gain,
double sampling_freq,
double low_cutoff_freq, // Hz center of transition band
double high_cutoff_freq, // Hz center of transition band
double transition_width, // Hz width of transition band
win_type window_type,
double beta) // used only with Kaiser
{
sanity_check_2f_c (sampling_freq,
low_cutoff_freq,
high_cutoff_freq, transition_width);
int ntaps = compute_ntaps (sampling_freq, transition_width,
window_type, beta);
// construct the truncated ideal impulse response times the window function
vector<gr_complex> taps(ntaps);
vector<float> lptaps(ntaps);
vector<float> w = window (window_type, ntaps, beta);
lptaps = low_pass(gain,sampling_freq,(high_cutoff_freq - low_cutoff_freq)/2,transition_width,window_type,beta);
gr_complex *optr = &taps[0];
float *iptr = &lptaps[0];
float freq = M_PI * (high_cutoff_freq + low_cutoff_freq)/sampling_freq;
float phase=0;
if (lptaps.size() & 01) {
phase = - freq * ( lptaps.size() >> 1 );
} else phase = - freq/2.0 * ((1 + 2*lptaps.size()) >> 1);
for(unsigned int i=0;i<lptaps.size();i++) {
*optr++ = gr_complex(*iptr * cos(phase),*iptr * sin(phase));
iptr++, phase += freq;
}
return taps;
}
//
// === Band Reject ===
//
vector<float>
gr_firdes::band_reject (double gain,
double sampling_freq,
double low_cutoff_freq, // Hz center of transition band
double high_cutoff_freq, // Hz center of transition band
double transition_width, // Hz width of transition band
win_type window_type,
double beta) // used only with Kaiser
{
sanity_check_2f (sampling_freq,
low_cutoff_freq,
high_cutoff_freq, transition_width);
int ntaps = compute_ntaps (sampling_freq, transition_width,
window_type, beta);
// construct the truncated ideal impulse response times the window function
vector<float> taps(ntaps);
vector<float> w = window (window_type, ntaps, beta);
int M = (ntaps - 1) / 2;
double fwT0 = 2 * M_PI * low_cutoff_freq / sampling_freq;
double fwT1 = 2 * M_PI * high_cutoff_freq / sampling_freq;
for (int n = -M; n <= M; n++){
if (n == 0)
taps[n + M] = 1.0 + ((fwT0 - fwT1) / M_PI * w[n + M]);
else {
taps[n + M] = (sin (n * fwT0) - sin (n * fwT1)) / (n * M_PI) * w[n + M];
}
}
// find the factor to normalize the gain, fmax.
// For band-reject, gain @ zero freq = 1.0
double fmax = taps[0 + M];
for (int n = 1; n <= M; n++)
fmax += 2 * taps[n + M];
gain /= fmax; // normalize
for (int i = 0; i < ntaps; i++)
taps[i] *= gain;
return taps;
}
//
// Hilbert Transform
//
vector<float>
gr_firdes::hilbert (unsigned int ntaps,
win_type windowtype,
double beta)
{
if(!(ntaps & 1))
throw std::out_of_range("Hilbert: Must have odd number of taps");
vector<float> taps(ntaps);
vector<float> w = window (windowtype, ntaps, beta);
unsigned int h = (ntaps-1)/2;
float gain=0;
for (unsigned int i = 1; i <= h; i++)
{
if(i&1)
{
float x = 1/(float)i;
taps[h+i] = x * w[h+i];
taps[h-i] = -x * w[h-i];
gain = taps[h+i] - gain;
}
else
taps[h+i] = taps[h-i] = 0;
}
gain = 2 * fabs(gain);
for ( unsigned int i = 0; i < ntaps; i++)
taps[i] /= gain;
return taps;
}
//
// Gaussian
//
vector<float>
gr_firdes::gaussian (double gain,
double spb,
double bt,
int ntaps)
{
vector<float> taps(ntaps);
double scale = 0;
double dt = 1.0/spb;
double s = 1.0/(sqrt(log(2)) / (2*M_PI*bt));
double t0 = -0.5 * ntaps;
double ts;
for(int i=0;i<ntaps;i++)
{
t0++;
ts = s*dt*t0;
taps[i] = exp(-0.5*ts*ts);
scale += taps[i];
}
for(int i=0;i<ntaps;i++)
taps[i] = taps[i] / scale * gain;
return taps;
}
//
// Root Raised Cosine
//
vector<float>
gr_firdes::root_raised_cosine (double gain,
double sampling_freq,
double symbol_rate,
double alpha,
int ntaps)
{
ntaps |= 1; // ensure that ntaps is odd
double spb = sampling_freq/symbol_rate; // samples per bit/symbol
vector<float> taps(ntaps);
double scale = 0;
for(int i=0;i<ntaps;i++)
{
double x1,x2,x3,num,den;
double xindx = i - ntaps/2;
x1 = M_PI * xindx/spb;
x2 = 4 * alpha * xindx / spb;
x3 = x2*x2 - 1;
if( fabs(x3) >= 0.000001 ) // Avoid Rounding errors...
{
if( i != ntaps/2 )
num = cos((1+alpha)*x1) + sin((1-alpha)*x1)/(4*alpha*xindx/spb);
else
num = cos((1+alpha)*x1) + (1-alpha) * M_PI / (4*alpha);
den = x3 * M_PI;
}
else
{
if(alpha==1)
{
taps[i] = -1;
continue;
}
x3 = (1-alpha)*x1;
x2 = (1+alpha)*x1;
num = (sin(x2)*(1+alpha)*M_PI
- cos(x3)*((1-alpha)*M_PI*spb)/(4*alpha*xindx)
+ sin(x3)*spb*spb/(4*alpha*xindx*xindx));
den = -32 * M_PI * alpha * alpha * xindx/spb;
}
taps[i] = 4 * alpha * num / den;
scale += taps[i];
}
for(int i=0;i<ntaps;i++)
taps[i] = taps[i] * gain / scale;
return taps;
}
//
// === Utilities ===
//
// delta_f / width_factor gives number of taps required.
static const float width_factor[5] = { // indexed by win_type
3.3, // WIN_HAMMING
3.1, // WIN_HANN
5.5, // WIN_BLACKMAN
2.0, // WIN_RECTANGULAR
//5.0 // WIN_KAISER (guesstimate compromise)
//2.0 // WIN_KAISER (guesstimate compromise)
10.0 // WIN_KAISER
};
int
gr_firdes::compute_ntaps (double sampling_freq,
double transition_width,
win_type window_type,
double beta)
{
// normalized transition width
double delta_f = transition_width / sampling_freq;
// compute number of taps required for given transition width
int ntaps = (int) (width_factor[window_type] / delta_f + 0.5);
if ((ntaps & 1) == 0) // if even...
ntaps++; // ...make odd
return ntaps;
}
double gr_firdes::bessi0(double x)
{
double ax,ans;
double y;
ax=fabs(x);
if (ax < 3.75)
{
y=x/3.75;
y*=y;
ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492
+y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
}
else
{
y=3.75/ax;
ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1
+y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2
+y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1
+y*0.392377e-2))))))));
}
return ans;
}
vector<float>
gr_firdes::window (win_type type, int ntaps, double beta)
{
vector<float> taps(ntaps);
int M = ntaps - 1; // filter order
switch (type){
case WIN_RECTANGULAR:
for (int n = 0; n < ntaps; n++)
taps[n] = 1;
case WIN_HAMMING:
for (int n = 0; n < ntaps; n++)
taps[n] = 0.54 - 0.46 * cos ((2 * M_PI * n) / M);
break;
case WIN_HANN:
for (int n = 0; n < ntaps; n++)
taps[n] = 0.5 - 0.5 * cos ((2 * M_PI * n) / M);
break;
case WIN_BLACKMAN:
for (int n = 0; n < ntaps; n++)
taps[n] = 0.42 - 0.50 * cos ((2*M_PI * n) / (M-1)) - 0.08 * cos ((4*M_PI * n) / (M-1));
break;
#if 0
case WIN_KAISER:
for (int n = 0; n < ntaps; n++)
taps[n] = bessi0(beta*sqrt(1.0 - (4.0*n/(M*M))))/bessi0(beta);
break;
#else
case WIN_KAISER:
{
double IBeta = 1.0/Izero(beta);
double inm1 = 1.0/((double)(ntaps));
double temp;
//fprintf(stderr, "IBeta = %g; inm1 = %g\n", IBeta, inm1);
for (int i=0; i<ntaps; i++) {
temp = i * inm1;
//fprintf(stderr, "temp = %g\n", temp);
taps[i] = Izero(beta*sqrt(1.0-temp*temp)) * IBeta;
//fprintf(stderr, "taps[%d] = %g\n", i, taps[i]);
}
}
break;
#endif
default:
throw std::out_of_range ("gr_firdes:window: type out of range");
}
return taps;
}
void
gr_firdes::sanity_check_1f (double sampling_freq,
double fa, // cutoff freq
double transition_width)
{
if (sampling_freq <= 0.0)
throw std::out_of_range ("gr_firdes check failed: sampling_freq > 0");
if (fa <= 0.0 || fa > sampling_freq / 2)
throw std::out_of_range ("gr_firdes check failed: 0 < fa <= sampling_freq / 2");
if (transition_width <= 0)
throw std::out_of_range ("gr_dirdes check failed: transition_width > 0");
}
void
gr_firdes::sanity_check_2f (double sampling_freq,
double fa, // first cutoff freq
double fb, // second cutoff freq
double transition_width)
{
if (sampling_freq <= 0.0)
throw std::out_of_range ("gr_firdes check failed: sampling_freq > 0");
if (fa <= 0.0 || fa > sampling_freq / 2)
throw std::out_of_range ("gr_firdes check failed: 0 < fa <= sampling_freq / 2");
if (fb <= 0.0 || fb > sampling_freq / 2)
throw std::out_of_range ("gr_firdes check failed: 0 < fb <= sampling_freq / 2");
if (fa > fb)
throw std::out_of_range ("gr_firdes check failed: fa <= fb");
if (transition_width <= 0)
throw std::out_of_range ("gr_firdes check failed: transition_width > 0");
}
void
gr_firdes::sanity_check_2f_c (double sampling_freq,
double fa, // first cutoff freq
double fb, // second cutoff freq
double transition_width)
{
if (sampling_freq <= 0.0)
throw std::out_of_range ("gr_firdes check failed: sampling_freq > 0");
if (fa < -sampling_freq / 2 || fa > sampling_freq / 2)
throw std::out_of_range ("gr_firdes check failed: 0 < fa <= sampling_freq / 2");
if (fb < -sampling_freq / 2 || fb > sampling_freq / 2)
throw std::out_of_range ("gr_firdes check failed: 0 < fb <= sampling_freq / 2");
if (fa > fb)
throw std::out_of_range ("gr_firdes check failed: fa <= fb");
if (transition_width <= 0)
throw std::out_of_range ("gr_firdes check failed: transition_width > 0");
}
|