Changeset 16 in flair-src for trunk/demos/Sinus/src/MeanFilter.cpp
- Timestamp:
- Apr 8, 2016, 3:48:40 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/demos/Sinus/src/MeanFilter.cpp
r14 r16 22 22 using namespace flair::gui; 23 23 24 namespace flair { namespace filter { 24 namespace flair { 25 namespace filter { 25 26 26 MeanFilter::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 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 30 35 31 //init storage 32 for(int i=0; i<MAX_NUMBER_OF_ELEMENTS; i++) previousValues[i]=0; 36 // init storage 37 for (int i = 0; i < MAX_NUMBER_OF_ELEMENTS; i++) 38 previousValues[i] = 0; 33 39 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); 40 // 1*1 output matrix 41 cvmatrix_descriptor *desc = new cvmatrix_descriptor(1, 1); 42 desc->SetElementName(0, 0, 43 "mean filter"); // name will be used for graphs and logs 44 output = new cvmatrix(this, desc, floatType, name); 38 45 39 46 AddDataToLog(output); 40 47 } 41 48 42 MeanFilter::~MeanFilter() { 43 } 49 MeanFilter::~MeanFilter() {} 44 50 45 cvmatrix *MeanFilter::GetMatrix() const { 46 return output; 47 } 51 cvmatrix *MeanFilter::GetMatrix() const { return output; } 48 52 49 float MeanFilter::GetValue(void) const { 50 return output->Value(0,0); 51 } 53 float MeanFilter::GetValue(void) const { return output->Value(0, 0); } 52 54 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 55 // UpdateFrom, where we implement the filter 56 // this method is automatically called when the parent IODevice calls 57 // ProcessUpdate 58 // in our case it is the sinus or the 1st orde law pass filter 56 59 //(see in Sinus::Run the call to ProcessUpdate) 57 60 void MeanFilter::UpdateFrom(const io_data *data) { 58 61 59 float result=0;60 //get input argument in a cvmatrix61 cvmatrix *input=(cvmatrix*)data;62 float result = 0; 63 // get input argument in a cvmatrix 64 cvmatrix *input = (cvmatrix *)data; 62 65 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]; 66 // simple (and not efficent!) implementation of the filter 67 previousValues[numberOfElements->Value() - 1] = input->Value(0, 0); 68 for (int i = 0; i < numberOfElements->Value(); i++) 69 result += previousValues[i]; 70 for (int i = 1; i < numberOfElements->Value(); i++) 71 previousValues[i - 1] = previousValues[i]; 67 72 68 //put the result in output matrix69 output->SetValue(0,0,result/numberOfElements->Value());70 //put corresponding time71 73 // put the result in output matrix 74 output->SetValue(0, 0, result / numberOfElements->Value()); 75 // put corresponding time 76 output->SetDataTime(data->DataTime()); 72 77 73 //ProcessUpdate is very important74 //we must call it after updating the output matrix75 //it allows:76 77 78 78 // ProcessUpdate is very important 79 // we must call it after updating the output matrix 80 // it allows: 81 // -to save value in the logs 82 // -to automatically call the next filter UpdateFrom method 83 ProcessUpdate(output); 79 84 } 80 } // end namespace filter81 } // end namespace flair85 } // end namespace filter 86 } // end namespace flair
Note:
See TracChangeset
for help on using the changeset viewer.