GCC Code Coverage Report


Directory: src/
File: src/LowPassFilter.cpp
Date: 2026-09-03 17:04:53
Exec Total Coverage
Lines: 0 37 0.0%
Branches: 0 48 0.0%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * Copyright (C) 2021 - 2022 Jonathan Grahl <jonathan.grahl@igh.de>
4 * 2021 - 2022 Florian Pose <florian.pose@igh.de>
5 *
6 * This file is part of the reta library (realtime-automation).
7 *
8 * The reta library is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation, either version 3 of the License, or (at
11 * your option) any later version.
12 *
13 * The reta library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with the reta library. If not, see <http://www.gnu.org/licenses/>.
20 *
21 ****************************************************************************/
22
23 #include "reta/LowPassFilter.h"
24
25 #include "Base.h"
26
27 using std::function;
28 using std::make_unique;
29 using std::shared_ptr;
30 using std::string;
31 using std::vector;
32
33 using namespace Reta;
34
35 /****************************************************************************/
36
37 struct RETA_LOCAL LowPassFilter::Impl : public Base
38 {
39 Impl(shared_ptr<Task>, const string &, double, unsigned int);
40
41 void
42 update(const vector<double> &,
43 const vector<uint8_t> & = vector<uint8_t>());
44
45 shared_ptr<Task> task;
46
47 vector<double> tau;
48 vector<double> output;
49 };
50
51 /****************************************************************************/
52
53 LowPassFilter::Impl::Impl(
54 shared_ptr<Task> task,
55 const string &prefix,
56 double initTau,
57 unsigned int width) :
58 Base {prefix}, task {task}, tau(width, initTau), output(width, 0.0)
59 {
60 checkZeroWidth(width);
61
62 pdserv *pdserv {task->getPdServ()};
63 pdtask *pdtask {task->getPdTask()};
64
65 pdserv_parameter(
66 pdserv, (prefix + "/Tau").c_str(), 0666, pd_double_T, tau.data(),
67 tau.size(), NULL, NULL, NULL);
68 pdserv_signal(
69 pdtask, 1, (prefix + "/Output").c_str(), pd_double_T,
70 output.data(), output.size(), NULL);
71 }
72
73 /****************************************************************************/
74
75 void LowPassFilter::Impl::update(
76 const vector<double> &input,
77 const vector<uint8_t> &reset)
78 {
79 for (unsigned int i = 0; i < output.size(); i++) {
80 if (tau.at(i) > 0.0 && !reset.at(i)) {
81 output.at(i) += task->getPeriod() / tau.at(i)
82 * (input.at(i) - output.at(i));
83 }
84 else {
85 output.at(i) = input.at(i);
86 }
87 }
88 }
89
90 /****************************************************************************/
91
92 LowPassFilter::LowPassFilter(
93 shared_ptr<Task> task,
94 const std::string &prefix,
95 double initTau,
96 unsigned int width) :
97 impl {make_unique<Impl>(task, prefix, initTau, width)}
98 {}
99
100 /****************************************************************************/
101
102 LowPassFilter::~LowPassFilter()
103 {}
104
105 /****************************************************************************/
106
107 void LowPassFilter::setTau(double newTau, unsigned int i)
108 {
109 impl->tau.at(i) = newTau;
110 }
111
112 /****************************************************************************/
113
114 void LowPassFilter::update(double input, bool reset)
115 {
116 impl->update(vector<double> {input}, vector<uint8_t> {reset});
117 }
118
119 /****************************************************************************/
120
121 void LowPassFilter::update(
122 const vector<double> &input,
123 const vector<uint8_t> &reset)
124 {
125 impl->update(input, reset);
126 }
127
128 /****************************************************************************/
129
130 void LowPassFilter::update(
131 function<double(unsigned int)> input,
132 function<bool(unsigned int)> reset)
133 {
134 vector<double> inputVector(impl->output.size());
135 vector<uint8_t> resetVector(impl->output.size());
136
137 for (unsigned int i = 0; i < impl->output.size(); i++) {
138 inputVector.at(i) = input(i);
139 resetVector.at(i) = reset(i);
140 }
141
142 impl->update(inputVector, resetVector);
143 }
144
145 /****************************************************************************/
146
147 double LowPassFilter::getOutput(unsigned int i) const
148 {
149 return impl->output.at(i);
150 }
151
152 /****************************************************************************/
153