KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.wavelet;
2
3 import JSci.maths.*;
4 import JSci.maths.wavelet.*;
5
6 /****************************************************
7 * This class is used to encapsulate wavelet
8 * packets coefficients.
9 * @author Daniel Lemire
10 *****************************************/

11 public final class FWTPacketCoef extends Object JavaDoc implements NumericalConstants, Cloneable JavaDoc {
12     protected double[][] coefs;
13     protected boolean[] StandardChoice;
14     final static double normalisation=1.0/SQRT2;
15
16     public FWTPacketCoef () {
17     }
18
19     /**********************************************
20     ***********************************************/

21     public FWTPacketCoef (double[][] v, boolean[] b) {
22         coefs=v;
23         StandardChoice=b;
24         if(b.length!=v.length-1) {
25             throw new IllegalArgumentException JavaDoc("boolean[].length must be exactly double[][].length -1: boolean[].length="+b.length+" and double[][].length="+v.length);
26         }
27     }
28   /********************************************
29   * Return a copy of this object
30   *********************************************/

31     public Object JavaDoc clone() {
32     try {
33       FWTPacketCoef fwtp=(FWTPacketCoef) super.clone();
34       if(coefs!=null)
35         fwtp.coefs=ArrayMath.copy(coefs);
36       if(StandardChoice!=null) {
37         fwtp.StandardChoice = new boolean [StandardChoice.length];
38         System.arraycopy(StandardChoice,0,fwtp.StandardChoice,0,StandardChoice.length);
39       }
40       return(fwtp);
41     } catch (CloneNotSupportedException JavaDoc cnse) {
42       throw new InternalError JavaDoc();
43     }
44     }
45
46     /*************************************************
47     **************************************************/

48     public int getJ () {
49         return(coefs.length);
50     }
51
52
53
54     /*************************************************
55     **************************************************/

56     public int dimension(int i) {
57         if ((i<0)||(i>=coefs.length)) {
58             throw new IllegalArgumentException JavaDoc("This scale doesn't exist : "+i+", "+coefs.length);
59         }
60         return(coefs[i].length);
61     }
62
63     /*******************************************************
64     ********************************************************/

65     public double[][] getCoefs() {
66         return(coefs);
67     }
68
69     /***************************
70   * Compute the L2 norm of the
71   * coefficients
72     ****************************/

73     public double[] norm() {
74         double[] ans=new double[coefs.length];
75         for(int j=0;j<coefs.length;j++) {
76             ans[j]=ArrayMath.norm(coefs[j]);
77         }
78         return(ans);
79     }
80
81     /***************************
82   * Compute the L2 norm of the
83   * coefficients at "scale" i.
84     ****************************/

85     public double norm(int i) {
86         if((i<0)||(i>=coefs.length)) {
87             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
88         }
89         double ans=ArrayMath.norm(coefs[i]);
90         return(ans);
91     }
92
93
94     /************************************
95   * Compute the sum of the squares of
96   * the coefficients
97     *************************************/

98     private double[] sumSquares() {
99         double[] ans=new double[coefs.length];
100         for(int j=0;j<coefs.length;j++) {
101             ans[j]=ArrayMath.sumSquares(coefs[j]);
102         }
103         return(ans);
104     }
105
106     /************************************
107   * Compute the sum of the squares of
108   * the coefficients
109     *************************************/

110     public double sumSquares(int i) {
111         if((i<0)||(i>=coefs.length)) {
112             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" must be between 0 et "+(coefs.length-1));
113         }
114         double ans=ArrayMath.sumSquares(coefs[i]);
115         return(ans);
116     }
117
118     /************************************
119     *************************************/

120     public double mass(int i) {
121         if((i<0)||(i>=coefs.length)) {
122             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
123         }
124         double ans=ArrayMath.mass(coefs[i]);
125         return(ans);
126     }
127
128     /************************************
129     *************************************/

130     private double[] variance() {
131         double[] ans=new double[coefs.length];
132         for(int j=0;j<coefs.length;j++) {
133             ans[j]=ArrayMath.variance(coefs[j]);
134         }
135         return(ans);
136     }
137
138     /************************************
139     *************************************/

140     public double variance(int i) {
141         if((i<0)||(i>=coefs.length)) {
142             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
143         }
144         double ans=ArrayMath.variance(coefs[i]);
145         return(ans);
146     }
147
148
149     public double sumEnergies() {
150         if(coefs.length<=1) {
151             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
152         }
153         double[] energies=sumSquares();
154         double ans=0.0;
155         for(int k=0;k<energies.length;k++) {
156             ans+=energies[k];
157         }
158         return(ans);
159     }
160
161     public double entropy() {
162         if(coefs.length<=1) {
163             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
164         }
165         double sumEnergies=sumEnergies();
166         int nombreDeCoefficients=0;
167         for(int k=0;k<coefs.length;k++) {
168             nombreDeCoefficients+=coefs[k].length;
169         }
170         double[] pourcentageDEnergie=new double[nombreDeCoefficients];
171         int pos=0;
172         for(int k=0;k<coefs.length;k++) {
173             for(int l=0;l<coefs[k].length;l++) {
174                 pourcentageDEnergie[pos]=coefs[k][l]*coefs[k][l]/sumEnergies;
175                 pos++;
176             }
177         }
178         return(EngineerMath.icf(pourcentageDEnergie));
179     }
180
181     public double sumVariance() {
182         if(coefs.length<=1) {
183             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
184         }
185         double[] variances=variance();
186         double ans=0.0;
187         for(int k=0;k<variances.length;k++) {
188             ans+=variances[k];
189         }
190         return(ans);
191     }
192
193     public double energyRatio(int i) {
194         if(coefs.length<=1) {
195             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
196         }
197         if((i<0)||(i>=coefs.length)) {
198             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
199         }
200         if(sumEnergies()==0) {
201             if(coefs.length!=0) {
202                 return(1/coefs.length);
203             } else {
204                 throw new IllegalArgumentException JavaDoc("No energy!");
205             }
206         }
207         return(sumSquares(i)/sumEnergies());
208     }
209
210
211
212     public double varianceRatio(int i) {
213         if(coefs.length<=1) {
214             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
215         }
216         if((i<0)||(i>=coefs.length)) {
217             throw new IllegalArgumentException JavaDoc("The integer parament "+i+" should be between 0 and "+(coefs.length-1));
218         }
219         if(sumVariance()==0) {
220             if(coefs.length!=0) {
221                 return(1/coefs.length);
222             } else {
223                 throw new IllegalArgumentException JavaDoc("No variance!");
224             }
225         }
226         return(variance(i)/sumVariance());
227     }
228
229         /**
230         * Compute the Shannon entropy.
231         */

232     public double icf() {
233         if(coefs.length<=1) {
234             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
235         }
236         double[] pe=new double[coefs.length-1];
237         for(int j=0;j<coefs.length;j++) {
238             pe[j]=energyRatio(j);
239         }
240         return(EngineerMath.icf(pe));
241     }
242
243     public double varianceICF() {
244         if(coefs.length<=1) {
245             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
246         }
247         double[] pv=new double[coefs.length-1];
248         for(int j=0;j<coefs.length;j++) {
249             pv[j]=varianceRatio(j);
250         }
251         return(EngineerMath.icf(pv));
252     }
253
254     public void setCoefs(double[][] v) {
255         coefs=v;
256     }
257
258     /***************************
259     * Allows the user to set
260     * the Wavelet Packet chosen
261     ****************************/

262     public void setPacket(boolean[] b) {
263         StandardChoice=b;
264     }
265
266     public void setCoefs(double[] v, int i) {
267         if((i<0)||(i>=coefs.length)) {
268             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
269         }
270         coefs[i]=v;
271     }
272
273     public void synthesize(Filter filtreprimaire, double[] param) {
274         if(coefs.length<=1) {
275             throw new IllegalArgumentException JavaDoc("No synthesis left to do: "+coefs.length);
276         }
277         double[] V0,W0;
278         if(StandardChoice[StandardChoice.length-1]) {
279             V0=filtreprimaire.lowpass(coefs[coefs.length-1],param);
280             // Lowpass filters must be renormalized.
281
V0=ArrayMath.scalarMultiply(normalisation,V0);
282             W0=filtreprimaire.highpass(coefs[coefs.length-2],param);
283         } else {
284             V0=filtreprimaire.lowpass(coefs[coefs.length-2],param);
285             // Lowpass filters must be renormalized.
286
V0=ArrayMath.scalarMultiply(normalisation,V0);
287             W0=filtreprimaire.highpass(coefs[coefs.length-1],param);
288         }
289         if(V0.length!=W0.length) {
290             throw new IllegalArgumentException JavaDoc("Synthesis is impossible : bad data/multiresolution? "+coefs[0].length+", "+coefs[coefs.length-1].length+", "+V0.length+", "+W0.length);
291         }
292         V0=ArrayMath.add(V0,W0);
293         double[][] c=new double[coefs.length-1][];
294         for(int j=0;j<coefs.length-2;j++) {
295             c[j]=coefs[j];
296         }
297         c[coefs.length-1]=V0;
298         coefs=c;
299         boolean[] b=new boolean[c.length-1];
300         for(int j=0;j<b.length;j++) {
301             b[j]=StandardChoice[j];
302         }
303         StandardChoice=b;
304     }
305
306     public void synthesize(Filter filtreprimaire, double[] param, int jmax) {
307         if ((jmax<0) || (jmax>coefs.length-1)) {
308             throw new IllegalArgumentException JavaDoc("The integer parameter "+jmax+" must be between 0 and "+(coefs.length-1));
309         }
310         for(int j=0;j<jmax;j++) {
311                 synthesize(filtreprimaire, param);
312         }
313     }
314
315     public void synthesizeTout(Filter filtreprimaire,double[] param) {
316         synthesize(filtreprimaire, param, coefs.length-1);
317     }
318
319     public void synthesize(Filter filtreprimaire) {
320         if(coefs.length<=1) {
321             throw new IllegalArgumentException JavaDoc("No synthesis left to do: "+coefs.length);
322         }
323         double[] V0,W0;
324         if(StandardChoice[StandardChoice.length-1]) {
325             V0=filtreprimaire.lowpass(coefs[coefs.length-1]);
326             // Lowpass filters must be renormalized.
327
V0=ArrayMath.scalarMultiply(normalisation,V0);
328             W0=filtreprimaire.highpass(coefs[coefs.length-2]);
329         } else {
330             V0=filtreprimaire.lowpass(coefs[coefs.length-2]);
331             // Lowpass filters must be renormalized.
332
V0=ArrayMath.scalarMultiply(normalisation,V0);
333             W0=filtreprimaire.highpass(coefs[coefs.length-1]);
334         }
335         if(V0.length!=W0.length) {
336             throw new IllegalArgumentException JavaDoc("Synthesis is impossible : bad data/multiresolution? "+coefs[0].length+", "+coefs[coefs.length-1].length+", "+V0.length+", "+W0.length);
337         }
338         V0=ArrayMath.add(V0,W0);
339         double[][] c=new double[coefs.length-1][];
340         for(int j=0;j<coefs.length-2;j++) {
341             c[j]=coefs[j];
342         }
343         c[coefs.length-1]=V0;
344         coefs=c;
345         boolean[] b=new boolean[c.length-1];
346         for(int j=0;j<b.length;j++) {
347             b[j]=StandardChoice[j];
348         }
349         StandardChoice=b;
350     }
351
352     public void synthesize(Filter filtreprimaire, int jmax) {
353         if ((jmax<0) || (jmax>coefs.length-1)) {
354             throw new IllegalArgumentException JavaDoc("The integer parameter "+jmax+" must be between 0 and "+(coefs.length-1));
355         }
356         for(int j=0;j<jmax;j++) {
357                 synthesize(filtreprimaire);
358         }
359     }
360
361     public void synthesizeAll(Filter filtreprimaire) {
362         synthesize(filtreprimaire, coefs.length-1);
363     }
364
365     public Signal rebuildSignal(Filter filtreprimaire) {
366         FWTCoef fwt=new FWTCoef(coefs);// copie
367
fwt.synthesizeAll(filtreprimaire);
368         return(new Signal(fwt.getCoefs()[0]));
369     }
370
371     public Signal rebuildSignal(Filter filtreprimaire, double[] param) {
372         FWTCoef fwt=new FWTCoef(coefs);// copie
373
fwt.synthesizeAll(filtreprimaire,param);
374         return(new Signal(fwt.getCoefs()[0]));
375     }
376
377     /*********************************************
378         * Denoises by zero-ing any value above a given percentile cut-off.
379         * @param p percentile cut-off, must be between 0 and 1.
380     **********************************************/

381     public void denoise(double p) {
382         for(int k=1;k<coefs.length;k++) {
383             coefs[k]=denoise(coefs[k],p);
384         }
385     }
386     /*********************************************
387         * Denoises by zero-ing any value above a given percentile cut-off.
388         * @param p percentile cut-off, must be between 0 and 1.
389         * @param k the index of the coefficient array to denoise.
390     **********************************************/

391     public void denoise(double p, int k) {
392             coefs[k]=denoise(coefs[k],p);
393     }
394
395     /**************************************
396         * Denoises by zero-ing any value above a given percentile cut-off.
397         * @param v an array to denoise.
398         * @param p percentile cut-off, must be between 0 and 1.
399     ***************************************/

400     public static double[] denoise(double[] v, double p) {
401         if(p==0.0) return(v);
402         double[] ans=v;
403         double seuil=ArrayMath.percentile(ArrayMath.abs(ans),1-p);
404         for(int k=0;k<ans.length;k++) {
405             if(Math.abs(ans[k])>=seuil) {
406                 ans[k]=0.0;
407             }
408         }
409         return(ans);
410     }
411
412     /*********************************************
413         * Compresses by zero-ing any value below a given percentile cut-off.
414         * @param p percentile cut-off, must be between 0 and 1.
415     **********************************************/

416     public void compress(double p) {
417         for(int k=0;k<coefs.length;k++) {
418             coefs[k]=compress(coefs[k],p);
419         }
420     }
421     /*********************************************
422         * Compresses by zero-ing any value below a given percentile cut-off.
423         * @param p percentile cut-off, must be between 0 and 1.
424         * @param k the index of the coefficient array to compress.
425     **********************************************/

426     public void compress(double p, int k) {
427             coefs[k]=compress(coefs[k],p);
428     }
429
430     /**************************************
431         * Compresses by zero-ing any value below a given percentile cut-off.
432         * @param v an array to compress.
433         * @param p percentile cut-off, must be between 0 and 1.
434     ***************************************/

435     public static double[] compress(double[] v, double p) {
436         if(p==0.0) return(v);
437         double[] ans=v;
438         double seuil=ArrayMath.percentile(ArrayMath.abs(ans),p);
439         for(int k=0;k<ans.length;k++) {
440             if(Math.abs(ans[k])<=seuil) {
441                 ans[k]=0.0;
442             }
443         }
444         return(ans);
445     }
446
447
448     /*********************************************
449         * Denoises by zero-ing any value above a given cut-off.
450         * @param p cut-off.
451     **********************************************/

452     public void denoiseHard(double p) {
453         for(int k=0;k<coefs.length;k++) {
454             coefs[k]=denoiseHard(coefs[k],p);
455         }
456     }
457     /*********************************************
458         * Denoises by zero-ing any value above a given cut-off.
459         * @param p cut-off.
460         * @param k the index of the coefficient array to denoise.
461     **********************************************/

462     public void denoiseHard(double p, int k) {
463             coefs[k]=denoiseHard(coefs[k],p);
464     }
465
466     /**************************************
467         * Denoises by zero-ing any value above a given cut-off.
468         * @param v an array to denoise.
469         * @param seuil cut-off/threshold.
470     ***************************************/

471     public static double[] denoiseHard(double[] v, double seuil) {
472         if(seuil<0.0) {
473             throw new IllegalArgumentException JavaDoc("The cutoff value must be positive.");
474         }
475         double[] ans=v;
476         for(int k=0;k<ans.length;k++) {
477             if(Math.abs(ans[k])>=seuil) {
478                 ans[k]=0.0;
479             }
480         }
481         return(ans);
482     }
483
484     /*********************************************
485         * Compresses by zero-ing any value below a given cut-off.
486         * @param p cut-off.
487     **********************************************/

488     public void compressHard(double p) {
489         for(int k=0;k<coefs.length;k++) {
490             coefs[k]=compressHard(coefs[k],p);
491         }
492     }
493     /*********************************************
494         * Compresses by zero-ing any value below a given cut-off.
495         * @param p cut-off.
496         * @param k the index of the coefficient array to compress.
497     **********************************************/

498     public void compressHard(double p, int k) {
499             coefs[k]=compressHard(coefs[k],p);
500     }
501
502     /**************************************
503         * Compresses by zero-ing any value below a given cut-off.
504         * @param v an array to compress.
505         * @param seuil cut-off/threshold.
506     ***************************************/

507     public static double[] compressHard(double[] v, double seuil) {
508         if(seuil<0.0) {
509             throw new IllegalArgumentException JavaDoc("The cutoff value must be positive.");
510         }
511         double[] ans=v;
512         for(int k=0;k<ans.length;k++) {
513             if(Math.abs(ans[k])<=seuil) {
514                 ans[k]=0.0;
515             }
516         }
517         return(ans);
518     }
519 }
520
521
Popular Tags