KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

19     public FWTCoef (double[][] v) {
20         coefs=v;
21
22     }
23   /********************************************
24   * Return a copy of this object
25   *********************************************/

26     public Object JavaDoc clone() {
27     try {
28       FWTCoef fwt=(FWTCoef) super.clone();
29       if(coefs!=null)
30         fwt.coefs=ArrayMath.copy(coefs);
31       return(fwt);
32     } catch (CloneNotSupportedException JavaDoc cnse) {
33       throw new InternalError JavaDoc();
34     }
35     }
36
37     /*************************************************
38     **************************************************/

39     public int getJ () {
40         return(coefs.length);
41     }
42
43
44
45     /*************************************************
46     **************************************************/

47     public int dimension(int i) {
48         if ((i<0)||(i>=coefs.length)) {
49             throw new IllegalArgumentException JavaDoc("This dimension doesn't exist : "+i+", "+coefs.length);
50         }
51         return(coefs[i].length);
52     }
53
54     /*******************************************************
55     ********************************************************/

56     public double[][] getCoefs() {
57         return(coefs);
58     }
59
60     /***************************
61   * Compute the L2 norm of the
62   * coefficients
63     ****************************/

64     public double[] norm() {
65         double[] ans=new double[coefs.length];
66         for(int j=0;j<coefs.length;j++) {
67             ans[j]=ArrayMath.norm(coefs[j]);
68         }
69         return(ans);
70     }
71
72     /***************************
73   * Compute the L2 norm of the
74   * coefficients at "scale" i.
75   * Wavelet coefficients are
76   * into the "scale" 1 to ... and
77   * the scale 0 is the coarsest
78   * scale containing scaling
79   * functions coefficients
80     ****************************/

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

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

106     public double sumSquares(int i) {
107         if((i<0)||(i>=coefs.length)) {
108             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
109         }
110         double ans=ArrayMath.sumSquares(coefs[i]);
111         return(ans);
112     }
113
114     /************************************
115     *************************************/

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

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

136     public double variance(int i) {
137         if((i<0)||(i>=coefs.length)) {
138             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
139         }
140         double ans=ArrayMath.variance(coefs[i]);
141         return(ans);
142     }
143
144
145     /**********************************************
146     ***********************************************/

147     public double sumEnergies() {
148         if(coefs.length<=1) {
149             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
150         }
151         double[] energies=sumSquares();
152         double ans=0;
153         for(int k=1;k<energies.length;k++) {
154             ans+=energies[k];
155         }
156         return(ans);
157     }
158
159     /******************************************************
160     *******************************************************/

161     public double entropy() {
162         if(coefs.length<=1) {
163             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
164         }
165         double se=sumEnergies();
166         int nombreDeCoefficients=0;
167         for(int k=1;k<coefs.length;k++) {
168             nombreDeCoefficients+=coefs[k].length;
169         }
170         double[] er=new double[nombreDeCoefficients];
171         int pos=0;
172         for(int k=1;k<coefs.length;k++) {
173             for(int l=0;l<coefs[k].length;l++) {
174                 er[pos]=coefs[k][l]*coefs[k][l]/se;
175                 pos++;
176             }
177         }
178         return(EngineerMath.icf(er));
179     }
180
181     /**********************************************
182     ***********************************************/

183     public double sumVariance() {
184         if(coefs.length<=1) {
185             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
186         }
187         double[] variances=variance();
188         double ans=0;
189         for(int k=1;k<variances.length;k++) {
190             ans+=variances[k];
191         }
192         return(ans);
193     }
194
195     /***********************************************
196     ************************************************/

197     public double energyRatio(int i) {
198         if(coefs.length<=1) {
199             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
200         }
201         if((i<1)||(i>=coefs.length)) {
202             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
203         }
204         if(sumEnergies()==0) {
205             if(coefs.length!=0) {
206                 return(1/coefs.length);
207             } else {
208                 throw new IllegalArgumentException JavaDoc("No energy!");
209             }
210         }
211         return(sumSquares(i)/sumEnergies());
212     }
213
214     /***********************************************
215     ************************************************/

216     public double varianceRatio(int i) {
217         if(coefs.length<=1) {
218             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
219         }
220         if((i<1)||(i>=coefs.length)) {
221             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
222         }
223         if(sumVariance()==0) {
224             if(coefs.length!=0) {
225                 return(1/coefs.length);
226             } else {
227                 throw new IllegalArgumentException JavaDoc("No energy!");
228             }
229         }
230         return(variance(i)/sumVariance());
231     }
232
233     /***************************************************
234         * Compute the Shannon entropy.
235     ****************************************************/

236     public double icf() {
237         if(coefs.length<=1) {
238             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
239         }
240         double[] pe=new double[coefs.length-1];
241         for(int j=1;j<coefs.length;j++) {
242             pe[j-1]=energyRatio(j);
243         }
244         return(EngineerMath.icf(pe));
245     }
246
247     /***************************************************
248     ****************************************************/

249     public double varianceICF() {
250         if(coefs.length<=1) {
251             throw new IllegalArgumentException JavaDoc("No wavelet coefficients!");
252         }
253         double[] pv=new double[coefs.length-1];
254         for(int j=1;j<coefs.length;j++) {
255             pv[j-1]=varianceRatio(j);
256         }
257         return(EngineerMath.icf(pv));
258     }
259
260     /***************************
261     ****************************/

262     public void setCoefs(double[][] v) {
263         coefs=v;
264     }
265
266     /***************************
267     ****************************/

268     public void setCoefs(double[] v, int i) {
269         if((i<0)||(i>=coefs.length)) {
270             throw new IllegalArgumentException JavaDoc("The integer parameter "+i+" should be between 0 and "+(coefs.length-1));
271         }
272         coefs[i]=v;
273     }
274
275     /**********************************************
276     ***********************************************/

277     public void synthesize(Filter filtreprimaire, double[] param) {
278         if(coefs.length<=1) {
279             throw new IllegalArgumentException JavaDoc("No synthesis possible : "+coefs.length);
280         }
281         double[] V0=filtreprimaire.lowpass(coefs[0],param);
282         double[] W0=filtreprimaire.highpass(coefs[coefs.length-1],param);
283         V0=ArrayMath.scalarMultiply(normalisation,V0);
284         if(V0.length!=W0.length) {
285             throw new IllegalArgumentException JavaDoc("Synthesis impossible : bad data/multiresolution?"+coefs[0].length+", "+coefs[coefs.length-1].length+", "+V0.length+", "+W0.length);
286         }
287         V0=ArrayMath.add(V0,W0);
288         double[][] c=new double[coefs.length-1][];
289         for(int j=1;j<coefs.length-1;j++) {
290             c[j]=coefs[j];
291         }
292         c[0]=V0;
293         coefs=c;
294     }
295
296     /**********************************************
297     ***********************************************/

298     public void synthesize(Filter filtreprimaire, double[] param, int jmax) {
299         if ((jmax<0) || (jmax>coefs.length-1)) {
300             throw new IllegalArgumentException JavaDoc("The integer parameter "+jmax+" must be between 0 and "+(coefs.length-1));
301         }
302         for(int j=0;j<jmax;j++) {
303                 synthesize(filtreprimaire, param);
304         }
305     }
306
307     /**********************************************
308     ***********************************************/

309     public void synthesizeAll(Filter filtreprimaire,double[] param) {
310         synthesize(filtreprimaire, param, coefs.length-1);
311     }
312
313     /**********************************************
314     ***********************************************/

315     public void synthesize(Filter filtreprimaire) {
316         if(coefs.length<=1) {
317             throw new IllegalArgumentException JavaDoc("No synthesis possible : "+coefs.length);
318         }
319         double[] V0=filtreprimaire.lowpass(coefs[0]);
320         double[] W0=filtreprimaire.highpass(coefs[coefs.length-1]);
321         V0=ArrayMath.scalarMultiply(normalisation,V0);
322         if(V0.length!=W0.length) {
323             throw new IllegalArgumentException JavaDoc("Synthesis impossible : bad data/multiresolution?"+coefs[0].length+", "+coefs[coefs.length-1].length+", "+V0.length+", "+W0.length);
324         }
325         V0=ArrayMath.add(V0,W0);
326         double[][] c=new double[coefs.length-1][];
327         for(int j=1;j<coefs.length-1;j++) {
328             c[j]=coefs[j];
329         }
330         c[0]=V0;
331         coefs=c;
332     }
333
334     /**********************************************
335     ***********************************************/

336     public void synthesize(Filter filtreprimaire, int jmax) {
337         if ((jmax<0) || (jmax>coefs.length-1)) {
338             throw new IllegalArgumentException JavaDoc("The integer parameter "+jmax+" must be between 0 and "+(coefs.length-1));
339         }
340         for(int j=0;j<jmax;j++) {
341                 synthesize(filtreprimaire);
342         }
343     }
344
345     /**********************************************
346     ***********************************************/

347     public void synthesizeAll(Filter filtreprimaire) {
348         synthesize(filtreprimaire, coefs.length-1);
349     }
350
351     /**************************************************
352     ***************************************************/

353     public Signal rebuildSignal(Filter filtreprimaire) {
354         FWTCoef fwt=new FWTCoef(coefs);// copie
355
fwt.synthesizeAll(filtreprimaire);
356         return(new Signal(fwt.getCoefs()[0]));
357     }
358
359     /**************************************************
360     ***************************************************/

361     public Signal rebuildSignal(Filter filtreprimaire, double[] param) {
362         FWTCoef fwt=new FWTCoef(coefs);// copie
363
fwt.synthesizeAll(filtreprimaire,param);
364         return(new Signal(fwt.getCoefs()[0]));
365     }
366
367     /*********************************************
368         * Denoises by zero-ing any value above a given percentile cut-off.
369         * @param p percentile cut-off, must be between 0 and 1.
370     **********************************************/

371     public void denoise(double p) {
372         for(int k=1;k<coefs.length;k++) {
373             coefs[k]=denoise(coefs[k],p);
374         }
375     }
376     /*********************************************
377         * Denoises by zero-ing any value above a given percentile cut-off.
378         * @param p percentile cut-off, must be between 0 and 1.
379         * @param k the index of the coefficient array to denoise.
380     **********************************************/

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

390     public static double[] denoise(double[] v, double p) {
391         if(p==0) return(v);
392         double[] ans=v;
393         double seuil=ArrayMath.percentile(ArrayMath.abs(ans),1-p);
394         for(int k=0;k<ans.length;k++) {
395             if(Math.abs(ans[k])>=seuil) {
396                 ans[k]=0;
397             }
398         }
399         return(ans);
400     }
401
402     /*********************************************
403         * Compresses by zero-ing any value below a given percentile cut-off.
404         * @param p percentile cut-off, must be between 0 and 1.
405     **********************************************/

406     public void compress(double p) {
407         for(int k=1;k<coefs.length;k++) {
408             coefs[k]=compress(coefs[k],p);
409         }
410     }
411     /*********************************************
412         * Compresses by zero-ing any value below a given percentile cut-off.
413         * @param p percentile cut-off, must be between 0 and 1.
414         * @param k the index of the coefficient array to compress.
415     **********************************************/

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

425     public static double[] compress(double[] v, double p) {
426         if(p==0) return(v);
427         double[] ans=v;
428         double seuil=ArrayMath.percentile(ArrayMath.abs(ans),p);
429         for(int k=0;k<ans.length;k++) {
430             if(Math.abs(ans[k])<=seuil) {
431                 ans[k]=0;
432             }
433         }
434         return(ans);
435     }
436
437
438     /*********************************************
439         * Denoises by zero-ing any value above a given cut-off.
440         * @param p cut-off.
441     **********************************************/

442     public void denoiseHard(double p) {
443         for(int k=1;k<coefs.length;k++) {
444             coefs[k]=denoiseHard(coefs[k],p);
445         }
446     }
447     /*********************************************
448         * Denoises by zero-ing any value above a given cut-off.
449         * @param p cut-off.
450         * @param k the index of the coefficient array to denoise.
451     **********************************************/

452     public void denoiseHard(double p, int k) {
453             coefs[k]=denoiseHard(coefs[k],p);
454     }
455
456     /**************************************
457         * Denoises by zero-ing any value above a given cut-off.
458         * @param v an array to denoise.
459         * @param seuil cut-off/threshold.
460     ***************************************/

461     public static double[] denoiseHard(double[] v, double seuil) {
462         if(seuil<0) {
463             throw new IllegalArgumentException JavaDoc("The cutoff value must be positive.");
464         }
465         double[] ans=v;
466         for(int k=0; k<ans.length; k++) {
467             if(Math.abs(ans[k]) >= seuil) {
468                 ans[k]=0;
469             }
470         }
471         return(ans);
472     }
473
474     /*********************************************
475         * Compresses by zero-ing any value below a given cut-off.
476         * @param p cut-off.
477     **********************************************/

478     public void compressHard(double p) {
479         for(int k=1;k<coefs.length;k++) {
480             coefs[k]=compressHard(coefs[k],p);
481         }
482     }
483     /*********************************************
484         * Compresses by zero-ing any value below a given cut-off.
485         * @param p cut-off.
486         * @param k the index of the coefficient array to compress.
487     **********************************************/

488     public void compressHard(double p, int k) {
489             coefs[k]=compressHard(coefs[k],p);
490     }
491
492     /**************************************
493         * Compresses by zero-ing any value below a given cut-off.
494         * @param v an array to compress.
495         * @param seuil cut-off/threshold.
496     ***************************************/

497     public static double[] compressHard(double[] v, double seuil) {
498         if(seuil<0) {
499             throw new IllegalArgumentException JavaDoc("The cutoff value must be positive.");
500         }
501         double[] ans=v;
502         for(int k=0; k<ans.length; k++) {
503             if(Math.abs(ans[k]) <= seuil) {
504                 ans[k]=0;
505             }
506         }
507         return(ans);
508     }
509 }
510
511
Popular Tags