|
Packit |
9c3e7e |
/**
|
|
Packit |
9c3e7e |
* @file stats.c
|
|
Packit |
9c3e7e |
* @note Copyright (C) 2013 Miroslav Lichvar <mlichvar@redhat.com>
|
|
Packit |
9c3e7e |
*
|
|
Packit |
9c3e7e |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
9c3e7e |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
9c3e7e |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit |
9c3e7e |
* (at your option) any later version.
|
|
Packit |
9c3e7e |
*
|
|
Packit |
9c3e7e |
* This program is distributed in the hope that it will be useful,
|
|
Packit |
9c3e7e |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
9c3e7e |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
9c3e7e |
* GNU General Public License for more details.
|
|
Packit |
9c3e7e |
*
|
|
Packit |
9c3e7e |
* You should have received a copy of the GNU General Public License along
|
|
Packit |
9c3e7e |
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
Packit |
9c3e7e |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
Packit |
9c3e7e |
*/
|
|
Packit |
9c3e7e |
#include <math.h>
|
|
Packit |
9c3e7e |
#include <string.h>
|
|
Packit |
9c3e7e |
#include <stdlib.h>
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
#include "stats.h"
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
struct stats {
|
|
Packit |
9c3e7e |
unsigned int num;
|
|
Packit |
9c3e7e |
double min;
|
|
Packit |
9c3e7e |
double max;
|
|
Packit |
9c3e7e |
double mean;
|
|
Packit |
9c3e7e |
double sum_sqr;
|
|
Packit |
9c3e7e |
double sum_diff_sqr;
|
|
Packit |
9c3e7e |
};
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
struct stats *stats_create(void)
|
|
Packit |
9c3e7e |
{
|
|
Packit |
9c3e7e |
struct stats *stats;
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
stats = calloc(1, sizeof *stats);
|
|
Packit |
9c3e7e |
return stats;
|
|
Packit |
9c3e7e |
}
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
void stats_destroy(struct stats *stats)
|
|
Packit |
9c3e7e |
{
|
|
Packit |
9c3e7e |
free(stats);
|
|
Packit |
9c3e7e |
}
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
void stats_add_value(struct stats *stats, double value)
|
|
Packit |
9c3e7e |
{
|
|
Packit |
9c3e7e |
double old_mean = stats->mean;
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
if (!stats->num || stats->max < value)
|
|
Packit |
9c3e7e |
stats->max = value;
|
|
Packit |
9c3e7e |
if (!stats->num || stats->min > value)
|
|
Packit |
9c3e7e |
stats->min = value;
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
stats->num++;
|
|
Packit |
9c3e7e |
stats->mean = old_mean + (value - old_mean) / stats->num;
|
|
Packit |
9c3e7e |
stats->sum_sqr += value * value;
|
|
Packit |
9c3e7e |
stats->sum_diff_sqr += (value - old_mean) * (value - stats->mean);
|
|
Packit |
9c3e7e |
}
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
unsigned int stats_get_num_values(struct stats *stats)
|
|
Packit |
9c3e7e |
{
|
|
Packit |
9c3e7e |
return stats->num;
|
|
Packit |
9c3e7e |
}
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
int stats_get_result(struct stats *stats, struct stats_result *result)
|
|
Packit |
9c3e7e |
{
|
|
Packit |
9c3e7e |
if (!stats->num)
|
|
Packit |
9c3e7e |
return -1;
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
result->min = stats->min;
|
|
Packit |
9c3e7e |
result->max = stats->max;
|
|
Packit |
9c3e7e |
result->max_abs = stats->max > -stats->min ? stats->max : -stats->min;
|
|
Packit |
9c3e7e |
result->mean = stats->mean;
|
|
Packit |
9c3e7e |
result->rms = sqrt(stats->sum_sqr / stats->num);
|
|
Packit |
9c3e7e |
result->stddev = sqrt(stats->sum_diff_sqr / stats->num);
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
return 0;
|
|
Packit |
9c3e7e |
}
|
|
Packit |
9c3e7e |
|
|
Packit |
9c3e7e |
void stats_reset(struct stats *stats)
|
|
Packit |
9c3e7e |
{
|
|
Packit |
9c3e7e |
memset(stats, 0, sizeof *stats);
|
|
Packit |
9c3e7e |
}
|