source: flair-src/trunk/lib/FlairFilter/src/LowPassFilter_impl.cpp@ 214

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

matrix

File size: 3.5 KB
Line 
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 <Matrix.h>
21#include <Layout.h>
22#include <GroupBox.h>
23#include <SpinBox.h>
24#include <DoubleSpinBox.h>
25#include <typeinfo>
26#define PI ((float)3.14159265358979323846)
27
28using std::string;
29using namespace flair::core;
30using namespace flair::gui;
31using namespace flair::filter;
32
33LowPassFilter_impl::LowPassFilter_impl(const LowPassFilter *self,
34 const LayoutPosition *position,
35 string name,
36 const Matrix *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 Matrix(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 Matrix(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 this->self = self;
71}
72
73LowPassFilter_impl::~LowPassFilter_impl() {}
74
75void LowPassFilter_impl::UpdateFrom(const io_data *data) {
76 float delta_t;
77 const Matrix* input = dynamic_cast<const Matrix*>(data);
78
79 if (!input) {
80 self->Warn("casting %s to Matrix failed\n",data->ObjectName().c_str());
81 return;
82 }
83
84 // on prend une fois pour toute les mutex et on fait des accès directs
85 output->GetMutex();
86 input->GetMutex();
87
88 if (T->Value() == 0) {
89 delta_t = (float)(data->DataTime() - previous_time) / 1000000000.;
90 } else {
91 delta_t = T->Value();
92 }
93 for (int i = 0; i < input->Rows(); i++) {
94 for (int j = 0; j < input->Cols(); j++) {
95 float cutoff=freq->Value();
96 if (cutoff == 0 || freq->ValueChanged()) {
97 output->SetValueNoMutex(i, j, input->ValueNoMutex(i, j));
98 } else {
99 output->SetValueNoMutex(i, j, (1 - 2 * PI * cutoff * delta_t) *
100 output->ValueNoMutex(i, j) +
101 2 * PI * cutoff * delta_t *
102 input->ValueNoMutex(i, j));
103 }
104 }
105 }
106
107 input->ReleaseMutex();
108 output->ReleaseMutex();
109
110 output->SetDataTime(data->DataTime());
111 previous_time = data->DataTime();
112}
Note: See TracBrowser for help on using the repository browser.