USRP Hardware Driver and USRP Manual  Version: 3.15.0.0-4+b1satnogs1
UHD and USRP Manual
filters.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2015 Ettus Research LLC
3 // Copyright 2018 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 
8 #ifndef INCLUDED_UHD_TYPES_FILTERS_HPP
9 #define INCLUDED_UHD_TYPES_FILTERS_HPP
10 
11 #include <uhd/config.hpp>
12 #include <uhd/utils/log.hpp>
13 #include <stdint.h>
14 #include <boost/scoped_array.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <iostream>
17 #include <ostream>
18 #include <sstream>
19 #include <string>
20 #include <vector>
21 
22 namespace uhd {
23 
25 {
26 public:
27  typedef boost::shared_ptr<filter_info_base> sptr;
28  enum filter_type { ANALOG_LOW_PASS, ANALOG_BAND_PASS, DIGITAL_I16, DIGITAL_FIR_I16 };
29 
30  filter_info_base(filter_type type, bool bypass, size_t position_index)
31  : _type(type), _bypass(bypass), _position_index(position_index)
32  {
33  // NOP
34  }
35 
36  UHD_INLINE virtual bool is_bypassed()
37  {
38  return _bypass;
39  }
40 
42  {
43  return _type;
44  }
45 
47  {
48  // NOP
49  }
50 
51  virtual std::string to_pp_string();
52 
53 protected:
55  bool _bypass;
57 };
58 
59 UHD_API std::ostream& operator<<(std::ostream& os, filter_info_base& f);
60 
62 {
63  std::string _analog_type;
64 
65 public:
66  typedef boost::shared_ptr<analog_filter_base> sptr;
68  bool bypass,
69  size_t position_index,
70  const std::string& analog_type)
71  : filter_info_base(type, bypass, position_index), _analog_type(analog_type)
72  {
73  // NOP
74  }
75 
76  UHD_INLINE const std::string& get_analog_type()
77  {
78  return _analog_type;
79  }
80 
81  virtual std::string to_pp_string();
82 };
83 
85 {
86  double _cutoff;
87  double _rolloff;
88 
89 public:
90  typedef boost::shared_ptr<analog_filter_lp> sptr;
92  bool bypass,
93  size_t position_index,
94  const std::string& analog_type,
95  double cutoff,
96  double rolloff)
97  : analog_filter_base(type, bypass, position_index, analog_type)
98  , _cutoff(cutoff)
99  , _rolloff(rolloff)
100  {
101  // NOP
102  }
103 
105  {
106  return _cutoff;
107  }
108 
110  {
111  return _rolloff;
112  }
113 
114  UHD_INLINE void set_cutoff(const double cutoff)
115  {
116  _cutoff = cutoff;
117  }
118 
119  virtual std::string to_pp_string();
120 };
121 
122 template <typename tap_t>
124 {
125 protected:
126  double _rate;
127  uint32_t _interpolation;
128  uint32_t _decimation;
130  uint32_t _max_num_taps;
131  std::vector<tap_t> _taps;
132 
133 public:
134  typedef boost::shared_ptr<digital_filter_base> sptr;
136  bool bypass,
137  size_t position_index,
138  double rate,
139  size_t interpolation,
140  size_t decimation,
141  double tap_full_scale,
142  size_t max_num_taps,
143  const std::vector<tap_t>& taps)
144  : filter_info_base(type, bypass, position_index)
145  , _rate(rate)
146  , _interpolation(interpolation)
147  , _decimation(decimation)
148  , _tap_full_scale(tap_full_scale)
149  , _max_num_taps(max_num_taps)
150  , _taps(taps)
151  {
152  // NOP
153  }
154 
156  {
157  return (_bypass ? _rate : (_rate / _decimation * _interpolation));
158  }
159 
161  {
162  return _rate;
163  }
164 
166  {
167  return _interpolation;
168  }
169 
171  {
172  return _decimation;
173  }
174 
176  {
177  return _tap_full_scale;
178  }
179 
180  UHD_INLINE std::vector<tap_t>& get_taps()
181  {
182  return _taps;
183  }
184 
185  virtual std::string to_pp_string()
186  {
187  std::ostringstream os;
188  os << filter_info_base::to_pp_string() << "\t[digital_filter_base]" << std::endl
189  << "\tinput rate: " << _rate << std::endl
190  << "\tinterpolation: " << _interpolation << std::endl
191  << "\tdecimation: " << _decimation << std::endl
192  << "\tfull-scale: " << _tap_full_scale << std::endl
193  << "\tmax num taps: " << _max_num_taps << std::endl
194  << "\ttaps: " << std::endl;
195 
196  os << "\t\t";
197  for (size_t i = 0; i < _taps.size(); i++) {
198  os << "(tap " << i << ": " << _taps[i] << ")";
199  if (((i % 10) == 0) && (i != 0)) {
200  os << std::endl << "\t\t";
201  }
202  }
203  os << std::endl;
204  return std::string(os.str());
205  }
206 };
207 
208 template <typename tap_t>
210 {
211 public:
212  typedef boost::shared_ptr<digital_filter_fir<tap_t> > sptr;
213 
215  bool bypass,
216  size_t position_index,
217  double rate,
218  size_t interpolation,
219  size_t decimation,
220  size_t tap_bit_width,
221  size_t max_num_taps,
222  const std::vector<tap_t>& taps)
223  : digital_filter_base<tap_t>(type,
224  bypass,
225  position_index,
226  rate,
227  interpolation,
228  decimation,
229  tap_bit_width,
230  max_num_taps,
231  taps)
232  {
233  // NOP
234  }
235 
236  void set_taps(const std::vector<tap_t>& taps)
237  {
238  std::size_t num_taps = taps.size();
239  if (num_taps < this->_max_num_taps) {
240  UHD_LOGGER_WARNING("FILTERS") << "digital_filter_fir::set_taps not enough "
241  "coefficients. Appending zeros";
242  std::vector<tap_t> coeffs;
243  for (size_t i = 0; i < this->_max_num_taps; i++) {
244  if (i < num_taps) {
245  coeffs.push_back(taps[i]);
246  } else {
247  coeffs.push_back(0);
248  }
249  }
250  this->_taps = coeffs;
251  } else {
252  this->_taps = taps;
253  }
254  }
255 };
256 
257 } // namespace uhd
258 
259 #endif /* INCLUDED_UHD_TYPES_FILTERS_HPP */
Definition: filters.hpp:62
UHD_INLINE const std::string & get_analog_type()
Definition: filters.hpp:76
boost::shared_ptr< analog_filter_base > sptr
Definition: filters.hpp:66
virtual std::string to_pp_string()
analog_filter_base(filter_type type, bool bypass, size_t position_index, const std::string &analog_type)
Definition: filters.hpp:67
Definition: filters.hpp:85
analog_filter_lp(filter_type type, bool bypass, size_t position_index, const std::string &analog_type, double cutoff, double rolloff)
Definition: filters.hpp:91
UHD_INLINE double get_cutoff()
Definition: filters.hpp:104
boost::shared_ptr< analog_filter_lp > sptr
Definition: filters.hpp:90
virtual std::string to_pp_string()
UHD_INLINE void set_cutoff(const double cutoff)
Definition: filters.hpp:114
UHD_INLINE double get_rolloff()
Definition: filters.hpp:109
Definition: filters.hpp:124
uint32_t _decimation
Definition: filters.hpp:128
boost::shared_ptr< digital_filter_base > sptr
Definition: filters.hpp:134
tap_t _tap_full_scale
Definition: filters.hpp:129
UHD_INLINE double get_interpolation()
Definition: filters.hpp:165
uint32_t _interpolation
Definition: filters.hpp:127
UHD_INLINE double get_output_rate()
Definition: filters.hpp:155
UHD_INLINE double get_tap_full_scale()
Definition: filters.hpp:175
digital_filter_base(filter_type type, bool bypass, size_t position_index, double rate, size_t interpolation, size_t decimation, double tap_full_scale, size_t max_num_taps, const std::vector< tap_t > &taps)
Definition: filters.hpp:135
double _rate
Definition: filters.hpp:126
virtual std::string to_pp_string()
Definition: filters.hpp:185
UHD_INLINE double get_input_rate()
Definition: filters.hpp:160
UHD_INLINE double get_decimation()
Definition: filters.hpp:170
uint32_t _max_num_taps
Definition: filters.hpp:130
std::vector< tap_t > _taps
Definition: filters.hpp:131
UHD_INLINE std::vector< tap_t > & get_taps()
Definition: filters.hpp:180
Definition: filters.hpp:210
boost::shared_ptr< digital_filter_fir< tap_t > > sptr
Definition: filters.hpp:212
void set_taps(const std::vector< tap_t > &taps)
Definition: filters.hpp:236
digital_filter_fir(filter_info_base::filter_type type, bool bypass, size_t position_index, double rate, size_t interpolation, size_t decimation, size_t tap_bit_width, size_t max_num_taps, const std::vector< tap_t > &taps)
Definition: filters.hpp:214
Definition: filters.hpp:25
UHD_INLINE filter_type get_type()
Definition: filters.hpp:41
virtual std::string to_pp_string()
virtual ~filter_info_base()
Definition: filters.hpp:46
size_t _position_index
Definition: filters.hpp:56
filter_type _type
Definition: filters.hpp:54
filter_type
Definition: filters.hpp:28
@ ANALOG_BAND_PASS
Definition: filters.hpp:28
boost::shared_ptr< filter_info_base > sptr
Definition: filters.hpp:27
virtual UHD_INLINE bool is_bypassed()
Definition: filters.hpp:36
filter_info_base(filter_type type, bool bypass, size_t position_index)
Definition: filters.hpp:30
bool _bypass
Definition: filters.hpp:55
#define UHD_INLINE
Definition: config.h:53
#define UHD_API
Definition: config.h:68
#define UHD_LOGGER_WARNING(component)
Definition: log.hpp:239
Definition: build_info.hpp:13
UHD_API std::ostream & operator<<(std::ostream &os, filter_info_base &f)