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.3
011: */
012: public final class FourierDisplay extends Frame {
013: private final int N = 128;
014: private List fns = new List(4);
015: private Checkbox inverse = new Checkbox("inverse");
016: private DefaultGraph2DModel model = new DefaultGraph2DModel();
017: private double signal[];
018: private boolean doInverse = false;
019:
020: public static void main(String arg[]) {
021: new FourierDisplay();
022: }
023:
024: private static void setDefaultSize(Component c, int defaultWidth,
025: int defaultHeight) {
026: Dimension screenSize = Toolkit.getDefaultToolkit()
027: .getScreenSize();
028: final int width = (defaultWidth < screenSize.width) ? defaultWidth
029: : screenSize.width;
030: final int height = (defaultHeight < screenSize.height) ? defaultHeight
031: : screenSize.height;
032: c.setSize(width, height);
033: }
034:
035: public FourierDisplay() {
036: super ("Fourier Display");
037: addWindowListener(new WindowAdapter() {
038: public void windowClosing(WindowEvent evt) {
039: dispose();
040: System.exit(0);
041: }
042: });
043: float xAxis[] = new float[N];
044: for (int i = 0; i < N; i++)
045: xAxis[i] = i - N / 2;
046: model.setXAxis(xAxis);
047: model.addSeries(xAxis);
048: model.addSeries(xAxis);
049: fns.add("Gaussian");
050: fns.add("Top hat");
051: fns.add("Constant");
052: fns.add("Square");
053: fns.add("Triangle");
054: fns.add("Sine");
055: fns.select(5);
056: fns.addItemListener(new ItemListener() {
057: public void itemStateChanged(ItemEvent evt) {
058: switch (fns.getSelectedIndex()) {
059: case 0:
060: signal = FourierTest.gaussian(N, 1.0, 5.0);
061: break;
062: case 1:
063: signal = FourierTest.topHat(N, 1.0);
064: break;
065: case 2:
066: signal = FourierTest.constant(N, 1.0);
067: break;
068: case 3:
069: signal = FourierTest.square(N, 1.0);
070: break;
071: case 4:
072: signal = FourierTest.triangle(N, 1.0);
073: break;
074: case 5:
075: signal = FourierTest.sine(N, 1.0, 16);
076: break;
077: }
078: displayTransform();
079: }
080: });
081: inverse.addItemListener(new ItemListener() {
082: public void itemStateChanged(ItemEvent evt) {
083: doInverse = !doInverse;
084: displayTransform();
085: }
086: });
087: LineGraph graph = new LineGraph(model);
088: graph.setColor(0, Color.red);
089: Panel cntrl = new Panel();
090: cntrl.add(fns);
091: cntrl.add(inverse);
092: add(graph, "Center");
093: add(cntrl, "South");
094: signal = FourierTest.sine(N, 1.0, 16);
095: displayTransform();
096: setDefaultSize(this , 400, 400);
097: setVisible(true);
098: }
099:
100: private void displayTransform() {
101: Complex result[];
102: if (doInverse)
103: result = FourierMath.sort(FourierMath
104: .inverseTransform(FourierMath.sort(signal)));
105: else
106: result = FourierMath.sort(FourierMath.transform(FourierMath
107: .sort(signal)));
108: float realpart[] = new float[N];
109: float imagpart[] = new float[N];
110: for (int i = 0; i < N; i++) {
111: realpart[i] = (float) result[i].real();
112: imagpart[i] = (float) result[i].imag();
113: }
114: model.changeSeries(0, realpart);
115: model.changeSeries(1, imagpart);
116: }
117: }
|