1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/hist.c

242 lines
4.9 KiB
C
Raw Normal View History

/** Histogram functions.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
2015-06-03 10:13:35 +02:00
#include <string.h>
#include <float.h>
#include <math.h>
#include <time.h>
#include "utils.h"
#include "hist.h"
#include "config.h"
#define VAL(h, i) ((h)->low + (i) * (h)->resolution)
#define INDEX(h, v) round((v - (h)->low) / (h)->resolution)
int hist_init(struct hist *h, double low, double high, double resolution)
{
h->low = low;
h->high = high;
h->resolution = resolution;
if (resolution > 0) {
h->length = (high - low) / resolution;
h->data = alloc(h->length * sizeof(hist_cnt_t));
}
else {
h->length = 0;
h->data = NULL;
}
2015-08-07 01:11:43 +02:00
hist_reset(h);
return 0;
}
int hist_destroy(struct hist *h)
{
if (h->data) {
free(h->data);
h->data = NULL;
}
return 0;
}
void hist_put(struct hist *h, double value)
{
int idx = INDEX(h, value);
2015-10-08 10:49:51 +02:00
h->last = value;
2015-08-07 01:11:43 +02:00
/* Update min/max */
if (value > h->highest)
h->highest = value;
if (value < h->lowest)
h->lowest = value;
2015-08-07 01:11:43 +02:00
/* Check bounds and increment */
if (idx >= h->length)
h->higher++;
else if (idx < 0)
h->lower++;
else if (h->data != NULL)
h->data[idx]++;
2015-08-07 01:11:43 +02:00
h->total++;
2015-08-07 01:11:43 +02:00
/* Online / running calculation of variance and mean
* by Donald Knuths Art of Computer Programming, Vol 2, page 232, 3rd edition */
if (h->total == 1) {
h->_m[1] = h->_m[0] = value;
h->_s[1] = 0.0;
}
else {
h->_m[0] = h->_m[1] + (value - h->_m[1]) / h->total;
h->_s[0] = h->_s[1] + (value - h->_m[1]) * (value - h->_m[0]);
2015-08-07 01:11:43 +02:00
// set up for next iteration
2015-08-07 01:11:43 +02:00
h->_m[1] = h->_m[0];
h->_s[1] = h->_s[0];
}
2015-08-07 01:11:43 +02:00
}
void hist_reset(struct hist *h)
{
h->total = 0;
h->higher = 0;
h->lower = 0;
2015-08-07 01:11:43 +02:00
h->highest = DBL_MIN;
h->lowest = DBL_MAX;
2015-08-07 01:11:43 +02:00
if (h->data)
memset(h->data, 0, h->length * sizeof(unsigned));
}
double hist_mean(struct hist *h)
{
return (h->total > 0) ? h->_m[0] : 0.0;
}
double hist_var(struct hist *h)
{
return (h->total > 1) ? h->_s[0] / (h->total - 1) : 0.0;
}
double hist_stddev(struct hist *h)
{
return sqrt(hist_var(h));
}
void hist_print(struct hist *h, int details)
{ INDENT
stats("Counted values: %ju (%ju between %f and %f)", h->total, h->total-h->higher-h->lower, h->high, h->low);
2015-10-13 12:06:50 +02:00
stats("Highest: %f Lowest: %f", h->highest, h->lowest);
stats("Mu: %f Sigma2: %f Sigma: %f", hist_mean(h), hist_var(h), hist_stddev(h));
2015-08-07 01:11:43 +02:00
if (details > 0 && h->total - h->higher - h->lower > 0) {
char *buf = hist_dump(h);
2015-10-13 12:06:50 +02:00
stats("Matlab: %s", buf);
free(buf);
2015-10-13 12:06:50 +02:00
hist_plot(h);
}
}
void hist_plot(struct hist *h)
{
char buf[HIST_HEIGHT];
memset(buf, '#', sizeof(buf));
2015-08-07 01:11:43 +02:00
hist_cnt_t max = 1;
/* Get highest bar */
for (int i = 0; i < h->length; i++) {
if (h->data[i] > max)
max = h->data[i];
}
2015-08-07 01:11:43 +02:00
/* Print plot */
2015-10-13 12:06:50 +02:00
stats("%9s | %5s | %s", "Value", "Count", "Plot");
line();
for (int i = 0; i < h->length; i++) {
2015-10-14 12:18:25 +02:00
double value = VAL(h, i);
hist_cnt_t cnt = h->data[i];
2015-10-14 12:18:25 +02:00
int bar = HIST_HEIGHT * ((double) cnt / max);
2015-08-07 01:11:43 +02:00
2015-10-14 12:18:25 +02:00
if (value > h->lowest || value < h->highest)
stats("%+9g | %5ju | %.*s", value, cnt, bar, buf);
}
}
char * hist_dump(struct hist *h)
{
char *buf = alloc(128);
strcatf(&buf, "[ ");
for (int i = 0; i < h->length; i++)
strcatf(&buf, "%ju ", h->data[i]);
strcatf(&buf, "]");
return buf;
}
2016-10-22 20:38:31 -04:00
#ifdef WITH_JANSSON
json_t * hist_json(struct hist *h)
{
json_t *json_buckets, *json_hist;
json_hist = json_pack("{ s: f, s: f, s: i, s: i, s: i, s: f, s: f, s: f, s: f, s: f }",
2016-10-22 20:38:31 -04:00
"low", h->low,
"high", h->high,
"total", h->total,
"higher", h->higher,
"lower", h->lower,
"highest", h->highest,
"lowest", h->lowest,
"mean", hist_mean(h),
"variance", hist_var(h),
"stddev", hist_stddev(h)
2016-10-22 20:38:31 -04:00
);
if (h->total - h->lower - h->higher > 0) {
json_buckets = json_array();
for (int i = 0; i < h->length; i++)
json_array_append(json_buckets, json_integer(h->data[i]));
json_object_set(json_hist, "buckets", json_buckets);
}
return json_hist;
2016-10-22 20:38:31 -04:00
}
int hist_dump_json(struct hist *h, FILE *f)
{
json_t *j = hist_json(h);
int ret = json_dumpf(j, f, 0);
json_decref(j);
return ret;
}
#endif /* WITH_JANNSON */
int hist_dump_matlab(struct hist *h, FILE *f)
{
fprintf(f, "struct(");
2016-10-22 20:38:31 -04:00
fprintf(f, "'low', %f, ", h->low);
fprintf(f, "'high', %f, ", h->high);
fprintf(f, "'total', %ju, ", h->total);
fprintf(f, "'higher', %ju, ", h->higher);
fprintf(f, "'lower', %ju, ", h->lower);
2016-10-22 20:38:31 -04:00
fprintf(f, "'highest', %f, ", h->highest);
fprintf(f, "'lowest', %f, ", h->lowest);
fprintf(f, "'mean', %f, ", hist_mean(h));
2016-10-22 20:38:31 -04:00
fprintf(f, "'variance', %f, ", hist_var(h));
fprintf(f, "'stddev', %f, ", hist_stddev(h));
if (h->total - h->lower - h->higher > 0) {
char *buf = hist_dump(h);
fprintf(f, "'buckets', %s", buf);
free(buf);
}
else
fprintf(f, "'buckets', zeros(1, %d)", h->length);
fprintf(f, ")");
2016-10-22 20:38:31 -04:00
return 0;
}