#include <deque>
namespace tutorials {
{
public:
MeanCalculator();
template<typename Reflector>
{
r.property("WindowSize", mWindowSize,
setter<int>(&MeanCalculator::setWindowSize, this),
"The window size for computing the average", 10);
r.roproperty("QueueSize",
getter<int>(&MeanCalculator::getQueueSize, this),
"The current size of the window for computing the average");
}
protected:
virtual void initialize();
private:
void setWindowSize(int newSize);
int getQueueSize() const;
private:
std::deque<float> mQueue;
int mWindowSize;
};
MeanCalculator::MeanCalculator()
{
}
void MeanCalculator::initialize()
{
subscribe<float>("FloatChannel", &MeanCalculator::onNewData);
mMeanChannel = publish<float>("MeanChannel");
}
{
mQueue.push_back(data->value());
if (mQueue.size() > mWindowSize)
mQueue.pop_front();
float sum = 0.0;
foreach(float f, mQueue)
sum += f;
float mean = sum / mQueue.size();
std::cout << "MeanCalculator: " << mean << std::endl;
w->value() = mean;
}
void MeanCalculator::setWindowSize(int newSize)
{
mWindowSize = newSize;
while (mQueue.size() > mWindowSize)
mQueue.pop_front();
}
int MeanCalculator::getQueueSize() const
{
return mQueue.size();
}
}