USRP Hardware Driver and USRP Manual  Version: 4.4.0.HEAD-0-g5fac246b
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
uhd::filter_info_base::filter_type
filter_type
Definition: filters.hpp:27
uhd::digital_filter_base::digital_filter_base
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
uhd::filter_info_base::get_type
UHD_INLINE filter_type get_type()
Definition: filters.hpp:40
uhd::digital_filter_base::get_input_rate
UHD_INLINE double get_input_rate()
Definition: filters.hpp:159
uhd::filter_info_base::_bypass
bool _bypass
Definition: filters.hpp:54
uhd::analog_filter_lp::set_cutoff
UHD_INLINE void set_cutoff(const double cutoff)
Definition: filters.hpp:113
uhd::digital_filter_fir
Definition: filters.hpp:208
config.hpp
uhd::digital_filter_base
Definition: filters.hpp:122
uhd::analog_filter_base::sptr
std::shared_ptr< analog_filter_base > sptr
Definition: filters.hpp:65
uhd::filter_info_base::filter_info_base
filter_info_base(filter_type type, bool bypass, size_t position_index)
Definition: filters.hpp:29
uhd::digital_filter_base::_decimation
uint32_t _decimation
Definition: filters.hpp:127
uhd::analog_filter_base::analog_filter_base
analog_filter_base(filter_type type, bool bypass, size_t position_index, const std::string &analog_type)
Definition: filters.hpp:66
uhd::filter_info_base::~filter_info_base
virtual ~filter_info_base()
Definition: filters.hpp:45
UHD_INLINE
#define UHD_INLINE
Definition: config.h:65
uhd::digital_filter_base::to_pp_string
std::string to_pp_string() override
Definition: filters.hpp:184
uhd::analog_filter_base
Definition: filters.hpp:60
UHD_API
#define UHD_API
Definition: config.h:87
uhd::filter_info_base::is_bypassed
virtual UHD_INLINE bool is_bypassed()
Definition: filters.hpp:35
uhd::filter_info_base::sptr
std::shared_ptr< filter_info_base > sptr
Definition: filters.hpp:26
uhd::digital_filter_base::get_interpolation
UHD_INLINE uint32_t get_interpolation()
Definition: filters.hpp:164
uhd
Definition: build_info.hpp:12
uhd::digital_filter_base::get_decimation
UHD_INLINE uint32_t get_decimation()
Definition: filters.hpp:169
uhd::digital_filter_base::_tap_full_scale
tap_t _tap_full_scale
Definition: filters.hpp:128
uhd::analog_filter_lp::get_rolloff
UHD_INLINE double get_rolloff()
Definition: filters.hpp:108
uhd::digital_filter_base::_rate
double _rate
Definition: filters.hpp:125
uhd::digital_filter_fir::digital_filter_fir
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
uhd::analog_filter_lp
Definition: filters.hpp:83
uhd::digital_filter_base::_interpolation
uint32_t _interpolation
Definition: filters.hpp:126
uhd::analog_filter_lp::get_cutoff
UHD_INLINE double get_cutoff()
Definition: filters.hpp:103
uhd::digital_filter_base::_taps
std::vector< tap_t > _taps
Definition: filters.hpp:130
UHD_LOGGER_WARNING
#define UHD_LOGGER_WARNING(component)
Definition: log.hpp:272
uhd::digital_filter_base::get_tap_full_scale
UHD_INLINE uint32_t get_tap_full_scale()
Definition: filters.hpp:174
uhd::digital_filter_base::sptr
std::shared_ptr< digital_filter_base > sptr
Definition: filters.hpp:133
uhd::filter_info_base::_position_index
size_t _position_index
Definition: filters.hpp:55
uhd::filter_info_base::to_pp_string
virtual std::string to_pp_string()
uhd::digital_filter_fir::set_taps
void set_taps(const std::vector< tap_t > &taps)
Definition: filters.hpp:235
log.hpp
uhd::analog_filter_base::get_analog_type
const UHD_INLINE std::string & get_analog_type()
Definition: filters.hpp:75
uhd::digital_filter_base::get_output_rate
UHD_INLINE double get_output_rate()
Definition: filters.hpp:154
uhd::digital_filter_base::_max_num_taps
uint32_t _max_num_taps
Definition: filters.hpp:129
uhd::digital_filter_fir::sptr
std::shared_ptr< digital_filter_fir< tap_t > > sptr
Definition: filters.hpp:211
uhd::operator<<
UHD_API_HEADER std::ostream & operator<<(std::ostream &os, filter_info_base &f)
uhd::analog_filter_lp::sptr
std::shared_ptr< analog_filter_lp > sptr
Definition: filters.hpp:89
uhd::filter_info_base::_type
filter_type _type
Definition: filters.hpp:53
UHD_API_HEADER
#define UHD_API_HEADER
Definition: config.h:88
uhd::analog_filter_lp::analog_filter_lp
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
uhd::filter_info_base
Definition: filters.hpp:23
uhd::digital_filter_base::get_taps
UHD_INLINE std::vector< tap_t > & get_taps()
Definition: filters.hpp:179