⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 firfilterdesign.java

📁 用Java 写的FIRFilter Design Applet,矩形窗
💻 JAVA
字号:
package FIRFilterDesign;

/*
 * @(#)FIRFilterDesign.java 1.0 06/04/21
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_2\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */

import java.awt.*;
import java.applet.Applet;

/* JDK 1.02 version */

public class FIRFilterDesign extends Applet{

  static float rate = 8000.0f;   // fixed sampling rate
  static int freqPoints = 2500;   // number of points in FR plot
  static int maxOrder = 256;
  int order;
  float freq1, freq2;
  float[] gain = new float[freqPoints+1];
  FIRFilter filter = new FIRFilter();
  GraphPlot frPlot = new GraphPlot();
  TextArea txtCoeffs = new TextArea();
  String windowType, filterType;
  
  Panel pnlDisplay = new Panel();
  Panel pnlFRPlot = new Panel();
  Panel pnlCoeffs = new Panel();
  Panel pnlControls = new Panel();
  Panel pnlLeftPanel = new Panel();
  Panel pnlWindowType = new Panel();
  Panel pnlOrder = new Panel();
  Panel pnlButtons = new Panel();
  Panel pnlRightPanel = new Panel();
  Panel pnlPassband = new Panel();
  Panel pnlAtten =  new Panel();
  Panel pnlTrBand =  new Panel();
  
  Choice chWindowType = new Choice();
  CheckboxGroup cbgFilterType = new CheckboxGroup();
  Checkbox cbLowPass = new Checkbox();
  Checkbox cbBandPass = new Checkbox();
  Checkbox cbHighPass = new Checkbox();
  
  TextField tfFreq1 = new TextField("0", 5);
  TextField tfFreq2 = new TextField("1000", 5);
  Label lblAttenValue = new Label();
  Label lblTrBandValue = new Label();
  TextField tfOrder = new TextField("20", 4);
  Label lblOrder = new Label();
  Label lblPassband = new Label();
  Label lblTo = new Label();
  Label lblPassbandUnit = new Label();
  Label lbldB = new Label();
  Label lblAtten = new Label();
  Label lblTrBand = new Label();
  Label lblTrBandUnit = new Label();
  Button btnDesign = new Button();
  Button btnResponse = new Button();
  Button btnCoeffs = new Button();
  GridLayout gridLayout1 = new GridLayout(1, 2);
  GridLayout gridLayout2 = new GridLayout(4, 1);
  GridLayout gridLayout3 = new GridLayout(4, 1);
  FlowLayout flowLayout1 = new FlowLayout(FlowLayout.LEFT, 10, 10);
  FlowLayout flowLayout2 = new FlowLayout(FlowLayout.LEFT, 10, 10);
  FlowLayout flowLayout3 = new FlowLayout(FlowLayout.LEFT, 10, 10);
  FlowLayout flowLayout4 = new FlowLayout(FlowLayout.LEFT, 10, 10);
  FlowLayout flowLayout5 = new FlowLayout(FlowLayout.LEFT, 10, 10);
  FlowLayout flowLayout6 = new FlowLayout(FlowLayout.LEFT, 10, 10);
  FlowLayout flowLayout7 = new FlowLayout(FlowLayout.LEFT, 10, 10);
  FlowLayout flowLayout8 = new FlowLayout(FlowLayout.LEFT, 10, 10);
  BorderLayout borderLayout1 = new BorderLayout();
  BorderLayout borderLayout2 = new BorderLayout();
  BorderLayout borderLayout3 = new BorderLayout();
  CardLayout cardLayout1 = new CardLayout();
  Label lblWindowType = new Label();
  Panel pnlFilterType = new Panel();
  Label lblFilterType = new Label();
  Panel pnlMinGain = new Panel();
  Label lblMinGain = new Label();
  Choice chMinGain = new Choice();
  Label lblMinGainUnit = new Label();
  Label lblcopy =new Label("(c)华南理工大学电子信息学院",Label.CENTER);

  public FIRFilterDesign() {
  }

  public void init() {
    this.setLayout(borderLayout1);
    pnlDisplay.setLayout(cardLayout1);
    // Display panel
    this.add("Center", pnlDisplay);
    
    pnlDisplay.add("FRPlot", pnlFRPlot);
    pnlFRPlot.setLayout(borderLayout2);
    pnlFRPlot.add("Center", frPlot);
    
    pnlDisplay.add("Coeffs", pnlCoeffs);
    pnlCoeffs.setLayout(borderLayout3);
    pnlCoeffs.add("Center", txtCoeffs);
    
    // Gain frequency response plot
    frPlot.setPlotStyle(GraphPlot.SPECTRUM);
    frPlot.setTracePlot(true);
    frPlot.setLogScale(true);
    frPlot.setPlotColor(Color.blue);
    frPlot.setAxisColor(Color.red);//darkGray);
    frPlot.setGridColor(Color.darkGray);
    frPlot.setBgColor(Color.lightGray);
    // Control panel
    pnlTrBand.setLayout(flowLayout6);
    pnlAtten.setLayout(flowLayout5);
    pnlPassband.setLayout(flowLayout4);
    pnlRightPanel.setLayout(gridLayout3);
    pnlWindowType.setLayout(flowLayout3);
    pnlButtons.setLayout(flowLayout2);
    pnlOrder.setLayout(flowLayout1);
    pnlLeftPanel.setLayout(gridLayout2);
    pnlControls.setLayout(gridLayout1);
    lblWindowType.setText("窗函数类型:");
    lblFilterType.setText("滤波器类型:");
    pnlFilterType.setLayout(flowLayout7);
    this.add("South",lblcopy);
    // Control panel
    this.add("North", pnlControls);//整个panel分为北 中  南
    //滤波器类型panel:
    pnlControls.add(pnlLeftPanel);
    
    pnlLeftPanel.add(pnlFilterType);
    pnlFilterType.add(lblFilterType);
    pnlFilterType.add(cbLowPass);
    pnlFilterType.add(cbBandPass);
    pnlFilterType.add(cbHighPass);
    cbLowPass.setLabel("低通");
    cbLowPass.setCheckboxGroup(cbgFilterType);
    cbBandPass.setLabel("带通");
    cbBandPass.setCheckboxGroup(cbgFilterType);
    cbHighPass.setLabel("高通");
    cbHighPass.setCheckboxGroup(cbgFilterType);
    cbLowPass.setState(true);
    //窗函数类型:
    pnlLeftPanel.add(pnlWindowType);
    pnlWindowType.add(lblWindowType);
    pnlWindowType.add(chWindowType);
    chWindowType.addItem("矩形窗");
    chWindowType.addItem("汉宁窗");
    chWindowType.addItem("哈明窗");
    chWindowType.addItem("布拉克曼窗");
    //滤波器阶数:
    pnlLeftPanel.add(pnlOrder);
    pnlOrder.add(lblOrder);
    lblOrder.setText("滤波器阶数(偶数):");
    pnlOrder.add(tfOrder);
    //通带:
    pnlLeftPanel.add(pnlPassband);
    pnlPassband.add(lblPassband);
    pnlPassband.add(tfFreq1);
    pnlPassband.add(lblTo);
    pnlPassband.add(tfFreq2);
    pnlPassband.add(lblPassbandUnit);
    lblPassband.setText("通带:");
    tfFreq1.setEditable(false);
    lblTo.setAlignment(1);
    lblTo.setText("to");
    tfFreq2.setEditable(true);
    lblPassbandUnit.setText("Hz");
    
   //按钮面板:
    pnlRightPanel.add(pnlButtons);
    pnlButtons.add(btnDesign);
    pnlButtons.add(btnResponse);
    pnlButtons.add(btnCoeffs);
    btnDesign.setLabel("设计");
    btnResponse.setLabel("频率响应");
    btnResponse.disable();
    btnCoeffs.setLabel("系数");
    btnCoeffs.disable();
    pnlControls.add(pnlRightPanel);
    //阻带衰减:
    pnlRightPanel.add(pnlAtten);
    pnlAtten.add(lblAtten);
    pnlAtten.add(lblAttenValue);
    pnlAtten.add(lbldB);
    lblAtten.setText("阻带衰减:");
    lblAttenValue.setText("       ");
    lblAttenValue.setAlignment(Label.LEFT);
    lbldB.setText("dB");

    //过渡带带宽:
    pnlRightPanel.add(pnlTrBand);
    pnlTrBand.add(lblTrBand);
    pnlTrBand.add(lblTrBandValue);
    lblTrBand.setText("过渡带带宽:");
    lblTrBandValue.setText("        ");
    lblTrBandValue.setAlignment(Label.LEFT);
    lblTrBandUnit.setText("Hz");
    pnlTrBand.add(lblTrBandUnit);
    //最小增益:
    lblMinGain.setText("图中的最小增益:");
    chMinGain.addItem("-10");
    chMinGain.addItem("-50");
    chMinGain.addItem("-100");
    chMinGain.addItem("-200");
    chMinGain.select("-100");
    lblMinGainUnit.setText("dB");
    pnlMinGain.setLayout(flowLayout8);
    pnlRightPanel.add(pnlMinGain);
    pnlMinGain.add(lblMinGain);
    pnlMinGain.add(chMinGain);
    pnlMinGain.add(lblMinGainUnit);
 
    //pnlTrBand.add(lblcopy);
    
  }

  void designFilter() {
    windowType = chWindowType.getSelectedItem();
    filter.setWindowType(windowType);
    
    filterType = cbgFilterType.getCurrent().getLabel();
    filter.setFilterType(filterType);
    filter.setRate(rate);
    
    order = Integer.parseInt(tfOrder.getText());
    // ensure filter order is even:
    if ((order % 2) != 0) {
      order++;
      tfOrder.setText(String.valueOf(order));
    }
    
    filter.setOrder(order);
    freq1 = Float.valueOf(tfFreq1.getText()).floatValue();
    filter.setFreq1(freq1);
    freq2 = Float.valueOf(tfFreq2.getText()).floatValue();
    filter.setFreq2(freq2);
    lblAttenValue.setText(String.valueOf(Math.round(filter.atten())));
    lblTrBandValue.setText(String.valueOf(Math.round(filter.trBand())));
    filter.design();
    btnResponse.enable();//频率响应按钮
  }

  float minPlotGain() {
    return Math.abs(Float.valueOf(chMinGain.getSelectedItem()).floatValue());
  }

  void plotResponse() {
    filter.setFreqPoints(freqPoints);
    gain = filter.filterGain();
    frPlot.setYmax(minPlotGain());
    frPlot.setPlotValues(gain);
    btnCoeffs.enable();
    repaint();
    cardLayout1.show(pnlDisplay, "FRPlot");
  }

  void listCoefficients() {
    // list coefficients in text area
    txtCoeffs.setText(windowType + " 加窗FIR滤波器\n\n");
    txtCoeffs.appendText("滤波器类型: " + filterType + "\n");
    txtCoeffs.appendText("通带: " + tfFreq1.getText() + " - " + tfFreq2.getText() + " Hz\n");
    txtCoeffs.appendText("阶数: " + String.valueOf(order) + "\n");
    txtCoeffs.appendText("过渡带: " + lblTrBandValue.getText() + " Hz\n");
    txtCoeffs.appendText("阻带衰减: " + lblAttenValue.getText() + " dB\n\n");
    txtCoeffs.appendText("系数:\n\n");
    for (int i = 0; i <= order; i++)
      txtCoeffs.appendText("a[" + String.valueOf(i) + "] =\t" +
        String.valueOf(filter.getCoeff(i)) + "\n");
    cardLayout1.show(pnlDisplay, "Coeffs");
  }

  public boolean keyDown(Event evt, int key) {
    btnResponse.disable();
    btnCoeffs.disable();
    return false;
  }


  public boolean action(Event evt, Object arg) {
    if (evt.target instanceof Checkbox) {
      if (cbLowPass.getState()) {
        tfFreq1.setText("0");
        tfFreq2.setText("1000");
        tfFreq1.setEditable(false);
        tfFreq2.setEditable(true);
      }
      if (cbBandPass.getState()) {
        tfFreq1.setText("2000");
        tfFreq2.setText("3000");
        tfFreq1.setEditable(true);
        tfFreq2.setEditable(true);
      }
      if (cbHighPass.getState()) {
        tfFreq1.setText("");
        tfFreq2.setText("4000");
        tfFreq1.setEditable(true);
        tfFreq2.setEditable(false);
      }
      btnResponse.disable();
      btnCoeffs.disable();
      return true;
    }
    if (evt.target == chWindowType) {
      btnResponse.disable();
      btnCoeffs.disable();
      return true;
    }
    if (evt.target == btnDesign) {
      designFilter();
      return true;
    }
    if (evt.target == btnResponse) {
      plotResponse();
      return true;
    }
    if (evt.target == btnCoeffs) {
      listCoefficients();
      return true;
    }
    return false;
  }

  //Get Applet information
  public String getAppletInfo() {
    return "(C) 华南理工大学电子信息学院";
  }

  //Get parameter info
  public String[][] getParameterInfo() {
    return null;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -