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}
|
---|
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,
|
---|
34 | const LayoutPosition *position,
|
---|
35 | string name,
|
---|
36 | const cvmatrix *init_value) {
|
---|
37 |
|
---|
38 | if (init_value != NULL) {
|
---|
39 | // init output matrix of same size as init
|
---|
40 | cvmatrix_descriptor *desc =new cvmatrix_descriptor(init_value->Rows(), init_value->Cols());
|
---|
41 |
|
---|
42 | for (int i = 0; i < init_value->Rows(); i++) {
|
---|
43 | for (int j = 0; j < init_value->Cols(); j++) {
|
---|
44 | desc->SetElementName(i, j, init_value->Name(i, j));
|
---|
45 | }
|
---|
46 | }
|
---|
47 | output = new cvmatrix(self, desc,init_value->GetDataType().GetElementDataType(), name);
|
---|
48 | for (int i = 0; i < init_value->Rows(); i++) {
|
---|
49 | for (int j = 0; j < init_value->Cols(); j++) {
|
---|
50 | output->SetValue(i, j, init_value->Value(i,j));
|
---|
51 | }
|
---|
52 | }
|
---|
53 | delete desc;
|
---|
54 | } else {
|
---|
55 | // if NULL, assume dimension 1, and init=0
|
---|
56 | cvmatrix_descriptor *desc = new cvmatrix_descriptor(1, 1);
|
---|
57 | desc->SetElementName(0, 0, "output");
|
---|
58 | output = new cvmatrix(self, desc, floatType, name);
|
---|
59 | delete desc;
|
---|
60 | }
|
---|
61 |
|
---|
62 | // init UI
|
---|
63 | GroupBox *reglages_groupbox = new GroupBox(position, name);
|
---|
64 | T = new DoubleSpinBox(reglages_groupbox->NewRow(), "period, 0 for auto", " s",
|
---|
65 | 0, 10, 0.01);
|
---|
66 | freq = new DoubleSpinBox(reglages_groupbox->NewRow(), "cutoff frequency",
|
---|
67 | " Hz", 0, 10000, 0.1, 2, 1);
|
---|
68 |
|
---|
69 | previous_time=GetTime();
|
---|
70 | }
|
---|
71 |
|
---|
72 | LowPassFilter_impl::~LowPassFilter_impl() {}
|
---|
73 |
|
---|
74 | void LowPassFilter_impl::UpdateFrom(const io_data *data) {
|
---|
75 | float delta_t;
|
---|
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 (T->Value() == 0) {
|
---|
83 | delta_t = (float)(data->DataTime() - previous_time) / 1000000000.;
|
---|
84 | } else {
|
---|
85 | delta_t = T->Value();
|
---|
86 | }
|
---|
87 | for (int i = 0; i < input->Rows(); i++) {
|
---|
88 | for (int j = 0; j < input->Cols(); j++) {
|
---|
89 | float cutoff=freq->Value();
|
---|
90 | if (cutoff == 0 || freq->ValueChanged()) {
|
---|
91 | output->SetValueNoMutex(i, j, input->ValueNoMutex(i, j));
|
---|
92 | } else {
|
---|
93 | output->SetValueNoMutex(i, j, (1 - 2 * PI * cutoff * delta_t) *
|
---|
94 | output->ValueNoMutex(i, j) +
|
---|
95 | 2 * PI * cutoff * delta_t *
|
---|
96 | input->ValueNoMutex(i, j));
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | input->ReleaseMutex();
|
---|
102 | output->ReleaseMutex();
|
---|
103 |
|
---|
104 | output->SetDataTime(data->DataTime());
|
---|
105 | previous_time = data->DataTime();
|
---|
106 | }
|
---|