KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > wavelet > FWTCoefMath


1 package JSci.maths.wavelet;
2
3 import JSci.maths.wavelet.*;
4 import JSci.maths.*;
5
6 /****************************************************
7 * This class allows to do some operations
8 * on wavelet coefficients
9 * @author Daniel Lemire
10 *****************************************/

11 public final class FWTCoefMath extends AbstractMath {
12   private FWTCoefMath () {}
13
14     public static boolean areCompatible(FWTCoef a,FWTCoef b) {
15         if(a.getJ()!=b.getJ()) {
16             return(false);
17         }
18         for(int i=0;i<a.getJ();i++) {
19             if(a.dimension(i)!=b.dimension(i)) {
20                 return(false);
21             }
22         }
23         return(true);
24     }
25
26
27     public static boolean areCompatible(FWTCoef[] a) {
28         if(a.length==1) return(true);
29         for(int i=0;i<a.length-1;i++) {
30             if(areCompatible(a[i],a[i+1])==false) {
31                 return(false);
32             }
33         }
34         return(true);
35     }
36     public static int getJ (FWTCoef[] a) {
37         if (areCompatible(a)==false) {
38             throw new IllegalArgumentException JavaDoc("The objects are not compatible.");
39         }
40         return(a[0].getJ());
41     }
42     /************************************
43   * Compute the sum of the squares of
44   * the coefficients
45     *************************************/

46     public static double sumSquares(FWTCoef[] a,int i) {
47         if (areCompatible(a)==false) {
48             throw new IllegalArgumentException JavaDoc("The objects are not compatible.");
49         }
50         if((i<0)||(i>=getJ(a))) {
51             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(getJ(a)-1));
52         }
53         double ans=0.0;
54         for(int k=0;k<a.length;k++) {
55             ans+=a[k].sumSquares(i);
56         }
57         return(ans);
58     }
59
60     public static double variance(FWTCoef[] a, int i) {
61         if (areCompatible(a)==false) {
62             throw new IllegalArgumentException JavaDoc("The objects are not compatible.");
63         }
64         if((i<0)||(i>=getJ(a))) {
65             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(getJ(a)-1));
66         }
67         double ans=0.0;
68         for(int k=0;k<a.length;k++) {
69             ans+=a[k].variance(i);
70         }
71         return(ans);
72     }
73
74     public static double sumEnergies(FWTCoef[] a) {
75         if (areCompatible(a)==false) {
76             throw new IllegalArgumentException JavaDoc("The Objects of type FWTCoef are not compatible.");
77         }
78         if(getJ(a)<=1) {
79             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
80         }
81         double ans=0.0;
82         for(int k=0;k<a.length;k++) {
83             ans+=a[k].sumEnergies();
84         }
85         return(ans);
86     }
87
88     public static double entropy(FWTCoef[] a, int i) {
89         if (areCompatible(a)==false) {
90             throw new IllegalArgumentException JavaDoc("The Objects of type FWTCoef are not compatible.");
91         }
92         if((i<0)||(i>=getJ(a))) {
93             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(getJ(a)-1));
94         }
95         double sumEnergies=sumSquares(a,i);
96         int nombreDeCoefficients=a[0].coefs[i].length;
97         double[] energyRatio=new double[nombreDeCoefficients];
98         int pos=0;
99         for(int l=0;l<a[0].coefs[i].length;l++) {
100             for(int m=0;m<a.length;m++) {
101                     energyRatio[pos]+=(a[m].coefs[i][l])*(a[m].coefs[i][l])/sumEnergies;
102             }
103             pos++;
104         }
105         return(EngineerMath.icf(energyRatio));
106     }
107
108     public static double entropy(FWTCoef[] a) {
109         if (areCompatible(a)==false) {
110             throw new IllegalArgumentException JavaDoc("The Objects of type FWTCoef are not compatible.");
111         }
112         if(getJ(a)<=1) {
113             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
114         }
115         double sumEnergies=sumEnergies(a);
116         int nombreDeCoefficients=0;
117         for(int k=1;k<a[0].coefs.length;k++) {
118             nombreDeCoefficients+=a[0].coefs[k].length;
119         }
120         double[] energyRatio=new double[nombreDeCoefficients];
121         int pos=0;
122         for(int k=1;k<a[0].coefs.length;k++) {
123             for(int l=0;l<a[0].coefs[k].length;l++) {
124                 for(int m=0;m<a.length;m++) {
125                         energyRatio[pos]+=(a[m].coefs[k][l])*(a[m].coefs[k][l])/sumEnergies;
126                 }
127                 pos++;
128             }
129         }
130         return(EngineerMath.icf(energyRatio));
131     }
132
133     public static double sumVariance(FWTCoef[] a) {
134         if (areCompatible(a)==false) {
135             throw new IllegalArgumentException JavaDoc("The objects are not compatible");
136         }
137         if(getJ(a)<=1) {
138             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
139         }
140         double ans=0.0;
141         for(int k=0;k<a.length;k++) {
142             ans+=a[k].sumVariance();
143         }
144         return(ans);
145     }
146
147     public static double energyRatio(FWTCoef[] a, int i) {
148         if (areCompatible(a)==false) {
149             throw new IllegalArgumentException JavaDoc("The Objects of type FWTCoef are not compatible.");
150         }
151         if(getJ(a)<=1) {
152             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
153         }
154         if((i<1)||(i>=getJ(a))) {
155             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(getJ(a)-1));
156         }
157         if(sumEnergies(a)==0) {
158             if(getJ(a)!=0) {
159                 return(1/getJ(a));
160             } else {
161                 throw new IllegalArgumentException JavaDoc("No energy!");
162             }
163         }
164         return(sumSquares(a,i)/sumEnergies(a));
165     }
166
167     public static double varianceRatio(FWTCoef[] a, int i) {
168         if (areCompatible(a)==false) {
169             throw new IllegalArgumentException JavaDoc("The Objects of type FWTCoef are not compatible.");
170         }
171         if(getJ(a)<=1) {
172             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
173         }
174         if((i<1)||(i>=getJ(a))) {
175             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(getJ(a)-1));
176         }
177         if(sumVariance(a)==0) {
178             if(getJ(a)!=0) {
179                 return(1/getJ(a));
180             } else {
181                 throw new IllegalArgumentException JavaDoc("No energy!");
182             }
183         }
184         return(variance(a,i)/sumVariance(a));
185     }
186
187
188         /**
189         * Compute the Shannon entropy.
190         */

191     public static double icf(FWTCoef[] a) {
192         if (areCompatible(a)==false) {
193             throw new IllegalArgumentException JavaDoc("The Objects of type FWTCoef are not compatible.");
194         }
195         if(getJ(a)<=1) {
196             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
197         }
198         double[] pe=new double[getJ(a)-1];
199         for(int j=1;j<getJ(a);j++) {
200             pe[j-1]=energyRatio(a,j);
201         }
202         return(EngineerMath.icf(pe));
203     }
204
205     public static double icfVariance(FWTCoef[] a) {
206         if (areCompatible(a)==false) {
207             throw new IllegalArgumentException JavaDoc("The Objects of type FWTCoef are not compatible.");
208         }
209         if(getJ(a)<=1) {
210             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
211         }
212         double[] pv=new double[getJ(a)-1];
213         for(int j=1;j<getJ(a);j++) {
214             pv[j-1]=varianceRatio(a,j);
215         }
216         return(EngineerMath.icf(pv));
217     }
218 }
219
220
Popular Tags