/Users/lyon/j4p/src/math/Stats.java

1    package math; 
2     
3    import futils.LineProcessor; 
4    import futils.PolymorphicProcessor; 
5     
6     
7    /** 
8     * GPL code by DocJava, Inc. 
9     * User: lyon 
10    * Date: Mar 5, 2003 
11    * Time: 4:33:57 PM 
12    *  
13    */ 
14   public class Stats  { 
15       // k = number of intensities 
16       private static final int k = 256; 
17       private double PMF[] = new double[k]; 
18       private double[] CMF = new double[k]; 
19       public static int count = 0; 
20       public static int sum = 0; 
21    
22    
23       public double[] getPMF() { 
24           return PMF; 
25       } 
26    
27       public double[] getCMF() { 
28           return CMF; 
29       } 
30    
31       protected void computeCMF() { 
32           double cumulative = 0; 
33           for (int i = 0; i < getPMF().length; i++) { 
34               cumulative = cumulative + getPMF()[i]; 
35               CMF[i] = cumulative; 
36           } 
37       } 
38    
39       public void printPMF() { 
40           System.out.println("PMF"); 
41           for (int i = 0; i < getPMF().length; i++) { 
42               System.out.println(getPMF()[i]); 
43           } 
44       } 
45    
46       public void printCMF() { 
47           System.out.println("CMF"); 
48           for (int i = 0; i < CMF.length; i++) { 
49               System.out.println(CMF[i]); 
50           } 
51       } 
52    
53       public void initStats(short plane[][]) { 
54           int width = plane.length; 
55           int height = plane[0].length; 
56           int total = 0; 
57           for (int i = 0; i < width; i++) 
58               for (int j = 0; j < height; j++) { 
59                   getPMF()[plane[i][j] & 0xFF]++; 
60                   total++; 
61               } 
62           // Normalize 
63           for (int i = 0; i < 256; i++) 
64               getPMF()[i] = (getPMF()[i] / total); 
65           computeCMF(); 
66       } 
67    
68       public void setPMF(double[] PMF) { 
69           this.PMF = PMF; 
70       } 
71    
72       public static void newNumber(String s){ 
73           count++; 
74           int i = Integer.parseInt(s); 
75           sum = sum + i; 
76       } 
77    
78       public static void averageNumbers() { 
79           new PolymorphicProcessor( 
80                   new LineProcessor() { 
81                       public void process(String s) { 
82                           newNumber(s); 
83                       } 
84                   } 
85           ); 
86           System.out.println("avg="+sum/count); 
87    
88       } 
89    
90       public static void main(String args[]){ 
91           averageNumbers(); 
92       } 
93   } 
94