KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Denoise


1 import java.awt.Color JavaDoc;
2 import java.awt.Frame JavaDoc;
3 import java.awt.GridLayout JavaDoc;
4 import java.awt.Label JavaDoc;
5 import java.awt.Panel JavaDoc;
6 import java.awt.event.WindowAdapter JavaDoc;
7 import java.awt.event.WindowEvent JavaDoc;
8 import java.io.BufferedReader JavaDoc;
9 import java.io.FileReader JavaDoc;
10 import java.io.FileWriter JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.util.StringTokenizer JavaDoc;
13
14 import JSci.awt.DefaultGraph2DModel;
15 import JSci.maths.wavelet.FWTCoef;
16 import JSci.maths.wavelet.Signal;
17 import JSci.maths.wavelet.daubechies2.Daubechies2;
18 import JSci.maths.wavelet.daubechies3.Daubechies3;
19 import JSci.maths.wavelet.daubechies4.Daubechies4;
20 import JSci.maths.wavelet.daubechies5.Daubechies5;
21 import JSci.maths.wavelet.daubechies6.Daubechies6;
22 import JSci.maths.wavelet.daubechies7.Daubechies7;
23 import JSci.maths.wavelet.daubechies8.Daubechies8;
24 import JSci.swing.JGraphLayout;
25 import JSci.swing.JLineGraph;
26
27 /**
28  * Example class for denoising a signal.
29  * <p>
30  * In this case an astronomical spectrum of a faint galaxy stored in
31  * the file "galaxy.txt" is used. The denoised spectrum is written to
32  * file "galaxy_denoise.txt" and both spectra are plotted in a graph.
33  *
34  * @authors Peter W. Draper (p.w.draper@durham.ac.uk).
35  */

36 public class Denoise extends Frame JavaDoc
37 {
38     public static void main( String JavaDoc[] args )
39     {
40         Denoise denoise = new Denoise();
41     }
42
43     protected Denoise()
44     {
45         super( "Denoise graphs" );
46         addWindowListener( new WindowAdapter JavaDoc() {
47                 public void windowClosing( WindowEvent JavaDoc evt )
48                 {
49                     dispose();
50                     System.exit(0);
51                 }
52             });
53         setLayout( new GridLayout JavaDoc( 3, 1 ) );
54         setSize( 400, 800 );
55
56         if ( readData() ) {
57             doCalcs();
58             doDisplay();
59             writeData();
60         }
61     }
62     
63     // Choose a filter to use.
64
//private Daubechies8 filter = new Daubechies8();
65
private Daubechies2 filter = new Daubechies2();
66     //private Daubechies6 filter = new Daubechies6();
67
//private Daubechies7 filter = new Daubechies7();
68
//private Daubechies4 filter = new Daubechies4();
69

70     /** Data read from input file, plus padding */
71     private double[] noisy = null;
72
73     /** Data after it has been denoised */
74     private double[] clean = null;
75
76     /** Offset into data array where real values start */
77     private int offset = 0;
78
79     /** Number of lines read from input file */
80     private int nlines = 0;
81
82     /** Power of 2 used to create data areas */
83     private int maxlevel = 1;
84
85     /** Name of input file */
86     public static final String JavaDoc INFILE = "galaxy.txt";
87
88     /** Name of output file */
89     public static final String JavaDoc OUTFILE = "galaxy_denoise.txt";
90
91     /** Denoise threshold (0-1). */
92     public static double threshold = 0.5;
93
94     /**
95      * Read in the "noisy" data.
96      */

97     private boolean readData()
98     {
99         int filtertype = filter.getFilterType();
100
101         // Read the data from the text file. This just uses the first
102
// word from each line.
103
try {
104             BufferedReader JavaDoc r = new BufferedReader JavaDoc( new FileReader JavaDoc( INFILE ) );
105             String JavaDoc raw = null;
106
107             // Count lines.
108
while ( ( raw = r.readLine() ) != null ) {
109                 nlines++;
110             }
111             r.close();
112
113             // Need a power of 2 for data size, plus padding for
114
// dyadic multiresolution scaling functions.
115
maxlevel = 1;
116             while ( nlines > Math.pow( 2.0, (double) maxlevel ) ) {
117                 maxlevel++;
118             }
119             int count = (int) Math.pow( 2.0, (double) maxlevel ) + filtertype;
120
121             // Now really read data. If buffered place near centre.
122
r = new BufferedReader JavaDoc( new FileReader JavaDoc( INFILE ) );
123             noisy = new double[count];
124             offset = count = ( count - nlines ) / 2;
125             StringTokenizer JavaDoc st = null;
126             while ( ( raw = r.readLine() ) != null ) {
127                 st = new StringTokenizer JavaDoc( raw );
128                 noisy[count++] = Float.parseFloat( st.nextToken() );
129             }
130             r.close();
131
132             // Fill any buffered regions with end values.
133
double value = noisy[offset];
134             for ( int i = 0; i < offset; i++ ) {
135                 noisy[i] = value;
136             }
137             value = noisy[offset + nlines - 1];
138             for ( int i = offset + nlines; i < noisy.length; i++ ) {
139                 noisy[i] = value;
140             }
141         }
142         catch ( Exception JavaDoc e ) {
143             e.printStackTrace();
144             return false;
145         }
146         return true;
147     }
148
149     /**
150      * Determine the wavelet coefficient and generate a denoised
151      * signal by setting some of the coefficients to zero and
152      * re-generating.
153      */

154     private void doCalcs()
155     {
156         // Choose a maximum level and use that. Note 20 is max
157
// possible, and we need to leave space for filtertype
158
// padding (-4).
159
int level = Math.min( maxlevel - 4, 20 );
160
161         // Make the Signal and filter it.
162
Signal signal = new Signal( noisy );
163         signal.setFilter( filter );
164         FWTCoef signalCoeffs = signal.fwt( level );
165
166         // Zero any coefficients that are less than some fraction of
167
// the total sum.
168
signalCoeffs.denoise( threshold );
169
170         // Rebuild the signal with the new set of coefficients.
171
double[] rebuild =
172             signalCoeffs.rebuildSignal( filter ).evaluate( 0 );
173
174         // Trim padding away from all data.
175
double[] trimmed = new double[nlines];
176         System.arraycopy( noisy, offset, trimmed, 0, nlines );
177         noisy = trimmed;
178         clean = new double[nlines];
179         System.arraycopy( rebuild, offset, clean, 0, nlines );
180     }
181
182     /**
183      * Write the denoised data to an output file.
184      */

185     private void writeData()
186     {
187         try {
188             PrintWriter JavaDoc r = new PrintWriter JavaDoc( new FileWriter JavaDoc( OUTFILE ) );
189             for ( int i = 0; i < clean.length; i++ ) {
190                 r.println( clean[i] );
191             }
192             r.close();
193         }
194         catch (Exception JavaDoc e) {
195             e.printStackTrace();
196         }
197     }
198     
199     /**
200      * Draw a graph showing the original data and the denoised version
201      * for comparison.
202      */

203     private void doDisplay()
204     {
205         DefaultGraph2DModel model1 = new DefaultGraph2DModel();
206         model1.setXAxis( 0.0f, (float) noisy.length, noisy.length );
207         model1.addSeries( noisy );
208         model1.addSeries( clean );
209         model1.setSeriesVisible( 1, true );
210
211         Panel JavaDoc panel1 = new Panel JavaDoc();
212         panel1.setLayout( new JGraphLayout() );
213         Label JavaDoc title = new Label JavaDoc( "Overlay graph", Label.CENTER );
214         panel1.add( title, "Title" );
215         JLineGraph graph1 = new JLineGraph( model1 );
216         graph1.setColor(0, Color.red);
217         graph1.setColor(1, Color.blue);
218         panel1.add( graph1, "Graph" );
219         add( panel1 );
220
221         DefaultGraph2DModel model2 = new DefaultGraph2DModel();
222         model2.setXAxis( 0.0f, (float) noisy.length, noisy.length );
223         model2.addSeries( noisy );
224
225         Panel JavaDoc panel2 = new Panel JavaDoc();
226         panel2.setLayout( new JGraphLayout() );
227         title = new Label JavaDoc( "Raw data", Label.CENTER );
228         panel2.add( title, "Title" );
229         JLineGraph graph2 = new JLineGraph( model2 );
230         graph2.setColor(0, Color.red);
231         panel2.add( graph2, "Graph" );
232         add( panel2 );
233
234         DefaultGraph2DModel model3 = new DefaultGraph2DModel();
235         model3.setXAxis( 0.0f, (float) clean.length, clean.length );
236         model3.addSeries( clean );
237
238         Panel JavaDoc panel3 = new Panel JavaDoc();
239         panel3.setLayout( new JGraphLayout() );
240         title = new Label JavaDoc( "Denoised data", Label.CENTER );
241         panel3.add( title, "Title" );
242         JLineGraph graph3 = new JLineGraph( model3 );
243         graph3.setColor(0, Color.blue);
244         panel3.add( graph3, "Graph" );
245         add( panel3 );
246
247         setVisible( true );
248     }
249 }
250
Popular Tags