1 import java.awt.*; 2 import java.awt.event.*; 3 import JSci.awt.*; 4 import JSci.maths.*; 5 import JSci.tests.FourierTest; 6 7 12 public final class FourierDisplay extends Frame { 13 private final int N=128; 14 private List fns=new List(4); 15 private Checkbox inverse=new Checkbox("inverse"); 16 private DefaultGraph2DModel model=new DefaultGraph2DModel(); 17 private double signal[]; 18 private boolean doInverse=false; 19 20 public static void main(String arg[]) { 21 new FourierDisplay(); 22 } 23 private static void setDefaultSize(Component c, int defaultWidth, int defaultHeight) { 24 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 25 final int width = (defaultWidth < screenSize.width) ? defaultWidth : screenSize.width; 26 final int height = (defaultHeight < screenSize.height) ? defaultHeight : screenSize.height; 27 c.setSize(width, height); 28 } 29 public FourierDisplay() { 30 super("Fourier Display"); 31 addWindowListener(new WindowAdapter() { 32 public void windowClosing(WindowEvent evt) { 33 dispose(); 34 System.exit(0); 35 } 36 }); 37 float xAxis[]=new float[N]; 38 for(int i=0;i<N;i++) 39 xAxis[i]=i-N/2; 40 model.setXAxis(xAxis); 41 model.addSeries(xAxis); 42 model.addSeries(xAxis); 43 fns.add("Gaussian"); 44 fns.add("Top hat"); 45 fns.add("Constant"); 46 fns.add("Square"); 47 fns.add("Triangle"); 48 fns.add("Sine"); 49 fns.select(5); 50 fns.addItemListener(new ItemListener() { 51 public void itemStateChanged(ItemEvent evt) { 52 switch(fns.getSelectedIndex()) { 53 case 0 : signal = FourierTest.gaussian(N,1.0,5.0); 54 break; 55 case 1 : signal = FourierTest.topHat(N,1.0); 56 break; 57 case 2 : signal = FourierTest.constant(N,1.0); 58 break; 59 case 3 : signal = FourierTest.square(N,1.0); 60 break; 61 case 4 : signal = FourierTest.triangle(N,1.0); 62 break; 63 case 5 : signal = FourierTest.sine(N,1.0,16); 64 break; 65 } 66 displayTransform(); 67 } 68 }); 69 inverse.addItemListener(new ItemListener() { 70 public void itemStateChanged(ItemEvent evt) { 71 doInverse=!doInverse; 72 displayTransform(); 73 } 74 }); 75 LineGraph graph=new LineGraph(model); 76 graph.setColor(0,Color.red); 77 Panel cntrl=new Panel(); 78 cntrl.add(fns); 79 cntrl.add(inverse); 80 add(graph,"Center"); 81 add(cntrl,"South"); 82 signal = FourierTest.sine(N, 1.0, 16); 83 displayTransform(); 84 setDefaultSize(this, 400, 400); 85 setVisible(true); 86 } 87 private void displayTransform() { 88 Complex result[]; 89 if(doInverse) 90 result=FourierMath.sort(FourierMath.inverseTransform(FourierMath.sort(signal))); 91 else 92 result=FourierMath.sort(FourierMath.transform(FourierMath.sort(signal))); 93 float realpart[]=new float[N]; 94 float imagpart[]=new float[N]; 95 for(int i=0;i<N;i++) { 96 realpart[i]=(float)result[i].real(); 97 imagpart[i]=(float)result[i].imag(); 98 } 99 model.changeSeries(0,realpart); 100 model.changeSeries(1,imagpart); 101 } 102 } 103 | Popular Tags |