?? kaiserfilterdesign.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("濾波器階數(shù): ");
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(" 華南理工大學(xué)電子信息學(xué)院");
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("設(shè)計");
btnResponse = new Button("頻率響應(yīng)");
btnResponse.disable();
btnCoeffs = new Button("系數(shù)");
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("階數(shù): " + String.valueOf(order) + "\n");
txtCoeffs.appendText("過渡帶: " + String.valueOf(trband) + " Hz\n");
txtCoeffs.appendText("阻帶衰減: " + String.valueOf(atten) + " dB\n\n");
txtCoeffs.appendText("系數(shù):\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) 華南理工大學(xué)電子信息學(xué)院";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -