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

Last change on this file since 218 was 214, checked in by Sanahuja Guillaume, 6 years ago

matrix

File size: 3.9 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"
[214]20#include <Matrix.h>
[7]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
[214]31ButterworthLowPass_impl::ButterworthLowPass_impl(const ButterworthLowPass *self,
[15]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");
[214]43 output = new Matrix(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;
[214]62 this->self=self;
[7]63}
64
[162]65ButterworthLowPass_impl::~ButterworthLowPass_impl() {
66 for(uint32_t i=0;i<nbRow;i++) {
67 for(uint32_t j=0;j<nbCol;j++) {
68 delete f[i*nbCol+j];
69 }
70 }
71 free(f);
72}
[7]73
[15]74void ButterworthLowPass_impl::UpdateFrom(const io_data *data) {
[147]75 float delta_t;
[214]76 const Matrix* input = dynamic_cast<const Matrix*>(data);
[147]77
[214]78 if (!input) {
79 self->Warn("casting %s to Matrix failed\n",data->ObjectName().c_str());
80 return;
81 }
82
[162]83 if (T->ValueChanged() && T->Value() != 0) {
84 for(uint32_t i=0;i<nbRow;i++) {
85 for(uint32_t j=0;j<nbCol;j++) {
86 f[i*nbCol+j]->setup(1. / T->Value(), cutoff->Value());
[165]87 //settingsChanged(f[i*nbCol+j],input->Value(i, j));
[162]88 }
[7]89 }
[15]90 }
[7]91
[147]92 if (T->Value() == 0) {
93 delta_t = (float)(data->DataTime() - previous_time) / 1000000000.;
[162]94 for(uint32_t i=0;i<nbRow;i++) {
95 for(uint32_t j=0;j<nbCol;j++) {
96 f[i*nbCol+j]->setup(1. / delta_t, cutoff->Value());
97 }
98 }
[147]99 } else {
100 delta_t=T->Value();
101 }
102
[15]103 // on prend une fois pour toute les mutex et on fait des accès directs
104 output->GetMutex();
105 input->GetMutex();
[7]106
[147]107 if (cutoff->ValueChanged()) {
[162]108 for(uint32_t i=0;i<nbRow;i++) {
109 for(uint32_t j=0;j<nbCol;j++) {
110 f[i*nbCol+j]->setup(1. / delta_t, cutoff->Value());
[165]111 //settingsChanged(f[i*nbCol+j],input->ValueNoMutex(i, j));
[162]112 }
113 }
[15]114 }
[7]115
[15]116 if (first_update == true) {
117 first_update = false;
118 } else {
[162]119 for(uint32_t i=0;i<nbRow;i++) {
120 for(uint32_t j=0;j<nbCol;j++) {
121 float result = f[i*nbCol+j]->filter(input->ValueNoMutex(i, j));
122 output->SetValueNoMutex(i, j, result);
123 }
124 }
[15]125 }
[7]126
[15]127 input->ReleaseMutex();
128 output->ReleaseMutex();
[7]129
[15]130 output->SetDataTime(data->DataTime());
131 previous_time = data->DataTime();
[7]132}
[147]133
134//ne gere pas les oscillations (s'arrete des qu'une valeure est bonne a 5%)
[162]135void ButterworthLowPass_impl::settingsChanged(PoleFilter*f,float inputValue) {
[147]136 float result=f->filter(inputValue);
137
138 while(result<inputValue*0.95 || result>inputValue*1.05) {
139 result=f->filter(inputValue);
140 }
141}
Note: See TracBrowser for help on using the repository browser.