KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package JSci.maths.wavelet;
3
4 import JSci.maths.*;
5
6 /**********************************************
7 * This class is used to be able to mix the wavelet
8 * and cosine transforms. It is in fact a normalised
9 * cosine.
10 * @author Daniel Lemire
11 *************************************************/

12 public final class Cosine extends MultiscaleFunction implements NumericalConstants, Cloneable JavaDoc {
13         private int n0; private int freq;private double normalisation;
14         double tol=Double.MIN_VALUE;
15   /*******************************
16   * Return a String representation
17   * of the object
18   ********************************/

19   public String JavaDoc toString() {
20     String JavaDoc ans=new String JavaDoc("[n0=");
21     ans.concat(Integer.toString(n0));
22     ans.concat("][freq=");
23     ans.concat(Integer.toString(freq));
24     ans.concat("]");
25     return(ans);
26   }
27
28   /*****************************************
29   * Check if another object is equal to this
30   * Cosine object
31   ******************************************/

32   public boolean equals(Object JavaDoc a) {
33     if((a!=null) && (a instanceof Cosine)) {
34       Cosine iv=(Cosine)a;
35       return (this.dimension(0)==iv.dimension(0)) && (this.getFrequency()==iv.getFrequency());
36     }
37     return false;
38   }
39
40   public int getFrequency() {
41     return(freq);
42   }
43
44         public Cosine (int N0,int FREQ) {
45             if(N0<0) {
46                 throw new IllegalArgumentException JavaDoc("The length paramenter "+n0+" must be positive");
47             }
48             if((FREQ<0)||(FREQ>=N0)) {
49                 throw new IllegalArgumentException JavaDoc("The frequency parameter "+FREQ+" must be between "+0+" and "+(N0-1));
50             }
51             n0=N0;freq=FREQ;
52             normalisation=Math.sqrt(n0/2d);
53             if((2*freq==n0)||(freq==0)) {
54                 normalisation*=SQRT2;
55             }
56         }
57     /************************************************
58   * Return as an array the sampled values
59   * of the function
60     *************************************************/

61         public double[] evaluate() {
62             return(ArrayMath.scalarMultiply(1/normalisation,evaluate(n0,freq)));
63         }
64
65     private static double[] evaluate(int N0,int FREQ) {
66             double[] ans=new double[N0];
67             for(int k=0;k<ans.length;k++) {
68                 ans[k]=Math.cos(TWO_PI*k*FREQ/N0);
69             }
70             return(ans);
71     }
72   /*****************************************
73   * Tells you how many samples you'll get
74   * from this function (will not depend
75   * on the parameter)
76   ******************************************/

77     public int dimension(int jfin) {
78         return(n0);
79     }
80   /*****************************************
81   * Tells you how many samples you'll get
82   * from this function
83   ******************************************/

84     public int dimension() {
85         return(n0);
86     }
87   /********************************************
88   * Return a copy of this object
89   *********************************************/

90     public Object JavaDoc clone() {
91     Cosine c = (Cosine)super.clone();
92     c.n0=n0;
93     c.freq=freq;
94         return(c);
95     }
96     /************************************************
97   * Return as an array the sampled values
98   * of the function
99   * @param j number of iterations (doesn't do anything)
100     *************************************************/

101     public double[] evaluate (int j) {
102         return(evaluate());
103     }
104   /******************************************
105   * Compute the mass (integral)
106   * @param a left boundary of the interval
107   * @param b right boundary of the interval
108   * @param jfin number of iterations to consider
109   * (precision)
110   **********************************************/

111     public double mass(double a, double b, int jfin) {
112         double somme=0;
113         double[] values=evaluate(jfin);
114         for(int k=0;k<values.length;k++) {
115             somme+=values[k];
116         }
117         somme=somme/(values.length-1)*Math.abs(b-a);
118         return(somme);
119     }
120     /****************************************
121   * This method is used to compute
122   * how the number of scaling functions
123   * changes from on scale to the other.
124   * Basically, if you have k scaling
125   * function and a Filter of type t, you'll
126   * have 2*k+t scaling functions at the
127   * next scale (dyadic case).
128   * Notice that this method assumes
129   * that one is working with the dyadic
130   * grid while the method "previousDimension"
131   * define in the interface "Filter" doesn't.
132     ******************************************/

133     public int getFilterType () {
134         return(n0);
135     }
136 }
137
138
Popular Tags