forked from gnuradio/gnuradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiir_filter.cc
149 lines (124 loc) · 3.82 KB
/
iir_filter.cc
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
/* -*- c++ -*- */
/*
* Copyright 2002,2012,2014 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 <gnuradio/filter/iir_filter.h>
namespace gr {
namespace filter {
namespace kernel {
template<>
gr_complex
iir_filter<gr_complex, gr_complex, float, gr_complex>::filter(const gr_complex input)
{
gr_complex acc;
unsigned i = 0;
unsigned n = ntaps_ff();
unsigned m = ntaps_fb();
if(n == 0)
return (gr_complex)0;
int latest_n = d_latest_n;
int latest_m = d_latest_m;
acc = d_fftaps[0] * input;
for(i = 1; i < n; i ++)
acc += (d_fftaps[i] * d_prev_input[latest_n + i]);
for(i = 1; i < m; i ++)
acc += (d_fbtaps[i] * d_prev_output[latest_m + i]);
// store the values twice to avoid having to handle wrap-around in the loop
d_prev_output[latest_m] = acc;
d_prev_output[latest_m+m] = acc;
d_prev_input[latest_n] = input;
d_prev_input[latest_n+n] = input;
latest_n--;
latest_m--;
if(latest_n < 0)
latest_n += n;
if(latest_m < 0)
latest_m += m;
d_latest_m = latest_m;
d_latest_n = latest_n;
return static_cast<gr_complex>(acc);
}
template<>
gr_complex
iir_filter<gr_complex, gr_complex, double, gr_complexd>::filter(const gr_complex input)
{
gr_complexd acc;
unsigned i = 0;
unsigned n = ntaps_ff();
unsigned m = ntaps_fb();
if(n == 0)
return (gr_complex)0;
int latest_n = d_latest_n;
int latest_m = d_latest_m;
acc = d_fftaps[0] * static_cast<gr_complexd>(input);
for(i = 1; i < n; i ++)
acc += (d_fftaps[i] * static_cast<gr_complexd>(d_prev_input[latest_n + i]));
for(i = 1; i < m; i ++)
acc += (d_fbtaps[i] * static_cast<gr_complexd>(d_prev_output[latest_m + i]));
// store the values twice to avoid having to handle wrap-around in the loop
d_prev_output[latest_m] = acc;
d_prev_output[latest_m+m] = acc;
d_prev_input[latest_n] = input;
d_prev_input[latest_n+n] = input;
latest_n--;
latest_m--;
if(latest_n < 0)
latest_n += n;
if(latest_m < 0)
latest_m += m;
d_latest_m = latest_m;
d_latest_n = latest_n;
return static_cast<gr_complex>(acc);
}
template<>
gr_complex
iir_filter<gr_complex, gr_complex, gr_complexd, gr_complexd>::filter(const gr_complex input)
{
gr_complexd acc;
unsigned i = 0;
unsigned n = ntaps_ff();
unsigned m = ntaps_fb();
if(n == 0)
return (gr_complex)0;
int latest_n = d_latest_n;
int latest_m = d_latest_m;
acc = d_fftaps[0] * static_cast<gr_complexd>(input);
for(i = 1; i < n; i ++)
acc += (d_fftaps[i] * static_cast<gr_complexd>(d_prev_input[latest_n + i]));
for(i = 1; i < m; i ++)
acc += (d_fbtaps[i] * static_cast<gr_complexd>(d_prev_output[latest_m + i]));
// store the values twice to avoid having to handle wrap-around in the loop
d_prev_output[latest_m] = acc;
d_prev_output[latest_m+m] = acc;
d_prev_input[latest_n] = input;
d_prev_input[latest_n+n] = input;
latest_n--;
latest_m--;
if(latest_n < 0)
latest_n += n;
if(latest_m < 0)
latest_m += m;
d_latest_m = latest_m;
d_latest_n = latest_n;
return static_cast<gr_complex>(acc);
}
} /* namespace kernel */
} /* namespace filter */
} /* namespace gr */