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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
|
/* -*- c++ -*- */
/*
* Copyright (C) 2017 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 this file; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "interpolating_resampler.h"
#include <gnuradio/math.h>
#include <stdexcept>
#include <deque>
namespace gr {
namespace digital {
interpolating_resampler::interpolating_resampler(enum ir_type type,
bool derivative)
: d_type(type),
d_derivative(derivative),
d_phase(0.0f),
d_phase_wrapped(0.0f),
d_phase_n(0),
d_prev_phase(0.0f),
d_prev_phase_wrapped(0.0f),
d_prev_phase_n(0)
{
switch (d_type) {
case IR_MMSE_8TAP:
break;
case IR_PFB_NO_MF:
break;
case IR_PFB_MF:
break;
case IR_NONE:
default:
throw std::invalid_argument(
"interpolating_resampler: invalid interpolating resampler type.");
break;
}
sync_reset(0.0f);
}
void
interpolating_resampler::next_phase(float increment,
float &phase,
int &phase_n,
float &phase_wrapped)
{
float n;
phase = d_phase_wrapped + increment;
n = floorf(phase);
phase_wrapped = phase - n;
phase_n = static_cast<int>(n);
}
void
interpolating_resampler::advance_phase(float increment)
{
d_prev_phase = d_phase;
d_prev_phase_wrapped = d_phase_wrapped;
d_prev_phase_n = d_phase_n;
next_phase(increment, d_phase, d_phase_n, d_phase_wrapped);
}
void
interpolating_resampler::revert_phase()
{
d_phase = d_prev_phase;
d_phase_wrapped = d_prev_phase_wrapped;
d_phase_n = d_prev_phase_n;
}
void
interpolating_resampler::sync_reset(float phase)
{
float n;
d_phase = phase;
n = floorf(d_phase);
d_phase_wrapped = d_phase - n;
d_phase_n = static_cast<int>(n);
d_prev_phase = d_phase;
d_prev_phase_wrapped = d_phase_wrapped;
d_prev_phase_n = d_phase_n;
}
/*************************************************************************/
interpolating_resampler_ccf *
interpolating_resampler_ccf::make(enum ir_type type,
bool derivative,
int nfilts,
const std::vector<float> &taps)
{
interpolating_resampler_ccf *ret = NULL;
switch (type) {
case IR_MMSE_8TAP:
ret = new interp_resampler_mmse_8tap_cc(derivative);
break;
case IR_PFB_NO_MF:
ret = new interp_resampler_pfb_no_mf_cc(derivative, nfilts);
break;
case IR_PFB_MF:
ret = new interp_resampler_pfb_mf_ccf(taps, nfilts, derivative);
break;
case IR_NONE:
default:
throw std::invalid_argument("interpolating_resampler_ccf: invalid "
"interpolating resampler type.");
break;
}
return ret;
}
/*************************************************************************/
interpolating_resampler_fff *
interpolating_resampler_fff::make(enum ir_type type,
bool derivative,
int nfilts,
const std::vector<float> &taps)
{
interpolating_resampler_fff *ret = NULL;
switch (type) {
case IR_MMSE_8TAP:
ret = new interp_resampler_mmse_8tap_ff(derivative);
break;
case IR_PFB_NO_MF:
ret = new interp_resampler_pfb_no_mf_ff(derivative, nfilts);
break;
case IR_PFB_MF:
ret = new interp_resampler_pfb_mf_fff(taps, nfilts, derivative);
break;
case IR_NONE:
default:
throw std::invalid_argument("interpolating_resampler_fff: invalid "
"interpolating resampler type.");
break;
}
return ret;
}
/*************************************************************************/
interp_resampler_mmse_8tap_cc::interp_resampler_mmse_8tap_cc(
bool derivative)
: interpolating_resampler_ccf(IR_MMSE_8TAP, derivative),
d_interp(NULL),
d_interp_diff(NULL)
{
d_interp = new filter::mmse_fir_interpolator_cc();
if (d_interp == NULL)
throw std::runtime_error("unable to create mmse_fir_interpolator_cc");
if (d_derivative) {
d_interp_diff = new filter::mmse_interp_differentiator_cc();
if (d_interp_diff == NULL)
throw std::runtime_error("unable to create "
"mmse_interp_differentiator_cc");
}
}
interp_resampler_mmse_8tap_cc::~interp_resampler_mmse_8tap_cc()
{
delete d_interp;
if (d_derivative)
delete d_interp_diff;
}
gr_complex
interp_resampler_mmse_8tap_cc::interpolate(const gr_complex input[],
float mu) const
{
return d_interp->interpolate(input, mu);
}
gr_complex
interp_resampler_mmse_8tap_cc::differentiate(const gr_complex input[],
float mu) const
{
return d_interp_diff->differentiate(input, mu);
}
unsigned int
interp_resampler_mmse_8tap_cc::ntaps() const
{
return d_interp->ntaps();
}
/*************************************************************************/
interp_resampler_mmse_8tap_ff::interp_resampler_mmse_8tap_ff(
bool derivative)
: interpolating_resampler_fff(IR_MMSE_8TAP, derivative),
d_interp(NULL),
d_interp_diff(NULL)
{
d_interp = new filter::mmse_fir_interpolator_ff();
if (d_interp == NULL)
throw std::runtime_error("unable to create mmse_fir_interpolator_ff");
if (d_derivative) {
d_interp_diff = new filter::mmse_interp_differentiator_ff();
if (d_interp_diff == NULL)
throw std::runtime_error("unable to create "
"mmse_interp_differentiator_ff");
}
}
interp_resampler_mmse_8tap_ff::~interp_resampler_mmse_8tap_ff()
{
delete d_interp;
if (d_derivative)
delete d_interp_diff;
}
float
interp_resampler_mmse_8tap_ff::interpolate(const float input[],
float mu) const
{
return d_interp->interpolate(input, mu);
}
float
interp_resampler_mmse_8tap_ff::differentiate(const float input[],
float mu) const
{
return d_interp_diff->differentiate(input, mu);
}
unsigned int
interp_resampler_mmse_8tap_ff::ntaps() const
{
return d_interp->ntaps();
}
/*************************************************************************/
#include "gnuradio/filter/interpolator_taps.h"
#include "gnuradio/filter/interp_differentiator_taps.h"
interp_resampler_pfb_no_mf_cc::interp_resampler_pfb_no_mf_cc(
bool derivative,
int nfilts)
: interpolating_resampler_ccf(IR_PFB_NO_MF, derivative),
d_nfilters(0),
d_filters(),
d_diff_filters()
{
if (nfilts <= 1)
throw std::invalid_argument("interpolating_resampler_pfb_no_mf_cc: "
"number of polyphase filter arms "
"must be greater than 1");
// Round up the number of filter arms to the current or next power of 2
d_nfilters =
1 << (static_cast<int>(log2f(static_cast<float>(nfilts-1))) + 1);
// N.B. We assume in this class: NSTEPS == DNSTEPS and NTAPS == DNTAPS
// Limit to the maximum number of precomputed MMSE tap sets
if (d_nfilters <= 0 or d_nfilters > NSTEPS)
d_nfilters = NSTEPS;
// Create our polyphase filter arms for the steps from 0.0 to 1.0 from
// the MMSE interpolating filter and MMSE interpolating differentiator
// taps rows.
// N.B. We create an extra final row for an offset of 1.0, because it's
// easier than dealing with wrap around from 0.99... to 0.0 shifted
// by 1 tap.
d_filters = std::vector<filter::kernel::fir_filter_ccf*>(d_nfilters + 1,
NULL);
d_diff_filters = std::vector<filter::kernel::fir_filter_ccf*>(
d_nfilters + 1,
NULL);
std::vector<float> t(NTAPS, 0);
int incr = NSTEPS/d_nfilters;
int src, dst;
for (src = 0, dst = 0; src <= NSTEPS; src += incr, dst++) {
t.assign(&taps[src][0], &taps[src][NTAPS]);
d_filters[dst] = new filter::kernel::fir_filter_ccf(1, t);
if (d_filters[dst] == NULL)
throw std::runtime_error("unable to create fir_filter_ccf");
if (d_derivative) {
t.assign(&Dtaps[src][0], &Dtaps[src][DNTAPS]);
d_diff_filters[dst] = new filter::kernel::fir_filter_ccf(1, t);
if (d_diff_filters[dst] == NULL)
throw std::runtime_error("unable to create fir_filter_ccf");
}
}
}
interp_resampler_pfb_no_mf_cc::~interp_resampler_pfb_no_mf_cc()
{
for (int i = 0; i <= d_nfilters; i++) {
delete d_filters[i];
if (d_derivative)
delete d_diff_filters[i];
}
}
gr_complex
interp_resampler_pfb_no_mf_cc::interpolate(const gr_complex input[],
float mu) const
{
int arm = static_cast<int>(rint(mu * d_nfilters));
if (arm < 0 or arm > d_nfilters)
throw std::runtime_error("interp_resampler_pfb_no_mf_cc: mu is not "
"in the range [0.0, 1.0]");
return d_filters[arm]->filter(input);
}
gr_complex
interp_resampler_pfb_no_mf_cc::differentiate(const gr_complex input[],
float mu) const
{
int arm = static_cast<int>(rint(mu * d_nfilters));
if (arm < 0 or arm > d_nfilters)
throw std::runtime_error("interp_resampler_pfb_no_mf_cc: mu is not "
"in the range [0.0, 1.0]");
return d_diff_filters[arm]->filter(input);
}
unsigned int
interp_resampler_pfb_no_mf_cc::ntaps() const
{
return NTAPS;
}
/*************************************************************************/
interp_resampler_pfb_no_mf_ff::interp_resampler_pfb_no_mf_ff(
bool derivative,
int nfilts)
: interpolating_resampler_fff(IR_PFB_NO_MF, derivative),
d_nfilters(0),
d_filters(),
d_diff_filters()
{
if (nfilts <= 1)
throw std::invalid_argument("interpolating_resampler_pfb_no_mf_ff: "
"number of polyphase filter arms "
"must be greater than 1");
// Round up the number of filter arms to the current or next power of 2
d_nfilters =
1 << (static_cast<int>(log2f(static_cast<float>(nfilts-1))) + 1);
// N.B. We assume in this class: NSTEPS == DNSTEPS and NTAPS == DNTAPS
// Limit to the maximum number of precomputed MMSE tap sets
if (d_nfilters <= 0 or d_nfilters > NSTEPS)
d_nfilters = NSTEPS;
// Create our polyphase filter arms for the steps from 0.0 to 1.0 from
// the MMSE interpolating filter and MMSE interpolating differentiator
// taps rows.
// N.B. We create an extra final row for an offset of 1.0, because it's
// easier than dealing with wrap around from 0.99... to 0.0 shifted
// by 1 tap.
d_filters = std::vector<filter::kernel::fir_filter_fff*>(d_nfilters + 1,
NULL);
d_diff_filters = std::vector<filter::kernel::fir_filter_fff*>(
d_nfilters + 1,
NULL);
std::vector<float> t(NTAPS, 0);
int incr = NSTEPS/d_nfilters;
int src, dst;
for (src = 0, dst = 0; src <= NSTEPS; src += incr, dst++) {
t.assign(&taps[src][0], &taps[src][NTAPS]);
d_filters[dst] = new filter::kernel::fir_filter_fff(1, t);
if (d_filters[dst] == NULL)
throw std::runtime_error("unable to create fir_filter_fff");
if (d_derivative) {
t.assign(&Dtaps[src][0], &Dtaps[src][DNTAPS]);
d_diff_filters[dst] = new filter::kernel::fir_filter_fff(1, t);
if (d_diff_filters[dst] == NULL)
throw std::runtime_error("unable to create fir_filter_fff");
}
}
}
interp_resampler_pfb_no_mf_ff::~interp_resampler_pfb_no_mf_ff()
{
for (int i = 0; i <= d_nfilters; i++) {
delete d_filters[i];
if (d_derivative)
delete d_diff_filters[i];
}
}
float
interp_resampler_pfb_no_mf_ff::interpolate(const float input[],
float mu) const
{
int arm = static_cast<int>(rint(mu * d_nfilters));
if (arm < 0 or arm > d_nfilters)
throw std::runtime_error("interp_resampler_pfb_no_mf_ff: mu is not "
"in the range [0.0, 1.0]");
return d_filters[arm]->filter(input);
}
float
interp_resampler_pfb_no_mf_ff::differentiate(const float input[],
float mu) const
{
int arm = static_cast<int>(rint(mu * d_nfilters));
if (arm < 0 or arm > d_nfilters)
throw std::runtime_error("interp_resampler_pfb_no_mf_ff: mu is not "
"in the range [0.0, 1.0]");
return d_diff_filters[arm]->filter(input);
}
unsigned int
interp_resampler_pfb_no_mf_ff::ntaps() const
{
return NTAPS;
}
/*************************************************************************/
interp_resampler_pfb_mf_ccf::interp_resampler_pfb_mf_ccf(
const std::vector<float> &taps,
int nfilts,
bool derivative)
: interpolating_resampler_ccf(IR_PFB_MF, derivative),
d_nfilters(nfilts),
d_taps_per_filter(static_cast<unsigned int>(
ceil( static_cast<double>(taps.size())
/ static_cast<double>(nfilts )))),
d_filters(),
d_diff_filters(),
d_taps(),
d_diff_taps()
{
if (d_nfilters <= 1)
throw std::invalid_argument("interpolating_resampler_pfb_mf_ccf: "
"number of polyphase filter arms "
"must be greater than 1");
if (taps.size() < static_cast<unsigned int>(d_nfilters))
throw std::invalid_argument("interpolating_resampler_pfb_mf_ccf: "
"length of the prototype filter taps"
" must be greater than or equal to "
"the number of polyphase filter arms.");
// Create a derivative filter from the provided taps
// First create a truncated ideal differentiator filter
int ideal_diff_filt_len = 3; // Must be odd; rest of init assumes odd.
std::vector<float> ideal_diff_taps(ideal_diff_filt_len, 0.0f);
int i, n;
n = ideal_diff_taps.size()/2;
for (i = -n; i < 0; i++) {
ideal_diff_taps[i+n] = (-i & 1) == 1 ? -1.0f/i : 1.0f/i;
ideal_diff_taps[n-i] = -ideal_diff_taps[i+n];
}
ideal_diff_taps[n] = 0.0f;
// Perform linear convolution of prototype filter taps and the truncated
// ideal differentiator taps to generate a derivative matched filter.
// N.B. the truncated ideal differentiator taps must have an odd length
int j, k, l, m;
m = ideal_diff_taps.size();
n = taps.size();
l = m + n - 1; // length of convolution
std::deque<float> diff_taps(l, 0.0f);
for (i = 0; i < l; i++) {
for (j = 0; j < m; j++) {
k = i + j - (m - 1);
if (k < 0 or k >= n)
continue;
diff_taps[i] += ideal_diff_taps[(m - 1) - j] * taps[k];
}
}
// Trim the convolution so the prototype derivative filter is the same
// length as the passed in prototype filter taps.
n = ideal_diff_taps.size()/2;
for (i = 0; i < n; i++) {
diff_taps.pop_back();
diff_taps.pop_front();
}
// Squash the differentiation noise spikes at the filter ends.
diff_taps[0] = 0.0f;
diff_taps[diff_taps.size()-1] = 0.0f;
// Normalize the prototype derviative filter gain to the number of
// filter arms
n = diff_taps.size();
float mag = 0.0f;
for (i = 0; i < n; i++)
mag += fabsf(diff_taps[i]);
for (i = 0; i < n; i++) {
diff_taps[i] *= d_nfilters/mag;
if (d_derivative and std::isnan(diff_taps[i]))
throw std::runtime_error("interpolating_resampler_pfb_mf_ccf: "
"NaN error creating derivative filter."
);
}
// Create our polyphase filter arms for the steps from 0.0 to 1.0 from
// the prototype matched filter.
// N.B. We create an extra final row for an offset of 1.0, because it's
// easier than dealing with wrap around from 0.99... to 0.0 shifted
// by 1 tap.
d_filters = std::vector<filter::kernel::fir_filter_ccf*>(d_nfilters + 1,
NULL);
d_diff_filters = std::vector<filter::kernel::fir_filter_ccf*>(
d_nfilters + 1,
NULL);
m = taps.size();
n = diff_taps.size();
d_taps.resize(d_nfilters+1);
d_diff_taps.resize(d_nfilters+1);
signed int taps_per_filter = static_cast<signed int>(d_taps_per_filter);
for (i = 0; i <= d_nfilters; i++) {
d_taps[i] = std::vector<float>(d_taps_per_filter, 0.0f);
for (j = 0; j < taps_per_filter; j++) {
k = i+j*d_nfilters;
if (k < m)
d_taps[i][j] = taps[k];
}
d_filters[i] = new filter::kernel::fir_filter_ccf(1, d_taps[i]);
if (d_filters[i] == NULL)
throw std::runtime_error("unable to create fir_filter_ccf");
if (not d_derivative)
continue;
d_diff_taps[i] = std::vector<float>(d_taps_per_filter, 0.0f);
for (j = 0; j < taps_per_filter; j++) {
k = i+j*d_nfilters;
if (k < n)
d_diff_taps[i][j] = diff_taps[k];
}
d_diff_filters[i] = new filter::kernel::fir_filter_ccf(
1,
d_diff_taps[i]);
if (d_diff_filters[i] == NULL)
throw std::runtime_error("unable to create fir_filter_ccf");
}
}
interp_resampler_pfb_mf_ccf::~interp_resampler_pfb_mf_ccf()
{
for (int i = 0; i <= d_nfilters; i++) {
delete d_filters[i];
if (d_derivative)
delete d_diff_filters[i];
}
}
gr_complex
interp_resampler_pfb_mf_ccf::interpolate(const gr_complex input[],
float mu) const
{
int arm = static_cast<int>(rint(mu * d_nfilters));
if (arm < 0 or arm > d_nfilters)
throw std::runtime_error("interp_resampler_pfb_mf_ccf: mu is not "
"in the range [0.0, 1.0]");
return d_filters[arm]->filter(input);
}
gr_complex
interp_resampler_pfb_mf_ccf::differentiate(const gr_complex input[],
float mu) const
{
int arm = static_cast<int>(rint(mu * d_nfilters));
if (arm < 0 or arm > d_nfilters)
throw std::runtime_error("interp_resampler_pfb_mf_ccf: mu is not "
"in the range [0.0, 1.0]");
return d_diff_filters[arm]->filter(input);
}
unsigned int
interp_resampler_pfb_mf_ccf::ntaps() const
{
return d_taps_per_filter;
}
/*************************************************************************/
interp_resampler_pfb_mf_fff::interp_resampler_pfb_mf_fff(
const std::vector<float> &taps,
int nfilts,
bool derivative)
: interpolating_resampler_fff(IR_PFB_MF, derivative),
d_nfilters(nfilts),
d_taps_per_filter(static_cast<unsigned int>(
ceil( static_cast<double>(taps.size())
/ static_cast<double>(nfilts )))),
d_filters(),
d_diff_filters(),
d_taps(),
d_diff_taps()
{
if (d_nfilters <= 1)
throw std::invalid_argument("interpolating_resampler_pfb_mf_fff: "
"number of polyphase filter arms "
"must be greater than 1");
if (taps.size() < static_cast<unsigned int>(d_nfilters))
throw std::invalid_argument("interpolating_resampler_pfb_mf_fff: "
"length of the prototype filter taps"
" must be greater than or equal to "
"the number of polyphase filter arms.");
// Create a derivative filter from the provided taps
// First create a truncated ideal differentiator filter
int ideal_diff_filt_len = 3; // Must be odd; rest of init assumes odd.
std::vector<float> ideal_diff_taps(ideal_diff_filt_len, 0.0f);
int i, n;
n = ideal_diff_taps.size()/2;
for (i = -n; i < 0; i++) {
ideal_diff_taps[i+n] = (-i & 1) == 1 ? -1.0f/i : 1.0f/i;
ideal_diff_taps[n-i] = -ideal_diff_taps[i+n];
}
ideal_diff_taps[n] = 0.0f;
// Perform linear convolution of prototype filter taps and the truncated
// ideal differentiator taps to generate a derivative matched filter.
// N.B. the truncated ideal differentiator taps must have an odd length
int j, k, l, m;
m = ideal_diff_taps.size();
n = taps.size();
l = m + n - 1; // length of convolution
std::deque<float> diff_taps(l, 0.0f);
for (i = 0; i < l; i++) {
for (j = 0; j < m; j++) {
k = i + j - (m - 1);
if (k < 0 or k >= n)
continue;
diff_taps[i] += ideal_diff_taps[(m - 1) - j] * taps[k];
}
}
// Trim the convolution so the prototype derivative filter is the same
// length as the passed in prototype filter taps.
n = ideal_diff_taps.size()/2;
for (i = 0; i < n; i++) {
diff_taps.pop_back();
diff_taps.pop_front();
}
// Squash the differentiation noise spikes at the filter ends.
diff_taps[0] = 0.0f;
diff_taps[diff_taps.size()-1] = 0.0f;
// Normalize the prototype derviative filter gain to the number of
// filter arms
n = diff_taps.size();
float mag = 0.0f;
for (i = 0; i < n; i++)
mag += fabsf(diff_taps[i]);
for (i = 0; i < n; i++) {
diff_taps[i] *= d_nfilters/mag;
if (d_derivative and std::isnan(diff_taps[i]))
throw std::runtime_error("interpolating_resampler_pfb_mf_fff: "
"NaN error creating derivative filter."
);
}
// Create our polyphase filter arms for the steps from 0.0 to 1.0 from
// the prototype matched filter.
// N.B. We create an extra final row for an offset of 1.0, because it's
// easier than dealing with wrap around from 0.99... to 0.0 shifted
// by 1 tap.
d_filters = std::vector<filter::kernel::fir_filter_fff*>(d_nfilters + 1,
NULL);
d_diff_filters = std::vector<filter::kernel::fir_filter_fff*>(
d_nfilters + 1,
NULL);
m = taps.size();
n = diff_taps.size();
d_taps.resize(d_nfilters+1);
d_diff_taps.resize(d_nfilters+1);
signed int taps_per_filter = static_cast<signed int>(d_taps_per_filter);
for (i = 0; i <= d_nfilters; i++) {
d_taps[i] = std::vector<float>(d_taps_per_filter, 0.0f);
for (j = 0; j < taps_per_filter; j++) {
k = i+j*d_nfilters;
if (k < m)
d_taps[i][j] = taps[k];
}
d_filters[i] = new filter::kernel::fir_filter_fff(1, d_taps[i]);
if (d_filters[i] == NULL)
throw std::runtime_error("unable to create fir_filter_fff");
if (not d_derivative)
continue;
d_diff_taps[i] = std::vector<float>(d_taps_per_filter, 0.0f);
for (j = 0; j < taps_per_filter; j++) {
k = i+j*d_nfilters;
if (k < n)
d_diff_taps[i][j] = diff_taps[k];
}
d_diff_filters[i] = new filter::kernel::fir_filter_fff(
1,
d_diff_taps[i]);
if (d_diff_filters[i] == NULL)
throw std::runtime_error("unable to create fir_filter_fff");
}
}
interp_resampler_pfb_mf_fff::~interp_resampler_pfb_mf_fff()
{
for (int i = 0; i <= d_nfilters; i++) {
delete d_filters[i];
if (d_derivative)
delete d_diff_filters[i];
}
}
float
interp_resampler_pfb_mf_fff::interpolate(const float input[],
float mu) const
{
int arm = static_cast<int>(rint(mu * d_nfilters));
if (arm < 0 or arm > d_nfilters)
throw std::runtime_error("interp_resampler_pfb_mf_fff: mu is not "
"in the range [0.0, 1.0]");
return d_filters[arm]->filter(input);
}
float
interp_resampler_pfb_mf_fff::differentiate(const float input[],
float mu) const
{
int arm = static_cast<int>(rint(mu * d_nfilters));
if (arm < 0 or arm > d_nfilters)
throw std::runtime_error("interp_resampler_pfb_mf_fff: mu is not "
"in the range [0.0, 1.0]");
return d_diff_filters[arm]->filter(input);
}
unsigned int
interp_resampler_pfb_mf_fff::ntaps() const
{
return d_taps_per_filter;
}
} /* namespace digital */
} /* namespace gr */
|