KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > wavelet > splines > SumOfDiracs


1
2
3 package JSci.maths.wavelet.splines;
4
5 import JSci.maths.wavelet.*;
6 import JSci.maths.*;
7
8 /****************************************
9 * This class is used to generate sum of
10 * Diracs (splines of order 0) to be used
11 * as wavelets or related functions. It can
12 * also be used for basic interpolation.
13 * @author Daniel Lemire
14 *****************************************/

15 public class SumOfDiracs extends Spline implements Filter, Cloneable JavaDoc {
16     protected final static int filtretype=-1;
17     private double[] vecteur;
18     /****************************************
19   * This method is used to compute
20   * how the number of scaling functions
21   * changes from on scale to the other.
22   * Basically, if you have k scaling
23   * function and a Filter of type t, you'll
24   * have 2*k+t scaling functions at the
25   * next scale (dyadic case).
26   * Notice that this method assumes
27   * that one is working with the dyadic
28   * grid while the method "previousDimension"
29   * define in the interface "Filter" doesn't.
30     ******************************************/

31     public int getFilterType () {
32                 return(filtretype);
33     }
34
35   /*******************************
36   * Return a String representation
37   * of the object
38   ********************************/

39   public String JavaDoc toString() {
40     return(ArrayMath.toString(vecteur));
41   }
42   /*****************************************
43   * Check if another object is equal to this
44   * SumOfDiracs object
45   ******************************************/

46   public boolean equals(Object JavaDoc a) {
47     if(a!=null && (a instanceof SumOfDiracs) && vecteur.length==((SumOfDiracs)a).dimension()) {
48       SumOfDiracs iv=(SumOfDiracs)a;
49         for(int i=0;i<vecteur.length;i++) {
50           if(vecteur[i]!=iv.getValue(i))
51             return false;
52           }
53         return true;
54       } else
55     return false;
56   }
57
58   /********************************************
59   * Return a copy of this object
60   *********************************************/

61     public Object JavaDoc clone() {
62     SumOfDiracs sod=(SumOfDiracs) super.clone();
63     if(vecteur!=null)
64       sod.vecteur=ArrayMath.copy(this.vecteur);
65     return(sod);
66     }
67
68
69     /****************************************
70   * This method return the number of "scaling"
71   * functions at the previous scale given a
72   * number of scaling functions. The answer
73   * is always smaller than the provided value
74   * (about half since this is a dyadic
75   * implementation). This serves as the defintion
76   * for the "Filter type".
77   * It is used by the interface
78   * "Filter".
79     *****************************************/

80     public int previousDimension (int k) {
81         int i=(int) Math.round((k+1)/2d);
82         if(2*i-1==k) {
83             return(i);
84         } else {
85             throw new IllegalArgumentException JavaDoc("Even number of values into an odd Filter! Change the number of data values/of iterations.");
86         }
87     }
88
89     /****************************************
90     * This is the implementation of the lowpass
91   * Filter. It is used by the interface
92   * "Filter". Lowpass filters are normalized
93   * so that they preserve constants away from
94   * the boundaries.
95     *****************************************/

96     public double[] lowpass (double[] v, double[] param) {
97         return(lowpass(v));
98     }
99
100     /****************************************
101     * This is the implementation of the highpass
102   * Filter. It is used by the interface
103   * "Filter". This class doesn't have a
104   * highpass Filter so that this method is
105   * only provided to be compatible with the
106   * interface.
107     *****************************************/

108     public double[] highpass (double[] v, double[] param) {
109         return(highpass(v));
110     }
111
112     /****************************************
113     * This is the implementation of the lowpass
114   * Filter. It is used by the interface
115   * "Filter". Lowpass filters are normalized
116   * so that they preserve constants away from
117   * the boundaries.
118     *****************************************/

119     public double[] lowpass (double[] gete) {
120         if(gete.length<2) {
121             throw new IllegalArgumentException JavaDoc("The array is not long enough : "+gete.length+" < 2");
122         }
123         double[] data=Cascades.oversample(gete);
124         int dl=data.length-1;
125         double[] sortie=new double[2*gete.length-1];
126         double[] vg={0,1d,0};
127         double[] v0={1d,0};
128         for(int k=1;k<=dl-1;k++) {
129             sortie[k]=ArrayMath.scalarProduct(ArrayMath.extract(k-1,k+1,data),vg);
130         }
131         sortie[0]=ArrayMath.scalarProduct(ArrayMath.extract(0,1,data),v0);
132         sortie[dl]=ArrayMath.scalarProduct(ArrayMath.extract(dl,dl-1,data),v0);
133         return(sortie);
134     }
135
136     /****************************************
137     * This is the implementation of the highpass
138   * Filter. It is used by the interface
139   * "Filter". This class doesn't have a
140   * highpass Filter so that this method is
141   * only provided to be compatible with the
142   * interface.
143     *****************************************/

144     public double[] highpass (double[] gete) {
145         return(lowpass(gete));
146     }
147
148     /************************************
149     *************************************/

150     public SumOfDiracs(double[] v) {
151         vecteur=v;
152     }
153     /************************************
154     *************************************/

155     public SumOfDiracs() {
156
157     }
158
159     /********************************************
160   * Get the i th sampled value of the function.
161   * @param i position (knot)
162   * @exception IllegalArgumentException if i is not
163   * a within 0 and the last knot (dimension()-1)
164     *********************************************/

165   public double getValue(int i) {
166     if ((i<0) || (i>vecteur.length-1)) {
167             throw new IllegalArgumentException JavaDoc("Incorrect parameters : "+i+", "+vecteur.length);
168     }
169     return(vecteur[i]);
170   }
171    /*********************************************
172    * Set the sampled values of the function
173    * to the specified array
174    **********************************************/

175     public void setValues(double[] v) {
176         vecteur=v;
177     }
178
179   /******************************************
180   * Compute the mass (integral)
181   * @param a left boundary of the interval
182   * @param b right boundary of the interval
183   **********************************************/

184     public double mass(double a, double b) {
185         double mass=0;
186         for(int i=0;i<vecteur.length;i++) {
187             mass+=vecteur[i];
188         }
189         mass=mass*Math.abs(b-a)/vecteur.length;
190         return(mass);
191     }
192
193
194
195    /*********************************************
196    * Set a particular value
197    * @param i position (knot)
198    * @param d value
199    * @exception IllegalArgumentException if the parameter i is negative
200    **********************************************/

201     public void setValue(int i, double d) {
202         if (i<0) {
203             throw new IllegalArgumentException JavaDoc("The parameter must be positive : "+i);
204         }
205         vecteur[i]=d;
206     }
207
208   /*********************************************
209   * Number of knots
210   **********************************************/

211   public int dimension () {
212     return(vecteur.length);
213   }
214
215     /*********************************************
216   * Number of knots after j iterations
217   * @param j number of iterations
218     **********************************************/

219   public int dimension(int j) {
220     return(Cascades.dimension(vecteur.length, j));
221   }
222
223     /************************************************
224   * Return as an array the interpolated values
225   * of the function. Will return the same values
226   * as the evaluate method because the Filter
227   * is interpolatory.
228   * @param j number of iterations
229     *************************************************/

230   public double[] interpolate(int j) {
231     if (j<0) {
232       throw new IllegalArgumentException JavaDoc("This parameter must be postive : "+j);
233     }
234     return(Cascades.evaluation(this,j,vecteur));
235   }
236
237
238     /************************************************
239   * Return as an array the sampled values
240   * of the function
241   * @param j number of iterations
242     *************************************************/

243     public double[] evaluate (int j) {
244         return(interpolate(j));
245     }
246 }
247
Popular Tags