1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/hooks/rms.cpp

133 lines
3.2 KiB
C++
Raw Permalink Normal View History

2021-06-18 13:24:49 +02:00
/** RMS hook.
*
* @author Manuel Pitz <manuel.pitz@eonerc.rwth-aachen.de>
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
/** @addtogroup hooks Hook functions
* @{
*/
#include <villas/hook.hpp>
#include <villas/sample.h>
namespace villas {
namespace node {
2021-07-06 18:41:03 +02:00
class RMSHook : public MultiSignalHook {
2021-06-18 13:24:49 +02:00
protected:
std::vector<std::vector<double>> smpMemory;
2021-07-06 18:41:03 +02:00
double accumulator;
2021-06-18 13:24:49 +02:00
unsigned windowSize;
2021-07-06 18:41:03 +02:00
uint64_t smpMemoryPosition;
2021-06-18 13:24:49 +02:00
public:
RMSHook(struct vpath *p, struct vnode *n, int fl, int prio, bool en = true) :
2021-07-06 18:41:03 +02:00
MultiSignalHook(p, n, fl, prio, en),
2021-06-18 13:24:49 +02:00
smpMemory(),
2021-07-06 18:41:03 +02:00
accumulator(0.0),
2021-06-18 13:24:49 +02:00
windowSize(0),
2021-07-06 18:41:03 +02:00
smpMemoryPosition(0)
2021-06-18 13:24:49 +02:00
{ }
2021-07-06 18:41:03 +02:00
virtual
void prepare()
2021-06-18 13:24:49 +02:00
{
2021-07-06 18:41:03 +02:00
MultiSignalHook::prepare();
2021-06-18 13:24:49 +02:00
2021-07-06 18:41:03 +02:00
/* Add signals */
for (auto index : signalIndices) {
2021-07-07 08:40:15 +02:00
auto *origSig = (struct signal *) vlist_at(&signals, index);
2021-06-18 13:24:49 +02:00
2021-07-06 18:41:03 +02:00
/* Check that signal has float type */
if (origSig->type != SignalType::FLOAT)
throw RuntimeError("The rms hook can only operate on signals of type float!");
2021-06-18 13:24:49 +02:00
}
/* Initialize sample memory */
smpMemory.clear();
2021-07-06 18:41:03 +02:00
for (unsigned i = 0; i < signalIndices.size(); i++)
smpMemory.emplace_back(windowSize, 0.0);
2021-06-18 13:24:49 +02:00
state = State::PREPARED;
}
2021-07-06 18:41:03 +02:00
virtual
void parse(json_t *json)
2021-06-18 13:24:49 +02:00
{
int ret;
json_error_t err;
assert(state != State::STARTED);
2021-07-06 18:41:03 +02:00
MultiSignalHook::parse(json);
2021-06-18 13:24:49 +02:00
2021-07-06 18:41:03 +02:00
ret = json_unpack_ex(json, &err, 0, "{ s?: i }",
"window_size", &windowSize
2021-06-18 13:24:49 +02:00
);
if (ret)
2021-07-06 18:41:03 +02:00
throw ConfigError(json, err, "node-config-hook-rms");
2021-06-18 13:24:49 +02:00
state = State::PARSED;
}
2021-07-06 18:41:03 +02:00
virtual
Hook::Reason process(struct sample *smp)
2021-06-18 13:24:49 +02:00
{
assert(state == State::STARTED);
2021-07-06 18:41:03 +02:00
unsigned i = 0;
for (auto index : signalIndices) {
/* Square the new value */
double newValue = pow(smp->data[index].f, 2);
2021-06-18 13:24:49 +02:00
2021-07-06 18:41:03 +02:00
/* Append the new value to the history memory */
smpMemory[i][smpMemoryPosition % windowSize] = newValue;
2021-06-18 13:24:49 +02:00
2021-07-06 18:41:03 +02:00
/* Get the old value from the history */
double oldValue = smpMemory[i][(smpMemoryPosition + 1) % windowSize];
2021-06-18 13:24:49 +02:00
2021-07-06 18:41:03 +02:00
/* Update the accumulator */
accumulator += newValue;
accumulator -= oldValue;
2021-06-18 13:24:49 +02:00
2021-07-06 18:41:03 +02:00
auto rms = pow(accumulator / windowSize, 0.5);
2021-06-18 13:24:49 +02:00
2021-07-06 18:41:03 +02:00
smp->data[index].f = rms;
i++;
2021-06-18 13:24:49 +02:00
}
2021-07-06 18:41:03 +02:00
smpMemoryPosition++;
2021-06-18 13:24:49 +02:00
2021-07-06 18:41:03 +02:00
return Reason::OK;
2021-06-18 13:24:49 +02:00
}
};
/* Register hook */
static char n[] = "rms";
static char d[] = "This hook calculates the root-mean-square (RMS) on a window";
static HookPlugin<RMSHook, n, d, (int) Hook::Flags::NODE_READ | (int) Hook::Flags::NODE_WRITE | (int) Hook::Flags::PATH> p;
} /* namespace node */
} /* namespace villas */
/** @} */