source: flair-src/trunk/demos/Sinus/src/MeanFilter.cpp@ 14

Last change on this file since 14 was 14, checked in by Sanahuja Guillaume, 6 years ago

sinus

File size: 2.5 KB
Line 
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"
15#include <cvmatrix.h>
16#include <LayoutPosition.h>
17#include <GroupBox.h>
18#include <SpinBox.h>
19
20using namespace std;
21using namespace flair::core;
22using namespace flair::gui;
23
24namespace flair { namespace filter {
25
26MeanFilter::MeanFilter(const IODevice* parent,const LayoutPosition* position,string name): IODevice(parent,name) {
27 //interface initialisation
28 groupBox=new GroupBox(position,name);
29 numberOfElements=new SpinBox(groupBox->NewRow(),"numberOfElements:",1,MAX_NUMBER_OF_ELEMENTS,1);//saturated to MAX_NUMBER_OF_ELEMENTS
30
31 //init storage
32 for(int i=0; i<MAX_NUMBER_OF_ELEMENTS; i++) previousValues[i]=0;
33
34 //1*1 output matrix
35 cvmatrix_descriptor* desc=new cvmatrix_descriptor(1,1);
36 desc->SetElementName(0,0,"mean filter");//name will be used for graphs and logs
37 output=new cvmatrix(this,desc,floatType,name);
38
39 AddDataToLog(output);
40}
41
42MeanFilter::~MeanFilter() {
43}
44
45cvmatrix *MeanFilter::GetMatrix() const {
46 return output;
47}
48
49float MeanFilter::GetValue(void) const {
50 return output->Value(0,0);
51}
52
53//UpdateFrom, where we implement the filter
54//this method is automatically called when the parent IODevice calls ProcessUpdate
55//in our case it is the sinus or the 1st orde law pass filter
56//(see in Sinus::Run the call to ProcessUpdate)
57void MeanFilter::UpdateFrom(const io_data *data) {
58
59 float result=0;
60 //get input argument in a cvmatrix
61 cvmatrix *input=(cvmatrix*)data;
62
63 //simple (and not efficent!) implementation of the filter
64 previousValues[numberOfElements->Value()-1]=input->Value(0,0);
65 for(int i=0; i<numberOfElements->Value(); i++) result+=previousValues[i];
66 for(int i=1; i<numberOfElements->Value(); i++) previousValues[i-1]=previousValues[i];
67
68 //put the result in output matrix
69 output->SetValue(0,0,result/numberOfElements->Value());
70 //put corresponding time
71 output->SetDataTime(data->DataTime());
72
73 //ProcessUpdate is very important
74 //we must call it after updating the output matrix
75 //it allows:
76 // -to save value in the logs
77 // -to automatically call the next filter UpdateFrom method
78 ProcessUpdate(output);
79}
80}// end namespace filter
81}// end namespace flair
Note: See TracBrowser for help on using the repository browser.