2017-03-05 10:06:32 -04:00
|
|
|
|
/** Decimate hook.
|
|
|
|
|
*
|
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
|
|
/** @addtogroup hooks Hook functions
|
|
|
|
|
* @{
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "hook.h"
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
|
|
static int hook_decimate(struct hook *h, int when, struct hook_info *j)
|
|
|
|
|
{
|
|
|
|
|
struct {
|
|
|
|
|
unsigned ratio;
|
|
|
|
|
unsigned counter;
|
|
|
|
|
} *private = hook_storage(h, when, sizeof(*private), NULL, NULL);
|
|
|
|
|
|
|
|
|
|
switch (when) {
|
|
|
|
|
case HOOK_PARSE:
|
|
|
|
|
if (!h->parameter)
|
2017-03-07 07:12:24 -04:00
|
|
|
|
error("Missing parameter for hook: '%s'", plugin_name(h));
|
2017-03-05 10:06:32 -04:00
|
|
|
|
|
|
|
|
|
private->ratio = strtol(h->parameter, NULL, 10);
|
|
|
|
|
if (!private->ratio)
|
|
|
|
|
error("Invalid parameter '%s' for hook 'decimate'", h->parameter);
|
|
|
|
|
|
|
|
|
|
private->counter = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case HOOK_READ:
|
|
|
|
|
assert(j->smps);
|
|
|
|
|
|
|
|
|
|
int i, ok;
|
|
|
|
|
for (i = 0, ok = 0; i < j->cnt; i++) {
|
|
|
|
|
if (private->counter++ % private->ratio == 0) {
|
|
|
|
|
struct sample *tmp;
|
|
|
|
|
|
|
|
|
|
tmp = j->smps[ok];
|
|
|
|
|
j->smps[ok++] = j->smps[i];
|
|
|
|
|
j->smps[i] = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct plugin p = {
|
|
|
|
|
.name = "decimate",
|
|
|
|
|
.description = "Downsamping by integer factor",
|
|
|
|
|
.type = PLUGIN_TYPE_HOOK,
|
|
|
|
|
.hook = {
|
|
|
|
|
.priority = 99,
|
|
|
|
|
.history = 0,
|
|
|
|
|
.cb = hook_decimate,
|
|
|
|
|
.type = HOOK_STORAGE | HOOK_DESTROY | HOOK_READ
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
REGISTER_PLUGIN(&p)
|
|
|
|
|
|
|
|
|
|
/** @} */
|