transfer_function.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 // Original version: Melonee Wise <mwise@willowgarage.com>
31 
32 #ifndef FILTERS_TRANSFER_FUNCTION_HPP_
33 #define FILTERS_TRANSFER_FUNCTION_HPP_
34 
35 #include <stdint.h>
36 #include <math.h>
37 #include <memory>
38 #include <vector>
39 #include <string>
40 
41 #include "filters/filter_base.hpp"
43 
44 namespace filters
45 {
46 
47 /***************************************************/
71 /***************************************************/
72 template <typename T>
74 {
75 public:
80 
84 
89  virtual bool configure();
90 
95  virtual bool update(const T & data_in, T& data_out) ;
96 
97 
98 
99 protected:
100 
101  std::unique_ptr<RealtimeCircularBuffer<T > > input_buffer_; //The input sample history.
102  std::unique_ptr<RealtimeCircularBuffer<T > > output_buffer_; //The output sample history.
103 
104  T temp_; //used for storage and preallocation
105 
106  std::vector<double> a_; //Transfer functon coefficients (output).
107  std::vector<double> b_; //Transfer functon coefficients (input).
108 
109 };
110 
111 template <typename T>
113 {
114 }
115 
116 template <typename T>
118 {
119 }
120 
121 template <typename T>
123 {
124 
125  // Parse a and b into a std::vector<double>.
126  if (!FilterBase<T>::getParam("a", a_))
127  {
128  ROS_ERROR("TransferFunctionFilter, \"%s\", params has no attribute a.", FilterBase<T>::getName().c_str());
129  return false;
130  }
131 
132 
133  if (!FilterBase<T>::getParam("b", b_))
134  {
135  ROS_ERROR("TransferFunctionFilter, \"%s\", params has no attribute b.", FilterBase<T>::getName().c_str());
136  return false;
137  }
138 
139  // Create the input and output buffers of the correct size.
140  input_buffer_.reset(new RealtimeCircularBuffer<T >(b_.size()-1, temp_));
141  output_buffer_.reset(new RealtimeCircularBuffer<T >(a_.size()-1, temp_));
142 
143  // Prevent divide by zero while normalizing coeffs.
144  if ( a_[0] == 0)
145  {
146  ROS_ERROR("a[0] can not equal 0.");
147  return false;
148  }
149 
150  // Normalize the coeffs by a[0].
151  if(a_[0] != 1)
152  {
153  for(uint32_t i = 0; i < b_.size(); i++)
154  {
155  b_[i] = (b_[i] / a_[0]);
156  }
157  for(uint32_t i = 1; i < a_.size(); i++)
158  {
159  a_[i] = (a_[i] / a_[0]);
160  }
161  a_[0] = (a_[0] / a_[0]);
162  }
163 
164  return true;
165 }
166 
167 
168 template <typename T>
169 bool SingleChannelTransferFunctionFilter<T>::update(const T & data_in, T & data_out)
170 {
172  return false;
173 
174  // Copy data to prevent mutation if in and out are the same ptr
175  temp_ = data_in;
176 
177  data_out=b_[0] * temp_;
178 
179  for (uint32_t row = 1; row <= input_buffer_->size(); row++)
180  {
181  data_out += b_[row] * (*input_buffer_)[row-1];
182  }
183  for (uint32_t row = 1; row <= output_buffer_->size(); row++)
184  {
185  data_out -= a_[row] * (*output_buffer_)[row-1];
186  }
187 
188  input_buffer_->push_front(temp_);
189  output_buffer_->push_front(data_out);
190 
191  return true;
192 }
193 
194 
195 
196 /***************************************************/
221 /***************************************************/
222 
223 template <typename T>
225 {
226 public:
231 
235 
240  virtual bool configure();
241 
246  virtual bool update(const std::vector<T> & data_in, std::vector<T>& data_out) ;
247 
248 
249 
250 protected:
251 
252  std::unique_ptr<RealtimeCircularBuffer<std::vector<T> > > input_buffer_; //The input sample history.
253  std::unique_ptr<RealtimeCircularBuffer<std::vector<T> > > output_buffer_; //The output sample history.
254 
255  std::vector<T> temp_; //used for storage and preallocation
256 
257  std::vector<double> a_; //Transfer functon coefficients (output).
258  std::vector<double> b_; //Transfer functon coefficients (input).
259 
260 };
261 
262 template <typename T>
264 {
265 }
266 
267 template <typename T>
269 {
270 }
271 
272 template <typename T>
274 {
275  // Parse a and b into a std::vector<double>.
276  if (!FilterBase<T>::getParam("a", a_))
277  {
278  ROS_ERROR("TransferFunctionFilter, \"%s\", params has no attribute a.", FilterBase<T>::getName().c_str());
279  return false;
280  }
281 
282 
283  if (!FilterBase<T>::getParam("b", b_))
284  {
285  ROS_ERROR("TransferFunctionFilter, \"%s\", params has no attribute b.", FilterBase<T>::getName().c_str());
286  return false;
287  }
288 
289  // Create the input and output buffers of the correct size.
290  temp_.resize(this->number_of_channels_);
291  input_buffer_.reset(new RealtimeCircularBuffer<std::vector<T> >(b_.size()-1, temp_));
292  output_buffer_.reset(new RealtimeCircularBuffer<std::vector<T> >(a_.size()-1, temp_));
293 
294  // Prevent divide by zero while normalizing coeffs.
295  if ( a_[0] == 0)
296  {
297  ROS_ERROR("a[0] can not equal 0.");
298  return false;
299  }
300 
301  // Normalize the coeffs by a[0].
302  if(a_[0] != 1)
303  {
304  for(uint32_t i = 0; i < b_.size(); i++)
305  {
306  b_[i] = (b_[i] / a_[0]);
307  }
308  for(uint32_t i = 1; i < a_.size(); i++)
309  {
310  a_[i] = (a_[i] / a_[0]);
311  }
312  a_[0] = (a_[0] / a_[0]);
313  }
314 
315  return true;
316 }
317 
318 
319 template <typename T>
320 bool MultiChannelTransferFunctionFilter<T>::update(const std::vector<T> & data_in, std::vector<T> & data_out)
321 {
322 
323  // Ensure the correct number of inputs
324  if (data_in.size() != this->number_of_channels_ || data_out.size() != this->number_of_channels_ )
325  {
326  ROS_ERROR("Number of channels is %d, but data_in.size() = %d and data_out.size() = %d. They must match", this->number_of_channels_, (int)data_in.size(), (int)data_out.size());
327  return false;
328  }
329  // Copy data to prevent mutation if in and out are the same ptr
330  temp_ = data_in;
331 
332  for (uint32_t i = 0; i < temp_.size(); i++)
333  {
334  data_out[i]=b_[0] * temp_[i];
335 
336  for (uint32_t row = 1; row <= input_buffer_->size(); row++)
337  {
338  (data_out)[i] += b_[row] * (*input_buffer_)[row-1][i];
339  }
340  for (uint32_t row = 1; row <= output_buffer_->size(); row++)
341  {
342  (data_out)[i] -= a_[row] * (*output_buffer_)[row-1][i];
343  }
344  }
345  input_buffer_->push_front(temp_);
346  output_buffer_->push_front(data_out);
347  return true;
348 }
349 
350 }
351 
352 #endif //#ifndef FILTERS_TRANSFER_FUNCTION_HPP_
filters::FilterBase
A Base filter class to provide a standard interface for all filters.
Definition: filter_base.hpp:47
filters::SingleChannelTransferFunctionFilter::SingleChannelTransferFunctionFilter
SingleChannelTransferFunctionFilter()
Construct the filter.
Definition: transfer_function.hpp:112
filter_base.hpp
filters
Definition: filter_base.hpp:38
filters::SingleChannelTransferFunctionFilter::a_
std::vector< double > a_
Definition: transfer_function.hpp:106
filters::RealtimeCircularBuffer
A realtime safe circular (ring) buffer.
Definition: realtime_circular_buffer.hpp:47
filters::MultiChannelTransferFunctionFilter::temp_
std::vector< T > temp_
Definition: transfer_function.hpp:255
filters::SingleChannelTransferFunctionFilter::configure
virtual bool configure()
Configure the filter with the correct number of channels and params.
Definition: transfer_function.hpp:122
filters::MultiChannelTransferFunctionFilter::update
virtual bool update(const std::vector< T > &data_in, std::vector< T > &data_out)
Update the filter and return the data seperately.
Definition: transfer_function.hpp:320
filters::SingleChannelTransferFunctionFilter
One-dimensional digital filter class.
Definition: transfer_function.hpp:73
filters::MultiChannelTransferFunctionFilter
One-dimensional digital filter class.
Definition: transfer_function.hpp:224
filters::MultiChannelTransferFunctionFilter::a_
std::vector< double > a_
Definition: transfer_function.hpp:257
filters::SingleChannelTransferFunctionFilter::temp_
T temp_
Definition: transfer_function.hpp:104
filters::SingleChannelTransferFunctionFilter::input_buffer_
std::unique_ptr< RealtimeCircularBuffer< T > > input_buffer_
Definition: transfer_function.hpp:101
filters::MultiChannelTransferFunctionFilter::configure
virtual bool configure()
Configure the filter with the correct number of channels and params.
Definition: transfer_function.hpp:273
filters::MultiChannelTransferFunctionFilter::output_buffer_
std::unique_ptr< RealtimeCircularBuffer< std::vector< T > > > output_buffer_
Definition: transfer_function.hpp:253
filters::MultiChannelFilterBase
Definition: filter_base.hpp:380
filters::SingleChannelTransferFunctionFilter::b_
std::vector< double > b_
Definition: transfer_function.hpp:107
filters::MultiChannelTransferFunctionFilter::~MultiChannelTransferFunctionFilter
~MultiChannelTransferFunctionFilter()
Destructor to clean up.
Definition: transfer_function.hpp:268
filters::SingleChannelTransferFunctionFilter::output_buffer_
std::unique_ptr< RealtimeCircularBuffer< T > > output_buffer_
Definition: transfer_function.hpp:102
filters::SingleChannelTransferFunctionFilter::update
virtual bool update(const T &data_in, T &data_out)
Update the filter and return the data seperately.
Definition: transfer_function.hpp:169
filters::MultiChannelTransferFunctionFilter::input_buffer_
std::unique_ptr< RealtimeCircularBuffer< std::vector< T > > > input_buffer_
Definition: transfer_function.hpp:252
filters::MultiChannelTransferFunctionFilter::MultiChannelTransferFunctionFilter
MultiChannelTransferFunctionFilter()
Construct the filter.
Definition: transfer_function.hpp:263
ROS_ERROR
#define ROS_ERROR(...)
filters::MultiChannelTransferFunctionFilter::b_
std::vector< double > b_
Definition: transfer_function.hpp:258
realtime_circular_buffer.hpp
filters::SingleChannelTransferFunctionFilter::~SingleChannelTransferFunctionFilter
~SingleChannelTransferFunctionFilter()
Destructor to clean up.
Definition: transfer_function.hpp:117


filters
Author(s):
autogenerated on Fri Nov 11 2022 03:09:05