| USRP Hardware Driver and USRP Manual Version: 4.9.0.makepkg-0-g9ec1f582 UHD and USRP Manual |

Loading...
Searching...
No Matches
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
21 class UHD_API filter_info_base
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
38 UHD_INLINE filter_type get_type()
39 {
40 return _type;
41 }
42
43 virtual ~filter_info_base()
44 {
45 // NOP
46 }
47
48 virtual std::string to_pp_string();
49
50 protected:
51 filter_type _type;
52 bool _bypass;
53 size_t _position_index;
54};
55
56 UHD_API_HEADER std::ostream& operator<<(std::ostream& os, filter_info_base& f);
57
58 class UHD_API analog_filter_base : public filter_info_base
59{
60 std::string _analog_type;
61
62 public:
63 typedef std::shared_ptr<analog_filter_base> sptr;
64 analog_filter_base(filter_type type,
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
81 class UHD_API analog_filter_lp : public analog_filter_base
82{
83 double _cutoff;
84 double _rolloff;
85
86 public:
87 typedef std::shared_ptr<analog_filter_lp> sptr;
88 analog_filter_lp(filter_type type,
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
101 UHD_INLINE double get_cutoff()
102 {
103 return _cutoff;
104 }
105
106 UHD_INLINE double get_rolloff()
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>
120 class UHD_API_HEADER digital_filter_base : public filter_info_base
121{
122 protected:
123 double _rate;
124 uint32_t _interpolation;
125 uint32_t _decimation;
126 tap_t _tap_full_scale;
127 uint32_t _max_num_taps;
128 std::vector<tap_t> _taps;
129
130 public:
131 typedef std::shared_ptr<digital_filter_base> sptr;
132 digital_filter_base(filter_type type,
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
152 UHD_INLINE double get_output_rate()
153 {
154 return (_bypass ? _rate : (_rate / _decimation * _interpolation));
155 }
156
157 UHD_INLINE double get_input_rate()
158 {
159 return _rate;
160 }
161
162 UHD_INLINE uint32_t get_interpolation()
163 {
164 return _interpolation;
165 }
166
167 UHD_INLINE uint32_t get_decimation()
168 {
169 return _decimation;
170 }
171
172 UHD_INLINE uint32_t get_tap_full_scale()
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>
206 class UHD_API_HEADER digital_filter_fir : public digital_filter_base<tap_t>
207{
208 public:
209 typedef std::shared_ptr<digital_filter_fir<tap_t>> sptr;
210
211 digital_filter_fir(filter_info_base::filter_type type,
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
uhd::analog_filter_base::get_analog_type
UHD_INLINE const std::string & get_analog_type()
Definition filters.hpp:73
uhd::analog_filter_base::to_pp_string
std::string to_pp_string() override
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:64
uhd::analog_filter_base::sptr
std::shared_ptr< analog_filter_base > sptr
Definition filters.hpp:63
uhd::analog_filter_lp::sptr
std::shared_ptr< analog_filter_lp > sptr
Definition filters.hpp:87
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:88
uhd::analog_filter_lp::to_pp_string
std::string to_pp_string() override
uhd::analog_filter_lp::get_cutoff
UHD_INLINE double get_cutoff()
Definition filters.hpp:101
uhd::analog_filter_lp::set_cutoff
UHD_INLINE void set_cutoff(const double cutoff)
Definition filters.hpp:111
uhd::analog_filter_lp::get_rolloff
UHD_INLINE double get_rolloff()
Definition filters.hpp:106
uhd::digital_filter_base::_decimation
uint32_t _decimation
Definition filters.hpp:125
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:132
uhd::digital_filter_base::get_interpolation
UHD_INLINE uint32_t get_interpolation()
Definition filters.hpp:162
uhd::digital_filter_base::to_pp_string
std::string to_pp_string() override
Definition filters.hpp:182
uhd::digital_filter_base::sptr
std::shared_ptr< digital_filter_base > sptr
Definition filters.hpp:131
uhd::digital_filter_base::_tap_full_scale
tap_t _tap_full_scale
Definition filters.hpp:126
uhd::digital_filter_base::get_taps
UHD_INLINE std::vector< tap_t > & get_taps()
Definition filters.hpp:177
uhd::digital_filter_base::_interpolation
uint32_t _interpolation
Definition filters.hpp:124
uhd::digital_filter_base::get_output_rate
UHD_INLINE double get_output_rate()
Definition filters.hpp:152
uhd::digital_filter_base::get_tap_full_scale
UHD_INLINE uint32_t get_tap_full_scale()
Definition filters.hpp:172
uhd::digital_filter_base::_rate
double _rate
Definition filters.hpp:123
uhd::digital_filter_base::get_input_rate
UHD_INLINE double get_input_rate()
Definition filters.hpp:157
uhd::digital_filter_base::_max_num_taps
uint32_t _max_num_taps
Definition filters.hpp:127
uhd::digital_filter_base::get_decimation
UHD_INLINE uint32_t get_decimation()
Definition filters.hpp:167
uhd::digital_filter_base::_taps
std::vector< tap_t > _taps
Definition filters.hpp:128
uhd::digital_filter_fir::sptr
std::shared_ptr< digital_filter_fir< tap_t > > sptr
Definition filters.hpp:209
uhd::digital_filter_fir::set_taps
void set_taps(const std::vector< tap_t > &taps)
Definition filters.hpp:233
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:211
uhd::filter_info_base
Definition filters.hpp:22
uhd::filter_info_base::get_type
UHD_INLINE filter_type get_type()
Definition filters.hpp:38
uhd::filter_info_base::to_pp_string
virtual std::string to_pp_string()
uhd::filter_info_base::~filter_info_base
virtual ~filter_info_base()
Definition filters.hpp:43
uhd::filter_info_base::_position_index
size_t _position_index
Definition filters.hpp:53
uhd::filter_info_base::_type
filter_type _type
Definition filters.hpp:51
uhd::filter_info_base::filter_type
filter_type
Definition filters.hpp:25
uhd::filter_info_base::DIGITAL_I16
@ DIGITAL_I16
Definition filters.hpp:25
uhd::filter_info_base::ANALOG_BAND_PASS
@ ANALOG_BAND_PASS
Definition filters.hpp:25
uhd::filter_info_base::DIGITAL_FIR_I16
@ DIGITAL_FIR_I16
Definition filters.hpp:25
uhd::filter_info_base::ANALOG_LOW_PASS
@ ANALOG_LOW_PASS
Definition filters.hpp:25
uhd::filter_info_base::is_bypassed
virtual UHD_INLINE bool is_bypassed()
Definition filters.hpp:33
uhd::filter_info_base::sptr
std::shared_ptr< filter_info_base > sptr
Definition filters.hpp:24
uhd::filter_info_base::filter_info_base
filter_info_base(filter_type type, bool bypass, size_t position_index)
Definition filters.hpp:27
uhd::filter_info_base::_bypass
bool _bypass
Definition filters.hpp:52
UHD_INLINE
#define UHD_INLINE
Definition config.h:65
UHD_API_HEADER
#define UHD_API_HEADER
Definition config.h:88
UHD_API
#define UHD_API
Definition config.h:87
config.hpp
log.hpp
UHD_LOGGER_WARNING
#define UHD_LOGGER_WARNING(component)
Definition log.hpp:272
uhd
Definition build_info.hpp:12
uhd::operator<<
UHD_API_HEADER std::ostream & operator<<(std::ostream &os, filter_info_base &f)