Example #1
0
/**
 * Creation date: (2/25/2004 10:53:54 AM)
 *
 * @author: Jarek Foltynski
 */
public abstract class FFT {
  public static final String items[] = {
    "INTEGER", "FLOAT", "NATIVE",
  };

  // Those values can be changed
  public int NO_OF_BITS; // 	number of bits in sample number
  public int MAX_AMPLITUDE_BITS; // max number of bits in signal amplitude

  public static final int DEFAULT_SAMPLES_NUMBER = 1024;
  public static final int DEFAULT_MAX_AMPLITUDE = 1024;

  public static final int C_INTEGER = 0;
  public static final int C_FLOAT = 1;
  public static final int C_NATIVE = 2;
  public static final int C_DOUBLE = 3;
  public static final int C_LONG = 4;

  public int samplesNumber;
  public int maxAmplitude;
  public int oPB;
  public int oPBV;
  public int type;

  protected static boolean debug = bioera.Debugger.get("fft");

  protected FFT(int len, int amp, int outputPrecBits) {
    samplesNumber = len;
    maxAmplitude = amp;
    oPB = outputPrecBits;
    oPBV = 1 << oPB;
  }

  public static final FFT getFFT(int type, int length, int maxAmplitude, int outputPrecisionBits)
      throws Exception {
    return newInstance(type, length, maxAmplitude, outputPrecisionBits);
  }

  public abstract int getPrecisionFactor();

  public void init() throws Exception {
    int x = 0;
    while ((1 << x) < samplesNumber) {
      x++;
    }
    NO_OF_BITS = x;

    if ((1 << NO_OF_BITS) != samplesNumber)
      throw new RuntimeException(
          "The length of the sample " + samplesNumber + " must be power of 2 (" + NO_OF_BITS + ")");

    x = 0;
    while ((1 << x) < maxAmplitude) {
      x++;
    }
    MAX_AMPLITUDE_BITS = x;
  }

  protected static final void initializeNative() {
    try {
      System.loadLibrary("bioerafft");
    } catch (Throwable e) {
      // e.printStackTrace();
      if (debug) {
        System.out.println("" + e.getMessage());
        System.out.println(
            "Native implementation (bioerafft) of FFT not found in "
                + System.getProperty("java.library.path")
                + ", using pure version");
      }
    }
  }

  public static final FFT newInstance() throws Exception {
    return newInstance(C_INTEGER, DEFAULT_SAMPLES_NUMBER, DEFAULT_MAX_AMPLITUDE, 0);
  }

  public static final FFT newInstance(
      int itype, int length, int maxAmplitude, int outputPrecisionBits) throws Exception {
    if (itype == C_NATIVE) initializeNative();

    FFT ret;
    switch (itype) {
      case C_INTEGER:
        ret = new FFTInteger(length, maxAmplitude, outputPrecisionBits);
        break;
      case C_FLOAT:
        ret = new FFTFloat(length, maxAmplitude, outputPrecisionBits);
        break;
      case C_NATIVE:
        ret = new FFTNative(length, maxAmplitude, outputPrecisionBits);
        break;
      case C_DOUBLE:
        ret = new FFTDouble(length, maxAmplitude, outputPrecisionBits);
        break;
      case C_LONG:
        ret = new FFTInteger(length, maxAmplitude, outputPrecisionBits);
        break;
      default:
        throw new RuntimeException("Unknown type: " + itype);
    }

    ret.type = itype;
    ret.init();

    return ret;
  }

  public abstract void perform(int input[], int output[]);

  public int type() throws Exception {
    return this.type;
  }

  public void performComplex(int inpRe[], int inpIm[], int outRe[], int outIm[]) {
    throw new RuntimeException("Complex transform not implemented");
  }

  public static void setDebug(boolean newValue) {
    debug = newValue;
  }
}
/**
 * Creation date: (2/25/2004 10:53:54 AM)
 *
 * @author: Jarek Foltynski
 */
public class DialogNewElement extends GenericDialog implements ActionListener {
  JButton bAdd;
  JButton bOK;
  JButton bCancel;
  JList list;
  int cX, cY;
  StringBuffer keys = new StringBuffer();
  BoxItem newItem;

  private static boolean debug = bioera.Debugger.get("designer.newdialog");
  //	BoxItem box;
  /** EdiDialog constructor comment. */
  public DialogNewElement(JFrame f) {
    super(f, "New Element");
  }
  //	BoxItem box;
  /** EdiDialog constructor comment. */
  public DialogNewElement(JFrame f, String name) {
    super(f, name);
  }
  /** Invoked when an action occurs. */
  public void actionAdd() {
    int i = list.getSelectedIndex();

    if (i == -1) return;

    newItem =
        createNewElement(
            (String) DesignFrame.processingElements[list.getSelectedIndex()][1], cX, cY);
  }
  /** Invoked when an action occurs. */
  public void actionOK() {
    int i = list.getSelectedIndex();

    if (i == -1) return;

    newItem =
        createNewElement(
            (String) DesignFrame.processingElements[list.getSelectedIndex()][1], cX, cY);

    dispose();
  }
  /** Invoked when an action occurs. */
  public void actionPerformed(java.awt.event.ActionEvent event) {
    // System.out.println("command=" + event.getActionCommand());

    if (event.getSource() == bCancel) {
      dispose();
      return;
    }

    if (event.getSource() == bAdd) actionAdd();
    else actionOK();
  }
  /** Invoked when an action occurs. */
  public static BoxItem createNewElement(String className, int posX, int posY) {
    BoxItem ret = null;
    Element elem = null;
    try {
      if (debug) System.out.println("Creating new element '" + className + "'");
      elem = Element.newInstance(className);

      if (elem == null) {
        if (debug) System.out.println("Element '" + className + "' not found");
        return null;
      }

      ret = elem.getDesignerBox();
      ret.setBounds(
          (posX / DesignEventHandler.ELEM_STEP) * DesignEventHandler.ELEM_STEP,
          (posY / DesignEventHandler.ELEM_STEP) * DesignEventHandler.ELEM_STEP,
          ConfigurableSystemSettings.getDesignerElementWidth(),
          ConfigurableSystemSettings.getDesignerElementHeight());
      Main.app.designFrame.addElement(ret);
      Item.highlighted.clear();
      Item.highlighted.add(ret);
      Main.app.processor.add(elem);
      // elem.setActive(false);
      Main.app.designFrame.panel.repaint();

      if (elem instanceof Display) {
        Chart chart = ((Display) elem).newChart();
        ((Display) elem).setChart(chart);
        chart.setAtTopLayer();
        // System.out.println("c1 " + chart.getBounds());
        Main.app.runtimeFrame.addChart(chart);
        // Main.app.runtimeFrame.show();
        Main.app.runtimeFrame.setVisible();
        // Main.app.runtimeFrame.repaint();

        // Change runtime to edit mode if not now
        if (!Main.app.runtimeFrame.framedCharts) {
          Main.app.runtimeFrame.framedCharts = true;
          try {
            Main.app.runtimeFrame.reload();
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
      elem.reinit();
    } catch (Exception e) {
      if (elem != null) elem.disactivate(e);
      System.out.println("New element error: " + e);
      // e.printStackTrace();
    } catch (Throwable e) {
      System.out.println("Critical error occurred while creating new element: " + e);
      e.printStackTrace();
    }

    return ret;
  }
  /** EdiDialog constructor comment. */
  public String getSelectedElementName(Object src) {
    String name = null;
    if (src == list) {
      name = (String) list.getSelectedValue();
    } else if (src == bOK) {
      name = (String) list.getSelectedValue();
      if (name == null) {
        // Button ok was pressed, but no selection was done, do nothing
        return null;
      }
    } else {
      dispose();
      return null;
    }

    return name;
  }
  /** EdiDialog constructor comment. */
  public void keyReleased(java.awt.event.KeyEvent e) {
    if (e.getSource() == list) {
      switch (e.getKeyCode()) {
        case KeyEvent.VK_DELETE:
        case KeyEvent.VK_BACK_SPACE:
          if (keys.length() > 0) keys.setLength(keys.length() - 1);
          break;
        case KeyEvent.VK_ESCAPE:
          dispose();
          break;
        case KeyEvent.VK_ENTER:
          dispose();
          actionOK();
          break;
        case KeyEvent.VK_SPACE:
          actionAdd();
          break;
        default:
          // keys.append((char) e.getKeyChar());
          list.ensureIndexIsVisible(list.getSelectedIndex());
      }

      //		if (debug)
      //			System.out.println("keys: " + keys);
      return;
    }

    switch (e.getKeyCode()) {
      case KeyEvent.VK_ENTER:
      case KeyEvent.VK_ESCAPE:
        dispose();
        break;
      default:
        break;
    }

    super.keyReleased(e);
  }
  /** Invoked when an action occurs. */
  public void locateOnComponent(int x, int y, java.awt.Component f) {
    cX = x;
    cY = y;
    super.locateOnComponent(x, y, f);
  }
  /** EdiDialog constructor comment. */
  public static void main(String args[]) throws Exception {
    try {
      System.out.println("started");
      DialogNewElement.test();
      System.out.println("Finished");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /** EdiDialog constructor comment. */
  public void show() {
    getContentPane().setLayout(new java.awt.BorderLayout());
    JScrollPane center = new JScrollPane();
    getContentPane().add(center, java.awt.BorderLayout.CENTER);
    list = new JList();
    list.setBackground(parentWindow.getBackground());
    DefaultListModel model = new DefaultListModel();
    list.setModel(model);
    for (int i = 0; i < DesignFrame.processingElements.length; i++) {
      model.addElement((String) DesignFrame.processingElements[i][0]);
    }

    // list.ActionListener((ActionListener) this);
    center.setViewportView(list);

    JPanel down = new JPanel();
    getContentPane().add(down, java.awt.BorderLayout.SOUTH);
    down.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 2, 2));
    bOK = new JButton("OK");
    bOK.addActionListener(this);
    bAdd = new JButton("Add");
    bAdd.addActionListener(this);
    bCancel = new JButton("Cancel");
    bCancel.addActionListener(this);
    down.add(bOK);
    down.add(bAdd);
    down.add(bCancel);
    pack();
    setSize(getWidth(), getHeight() * 2);

    list.addKeyListener(this);
    super.show();
  }
  /** EdiDialog constructor comment. */
  public static void test() {
    JFrame frame = new JFrame();
    frame.addWindowListener(new CloseSaveWindowsListener());
    frame.setBackground(ConfigurableSystemSettings.backgroundColor.getAWTColor());
    frame.getContentPane().setLayout(new java.awt.BorderLayout());
    frame.setBounds(50, 50, 400, 400);
    frame.show();

    DialogNewElement dialog = new DialogNewElement(frame);
    dialog.show();
  }

  public static void setDebug(boolean newValue) {
    debug = newValue;
  }
}