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

📄 kaiserfilterdesign.java

📁 用Java 写的Kaiser Filter设计
💻 JAVA
字号:
/*
 * @(#)KaiserFilterDesign.java 1.0 06/04/20
 *
 * 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;

public class KaiserFilterDesign extends Applet{

	static float rate = 8000.0f;   // fixed sampling rate
	static int freqPoints = 250;   // number of points in FR plot
	int order;
	String strFilterType;
    float freq1, freq2, atten, trband;
    float[] gain = new float[freqPoints+1];
	KaiserFilter kf = new KaiserFilter();
	GraphPlot frPlot = new GraphPlot();
    TextArea txtCoeffs = new TextArea();
    Panel pnlDisplay = new Panel();
    Panel pnlFRPlot = new Panel();
    Panel pnlCoeffs = new Panel();
	Panel pnlControls = new Panel();
	Panel pnlLeftPanel = new Panel();
	Panel pnlFilterType = 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();
	CheckboxGroup cbgFilterType;
	TextField tfFreq1, tfFreq2, tfAtten, tfTrBand, tfOrder;
	Button btnDesign, btnResponse, btnCoeffs;
	Label lblOrder = new Label("滤波器阶数: ");
	Label lblPassband = new Label("通带:");
	Label lblTo = new Label("到");
	Label lblHertz = new Label("Hz");
    Label lblAtten = new Label("阻带衰减:");
    Label lbldB = new Label("dB");
	Label lblTrBand = new Label("过渡带:");
	Label lblcopy = new Label("   华南理工大学电子信息学院");
	BorderLayout borderLayout2 = new BorderLayout();
    BorderLayout borderLayout3 = new BorderLayout();
    CardLayout cardLayout1 = new CardLayout();
	GridBagLayout gridBagLayout1 = new GridBagLayout();
	GridBagConstraints gridBagConstraints1 = new GridBagConstraints();

	void buildConstraints(GridBagConstraints gridBagConstraints1,
	                       int gx, int gy, int gw, int gh, int wx, int wy) {
	    gridBagConstraints1.gridx = gx;
	    gridBagConstraints1.gridy = gy;
	    gridBagConstraints1.gridwidth = gw;
	    gridBagConstraints1.gridheight = gh;
	    gridBagConstraints1.weightx = wx;
	    gridBagConstraints1.weighty = wy;
	}

	public void init() {

	    buildConstraints(gridBagConstraints1, 0, 0, 1, 1, 100, 70);
	    gridBagConstraints1.fill = GridBagConstraints.BOTH;
	    this.setLayout(gridBagLayout1);
	    gridBagLayout1.setConstraints(pnlDisplay, gridBagConstraints1);
	    this.add(pnlDisplay);
        // Display panel
        pnlDisplay.setLayout(cardLayout1);
        pnlDisplay.add("FRPlot", pnlFRPlot);
        pnlFRPlot.setLayout(borderLayout2);
        pnlFRPlot.add("Center", frPlot);
        pnlDisplay.add("Coeffs", pnlCoeffs);
        pnlCoeffs.setLayout(borderLayout3);
        pnlCoeffs.add("Center", txtCoeffs);

	    // Control panel
	    buildConstraints(gridBagConstraints1, 0, 1, 1, 1, 100, 30);
	    gridBagConstraints1.fill = GridBagConstraints.BOTH;
	    gridBagLayout1.setConstraints(pnlControls, gridBagConstraints1);
	    add(pnlControls);
	    pnlControls.setLayout(new GridLayout(1,2));

	    pnlLeftPanel.setLayout(new GridLayout(3,1));
	    pnlFilterType.setLayout(new FlowLayout(FlowLayout.LEFT));
	    pnlLeftPanel.add(pnlFilterType);
	    cbgFilterType = new CheckboxGroup();
	    pnlFilterType.add(new Checkbox("低通", cbgFilterType, true));
	    pnlFilterType.add(new Checkbox("高通", cbgFilterType, false));
	    pnlFilterType.add(new Checkbox("带通", cbgFilterType, false));
	    pnlOrder.setLayout(new FlowLayout());
	    pnlLeftPanel.add(pnlOrder);
	    pnlOrder.add(lblOrder);
	    tfOrder = new TextField("    ", 4);
	    tfOrder.setEditable(false);
	    pnlOrder.add(tfOrder);
	    pnlButtons.setLayout(new FlowLayout());
	    pnlLeftPanel.add(pnlButtons);
	    btnDesign = new Button("设计");
	    btnResponse = new Button("频率响应");
	    btnResponse.disable();
	    btnCoeffs = new Button("系数");
	    btnCoeffs.disable();
	    pnlButtons.add(btnDesign);
	    pnlButtons.add(btnResponse);
	    pnlButtons.add(btnCoeffs);
	    pnlControls.add(pnlLeftPanel);
	    pnlRightPanel.setLayout(new GridLayout(3,1));
	    pnlRightPanel.add(pnlPassband);
	    pnlRightPanel.add(pnlAtten);
	    pnlRightPanel.add(pnlTrBand);
	    pnlControls.add(pnlRightPanel);
	    pnlPassband.setLayout(new FlowLayout(FlowLayout.LEFT));
	    tfFreq1 = new TextField("0", 5);
	    tfFreq1.setEditable(false);
	    tfFreq2 = new TextField("1000", 5);
	    tfFreq2.setEditable(true);
        pnlPassband.add(lblPassband);
        pnlPassband.add(tfFreq1);
        pnlPassband.add(lblTo);
        pnlPassband.add(tfFreq2);
        pnlPassband.add(lblHertz);//why this not show
	    pnlAtten.setLayout(new FlowLayout(FlowLayout.LEFT));
	    tfAtten = new TextField("60", 4);
	    pnlAtten.add(lblAtten);
	    pnlAtten.add(tfAtten);
	    pnlAtten.add(lbldB);
	    pnlTrBand.setLayout(new FlowLayout(FlowLayout.LEFT));
	    tfTrBand = new TextField("500", 5);
        pnlTrBand.add(lblTrBand);
        pnlTrBand.add(tfTrBand);
        pnlTrBand.add(lblHertz);
        pnlTrBand.add(lblcopy);
    }

    void designFilter() {
        freq1 = Float.valueOf(tfFreq1.getText()).floatValue();
        freq2 = Float.valueOf(tfFreq2.getText()).floatValue();
        atten = Float.valueOf(tfAtten.getText()).floatValue();
        trband = Float.valueOf(tfTrBand.getText()).floatValue();
        kf.setRate(rate);
        kf.setFreq1(freq1);
        kf.setFreq2(freq2);
        kf.setAtten(atten);
        kf.setTrBand(trband);
        order = kf.estimatedOrder();
 	    tfOrder.setText(String.valueOf(order));
 	    strFilterType = cbgFilterType.getCurrent().getLabel();
 	    if (strFilterType == "低通")  kf.setFilterType(KaiserFilter.LP);
 	    if (strFilterType == "高通") kf.setFilterType(KaiserFilter.HP);
 	    if (strFilterType == "带通") kf.setFilterType(KaiserFilter.BP);
 	    kf.design();
    }

    void plotResponse() {
        // plot gain v. frequency graph (dB scale)
        kf.setFreqPoints(freqPoints);
	    gain = kf.filterGain();
        frPlot.setPlotStyle(GraphPlot.SPECTRUM);
        frPlot.setLogScale(true);
        frPlot.setYmax(100.0f);
        frPlot.setPlotValues(gain);
        cardLayout1.show(pnlDisplay, "FRPlot");
    }

    void listCoeffs() {
        // list coefficients in text area
        txtCoeffs.setText("Kaiser窗FIR滤波器\n\n");
        txtCoeffs.appendText("滤波器类型: " + strFilterType + "\n");
        txtCoeffs.appendText("通带: " + String.valueOf(freq1) + " - " + String.valueOf(freq2) + " Hz\n");
        txtCoeffs.appendText("阶数: " + String.valueOf(order) + "\n");
        txtCoeffs.appendText("过渡带: " + String.valueOf(trband) + " Hz\n");
        txtCoeffs.appendText("阻带衰减: " + String.valueOf(atten) + " dB\n\n");
        txtCoeffs.appendText("系数:\n\n");
        for (int i = 0; i <= order; i++)
            txtCoeffs.appendText("a[" + String.valueOf(i) + "] =\t" +
                        String.valueOf(kf.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 obj) {
        if (evt.target instanceof Checkbox) {
            if (((Checkbox)evt.target).getLabel() == "低通") {
	            tfFreq1.setText("0");
	            tfFreq2.setText("1000");
        	    tfFreq1.setEditable(false);
        	    tfFreq2.setEditable(true);
        	}
            if (((Checkbox)evt.target).getLabel() == "带通") {
	            tfFreq1.setText("2000");
	            tfFreq2.setText("3000");
        	    tfFreq1.setEditable(true);
        	    tfFreq2.setEditable(true);
        	}
            if (((Checkbox)evt.target).getLabel() == "高通") {
	            tfFreq1.setText("3000");
	            tfFreq2.setText("4000");
        	    tfFreq1.setEditable(true);
        	    tfFreq2.setEditable(false);
        	}
        	btnResponse.disable();
            btnCoeffs.disable();
        	return true;
        }
        if (evt.target == btnDesign) {
            designFilter();
            btnResponse.enable();
            btnCoeffs.disable();
        	return true;
        }
        if (evt.target == btnResponse) {
            plotResponse();
            btnCoeffs.enable();
        	return true;
        }
        if (evt.target == btnCoeffs) {
            listCoeffs();
            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 + -