001: import java.awt.*;
002: import java.awt.event.*;
003: import JSci.awt.*;
004: import JSci.maths.*;
005: import JSci.tests.FourierTest;
006:
007: /**
008: * Sample program demonstrating use of FourierMath and LineGraph classes.
009: * @author Mark Hale
010: * @version 1.0
011: */
012: public final class FourierDisplay2 extends Frame {
013: private final int N = 128;
014: private List fns = new List(4);
015: private DefaultGraph2DModel signalModel = new DefaultGraph2DModel();
016: private DefaultGraph2DModel transformModel = new DefaultGraph2DModel();
017: private double signal[];
018:
019: public static void main(String arg[]) {
020: new FourierDisplay2();
021: }
022:
023: private static void setDefaultSize(Component c, int defaultWidth,
024: int defaultHeight) {
025: Dimension screenSize = Toolkit.getDefaultToolkit()
026: .getScreenSize();
027: final int width = (defaultWidth < screenSize.width) ? defaultWidth
028: : screenSize.width;
029: final int height = (defaultHeight < screenSize.height) ? defaultHeight
030: : screenSize.height;
031: c.setSize(width, height);
032: }
033:
034: public FourierDisplay2() {
035: super ("Fourier Display 2");
036: addWindowListener(new WindowAdapter() {
037: public void windowClosing(WindowEvent evt) {
038: dispose();
039: System.exit(0);
040: }
041: });
042: float xAxis[] = new float[N];
043: for (int i = 0; i < N; i++)
044: xAxis[i] = i - N / 2;
045: signalModel.setXAxis(xAxis);
046: signalModel.addSeries(xAxis);
047: transformModel.setXAxis(xAxis);
048: transformModel.addSeries(xAxis);
049: transformModel.addSeries(xAxis);
050: fns.add("Gaussian");
051: fns.add("Top hat");
052: fns.add("Constant");
053: fns.add("Square");
054: fns.add("Triangle");
055: fns.add("Sine");
056: fns.select(5);
057: fns.addItemListener(new ItemListener() {
058: public void itemStateChanged(ItemEvent evt) {
059: switch (fns.getSelectedIndex()) {
060: case 0:
061: signal = FourierTest.gaussian(N, 1.0, 5.0);
062: break;
063: case 1:
064: signal = FourierTest.topHat(N, 1.0);
065: break;
066: case 2:
067: signal = FourierTest.constant(N, 1.0);
068: break;
069: case 3:
070: signal = FourierTest.square(N, 1.0);
071: break;
072: case 4:
073: signal = FourierTest.triangle(N, 1.0);
074: break;
075: case 5:
076: signal = FourierTest.sine(N, 1.0, 16);
077: break;
078: }
079: displaySignal();
080: displayTransform();
081: }
082: });
083: LineGraph signalGraph = new LineGraph(signalModel);
084: signalGraph.setColor(0, Color.red);
085: LineGraph transformGraph = new LineGraph(transformModel);
086: transformGraph.setColor(0, Color.red);
087: Panel graphs = new Panel();
088: graphs.setLayout(new GridLayout(1, 2));
089: graphs.add(signalGraph);
090: graphs.add(transformGraph);
091: add(graphs, "Center");
092: add(fns, "South");
093: signal = FourierTest.sine(N, 1.0, 16);
094: displaySignal();
095: displayTransform();
096: setDefaultSize(this , 600, 400);
097: setVisible(true);
098: }
099:
100: private void displaySignal() {
101: signalModel.changeSeries(0, signal);
102: }
103:
104: private void displayTransform() {
105: Complex result[] = FourierMath.sort(FourierMath
106: .transform(FourierMath.sort(signal)));
107: float realpart[] = new float[N];
108: float imagpart[] = new float[N];
109: for (int i = 0; i < N; i++) {
110: realpart[i] = (float) result[i].real();
111: imagpart[i] = (float) result[i].imag();
112: }
113: transformModel.changeSeries(0, realpart);
114: transformModel.changeSeries(1, imagpart);
115: }
116: }
|