KickJava   Java API By Example, From Geeks To Geeks.

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


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

13 public class LinearSpline extends Spline implements Filter, Cloneable JavaDoc {
14     protected final static int filtretype=1;
15     private double[] vecteur;
16     static final double[] vg={1/2d,1d,1/2d};
17     static final double[] v0={1d,1/2d};
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   /*****************************************
44   * Check if another object is equal to this
45   * LinearSpline object
46   ******************************************/

47   public boolean equals(Object JavaDoc a) {
48     if(a!=null && (a instanceof LinearSpline) && vecteur.length==((LinearSpline)a).dimension()) {
49       LinearSpline iv=(LinearSpline)a;
50         for(int i=0;i<vecteur.length;i++) {
51           if(vecteur[i]!=iv.getValue(i))
52             return false;
53           }
54         return true;
55       } else
56     return false;
57   }
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         int i=(int) Math.round((k+1)/2d);
72         if(2*i-1==k) {
73             return(i);
74         } else {
75             throw new IllegalArgumentException JavaDoc("Even number of values into an odd Filter! Change the number of data values/of iterations.");
76         }
77     }
78
79     /****************************************
80     * This is the implementation of the lowpass
81   * Filter. It is used by the interface
82   * "Filter". Lowpass filters are normalized
83   * so that they preserve constants away from
84   * the boundaries.
85     *****************************************/

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

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

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

135     public double[] highpass (double[] donnee) {
136         return(lowpass(donnee));
137     }
138
139     /***************************************
140     ****************************************/

141     public LinearSpline(double[] v) {
142         vecteur=v;
143     }
144
145
146     /***************************************
147     ****************************************/

148     public LinearSpline() {
149     }
150   /********************************************
151   * Return a copy of this object
152   *********************************************/

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

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

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

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

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

208     public PiecewiseConstant derive(double a, double b) {
209         double[] v=new double[vecteur.length-1];
210         for(int i=0;i<vecteur.length-1;i++) {
211                 v[i]=(vecteur[i+1]-vecteur[i])*(vecteur.length-1)/Math.abs(b-a);
212         }
213         PiecewiseConstant d=new PiecewiseConstant(v);
214         return(d);
215     }
216
217   /*********************************************
218   * Number of knots
219   **********************************************/

220    public int dimension () {
221         return(vecteur.length);
222    }
223
224     /*********************************************
225   * Number of knots after j iterations
226   * @param j number of iterations
227     **********************************************/

228     public int dimension(int j) {
229         return(Cascades.dimension(vecteur.length, j));
230     }
231
232     /************************************************
233   * Return as an array the interpolated values
234   * of the function. Will return the same values
235   * as the evaluate method because the Filter
236   * is interpolatory.
237   * @param j number of iterations
238     *************************************************/

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

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