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