KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > wavelet > cdf2_4 > FastCDF2_4


1 package JSci.maths.wavelet.cdf2_4;
2
3 import JSci.maths.wavelet.FWT;
4 import JSci.maths.*;
5 import JSci.util.*;
6
7 /**********************************
8 * Cohen-Daubechies-Feauveau
9 * with N=2 and
10 * Ntilde=4 adapted to the interval
11 * by Deslauriers-Dubuc-Lemire
12 ***********************************/

13 public class FastCDF2_4 extends FWT {
14   static int FilterType = 1;
15
16   static final float[] scale = {0.03314563036811941f , -0.06629126073623882f , -0.17677669529663687f , 0.41984465132951254f , 0.9943689110435824f , 0.41984465132951254f , -0.17677669529663687f , -0.06629126073623882f , 0.03314563036811941f};
17   static final float[] wavelet = {-0.5f , 1.0f , -0.5f};
18   static final float[][] scaleLeft = {{1.0275145414117017f , 0.7733980419227863f , -0.22097086912079608f , -0.3314563036811941f , 0.16572815184059705f},
19                         {-0.22189158107546605f , 0.4437831621509321f , 0.902297715576584f , 0.5800485314420897f , -0.25687863535292543f , -0.06629126073623882f , 0.03314563036811941f},
20                         {0.07549838028293866f , -0.15099676056587732f , -0.0957540432856783f , 0.34250484713723395f , 1.0330388131397217f , 0.41984465132951254f , -0.17677669529663687f , -0.06629126073623882f , 0.03314563036811941f},
21                         {-0.013810679320049755f , 0.02762135864009951f , 0.011048543456039804f , -0.04971844555217912f , -0.18506310288866673f , 0.41984465132951254f , 0.9943689110435824f , 0.41984465132951254f , -0.17677669529663687f , -0.06629126073623882f , 0.03314563036811941f }};
22   static final float[][] scaleRight = {{ 0.03314563036811941f , -0.06629126073623882f , -0.17677669529663687f , 0.41984465132951254f , 0.9943689110435824f , 0.41984465132951254f , -0.18506310288866673f , -0.04971844555217912f , 0.011048543456039804f , 0.02762135864009951f , -0.013810679320049755f},
23                         {0.03314563036811941f , -0.06629126073623882f , -0.17677669529663687f , 0.41984465132951254f , 1.0330388131397217f , 0.34250484713723395f , -0.0957540432856783f , -0.15099676056587732f , 0.07549838028293866f},
24                         {0.03314563036811941f , -0.06629126073623882f , -0.25687863535292543f , 0.5800485314420897f , 0.902297715576584f , 0.4437831621509321f , -0.22189158107546605f},
25                         {0.16572815184059705f , -0.3314563036811941f , -0.22097086912079608f , 0.7733980419227863f , 1.0275145414117017f}};
26 // static final float[] waveletLeft = null;
27
// static final float[] waveletRight = null;
28

29   static final float[] scalePrimary = { 0.35355339059327373f , 0.7071067811865475f , 0.35355339059327373f };
30   static final float[] waveletPrimary = { 0.0234375f , 0.046875f , -0.125f , -0.296875f , 0.703125f , -0.296875f , -0.125f , 0.046875f , 0.0234375f };
31   static final float[][] scalePrimaryLeft = {{0.7071067811865475f , 0.35355339059327373f}};
32   static final float[][] scalePrimaryRight = {{0.35355339059327373f , 0.7071067811865475f}};
33   static final float[][] waveletPrimaryLeft = {{-0.546875f , 0.5696614583333334f , -0.3138020833333333f , -0.103515625f , 0.10677083333333333f , 0.043619791666666664f , -0.01953125f , -0.009765625f },
34                       {0.234375f , -0.087890625f , -0.41015625f , 0.673828125f , -0.2421875f , -0.103515625f , 0.03515625f , 0.017578125f}};
35   static final float[][] waveletPrimaryRight = {{ 0.017578125f , 0.03515625f , -0.103515625f , -0.2421875f , 0.673828125f , -0.41015625f , -0.087890625f , 0.234375f},
36                       {-0.009765625f , -0.01953125f , 0.043619791666666664f , 0.10677083333333333f , -0.103515625f , -0.3138020833333333f , 0.5696614583333334f , -0.546875f}};
37
38   public FastCDF2_4() {
39   }
40
41   public void transform (float[] v) {
42     int last;
43     for (last = v.length;last >= 15; last = (last + FilterType) /2) {
44       transform(v , last);
45     }
46   }
47
48   public void invTransform(float[] v) {
49     int last;
50     for (last = v.length;last >= 15;last = last/2 + FilterType);
51     for (;2 * last - FilterType <= v.length;last = 2 * last - FilterType) {
52       invTransform(v,last);
53     }
54   }
55
56
57   public static void transform (float[] v,int last) {
58     float[] ans = new float[last];
59     int half = (last + FilterType) / 2;
60     if (2* half - FilterType != last)
61       throw new IllegalArgumentException JavaDoc ("Illegal subband : "+last+" within array of length "+v.length);
62     //lowpass
63
for(int k = 0; k < scaleLeft.length ; k++) {
64       for(int l = 0;l <scaleLeft[k].length; l++) {
65         ans[k]+=scaleLeft[k][l] * v[l];
66       }
67     }
68     for (int k = scaleLeft.length; k < half - scaleRight.length; k++) {
69       for(int l = 0;l <scale.length; l++) {
70
71         ans[k]+=scale[l] * v[2 * k + l - scaleLeft.length];
72
73       }
74     }
75     for(int k =0 ; k < scaleRight.length ; k++) {
76       for(int l = 0;l <scaleRight[k].length; l++) {
77         ans[k + half - scaleRight.length]+=scaleRight[k][l] * v[v.length - scaleRight[k].length + l];
78       }
79     }
80     //highpass
81
for (int k = 0; k < half - FilterType; k++) {
82       for(int l = 0;l <wavelet.length; l++) {
83         ans[k + half] += wavelet[l] * v[2 * k + l];
84   // System.out.println((k + half)+" "+(2 * k + l)+" "+wavelet[l] * v[2 * k + l]);
85
}
86     }
87     System.arraycopy(ans,0,v,0,last);
88   }
89
90
91   public static void invTransform (float[] v, int last) {
92     float[] ans = new float[2*last - FilterType];
93     //scale coefficients
94
for(int k = 0; k < scalePrimaryLeft.length; k++) {
95       for(int l = 0; l < scalePrimaryLeft[k].length; l++) {
96         ans[l] += scalePrimaryLeft[k][l] * v[k];
97       }
98     }
99     for(int k = scalePrimaryLeft.length; k < last-scalePrimaryRight.length; k++) {
100       for(int l = 0; l < scalePrimary.length; l++) {
101         ans[2 * k - FilterType + l] += scalePrimary[l]*v[k];
102       }
103     }
104     for(int k = 0; k < scalePrimaryRight.length; k++) {
105       for(int l = 0; l < scalePrimaryRight[k].length; l++) {
106         ans[l - scalePrimaryRight[k].length + ans.length] += scalePrimaryRight[k][l] * v[k + last - scalePrimaryRight.length];
107       }
108     }
109     //wavelet coefficients
110
for(int k = 0; k < waveletPrimaryLeft.length; k++) {
111       for(int l = 0; l < waveletPrimaryLeft[k].length; l++) {
112         ans[l] += waveletPrimaryLeft[k][l] * v[k + last];
113       }
114     }
115     for(int k = waveletPrimaryLeft.length; k < last - FilterType -waveletPrimaryRight.length; k++) {
116       for(int l = 0; l < waveletPrimary.length; l++) {
117         ans[2 * (k- FilterType) -1 + l ] += waveletPrimary[l]*v[k + last];
118       }
119     }
120     for(int k = 0; k < waveletPrimaryRight.length; k++) {
121       for(int l = 0; l < waveletPrimaryRight[k].length; l++) {
122         ans[l - waveletPrimaryRight[k].length + ans.length] += waveletPrimaryRight[k][l] * v[k + 2* last - FilterType- waveletPrimaryRight.length];
123       }
124     }
125     System.arraycopy(ans,0,v,0,ans.length);
126   }
127
128
129 }
130
Popular Tags