source: SH_SHM/trunk/util/stddev.c @ 92

Revision 16, 2.6 KB checked in by marcus, 15 years ago (diff)

r1 | svn | 2007-12-13 11:10:29 +0100 (Do, 13 Dez 2007) | 2 lines

Initial import

Line 
1
2/* file stddev.c
3 *      ========
4 *
5 * version 3, 21-Nov-2005
6 *
7 * Retuns mean value and standard deviation of a series of numbers in a file
8 */
9
10
11/*
12 *
13 *  SeismicHandler, seismic analysis software
14 *  Copyright (C) 1996,  Klaus Stammler, Federal Institute for Geosciences
15 *                                       and Natural Resources (BGR), Germany
16 *
17 *  This program is free software; you can redistribute it and/or modify
18 *  it under the terms of the GNU General Public License as published by
19 *  the Free Software Foundation; either version 2 of the License, or
20 *  (at your option) any later version.
21 *
22 *  This program is distributed in the hope that it will be useful,
23 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 *  GNU General Public License for more details.
26 *
27 *  You should have received a copy of the GNU General Public License
28 *  along with this program; if not, write to the Free Software
29 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
30 *
31 */
32
33
34#include <stdio.h>
35#include <string.h>
36#include <math.h>
37
38
39#define NAMELTH 132
40#define MAXNUM 5000
41
42
43int main( int argc, char *argv[] )
44{
45        /* local variables */
46        static float buf[MAXNUM];      /* number buffer */
47        char     fname[NAMELTH+1];     /* name of input file */
48        FILE     *fp;                  /* pointer to input file */
49        char     line[NAMELTH+1];      /* current line */
50        float    num;                  /* current number */
51        int      lth;                  /* number of numbers in file */
52        float    mean;                 /* mean value */
53        float    dev;                  /* standard deviation */
54        float    sum;                  /* sum */
55        int      i;                    /* counter */
56
57        /* executable code */
58
59        if  (argc != 2)  {
60                fprintf( stderr, "Usage: %s <file>\n", argv[0] );
61                return 1;
62        } /*endif*/
63
64        strcpy( fname, argv[1] );
65        if  (strcmp(fname,"-") == 0)  {
66                fp = stdin;
67        } else {
68                fp = fopen( fname, "r" );
69                if  (fp == NULL)  {
70                        fprintf( stderr, "%s: error opening file %s\n", argv[0], fname );
71                        return 1;
72                } /*endif*/
73        } /*endif*/
74
75        lth = 0;
76        sum = 0.0;
77        while  (fgets(line,NAMELTH,fp) != NULL)  {
78                sscanf( line, "%f", &num );
79                sum += num;
80                if  (lth == MAXNUM)  {printf( "MAXNUM 500\n" ); return 1;}
81                buf[lth++] = num;
82        } /*endif*/
83        mean = sum / (float)lth;
84
85        sum = 0.0;
86        i = 0;
87        for  (i=0; i<lth; i++)  {
88                num = buf[i] - mean;
89                sum += num*num;
90        } /*endfor*/
91        dev = sqrt(sum/(float)(lth-1));
92
93        printf( "%d numbers found   mean: %f   stddev %f\n", lth, mean, dev );
94
95        if  (fp != stdin)
96                fclose( fp );
97
98        return 0;
99
100} /* end of main */
Note: See TracBrowser for help on using the repository browser.