|
Packit |
67cb25 |
/* histogram/gsl-histogram.c
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
67cb25 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
67cb25 |
* the Free Software Foundation; either version 3 of the License, or (at
|
|
Packit |
67cb25 |
* your option) any later version.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is distributed in the hope that it will be useful, but
|
|
Packit |
67cb25 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
67cb25 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
67cb25 |
* General Public License for more details.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
67cb25 |
* along with this program; if not, write to the Free Software
|
|
Packit |
67cb25 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
Packit |
67cb25 |
*/
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#include <config.h>
|
|
Packit |
67cb25 |
#include <stdio.h>
|
|
Packit |
67cb25 |
#include <stdlib.h>
|
|
Packit |
67cb25 |
#include <getopt.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_histogram.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
void
|
|
Packit |
67cb25 |
print_help(void)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
fprintf (stderr, "Usage: gsl-histogram [-u] xmin xmax [n]\n");
|
|
Packit |
67cb25 |
fprintf (stderr, "Computes a histogram of the data on stdin using n bins from xmin to xmax.\n"
|
|
Packit |
67cb25 |
"If n is unspecified then bins of integer width are used.\n"
|
|
Packit |
67cb25 |
"If -u is given, histogram is normalized so that sum of all bins is unity.\n");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
int
|
|
Packit |
67cb25 |
main (int argc, char **argv)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double a = 0.0, b = 1.0;
|
|
Packit |
67cb25 |
size_t n = 10;
|
|
Packit |
67cb25 |
int unit = 0;
|
|
Packit |
67cb25 |
int c;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
while ((c = getopt(argc, argv, "u")) != (-1))
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
switch (c)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
case 'u':
|
|
Packit |
67cb25 |
unit = 1;
|
|
Packit |
67cb25 |
break;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
default:
|
|
Packit |
67cb25 |
print_help();
|
|
Packit |
67cb25 |
exit(0);
|
|
Packit |
67cb25 |
break;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (argc - optind < 2)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
print_help();
|
|
Packit |
67cb25 |
exit(0);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
a = atof (argv[optind++]);
|
|
Packit |
67cb25 |
b = atof (argv[optind++]);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (argc - optind > 0)
|
|
Packit |
67cb25 |
n = atoi (argv[optind++]);
|
|
Packit |
67cb25 |
else
|
|
Packit |
67cb25 |
n = (int)(b - a) ;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double x;
|
|
Packit |
67cb25 |
gsl_histogram *h = gsl_histogram_alloc (n);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_histogram_set_ranges_uniform (h, a, b);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
while (fscanf(stdin, "%lg", &x) == 1)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_histogram_increment(h, x);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef DISPLAY_STATS
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double mean = gsl_histogram_mean (h);
|
|
Packit |
67cb25 |
double sigma = gsl_histogram_sigma (h);
|
|
Packit |
67cb25 |
fprintf (stdout, "# mean = %g\n", mean);
|
|
Packit |
67cb25 |
fprintf (stdout, "# sigma = %g\n", sigma);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* normalize histogram if needed */
|
|
Packit |
67cb25 |
if (unit)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double sum = gsl_histogram_sum(h);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (sum > 0.0)
|
|
Packit |
67cb25 |
gsl_histogram_scale(h, 1.0 / sum);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_histogram_fprintf (stdout, h, "%g", "%g");
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_histogram_free (h);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return 0;
|
|
Packit |
67cb25 |
}
|