KickJava   Java API By Example, From Geeks To Geeks.

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


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 piecewise constant
10 * splines to be used as wavelets or related
11 * functions. It can also be used for basic
12 * interpolation.
13 * @author Daniel Lemire
14 *****************************************/

15 public class PiecewiseConstant extends Spline implements Filter, Cloneable JavaDoc {
16     protected final static int filtretype=0;
17     private double[] vecteur;
18     static final double[] vg={1d,1d};
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   /*****************************************
29   * Check if another object is equal to this
30   * PiecewiseConstant object
31   ******************************************/

32   public boolean equals(Object JavaDoc a) {
33     if(a!=null && (a instanceof PiecewiseConstant) && vecteur.length==((PiecewiseConstant)a).dimension()) {
34       PiecewiseConstant iv=(PiecewiseConstant)a;
35         for(int i=0;i<vecteur.length;i++) {
36           if(vecteur[i]!=iv.getValue(i))
37             return false;
38           }
39         return true;
40       } else
41     return false;
42   }
43     /****************************************
44   * This method is used to compute
45   * how the number of scaling functions
46   * changes from on scale to the other.
47   * Basically, if you have k scaling
48   * function and a Filter of type t, you'll
49   * have 2*k+t scaling functions at the
50   * next scale (dyadic case).
51   * Notice that this method assumes
52   * that one is working with the dyadic
53   * grid while the method "previousDimension"
54   * define in the interface "Filter" doesn't.
55     ******************************************/

56     public int getFilterType () {
57                 return(filtretype);
58     }
59     /****************************************
60   * This method return the number of "scaling"
61   * functions at the previous scale given a
62   * number of scaling functions. The answer
63   * is always smaller than the provided value
64   * (about half since this is a dyadic
65   * implementation). This relates to the same idea
66   * as the "Filter type". It is used by
67   * the interface "Filter".
68     *****************************************/

69     public int previousDimension(int k) {
70         int i=(int) Math.round(k/2d);
71         if(2*i==k) {
72             return(i);
73         } else {
74             throw new IllegalArgumentException JavaDoc("Odd number of values into an even Filter.");
75         }
76     }
77
78
79
80     /****************************************
81     * This is the implementation of the lowpass
82   * Filter. It is used by the interface
83   * "Filter". Lowpass filters are normalized
84   * so that they preserve constants away from
85   * the boundaries.
86     *****************************************/

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

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

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

129     public double[] highpass (double[] gete) {
130         if(gete.length<2) {
131             throw new IllegalArgumentException JavaDoc("The array is not long enough : "+gete.length+" < 2");
132         }
133         double[] data=Cascades.doublesample(gete);
134         int dl=data.length-1;
135         double[] sortie=new double[2*gete.length];
136         double[] vg={-1d,1d};
137         for(int k=0;k<=dl-1;k++) {
138             sortie[k]=ArrayMath.scalarProduct(ArrayMath.extract(k,k+1,data),vg);
139         }
140         sortie[dl]=ArrayMath.scalarProduct(ArrayMath.extract(dl-vg.length+2,dl,data),ArrayMath.extract(0,vg.length-2,vg));
141         return(sortie);
142     }
143
144     /************************************
145     *************************************/

146     public PiecewiseConstant(double[] v) {
147         vecteur=v;
148     }
149
150     /************************************
151     *************************************/

152     public PiecewiseConstant() {
153     }
154   /********************************************
155   * Return a copy of this object
156   *********************************************/

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

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

180     public void setValues(double[] v) {
181         vecteur=v;
182     }
183
184   /******************************************
185   * Compute the mass (integral)
186   * @param a left boundary of the interval
187   * @param b right boundary of the interval
188   **********************************************/

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

203    public void setValue(int i, double d) {
204      if (i<0) {
205        throw new IllegalArgumentException JavaDoc("The parameter must be positive : "+i+" < 0");
206      }
207      vecteur[i]=d;
208    }
209
210     /*********************************************
211   * compute the derivative of the function -
212   * useful for numerical analysis
213     **********************************************/

214   public SumOfDiracs derive() {
215     return(derive(0,1));
216   }
217
218     /*********************************************
219   * compute the derivative of the function -
220   * useful for numerical analysis
221   * @param a left boundary of the interval
222   * @param b right boundary of the interval
223     **********************************************/

224   public SumOfDiracs derive(double a, double b) {
225
226     double[] v=new double[vecteur.length+1];
227     for(int i=1;i<vecteur.length;i++) {
228       v[i]=(vecteur[i]-vecteur[i-1])*vecteur.length/Math.abs(b-a);
229     }
230     v[0]=0;
231     v[vecteur.length]=0;
232     SumOfDiracs d=new SumOfDiracs(v);
233   return(d);
234   }
235
236   /*********************************************
237   * Number of knots
238   **********************************************/

239   public int dimension () {
240         return(vecteur.length+1);
241   }
242     /*********************************************
243   * Number of knots after j iterations
244   * @param j number of iterations
245     **********************************************/

246   public int dimension(int j) {
247     return(Cascades.dimensionHaar(vecteur.length, j));
248   }
249
250     /************************************************
251   * Return as an array the interpolated values
252   * of the function. Will return the same values
253   * as the evaluate method because the Filter
254   * is interpolatory.
255   * @param j number of iterations
256     *************************************************/

257     public double[] interpolate(int j) {
258         if (j<0) {
259             throw new IllegalArgumentException JavaDoc("This parameter must be postive : "+j);
260         }
261         return(Cascades.evaluation(this,j,vecteur));
262     }
263
264     /************************************************
265   * Return as an array the sampled values
266   * of the function
267   * @param j number of iterations
268     *************************************************/

269     public double[] evaluate (int j) {
270         return(interpolate(j));
271     }
272 }
273
Popular Tags