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