home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 June
/
SIMTEL_0692.cdr
/
msdos
/
ddjmag
/
ddj8705.arc
/
HOLUBLST.LST
< prev
next >
Wrap
File List
|
1987-04-10
|
6KB
|
193 lines
Listing 1 -- stat.c
1| /*----------------------------------------------------------
2| * STAT.C Statistics routines:
3| *
4| * newsample( n ) Add a new sample to the mean/average
5| * totals.
6| * running_mean() Returns the running mean of the samples.
7| * true_mean() Returns the true mean of the samples.
8| * deviation() Returns the standard deviation from the
9| * running mean.
10| * reset_mean(n) Resets everyting to 0. 'n' is the boxcar
11| * length that will be used for subsequent
12| * samples. If n is 0, the default length of
13| * 4 is used.
14| */
15|
16| #define DEF_BOXLEN 4
17|
18| static unsigned long Average = 0;
19| static unsigned long Numnums = 0;
20| static unsigned long Mean_total = 0;
21| static unsigned long Mean = 0;
22| static unsigned long Dev_total = 0;
23| static unsigned long Dev = 0;
24| static unsigned int Boxlen = DEF_BOXLEN ;
25|
26| /*--------------------------------------------------------*/
27|
28| void newsample( n )
29| {
30| /* Add a new point into the various mean and deviation
31| * variables.
32| */
33|
34| register unsigned long dif;
35|
36| Average += n;
37| Numnums++;
38|
39| Mean_total -= Mean ; /* find running mean */
40| Mean_total += n;
41| Mean = Mean_total >> Boxlen;
42|
43| dif = abs( Mean - n ); /* Distance to point */
44| dif *= dif; /* square it. */
45|
46| Dev_total -= Dev; /* find average *
47| Dev_total += dif; /* difference */
48| Dev = Dev_total >> Boxlen;
49| }
50|
51| /*--------------------------------------------------------*/
52|
53| int running_mean() /* Return the current running mean */
54| {
55| return Mean;
56| }
57|
58| /*--------------------------------------------------------*/
59|
60| int true_mean() /* Return the current true mean */
61| {
62| return Average / Numnums;
63| }
64|
65| /*--------------------------------------------------------*/
66|
67| int deviation() /* Return the current standard */
68| { /* deviation from the running mean. */
69|
70| extern double sqrt();
71| return (int) sqrt( (double)Dev );
72| }
73|
74| /*--------------------------------------------------------*/
75|
76| void reset_mean( boxcar_val )
77| {
78| /* Reset various global variables to their initial
79| * values, "boxcar_val" is used for the boxcar
80| * width. It is a shift value, not a true width. If
81| * it's 0, the default value of 4 is used instead.
82| */
83|
84| Average = 0;
85| Numnums = 0;
86| Mean_total = 0;
87| Mean = 0;
88| Dev_total = 0;
89| Dev = 0;
90| Boxlen = boxcar_val ? boxcar_val : DEF_BOXLEN ;
91| }
92|
93| /*--------------------------------------------------------*/
94|
95| #ifdef MAIN
96|
97| #define NUMSAMPLES 50
98|
99| test( how )
100| {
101| /* how = 0 Straight line.
102| * how = 1 Triangle
103| * how = 2 Random
104| */
105|
106| int i, j, count, m, a, d, dir = 1 ;
107|
108| for( count = 0; count += dir; )
109| {
110| if( count == NUMSAMPLES )
111| dir = -1;
112|
113| newsample( i = (( how == 2 ) ? (rand() % NUMSAMPLES) :
114| ( how == 1 ) ? (count ) :
115| (NUMSAMPLES / 2 )));
116| m = running_mean();
117| d = deviation();
118| a = true_mean();
119|
120| for( j = 1; j <= NUMSAMPLES; j++ )
121| {
122| if( j>i && j>m && j>a )
123| break;
124|
125| if ( j == i ) printf("*");
126| else if ( j == m ) printf("m");
127| else if ( j == a ) printf("a");
128| else printf(" ");
129| }
130| printf("\n");
131| }
132| }
133|
134| /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - */
135|
136| main()
137| {
138| char buf[80];
139| int how;
140|
141| reset_mean( 4 );
142| test( 0 );
143| printf("Straight line with length 16 boxcar\n\f");
144|
145| reset_mean( 2 );
146| test( 1 );
147| test( 1 );
148| printf("Triangle wave with length 4 boxcar\n\f");
149|
150| reset_mean( 4 );
151| test( 1 );
152| test( 1 );
153| printf("Triangle wave with length 16 boxcar\n\f");
154|
155| reset_mean( 6 );
156| test( 1 );
157| test( 1 );
158| printf("Triangle wave with length 64 boxcar\n\f");
159|
160| reset_mean( 2 );
161| test( 2 );
162| printf("Random input with length 4 boxcar\n\f");
163|
164| reset_mean( 4 );
165| test( 2 );
166| printf("Random input with length 16 boxcar\n\f");
167|
168| reset_mean( 6 );
169| test( 2 );
170| test( 2 );
171| printf("Random input with length 64 boxcar\n\f");
172|
173| #ifdef NEVER
174|
175| while( 1 )
176| {
177| printf( "triangle, random, or linear (r/t/l)?" );
178| gets( buf );
179|
180| how = ( *buf == 'r' ) ? 2 : ( *buf == 't' ) ? 1 : 0;
181|
182| printf("Boxcar length? ");
183| gets( buf );
184| test( atoi(buf), how );
185| }
186|
187| #endif
188| }
189|
190| #endif