KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > WaveletMath


1 package JSci.maths;
2
3 /**
4 * The wavelet math library.
5 * This class cannot be subclassed or instantiated because all methods are static.
6 * @version 1.0
7 * @author Daniel Lemire
8 */

9 public final class WaveletMath extends AbstractMath {
10         private WaveletMath() {}
11
12         /**
13         * Part of the Fast Wavelet Scheme.
14         * Downsampling of a set of data points in base 2
15         * with an arbitrary filter using zero-padding
16         * at the boundaries in 1D.
17         * @return an array half the length of the input data[]
18         * as long as data.length was even.
19         */

20         public static Complex[] downsample(Complex filter[], Complex data[]) {
21                 int loc=filter.length;
22                 int demiloc=Math.round(loc/2f-0.5f);
23                 int Nombre=data.length;
24                 int DemiNombre=Math.round(Nombre/2f-0.5f);
25                 Complex answer[]=new Complex[DemiNombre];
26                 for(int i=0;i<DemiNombre;i++) {
27                         answer[i]=Complex.ZERO;
28                         for(int j=Math.max(0,2*i-demiloc+1);j<Math.min(Nombre,2*i+loc-demiloc+1);j++)
29                                 answer[i]=answer[i].add(data[j].multiply(filter[j-2*i+demiloc-1]));
30         }
31         return answer;
32         }
33     /**
34     * Part of the Fast Wavelet Scheme.
35     * Downsampling of a set of data points in base 2
36     * with an arbitrary filter using zero-padding
37     * at the boundaries in 1D.
38         * @return an array half the length of the input data[]
39         * as long as data.length was even.
40     */

41         public static double[] downsample(double filter[], double data[]) {
42         int loc=filter.length;
43         int demiloc=Math.round(loc/2f-0.5f);
44         int Nombre=data.length;
45         int DemiNombre=Math.round(Nombre/2f-0.5f);
46         double answer[]=new double[DemiNombre];
47         for(int i=0;i<DemiNombre;i++) {
48             answer[i]=0.0;
49             for(int j=Math.max(0,2*i-demiloc+1);j<Math.min(Nombre,2*i+loc-demiloc+1);j++)
50                 answer[i]+=data[j]*filter[j-2*i+demiloc-1];
51         }
52         return answer;
53         }
54     /**
55     * Insertion of zeros between every other data point in 1D.
56         * @return an array twice as long as the input data[].
57     */

58         public static Complex[] upsample(Complex data[]) {
59         int Nombre=data.length;
60         Complex answer[]=new Complex[2*Nombre];
61         for(int i=0;i<Nombre;i++) {
62             answer[2*i]=data[i];
63             answer[2*i+1]=Complex.ZERO;
64         }
65         return answer;
66         }
67     /**
68     * Insertion of zeros between every other data point in 1D.
69         * @return an array twice as long as the input data[].
70     */

71         public static double[] upsample(double data[]) {
72         int Nombre=data.length;
73         double answer[]=new double[2*Nombre];
74         for(int i=0;i<Nombre;i++) {
75             answer[2*i]=data[i];
76             answer[2*i+1]=0.0;
77         }
78         return answer;
79         }
80     /**
81     * Part of the Fast Wavelet Scheme.
82     * Upsampling of a set of data points in base 2
83     * with an arbitrary filter using zero-padding
84     * at the boundaries in 1D.
85         * @return an array twice as long as the input data[].
86     */

87         public static Complex[] upsample(Complex filter[], Complex data[]) {
88         int loc=filter.length;
89         int demiloc=Math.round(loc/2f-0.5f);
90         int Nombre=data.length;
91         Complex answer[]=new Complex[2*Nombre];
92         Complex tmp[]=new Complex[2*Nombre];
93         tmp=upsample(data);
94         for(int i=0;i<2*Nombre;i++) {
95             answer[i]=Complex.ZERO;
96             for(int j=Math.max(0,i-demiloc);j<Math.min(2*Nombre,i+loc-demiloc);j++)
97                 answer[i]=answer[i].add(tmp[j].multiply(filter[i+loc-demiloc-j-1]));
98         }
99         return answer;
100     }
101     /**
102     * Part of the Fast Wavelet Scheme.
103     * Upsampling of a set of data points in base 2
104     * with an arbitrary filter using zero-padding
105     * at the boundaries in 1D.
106         * @return an array twice as long as the input data[].
107     */

108         public static double[] upsample(double filter[], double data[]) {
109         int loc=filter.length;
110         int demiloc=Math.round(loc/2f-0.5f);
111         int Nombre=data.length;
112         double answer[]=new double[2*Nombre];
113         double tmp[]=new double[2*Nombre];
114         tmp=upsample(data);
115         for(int i=0;i<2*Nombre;i++) {
116             answer[i]=0.0;
117             for(int j=Math.max(0,i-demiloc);j<Math.min(2*Nombre,i+loc-demiloc);j++)
118                 answer[i]+=tmp[j]*filter[i+loc-demiloc-j-1];
119         }
120         return answer;
121         }
122         /**
123         * Returns the highpass filter from the lowpass filter
124         * using Cohen's formula.
125         */

126         public static double[] lowToHigh(double v[]) {
127                 double ans[]=ArrayMath.invert(v);
128                 int b=1;
129                 for(int k=0;k<ans.length;k++) {
130                         ans[k]=b*ans[k];
131                         b=-b;
132                 }
133                 return ans;
134         }
135 }
136
137
Popular Tags