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: 2013/12/10
|
---|
6 | // filename: ButterworthLowPass_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 de Butterworth
|
---|
14 | //
|
---|
15 | //
|
---|
16 | /*********************************************************************/
|
---|
17 |
|
---|
18 | #include "ButterworthLowPass_impl.h"
|
---|
19 | #include "ButterworthLowPass.h"
|
---|
20 | #include <Matrix.h>
|
---|
21 | #include <Layout.h>
|
---|
22 | #include <GroupBox.h>
|
---|
23 | #include <SpinBox.h>
|
---|
24 | #include <DoubleSpinBox.h>
|
---|
25 |
|
---|
26 | using std::string;
|
---|
27 | using namespace flair::core;
|
---|
28 | using namespace flair::gui;
|
---|
29 | using namespace flair::filter;
|
---|
30 |
|
---|
31 | ButterworthLowPass_impl::ButterworthLowPass_impl(const ButterworthLowPass *self,
|
---|
32 | const LayoutPosition *position,
|
---|
33 | string name, uint32_t order,uint32_t nbRow,uint32_t nbCol) {
|
---|
34 | // init UI
|
---|
35 | GroupBox *reglages_groupbox = new GroupBox(position, name);
|
---|
36 | T = new DoubleSpinBox(reglages_groupbox->NewRow(), "period, 0 for auto", " s",
|
---|
37 | 0, 10, 0.01);
|
---|
38 | cutoff = new DoubleSpinBox(reglages_groupbox->NewRow(), "cutoff frequency",
|
---|
39 | " Hz", 0, 10000, 0.1, 2, 1);
|
---|
40 |
|
---|
41 | cvmatrix_descriptor *desc = new cvmatrix_descriptor(nbRow, nbCol);
|
---|
42 | //desc->SetElementName(0, 0, "output");
|
---|
43 | output = new Matrix(self, desc, floatType, name);
|
---|
44 | delete desc;
|
---|
45 |
|
---|
46 | f=(PoleFilter**)malloc(sizeof(PoleFilter*)*nbRow*nbCol);
|
---|
47 |
|
---|
48 | for(uint32_t i=0;i<nbRow;i++) {
|
---|
49 | for(uint32_t j=0;j<nbCol;j++) {
|
---|
50 | f[i*nbCol+j] = new PoleFilter(order);
|
---|
51 | if (T->Value() != 0) {
|
---|
52 | f[i*nbCol+j]->setup(1. / T->Value(), cutoff->Value());
|
---|
53 | }
|
---|
54 | f[i*nbCol+j]->reset();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | this->order=order;
|
---|
59 | this->nbRow=nbRow;
|
---|
60 | this->nbCol=nbCol;
|
---|
61 | this->self=self;
|
---|
62 | }
|
---|
63 |
|
---|
64 | ButterworthLowPass_impl::~ButterworthLowPass_impl() {
|
---|
65 | for(uint32_t i=0;i<nbRow;i++) {
|
---|
66 | for(uint32_t j=0;j<nbCol;j++) {
|
---|
67 | delete f[i*nbCol+j];
|
---|
68 | }
|
---|
69 | }
|
---|
70 | free(f);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void ButterworthLowPass_impl::UpdateFrom(const io_data *data) {
|
---|
74 | float delta_t;
|
---|
75 | const Matrix* input = dynamic_cast<const Matrix*>(data);
|
---|
76 |
|
---|
77 | if (!input) {
|
---|
78 | self->Warn("casting %s to Matrix failed\n",data->ObjectName().c_str());
|
---|
79 | return;
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (T->ValueChanged() && T->Value() != 0) {
|
---|
83 | for(uint32_t i=0;i<nbRow;i++) {
|
---|
84 | for(uint32_t j=0;j<nbCol;j++) {
|
---|
85 | f[i*nbCol+j]->setup(1. / T->Value(), cutoff->Value());
|
---|
86 | //settingsChanged(f[i*nbCol+j],input->Value(i, j));
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (T->Value() == 0) {
|
---|
92 | delta_t = (float)(data->DataDeltaTime()) / 1000000000.;
|
---|
93 | for(uint32_t i=0;i<nbRow;i++) {
|
---|
94 | for(uint32_t j=0;j<nbCol;j++) {
|
---|
95 | f[i*nbCol+j]->setup(1. / delta_t, cutoff->Value());
|
---|
96 | }
|
---|
97 | }
|
---|
98 | } else {
|
---|
99 | delta_t=T->Value();
|
---|
100 | }
|
---|
101 |
|
---|
102 | // on prend une fois pour toute les mutex et on fait des accès directs
|
---|
103 | output->GetMutex();
|
---|
104 | input->GetMutex();
|
---|
105 |
|
---|
106 | if (cutoff->ValueChanged()) {
|
---|
107 | for(uint32_t i=0;i<nbRow;i++) {
|
---|
108 | for(uint32_t j=0;j<nbCol;j++) {
|
---|
109 | f[i*nbCol+j]->setup(1. / delta_t, cutoff->Value());
|
---|
110 | //settingsChanged(f[i*nbCol+j],input->ValueNoMutex(i, j));
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (delta_t!=0) {
|
---|
116 | for(uint32_t i=0;i<nbRow;i++) {
|
---|
117 | for(uint32_t j=0;j<nbCol;j++) {
|
---|
118 | float result = f[i*nbCol+j]->filter(input->ValueNoMutex(i, j));
|
---|
119 | output->SetValueNoMutex(i, j, result);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | input->ReleaseMutex();
|
---|
125 | output->ReleaseMutex();
|
---|
126 |
|
---|
127 | output->SetDataTime(data->DataTime());
|
---|
128 | }
|
---|
129 |
|
---|
130 | //ne gere pas les oscillations (s'arrete des qu'une valeure est bonne a 5%)
|
---|
131 | void ButterworthLowPass_impl::settingsChanged(PoleFilter*f,float inputValue) {
|
---|
132 | float result=f->filter(inputValue);
|
---|
133 |
|
---|
134 | while(result<inputValue*0.95 || result>inputValue*1.05) {
|
---|
135 | result=f->filter(inputValue);
|
---|
136 | }
|
---|
137 | } |
---|