/Users/lyon/j4p/src/ip/transforms/Haar.java

1    package ip.transforms; 
2     
3    public class Haar { 
4     
5        public static void main(String args[]) { 
6            int data[][] = 
7                    { 
8                        {9, 7, 5, 3}, 
9                        {3, 5, 7, 9}, 
10                       {2, 4, 6, 8}, 
11                       {4, 6, 8, 10} 
12                   }; 
13           int in[] = {9, 7, 5, 3, 9, 7, 5, 3}; 
14           fhw(in, in.length); 
15           println(in); 
16           decompositionStep(in, in.length); 
17           println(in); 
18    
19       } 
20    
21       public static void fhw(int in[], int size) { 
22           int tmp[] = new int[size]; 
23    
24           for (int i = 0; i < size; i += 2) { 
25               tmp[i / 2] = (in[i] + in[i + 1]) / 2; 
26               tmp[size / 2 + i / 2] = (in[i] - in[i + 1]) / 2; 
27           } 
28    
29           for (int i = 0; i < size; i++) 
30               in[i] = tmp[i]; 
31           show(in, size, true); 
32       } 
33    
34       public static void show(int in[], int size, boolean colon) { 
35    
36           for (int i = 0; i < size; i++) { 
37               if ((size / 2 == i) && colon) 
38                   System.out.print(", "); 
39               System.out.print(in[i] + "\t"); 
40           } 
41           System.out.println(""); 
42       } 
43    
44       public static void fhw2(int in[]) { 
45           int g = in.length; 
46           while (g > 2) { 
47               println(in); 
48               decompositionStep(in, g); 
49               g = g / 2; 
50           } 
51       } 
52    
53       public static void decompositionStep(int a[], int g) { 
54           println("g=" + g); 
55           println("g/2=" + g / 2); 
56           int c[] = new int[a.length]; 
57           for (int i = 1; i < g / 2; i++) { 
58               c[i] = (a[2 * i - 1] + a[2 * i]) / 2; 
59               c[g / 2 + i] = (a[2 * i - 1] - a[2 * i]) / 2; 
60           } 
61           for (int i = 0; i < c.length; i++) 
62               a[i] = c[i]; 
63       } 
64    
65       public static void println(int a[]) { 
66           print(a); 
67           System.out.println(); 
68       } 
69    
70       public static void println(String s) { 
71           System.out.println(s); 
72       } 
73    
74       public static void print(String s) { 
75           System.out.print(s); 
76       } 
77    
78       public static void print(int a[]) { 
79           for (int i = 0; i < a.length; i++) 
80               System.out.print(a[i] + " "); 
81       } 
82    
83   }