00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef FILTERS_MEAN_H
00031 #define FILTERS_MEAN_H
00032
00033 #include <stdint.h>
00034 #include <cstring>
00035 #include <stdio.h>
00036
00037 #include <boost/scoped_ptr.hpp>
00038
00039 #include "filters/filter_base.h"
00040 #include "ros/assert.h"
00041
00042 #include "filters/realtime_circular_buffer.h"
00043
00044 namespace filters
00045 {
00046
00050 template <typename T>
00051 class MeanFilter: public FilterBase <T>
00052 {
00053 public:
00055 MeanFilter();
00056
00059 ~MeanFilter();
00060
00061 virtual bool configure();
00062
00067 virtual bool update( const T & data_in, T& data_out);
00068
00069 protected:
00070 boost::scoped_ptr<RealtimeCircularBuffer<T > > data_storage_;
00071 uint32_t last_updated_row_;
00072 T temp_;
00073 uint32_t number_of_observations_;
00074
00075 };
00076
00077
00078 template <typename T>
00079 MeanFilter<T>::MeanFilter():
00080 number_of_observations_(0)
00081 {
00082 }
00083
00084 template <typename T>
00085 bool MeanFilter<T>::configure()
00086 {
00087
00088 if (!FilterBase<T>::getParam(std::string("number_of_observations"), number_of_observations_))
00089 {
00090 ROS_ERROR("MeanFilter did not find param number_of_observations");
00091 return false;
00092 }
00093
00094 data_storage_.reset(new RealtimeCircularBuffer<T >(number_of_observations_, temp_));
00095
00096 return true;
00097 }
00098
00099 template <typename T>
00100 MeanFilter<T>::~MeanFilter()
00101 {
00102 }
00103
00104
00105 template <typename T>
00106 bool MeanFilter<T>::update(const T & data_in, T& data_out)
00107 {
00108
00109 if (last_updated_row_ >= number_of_observations_ - 1)
00110 last_updated_row_ = 0;
00111 else
00112 last_updated_row_++;
00113
00114 data_storage_->push_back(data_in);
00115
00116
00117 unsigned int length = data_storage_->size();
00118
00119 data_out = 0;
00120 for (uint32_t row = 0; row < length; row ++)
00121 {
00122 data_out += data_storage_->at(row);
00123 }
00124 data_out /= length;
00125
00126
00127 return true;
00128 };
00129
00133 template <typename T>
00134 class MultiChannelMeanFilter: public MultiChannelFilterBase <T>
00135 {
00136 public:
00138 MultiChannelMeanFilter();
00139
00142 ~MultiChannelMeanFilter();
00143
00144 virtual bool configure();
00145
00150 virtual bool update( const std::vector<T> & data_in, std::vector<T>& data_out);
00151
00152 protected:
00153 boost::scoped_ptr<RealtimeCircularBuffer<std::vector<T> > > data_storage_;
00154 uint32_t last_updated_row_;
00155
00156 std::vector<T> temp;
00157
00158 uint32_t number_of_observations_;
00159 using MultiChannelFilterBase<T>::number_of_channels_;
00160
00161
00162
00163 };
00164
00165
00166 template <typename T>
00167 MultiChannelMeanFilter<T>::MultiChannelMeanFilter():
00168 number_of_observations_(0)
00169 {
00170 }
00171
00172 template <typename T>
00173 bool MultiChannelMeanFilter<T>::configure()
00174 {
00175
00176 if (!FilterBase<T>::getParam("number_of_observations", number_of_observations_))
00177 {
00178 ROS_ERROR("MultiChannelMeanFilter did not find param number_of_observations");
00179 return false;
00180 }
00181
00182 temp.resize(number_of_channels_);
00183 data_storage_.reset(new RealtimeCircularBuffer<std::vector<T> >(number_of_observations_, temp));
00184
00185 return true;
00186 }
00187
00188 template <typename T>
00189 MultiChannelMeanFilter<T>::~MultiChannelMeanFilter()
00190 {
00191 }
00192
00193
00194 template <typename T>
00195 bool MultiChannelMeanFilter<T>::update(const std::vector<T> & data_in, std::vector<T>& data_out)
00196 {
00197
00198
00199 if (data_in.size() != number_of_channels_ || data_out.size() != number_of_channels_)
00200 {
00201 ROS_ERROR("Configured with wrong size config:%d in:%d out:%d", number_of_channels_, (int)data_in.size(), (int)data_out.size());
00202 return false;
00203 }
00204
00205
00206 if (last_updated_row_ >= number_of_observations_ - 1)
00207 last_updated_row_ = 0;
00208 else
00209 last_updated_row_++;
00210
00211 data_storage_->push_back(data_in);
00212
00213
00214 unsigned int length = data_storage_->size();
00215
00216
00217 for (uint32_t i = 0; i < number_of_channels_; i++)
00218 {
00219 data_out[i] = 0;
00220 for (uint32_t row = 0; row < length; row ++)
00221 {
00222 data_out[i] += data_storage_->at(row)[i];
00223 }
00224 data_out[i] /= length;
00225 }
00226
00227 return true;
00228 };
00229
00230 }
00231 #endif// FILTERS_MEAN_H