KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package JSci.maths.wavelet;
3
4 import JSci.maths.*;
5
6 /****************************************
7 * This class is a standard implementation
8 * of the Cascades algorithm.
9 * @author Daniel Lemire
10 *****************************************/

11 public final class Cascades extends AbstractMath {
12     private Cascades(){}
13
14     /****************************************
15   * This method return the number of "scaling"
16   * functions at the previous scale given a
17   * number of scaling functions. The answer
18   * is always smaller than the provided value
19   * (about half since this is a dyadic
20   * implementation). This relates to the same idea
21   * as the "filter type".
22     *****************************************/

23     public static int previousDimension (int filtretype, int k) {
24         int i=(int) Math.round((k+filtretype)/2d);
25         if(2*i-filtretype==k) {
26             return(i);
27         } else {
28             if(2*Math.round(k/2d)==k) {
29                 throw new IllegalScalingException("Odd number of values into an even filter! Change the number of values/iterations: "+k);
30             } else {
31                 throw new IllegalScalingException("Even number of values into an odd filter! Change the number of values/iterations: "+k);
32             }
33         }
34     }
35
36     public static double[] evalScaling (Filter filtre,int n0, int j1, int k) {
37         if (j1>20) {
38             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
39         }
40         if(n0<=0) {
41             throw new IllegalArgumentException JavaDoc("Must have a positive number of scaling functions : "+n0+" < 0.");
42         }
43         double[] init=new double[n0];
44         if((k>=init.length)||(k<0)) {
45             throw new IllegalArgumentException JavaDoc("There are "+init.length+" scaling functions going from 0 to "+(init.length-1)+" and you are trying to get the "+k+"th function.");
46
47         }
48         init[k]=1;/* initialisation */
49         return(evaluation(filtre, j1, init));
50     }
51
52
53     public static double[] evalWavelet (Filter filtre, int filtretype, int n0, int j1, int k) {
54         if (j1>20) {
55             throw new IllegalArgumentException JavaDoc("Too many iterations : "+j1);
56         }
57         if(n0-filtretype<=0) {
58             throw new IllegalArgumentException JavaDoc("With "+n0+" scaling functions and a filter of type "+filtretype+", you are left with no wavelets. Please change the number of scaling functions, the multiresolution or the number of iterations");
59         }
60         double[] init=new double[n0-filtretype];
61         if((k>=init.length)||(k<0)) {
62             throw new IllegalArgumentException JavaDoc("There are "+init.length+" wavelets going from 0 to "+(init.length-1)+" and you are trying to get to the "+k+"th wavelet.");
63         }
64         init[k]=1;
65         double[] filtreWavelet=filtre.highpass(init);
66         return(evaluation(filtre,j1,filtreWavelet));
67     }
68
69     public static double[] evalWavelet (Filter filtre,int n0, int j1, int k) {
70         return(evalWavelet(filtre,1,n0,j1,k));
71     }
72
73     public static double[] evalWaveletHaar (Filter filtre,int n0, int j1, int k) {
74         return(evalWavelet(filtre,0,n0,j1,k));
75     }
76
77     public static double[] evalWaveletQuadratic (Filter filtre, int n0, int j1, int k) {
78         return(evalWavelet(filtre,2,n0,j1,k));
79     }
80
81     public static int PowerOf2(int pwrOf2) {
82         if(pwrOf2<0) {
83             throw new IllegalArgumentException JavaDoc("This parametre must be positive : "+pwrOf2+" < 0");
84         }
85         int reponse=1;
86         for(int i=pwrOf2;i>0;i--) {
87             reponse=reponse*2;
88         }
89         return(reponse);
90     }
91
92
93     /************************************************
94     * method used to oversample according to
95     * the lazy (Dirac Delta Function) interpolation
96     *************************************************/

97     public static double[] oversample(double data[]) {
98         double answer[]=new double[2*data.length-1];
99         for(int i=0;i<data.length-1;i++) {
100             answer[2*i]=data[i];
101         }
102         answer[2*data.length-2]=data[data.length-1];
103         return answer;
104     }
105
106     /*********************************************
107     * Method used to oversample according to the
108     * Haar transform
109     **********************************************/

110     public static double[] doublesample(double data[]) {
111         int Nombre=data.length;
112         double answer[]=new double[2*Nombre];
113         for(int i=0;i<Nombre;i++) {
114             answer[2*i+1]=data[i];
115         }
116         return answer;
117         }
118     /**************************************************
119     * Special oversampling for the linear spline
120     * interpolation
121     ***************************************************/

122     public static double[] supersample(double data[]) {
123         int Nombre=data.length;
124         double answer[]=new double[2*Nombre+1];
125         for(int i=0;i<Nombre;i++) {
126             answer[2*i+1]=data[i];
127         }
128         return answer;
129         }
130   /**********************************
131   * Special oversampling for the
132   * quadratic spline interpolation
133   ***********************************/

134     public static double[] quadraticOversample( double[] v) {
135         double[] ans=new double[2*v.length-2];
136         ans[0]=v[0];
137         ans[ans.length-1]=v[v.length-1];
138         for(int k=1;k<v.length-1;k++) {
139             ans[2*k-1]=v[k];
140         }
141         return(ans);
142     }
143   /****************************************************
144   * Starting with n0 scaling functions and
145   * going jfin scales ahead (iterating jfin times),
146   * tells you how many scaling functions you'll have.
147   * (Dyadic multiresolutions of type filtretype.)
148   * @param filtertype filter type
149   * @param n0 number of scaling functions initially
150   * @param jfin number of iterations
151   ******************************************************/

152     public static int dimension(int n0, int jfin, int filtertype) {
153         return(PowerOf2(jfin)*(n0-filtertype)+filtertype);
154
155     }
156
157   /****************************************************
158   * Starting with n0 scaling functions and
159   * going jfin scales ahead (iterating jfin times),
160   * tells you how many scaling functions you'll have.
161   * (Linear spline and dyadic multiresolution of type 1.)
162   * @param n0 number of scaling functions initially
163   * @param jfin number of iterations
164   ******************************************************/

165     public static int dimension(int n0, int jfin) {
166         return(Cascades.PowerOf2(jfin)*(n0-1)+1);
167     }
168   /****************************************************
169   * Starting with n0 scaling functions and
170   * going jfin scales ahead (iterating jfin times),
171   * tells you how many scaling functions you'll have.
172   * (Haar and dyadic multiresolution of type 0.)
173   * @param n0 number of scaling functions initially
174   * @param jfin number of iterations
175   ******************************************************/

176     public static int dimensionHaar(int n0, int jfin) {
177         return(Cascades.PowerOf2(jfin)*(n0));
178     }
179
180     public static double[] evaluation (Filter filtre, int j1, double[] init) {
181         if (j1<0) {
182             throw new IllegalArgumentException JavaDoc("Incorrect parameters : "+j1+" < 0 ");
183         }
184         if (j1>20) {
185             throw new IllegalArgumentException JavaDoc("Excessive number of iterations: "+j1);
186         }
187         double[] data=init;
188         for (int j=0;j<j1;j++){
189             data=filtre.lowpass(data);
190         }
191         return(data);
192     }
193
194 }
195
Popular Tags