2021-07-06 16:28:31 +02:00
|
|
|
/** Moving average filter.
|
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.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-07-06 16:28:31 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <villas/hook.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/sample.hpp>
|
2021-07-06 16:28:31 +02:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
class MovingAverageHook : public MultiSignalHook {
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::vector<std::vector<double>> smpMemory;
|
|
|
|
|
|
|
|
double accumulator;
|
|
|
|
unsigned windowSize;
|
|
|
|
uint64_t smpMemoryPosition;
|
|
|
|
|
|
|
|
public:
|
2021-08-10 10:12:48 -04:00
|
|
|
MovingAverageHook(Path *p, Node *n, int fl, int prio, bool en = true) :
|
2021-07-06 16:28:31 +02:00
|
|
|
MultiSignalHook(p, n, fl, prio, en),
|
|
|
|
smpMemory(),
|
|
|
|
accumulator(0.0),
|
2021-08-10 10:12:48 -04:00
|
|
|
windowSize(10),
|
2021-07-06 16:28:31 +02:00
|
|
|
smpMemoryPosition(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual
|
|
|
|
void prepare()
|
|
|
|
{
|
|
|
|
MultiSignalHook::prepare();
|
|
|
|
|
|
|
|
/* Add signals */
|
|
|
|
for (auto index : signalIndices) {
|
2021-08-10 10:12:48 -04:00
|
|
|
auto origSig = signals->getByIndex(index);
|
2021-07-06 16:28:31 +02:00
|
|
|
|
|
|
|
/* Check that signal has float type */
|
|
|
|
if (origSig->type != SignalType::FLOAT)
|
2021-07-06 18:41:03 +02:00
|
|
|
throw RuntimeError("The ma hook can only operate on signals of type float!");
|
2021-07-06 16:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize sample memory */
|
|
|
|
smpMemory.clear();
|
|
|
|
for (unsigned i = 0; i < signalIndices.size(); i++)
|
|
|
|
smpMemory.emplace_back(windowSize, 0.0);
|
|
|
|
|
|
|
|
state = State::PREPARED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual
|
|
|
|
void parse(json_t *json)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
assert(state != State::STARTED);
|
|
|
|
|
|
|
|
MultiSignalHook::parse(json);
|
|
|
|
|
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: i }",
|
|
|
|
"window_size", &windowSize
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-hook-rms");
|
|
|
|
|
|
|
|
state = State::PARSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual
|
2021-08-10 10:12:48 -04:00
|
|
|
Hook::Reason process(struct Sample *smp)
|
2021-07-06 16:28:31 +02:00
|
|
|
{
|
|
|
|
assert(state == State::STARTED);
|
|
|
|
|
|
|
|
unsigned i = 0;
|
|
|
|
for (auto index : signalIndices) {
|
|
|
|
/* The new value */
|
|
|
|
double newValue = smp->data[index].f;
|
|
|
|
|
|
|
|
/* Append the new value to the history memory */
|
|
|
|
smpMemory[i][smpMemoryPosition % windowSize] = newValue;
|
|
|
|
|
|
|
|
/* Get the old value from the history */
|
|
|
|
double oldValue = smpMemory[i][(smpMemoryPosition + 1) % windowSize];
|
|
|
|
|
|
|
|
/* Update the accumulator */
|
|
|
|
accumulator += newValue;
|
|
|
|
accumulator -= oldValue;
|
|
|
|
|
|
|
|
smp->data[index].f = accumulator / windowSize;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
smpMemoryPosition++;
|
|
|
|
|
|
|
|
return Reason::OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Register hook */
|
|
|
|
static char n[] = "ma";
|
|
|
|
static char d[] = "A simple moving average filter over a fixed number of past samples";
|
|
|
|
static HookPlugin<MovingAverageHook, n, d, (int) Hook::Flags::NODE_READ | (int) Hook::Flags::NODE_WRITE | (int) Hook::Flags::PATH> p;
|
|
|
|
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|