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

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

add delta time to io_data

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