KickJava   Java API By Example, From Geeks To Geeks.

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


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

14 public class QuadraticSpline extends Spline implements Filter, Cloneable JavaDoc {
15     protected final static int filtretype=2;
16     private double[] vecteur;
17     static final double[] vg={1/4d,3/4d,3/4d,1/4d};
18     static final double[] v0={3/4d,1/4d};
19
20   /*******************************
21   * Return a String representation
22   * of the object
23   ********************************/

24   public String JavaDoc toString() {
25     return(ArrayMath.toString(vecteur));
26   }
27     /****************************************
28   * This method is used to compute
29   * how the number of scaling functions
30   * changes from on scale to the other.
31   * Basically, if you have k scaling
32   * function and a Filter of type t, you'll
33   * have 2*k+t scaling functions at the
34   * next scale (dyadic case).
35   * Notice that this method assumes
36   * that one is working with the dyadic
37   * grid while the method "previousDimension"
38   * define in the interface "Filter" doesn't.
39     ******************************************/

40     public int getFilterType () {
41                 return(filtretype);
42     }
43
44   /*****************************************
45   * Check if another object is equal to this
46   * QuadraticSpline object
47   ******************************************/

48   public boolean equals(Object JavaDoc a) {
49     if(a!=null && (a instanceof QuadraticSpline) && vecteur.length==((QuadraticSpline)a).dimension()) {
50       QuadraticSpline iv=(QuadraticSpline)a;
51         for(int i=0;i<vecteur.length;i++) {
52           if(vecteur[i]!=iv.getValue(i))
53             return false;
54           }
55         return true;
56       } else
57     return false;
58   }
59
60     /****************************************
61   * This method return the number of "scaling"
62   * functions at the previous scale given a
63   * number of scaling functions. The answer
64   * is always smaller than the provided value
65   * (about half since this is a dyadic
66   * implementation). This relates to the same idea
67   * as the "Filter type". It is used by
68   * the interface "Filter".
69     *****************************************/

70     public int previousDimension (int k) {
71         return(Cascades.previousDimension(filtretype,k));
72     }
73
74     /****************************************
75     * This is the implementation of the lowpass
76   * Filter. It is used by the interface
77   * "Filter". Lowpass filters are normalized
78   * so that they preserve constants away from
79   * the boundaries.
80     *****************************************/

81     public double[] lowpass (double[] v, double[] param) {
82         return(lowpass(v));
83     }
84
85
86     /****************************************
87     * This is the implementation of the highpass
88   * Filter. It is used by the interface
89   * "Filter". This class doesn't have a
90   * highpass Filter so that this method is
91   * only provided to be compatible with the
92   * interface.
93     *****************************************/

94     public double[] highpass (double[] v, double[] param) {
95         return(highpass(v));
96     }
97
98
99     /****************************************
100     * This is the implementation of the lowpass
101   * Filter. It is used by the interface
102   * "Filter". Lowpass filters are normalized
103   * so that they preserve constants away from
104   * the boundaries.
105     *****************************************/

106     public double[] lowpass (double[] gete) {
107         if(gete.length<3) {
108             throw new IllegalArgumentException JavaDoc("The array is not long enough : "+gete.length+" < 3");
109         }
110         double[] sortie=new double[2*gete.length-2];
111         for(int k=1;k<gete.length-1;k++) {
112             sortie[2*k-2]+=gete[k]*vg[0];
113             sortie[2*k-1]+=gete[k]*vg[1];
114             sortie[2*k]+=gete[k]*vg[2];
115             sortie[2*k+1]+=gete[k]*vg[3];
116         }
117         sortie[0]+=v0[0]*gete[0];
118         sortie[1]+=v0[1]*gete[0];
119         sortie[sortie.length-1]+=v0[0]*gete[gete.length-1];
120         sortie[sortie.length-2]+=v0[1]*gete[gete.length-1];
121         return(sortie);
122     }
123
124     /****************************************
125     * This is the implementation of the highpass
126   * Filter. It is used by the interface
127   * "Filter". This class doesn't have a
128   * highpass Filter so that this method is
129   * only provided to be compatible with the
130   * interface.
131     *****************************************/

132     public double[] highpass (double[] gete) {
133         return(lowpass(gete));
134     }
135
136     /***************************************
137     ****************************************/

138     public QuadraticSpline(double[] v) {
139         vecteur=v;
140     }
141
142
143     /***************************************
144     ****************************************/

145     public QuadraticSpline() {
146     }
147   /********************************************
148   * Return a copy of this object
149   *********************************************/

150     public Object JavaDoc clone() {
151     QuadraticSpline sod=(QuadraticSpline) super.clone();
152     if(vecteur!=null)
153       sod.vecteur=ArrayMath.copy(this.vecteur);
154     return(sod);
155     }
156
157     /********************************************
158   * Get the i th sampled value of the function.
159   * @param i position (knot)
160   * @exception IllegalArgumentException if i is not
161   * a within 0 and the last knot (dimension()-1)
162     *********************************************/

163     public double getValue(int i) {
164         if ((i<0) || (i>vecteur.length-1)) {
165             throw new IllegalArgumentException JavaDoc("Parameter incorrect : "+i+", "+vecteur.length);
166         }
167         return(vecteur[i]);
168     }
169
170    /*********************************************
171    * Set a particular value
172    * @param i position (knot)
173    * @param d value
174    * @exception IllegalArgumentException if the parameter i is negative
175    **********************************************/

176    public void setValue(int i, double d) {
177         if (i<0) {
178             throw new IllegalArgumentException JavaDoc("The parameter must be positive : "+i);
179         }
180         vecteur[i]=d;
181     }
182
183    /*********************************************
184    * Set the sampled values of the function
185    * to the specified array
186    **********************************************/

187     public void setValues(double[] v) {
188         vecteur=v;
189     }
190
191     /*********************************************
192   * compute the derivative of the function -
193   * useful for numerical analysis
194     **********************************************/

195     public LinearSpline derive() {
196         return(this.derive(0,1));
197     }
198
199     /*********************************************
200   * compute the derivative of the function -
201   * useful for numerical analysis
202   * @param a left boundary of the interval
203   * @param b right boundary of the interval
204     **********************************************/

205   public LinearSpline derive(double a, double b) {
206         double[] v=new double[vecteur.length-1];
207         for(int i=0;i<vecteur.length-1;i++) {
208                 v[i]=2*(vecteur[i+1]-vecteur[i])*vecteur.length/Math.abs(b-a);
209         }
210         return(new LinearSpline(v));
211     }
212
213   /*********************************************
214   * Number of knots
215   **********************************************/

216   public int dimension () {
217     return(vecteur.length);
218   }
219
220     /*********************************************
221   * Number of knots after j iterations
222   * @param j number of iterations
223     **********************************************/

224     public int dimension(int j) {
225         return(Cascades.dimension(vecteur.length,j,filtretype));
226     }
227
228     /************************************************
229   * Return as an array the interpolated values
230   * of the function.
231   * WARNING: Will return the same values
232   * as the evaluate method while it should really
233   * proceed by interpolation. Postprocessing should
234   * be added to this method in order that it
235   * returns the correct values.
236   * @param j number of iterations
237     *************************************************/

238   public double[] interpolate(int j) {
239         if (j<0) {
240             throw new IllegalArgumentException JavaDoc("This parameter must be postive : "+j);
241         }
242         return(Cascades.evaluation(this,j,vecteur));
243   }
244
245     /************************************************
246   * Return as an array the sampled values
247   * of the function
248   * @param j number of iterations
249     *************************************************/

250     public double[] evaluate (int j) {
251         return(interpolate(j));
252     }
253
254
255 }
256
Popular Tags