source: flair-src/trunk/lib/FlairFilter/src/ButterworthLowPass_impl.cpp@ 180

Last change on this file since 180 was 165, checked in by Sanahuja Guillaume, 7 years ago

modifs

File size: 3.8 KB
RevLine 
[10]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[10]4// %flair:license}
[7]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 <cvmatrix.h>
21#include <Layout.h>
22#include <GroupBox.h>
23#include <SpinBox.h>
24#include <DoubleSpinBox.h>
25
26using std::string;
27using namespace flair::core;
28using namespace flair::gui;
29using namespace flair::filter;
30
[15]31ButterworthLowPass_impl::ButterworthLowPass_impl(ButterworthLowPass *self,
32 const LayoutPosition *position,
[162]33 string name, uint32_t order,uint32_t nbRow,uint32_t nbCol) {
[15]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);
[7]40
[162]41 cvmatrix_descriptor *desc = new cvmatrix_descriptor(nbRow, nbCol);
42 //desc->SetElementName(0, 0, "output");
[15]43 output = new cvmatrix(self, desc, floatType, name);
[148]44 delete desc;
[7]45
[162]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 }
[147]56 }
[7]57
[15]58 first_update = true;
[147]59 this->order=order;
[162]60 this->nbRow=nbRow;
61 this->nbCol=nbCol;
[7]62}
63
[162]64ButterworthLowPass_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}
[7]72
[15]73void ButterworthLowPass_impl::UpdateFrom(const io_data *data) {
74 cvmatrix *input = (cvmatrix *)data;
[147]75 float delta_t;
76
[162]77 if (T->ValueChanged() && T->Value() != 0) {
78 for(uint32_t i=0;i<nbRow;i++) {
79 for(uint32_t j=0;j<nbCol;j++) {
80 f[i*nbCol+j]->setup(1. / T->Value(), cutoff->Value());
[165]81 //settingsChanged(f[i*nbCol+j],input->Value(i, j));
[162]82 }
[7]83 }
[15]84 }
[7]85
[147]86 if (T->Value() == 0) {
87 delta_t = (float)(data->DataTime() - previous_time) / 1000000000.;
[162]88 for(uint32_t i=0;i<nbRow;i++) {
89 for(uint32_t j=0;j<nbCol;j++) {
90 f[i*nbCol+j]->setup(1. / delta_t, cutoff->Value());
91 }
92 }
[147]93 } else {
94 delta_t=T->Value();
95 }
96
[15]97 // on prend une fois pour toute les mutex et on fait des accès directs
98 output->GetMutex();
99 input->GetMutex();
[7]100
[147]101 if (cutoff->ValueChanged()) {
[162]102 for(uint32_t i=0;i<nbRow;i++) {
103 for(uint32_t j=0;j<nbCol;j++) {
104 f[i*nbCol+j]->setup(1. / delta_t, cutoff->Value());
[165]105 //settingsChanged(f[i*nbCol+j],input->ValueNoMutex(i, j));
[162]106 }
107 }
[15]108 }
[7]109
[15]110 if (first_update == true) {
111 first_update = false;
112 } else {
[162]113 for(uint32_t i=0;i<nbRow;i++) {
114 for(uint32_t j=0;j<nbCol;j++) {
115 float result = f[i*nbCol+j]->filter(input->ValueNoMutex(i, j));
116 output->SetValueNoMutex(i, j, result);
117 }
118 }
[15]119 }
[7]120
[15]121 input->ReleaseMutex();
122 output->ReleaseMutex();
[7]123
[15]124 output->SetDataTime(data->DataTime());
125 previous_time = data->DataTime();
[7]126}
[147]127
128//ne gere pas les oscillations (s'arrete des qu'une valeure est bonne a 5%)
[162]129void ButterworthLowPass_impl::settingsChanged(PoleFilter*f,float inputValue) {
[147]130 float result=f->filter(inputValue);
131
132 while(result<inputValue*0.95 || result>inputValue*1.05) {
133 result=f->filter(inputValue);
134 }
135}
Note: See TracBrowser for help on using the repository browser.