home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / ddjmag / ddj8705.arc / HOLUBLST.LST < prev    next >
File List  |  1987-04-10  |  6KB  |  193 lines

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