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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
2.9 KiB
C++
Raw Permalink Normal View History

/* RMS hook.
2021-06-18 13:24:49 +02:00
*
* Author: Manuel Pitz <manuel.pitz@eonerc.rwth-aachen.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
2021-06-18 13:24:49 +02:00
#include <villas/hook.hpp>
#include <villas/sample.hpp>
2021-06-18 13:24:49 +02:00
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-06-18 13:24:49 +02:00
std::vector<double> accumulator;
unsigned windowSize;
uint64_t smpMemoryPosition;
2021-06-18 13:24:49 +02:00
public:
RMSHook(Path *p, Node *n, int fl, int prio, bool en = true)
: MultiSignalHook(p, n, fl, prio, en), smpMemory(), windowSize(0),
smpMemoryPosition(0) {}
2021-06-18 13:24:49 +02:00
virtual void prepare() {
MultiSignalHook::prepare();
2021-06-18 13:24:49 +02:00
// Add signals
for (auto index : signalIndices) {
auto origSig = signals->getByIndex(index);
2021-06-18 13:24:49 +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 memory for each channel*/
smpMemory.clear();
for (unsigned i = 0; i < signalIndices.size(); i++) {
accumulator.push_back(0.0);
smpMemory.emplace_back(windowSize, 0.0);
}
2021-06-18 13:24:49 +02:00
state = State::PREPARED;
}
2021-06-18 13:24:49 +02:00
virtual void parse(json_t *json) {
int ret;
int windowSizeIn;
json_error_t err;
2021-06-18 13:24:49 +02:00
assert(state != State::STARTED);
2021-06-18 13:24:49 +02:00
MultiSignalHook::parse(json);
2021-06-18 13:24:49 +02:00
ret =
json_unpack_ex(json, &err, 0, "{ s: i }", "window_size", &windowSizeIn);
if (ret)
throw ConfigError(json, err, "node-config-hook-rms");
2021-06-18 13:24:49 +02:00
if (windowSizeIn < 1)
throw ConfigError(json, "node-config-hook-rms",
"Window size must be greater 0 but is set to {}",
windowSizeIn);
2022-03-28 10:32:28 +02:00
windowSize = (unsigned)windowSizeIn;
2022-03-28 10:32:28 +02:00
state = State::PARSED;
}
2021-06-18 13:24:49 +02:00
virtual Hook::Reason process(struct Sample *smp) {
assert(state == State::STARTED);
2021-06-18 13:24:49 +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
// Get the old value from the history
double oldValue = smpMemory[i][smpMemoryPosition % windowSize];
// Append the new value to the history memory
smpMemory[i][smpMemoryPosition % windowSize] = newValue;
2021-06-18 13:24:49 +02:00
// Update the accumulator
accumulator[index] += newValue;
accumulator[index] -= oldValue;
2021-06-18 13:24:49 +02:00
auto rms = pow(accumulator[index] / windowSize, 0.5);
2021-06-18 13:24:49 +02:00
smp->data[index].f = rms;
i++;
}
2021-06-18 13:24:49 +02:00
smpMemoryPosition++;
2021-06-18 13:24:49 +02:00
return Reason::OK;
}
2021-06-18 13:24:49 +02:00
};
// Register hook
2021-06-18 13:24:49 +02:00
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;
2021-06-18 13:24:49 +02:00
} // namespace node
} // namespace villas