[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"
|
---|
| 20 | #include <cvmatrix.h>
|
---|
| 21 | #include <Layout.h>
|
---|
| 22 | #include <GroupBox.h>
|
---|
| 23 | #include <SpinBox.h>
|
---|
| 24 | #include <DoubleSpinBox.h>
|
---|
| 25 |
|
---|
| 26 | #define PI ((float)3.14159265358979323846)
|
---|
| 27 |
|
---|
| 28 | using std::string;
|
---|
| 29 | using namespace flair::core;
|
---|
| 30 | using namespace flair::gui;
|
---|
| 31 | using namespace flair::filter;
|
---|
| 32 |
|
---|
[15] | 33 | LowPassFilter_impl::LowPassFilter_impl(const LowPassFilter *self,
|
---|
| 34 | const LayoutPosition *position,
|
---|
| 35 | string name,
|
---|
| 36 | const cvmatrix *init_value) {
|
---|
| 37 | first_update = true;
|
---|
[7] | 38 |
|
---|
[15] | 39 | if (init_value != NULL) {
|
---|
| 40 | prev_value = (cvmatrix *)init_value;
|
---|
| 41 | } else {
|
---|
| 42 | // if NULL, assume dimension 1, and init=0
|
---|
| 43 | cvmatrix_descriptor *desc = new cvmatrix_descriptor(1, 1);
|
---|
| 44 | desc->SetElementName(0, 0, "output");
|
---|
| 45 | prev_value = new cvmatrix(self, desc, floatType, name);
|
---|
| 46 | prev_value->SetValue(0, 0, 0);
|
---|
| 47 | }
|
---|
[7] | 48 |
|
---|
[15] | 49 | // init UI
|
---|
| 50 | GroupBox *reglages_groupbox = new GroupBox(position, name);
|
---|
| 51 | T = new DoubleSpinBox(reglages_groupbox->NewRow(), "period, 0 for auto", " s",
|
---|
| 52 | 0, 10, 0.01);
|
---|
| 53 | freq = new DoubleSpinBox(reglages_groupbox->NewRow(), "cutoff frequency",
|
---|
| 54 | " Hz", 0, 10000, 0.1, 2, 1);
|
---|
[7] | 55 |
|
---|
[15] | 56 | // init output matrix of same size as init
|
---|
| 57 | cvmatrix_descriptor *desc =
|
---|
| 58 | new cvmatrix_descriptor(prev_value->Rows(), prev_value->Cols());
|
---|
[7] | 59 |
|
---|
[15] | 60 | for (int i = 0; i < prev_value->Rows(); i++) {
|
---|
| 61 | for (int j = 0; j < prev_value->Cols(); j++) {
|
---|
| 62 | desc->SetElementName(i, j, prev_value->Name(i, j));
|
---|
[7] | 63 | }
|
---|
[15] | 64 | }
|
---|
[7] | 65 |
|
---|
[15] | 66 | output = new cvmatrix(self, desc,
|
---|
| 67 | prev_value->GetDataType().GetElementDataType(), name);
|
---|
[7] | 68 |
|
---|
[15] | 69 | output->SetValue(0, 0, 0);
|
---|
[7] | 70 | }
|
---|
| 71 |
|
---|
[15] | 72 | LowPassFilter_impl::~LowPassFilter_impl() {}
|
---|
[7] | 73 |
|
---|
[15] | 74 | void LowPassFilter_impl::UpdateFrom(const io_data *data) {
|
---|
| 75 | float delta_t;
|
---|
| 76 | float result;
|
---|
| 77 | cvmatrix *input = (cvmatrix *)data;
|
---|
[7] | 78 |
|
---|
[15] | 79 | // on prend une fois pour toute les mutex et on fait des accès directs
|
---|
| 80 | output->GetMutex();
|
---|
| 81 | input->GetMutex();
|
---|
[7] | 82 |
|
---|
[15] | 83 | if (first_update == true) {
|
---|
| 84 | for (int i = 0; i < input->Rows(); i++) {
|
---|
| 85 | for (int j = 0; j < input->Cols(); j++) {
|
---|
| 86 | output->SetValueNoMutex(i, j, prev_value->ValueNoMutex(i, j));
|
---|
| 87 | prev_value->SetValueNoMutex(i, j, input->ValueNoMutex(i, j));
|
---|
| 88 | }
|
---|
[7] | 89 | }
|
---|
[15] | 90 | first_update = false;
|
---|
| 91 | } else {
|
---|
| 92 | if (T->Value() == 0) {
|
---|
| 93 | delta_t = (float)(data->DataTime() - previous_time) / 1000000000.;
|
---|
| 94 | } else {
|
---|
| 95 | delta_t = T->Value();
|
---|
| 96 | }
|
---|
| 97 | for (int i = 0; i < input->Rows(); i++) {
|
---|
| 98 | for (int j = 0; j < input->Cols(); j++) {
|
---|
[7] | 99 |
|
---|
[15] | 100 | if (freq->Value() != 0) {
|
---|
| 101 | output->SetValueNoMutex(i, j, (1 - 2 * PI * freq->Value() * delta_t) *
|
---|
| 102 | prev_value->ValueNoMutex(i, j) +
|
---|
| 103 | 2 * PI * freq->Value() * delta_t *
|
---|
| 104 | input->ValueNoMutex(i, j));
|
---|
| 105 | } else {
|
---|
| 106 | output->SetValueNoMutex(i, j, input->ValueNoMutex(i, j));
|
---|
[7] | 107 | }
|
---|
[15] | 108 | prev_value->SetValueNoMutex(i, j, output->ValueNoMutex(i, j));
|
---|
| 109 | }
|
---|
[7] | 110 | }
|
---|
[15] | 111 | }
|
---|
| 112 | input->ReleaseMutex();
|
---|
| 113 | output->ReleaseMutex();
|
---|
[7] | 114 |
|
---|
[15] | 115 | output->SetDataTime(data->DataTime());
|
---|
| 116 | previous_time = data->DataTime();
|
---|
[7] | 117 | }
|
---|