[10] | 1 | // %flair:license{
|
---|
| 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %flair:license}
|
---|
[7] | 5 | // created: 2011/05/01
|
---|
| 6 | // filename: LowPassFilter_impl.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: objet permettant le calcul d'un filtre passe bas
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "LowPassFilter_impl.h"
|
---|
| 19 | #include "LowPassFilter.h"
|
---|
| 20 | #include <cvmatrix.h>
|
---|
| 21 | #include <Layout.h>
|
---|
| 22 | #include <GroupBox.h>
|
---|
| 23 | #include <SpinBox.h>
|
---|
| 24 | #include <DoubleSpinBox.h>
|
---|
| 25 |
|
---|
| 26 | #define PI ((float)3.14159265358979323846)
|
---|
| 27 |
|
---|
| 28 | using std::string;
|
---|
| 29 | using namespace flair::core;
|
---|
| 30 | using namespace flair::gui;
|
---|
| 31 | using namespace flair::filter;
|
---|
| 32 |
|
---|
| 33 | LowPassFilter_impl::LowPassFilter_impl(const LowPassFilter* self,const LayoutPosition* position,string name,const cvmatrix* init_value)
|
---|
| 34 | {
|
---|
| 35 | first_update=true;
|
---|
| 36 |
|
---|
| 37 | if(init_value!=NULL)
|
---|
| 38 | {
|
---|
| 39 | prev_value=(cvmatrix*)init_value;
|
---|
| 40 | }
|
---|
| 41 | else
|
---|
| 42 | {
|
---|
| 43 | //if NULL, assume dimension 1, and init=0
|
---|
| 44 | cvmatrix_descriptor* desc=new cvmatrix_descriptor(1,1);
|
---|
| 45 | desc->SetElementName(0,0,"output");
|
---|
| 46 | prev_value=new cvmatrix(self,desc,floatType,name);
|
---|
| 47 | prev_value->SetValue(0,0,0);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | //init UI
|
---|
| 51 | GroupBox* reglages_groupbox=new GroupBox(position,name);
|
---|
| 52 | T=new DoubleSpinBox(reglages_groupbox->NewRow(),"period, 0 for auto"," s",0,10,0.01);
|
---|
| 53 | freq=new DoubleSpinBox(reglages_groupbox->NewRow(),"cutoff frequency"," Hz",0,10000,0.1,2,1);
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | //init output matrix of same size as init
|
---|
| 57 | cvmatrix_descriptor* desc=new cvmatrix_descriptor(prev_value->Rows(),prev_value->Cols());
|
---|
| 58 |
|
---|
| 59 | for(int i=0;i<prev_value->Rows();i++)
|
---|
| 60 | {
|
---|
| 61 | for(int j=0;j<prev_value->Cols();j++)
|
---|
| 62 | {
|
---|
| 63 | desc->SetElementName(i,j,prev_value->Name(i,j));
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | output=new cvmatrix(self,desc,prev_value->GetDataType().GetElementDataType(),name);
|
---|
| 68 |
|
---|
| 69 | output->SetValue(0,0,0);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | LowPassFilter_impl::~LowPassFilter_impl()
|
---|
| 73 | {
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void LowPassFilter_impl::UpdateFrom(const io_data *data)
|
---|
| 77 | {
|
---|
| 78 | float delta_t;
|
---|
| 79 | float result;
|
---|
| 80 | cvmatrix *input=(cvmatrix*)data;
|
---|
| 81 |
|
---|
| 82 | //on prend une fois pour toute les mutex et on fait des accès directs
|
---|
| 83 | output->GetMutex();
|
---|
| 84 | input->GetMutex();
|
---|
| 85 |
|
---|
| 86 | if(first_update==true)
|
---|
| 87 | {
|
---|
| 88 | for(int i=0;i<input->Rows();i++)
|
---|
| 89 | {
|
---|
| 90 | for(int j=0;j<input->Cols();j++)
|
---|
| 91 | {
|
---|
| 92 | output->SetValueNoMutex(i,j,prev_value->ValueNoMutex(i,j));
|
---|
| 93 | prev_value->SetValueNoMutex(i,j,input->ValueNoMutex(i,j));
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | first_update=false;
|
---|
| 97 | }
|
---|
| 98 | else
|
---|
| 99 | {
|
---|
| 100 | if(T->Value()==0)
|
---|
| 101 | {
|
---|
| 102 | delta_t=(float)(data->DataTime()-previous_time)/1000000000.;
|
---|
| 103 | }
|
---|
| 104 | else
|
---|
| 105 | {
|
---|
| 106 | delta_t=T->Value();
|
---|
| 107 | }
|
---|
| 108 | for(int i=0;i<input->Rows();i++)
|
---|
| 109 | {
|
---|
| 110 | for(int j=0;j<input->Cols();j++)
|
---|
| 111 | {
|
---|
| 112 |
|
---|
| 113 | if(freq->Value()!=0)
|
---|
| 114 | {
|
---|
| 115 | output->SetValueNoMutex(i,j,(1-2*PI*freq->Value()*delta_t)*prev_value->ValueNoMutex(i,j)+2*PI*freq->Value()*delta_t*input->ValueNoMutex(i,j));
|
---|
| 116 | }
|
---|
| 117 | else
|
---|
| 118 | {
|
---|
| 119 | output->SetValueNoMutex(i,j,input->ValueNoMutex(i,j));
|
---|
| 120 | }
|
---|
| 121 | prev_value->SetValueNoMutex(i,j,output->ValueNoMutex(i,j));
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | input->ReleaseMutex();
|
---|
| 126 | output->ReleaseMutex();
|
---|
| 127 |
|
---|
| 128 | output->SetDataTime(data->DataTime());
|
---|
| 129 | previous_time=data->DataTime();
|
---|
| 130 | }
|
---|
| 131 |
|
---|