[14] | 1 | // created: 2013/06/27
|
---|
| 2 | // filename: MeanFilter.cpp
|
---|
| 3 | //
|
---|
| 4 | // author: Guillaume Sanahuja
|
---|
| 5 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 6 | //
|
---|
| 7 | // version: $Id: $
|
---|
| 8 | //
|
---|
| 9 | // purpose: object computing a mean filter
|
---|
| 10 | //
|
---|
| 11 | //
|
---|
| 12 | /*********************************************************************/
|
---|
| 13 |
|
---|
| 14 | #include "MeanFilter.h"
|
---|
[214] | 15 | #include <Matrix.h>
|
---|
[14] | 16 | #include <LayoutPosition.h>
|
---|
| 17 | #include <GroupBox.h>
|
---|
| 18 | #include <SpinBox.h>
|
---|
| 19 |
|
---|
| 20 | using namespace std;
|
---|
| 21 | using namespace flair::core;
|
---|
| 22 | using namespace flair::gui;
|
---|
| 23 |
|
---|
[16] | 24 | namespace flair {
|
---|
| 25 | namespace filter {
|
---|
[14] | 26 |
|
---|
[16] | 27 | MeanFilter::MeanFilter(const IODevice *parent, const LayoutPosition *position,
|
---|
| 28 | string name)
|
---|
| 29 | : IODevice(parent, name) {
|
---|
| 30 | // interface initialisation
|
---|
| 31 | groupBox = new GroupBox(position, name);
|
---|
| 32 | numberOfElements = new SpinBox(groupBox->NewRow(), "numberOfElements:", 1,
|
---|
| 33 | MAX_NUMBER_OF_ELEMENTS,
|
---|
| 34 | 1); // saturated to MAX_NUMBER_OF_ELEMENTS
|
---|
[14] | 35 |
|
---|
[16] | 36 | // init storage
|
---|
| 37 | for (int i = 0; i < MAX_NUMBER_OF_ELEMENTS; i++)
|
---|
| 38 | previousValues[i] = 0;
|
---|
[14] | 39 |
|
---|
[16] | 40 | // 1*1 output matrix
|
---|
[318] | 41 | MatrixDescriptor *desc = new MatrixDescriptor(1, 1);
|
---|
[16] | 42 | desc->SetElementName(0, 0,
|
---|
| 43 | "mean filter"); // name will be used for graphs and logs
|
---|
[214] | 44 | output = new Matrix(this, desc, floatType, name);
|
---|
[148] | 45 | delete desc;
|
---|
[14] | 46 |
|
---|
[16] | 47 | AddDataToLog(output);
|
---|
[157] | 48 |
|
---|
| 49 | SetIsReady(true);
|
---|
[14] | 50 | }
|
---|
| 51 |
|
---|
[16] | 52 | MeanFilter::~MeanFilter() {}
|
---|
[14] | 53 |
|
---|
[214] | 54 | Matrix *MeanFilter::GetMatrix() const { return output; }
|
---|
[14] | 55 |
|
---|
[16] | 56 | float MeanFilter::GetValue(void) const { return output->Value(0, 0); }
|
---|
[14] | 57 |
|
---|
[16] | 58 | // UpdateFrom, where we implement the filter
|
---|
| 59 | // this method is automatically called when the parent IODevice calls
|
---|
| 60 | // ProcessUpdate
|
---|
| 61 | // in our case it is the sinus or the 1st orde law pass filter
|
---|
[14] | 62 | //(see in Sinus::Run the call to ProcessUpdate)
|
---|
| 63 | void MeanFilter::UpdateFrom(const io_data *data) {
|
---|
| 64 |
|
---|
[16] | 65 | float result = 0;
|
---|
[214] | 66 | // get input argument in a matrix
|
---|
| 67 | const Matrix* input = dynamic_cast<const Matrix*>(data);
|
---|
| 68 |
|
---|
| 69 | if (!input) {
|
---|
| 70 | Warn("casting %s to Matrix failed\n",data->ObjectName().c_str());
|
---|
| 71 | return;
|
---|
| 72 | }
|
---|
[14] | 73 |
|
---|
[16] | 74 | // simple (and not efficent!) implementation of the filter
|
---|
| 75 | previousValues[numberOfElements->Value() - 1] = input->Value(0, 0);
|
---|
| 76 | for (int i = 0; i < numberOfElements->Value(); i++)
|
---|
| 77 | result += previousValues[i];
|
---|
| 78 | for (int i = 1; i < numberOfElements->Value(); i++)
|
---|
| 79 | previousValues[i - 1] = previousValues[i];
|
---|
[14] | 80 |
|
---|
[16] | 81 | // put the result in output matrix
|
---|
| 82 | output->SetValue(0, 0, result / numberOfElements->Value());
|
---|
| 83 | // put corresponding time
|
---|
| 84 | output->SetDataTime(data->DataTime());
|
---|
[14] | 85 |
|
---|
[16] | 86 | // ProcessUpdate is very important
|
---|
| 87 | // we must call it after updating the output matrix
|
---|
| 88 | // it allows:
|
---|
| 89 | // -to save value in the logs
|
---|
| 90 | // -to automatically call the next filter UpdateFrom method
|
---|
| 91 | ProcessUpdate(output);
|
---|
[14] | 92 | }
|
---|
[16] | 93 | } // end namespace filter
|
---|
| 94 | } // end namespace flair
|
---|