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