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

119 lines
2.7 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>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license 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;
std::vector<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(Path *p, Node *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(),
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) {
auto origSig = signals->getByIndex(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 memory for each channel*/
2021-06-18 13:24:49 +02:00
smpMemory.clear();
for (unsigned i = 0; i < signalIndices.size(); i++){
accumulator.push_back(0.0);
2021-07-06 18:41:03 +02:00
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;
2022-03-28 10:32:28 +02:00
int windowSizeIn;
2021-06-18 13:24:49 +02:00
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
2022-03-28 10:32:28 +02:00
ret = json_unpack_ex(json, &err, 0, "{ s: i }",
"window_size", &windowSizeIn
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
2022-03-28 10:32:28 +02:00
if (windowSizeIn < 1)
throw ConfigError(json, "node-config-hook-rms", "Window size must be greater 0 but is set to {}", windowSizeIn);
windowSize = (unsigned)windowSizeIn;
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
/* Get the old value from the history */
double oldValue = smpMemory[i][smpMemoryPosition % windowSize];
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
/* 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
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 */