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/decimate.c

97 lines
2.2 KiB
C
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** Decimate hook.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, 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.h>
#include <villas/plugin.h>
struct decimate {
int ratio;
unsigned counter;
};
static int decimate_init(struct hook *h)
{
struct decimate *p = (struct decimate *) h->_vd;
p->counter = 0;
return 0;
}
static int decimate_parse(struct hook *h, json_t *cfg)
{
struct decimate *p = (struct decimate *) h->_vd;
int ret;
json_error_t err;
ret = json_unpack_ex(cfg, &err, 0, "{ s: i }",
"ratio", &p->ratio
);
if (ret)
jerror(&err, "Failed to parse configuration of hook '%s'", plugin_name(h->_vt));
return 0;
}
static int decimate_read(struct hook *h, struct sample *smps[], unsigned *cnt)
{
struct decimate *p = (struct decimate *) h->_vd;
int i, ok;
for (i = 0, ok = 0; i < *cnt; i++) {
if (p->counter++ % p->ratio == 0) {
struct sample *tmp;
tmp = smps[ok];
smps[ok++] = smps[i];
smps[i] = tmp;
}
}
*cnt = ok;
return 0;
}
static struct plugin p = {
.name = "decimate",
.description = "Downsamping by integer factor",
.type = PLUGIN_TYPE_HOOK,
.hook = {
.flags = HOOK_NODE | HOOK_PATH,
.priority = 99,
.init = decimate_init,
.parse = decimate_parse,
.read = decimate_read,
.process= decimate_read,
.size = sizeof(struct decimate)
}
};
REGISTER_PLUGIN(&p)
/** @} */