📄 iirfilterdesign.java
字号:
package IIRFilterDesign;
/*
* @(#)IIRFilterDesign.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.*;
public class IIRFilterDesign extends Applet {
float rate = 8000.0f; // fixed sampling rate
int freqPoints = 250; // number of points in FR plot
int maxOrder = 16; // maximum filter order
int order;
String filterType, prototype;
float[] gain = new float[freqPoints+1];
IIRFilter filter = new IIRFilter();
GraphPlot frPlot = new GraphPlot();
PoleZeroPlot pzPlot = new PoleZeroPlot();
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
BorderLayout borderLayout3 = new BorderLayout();
BorderLayout borderLayout4 = new BorderLayout();
FlowLayout flowLayout1 = new FlowLayout(FlowLayout.LEFT);
FlowLayout flowLayout2 = new FlowLayout(FlowLayout.LEFT);
FlowLayout flowLayout3 = new FlowLayout(FlowLayout.LEFT);
FlowLayout flowLayout4 = new FlowLayout(FlowLayout.LEFT);
FlowLayout flowLayout5 = new FlowLayout(FlowLayout.LEFT);
FlowLayout flowLayout6 = new FlowLayout(FlowLayout.LEFT);
FlowLayout flowLayout7 = new FlowLayout(FlowLayout.LEFT);
FlowLayout flowLayout8 = new FlowLayout(FlowLayout.LEFT);
GridLayout gridLayout1 = new GridLayout(1, 2);
GridLayout gridLayout2 = new GridLayout(4, 1);
GridLayout gridLayout3 = new GridLayout(4, 1);
CardLayout cardLayout1 = new CardLayout();
Panel pnlControls = new Panel();
Panel pnlLeftPanel = new Panel();
Panel pnlRightPanel = new Panel();
Panel pnlFilterType = new Panel();
Panel pnlPrototype = new Panel();
Panel pnlOrder = new Panel();
Panel pnlPassband = new Panel();
Panel pnlRipple = new Panel();
Panel pnlButtons1 = new Panel();
Panel pnlMinGain = new Panel();
Panel pnlDisplay = new Panel();
Panel pnlPZPlot = new Panel();
Panel pnlFRPlot = new Panel();
Panel pnlButtons2 = new Panel();
Panel pnlCoeffs = new Panel();
Label lblFilterType = new Label();
Label lblPrototype = new Label();
Label lblOrder = new Label();
Label lblPassband = new Label();
Label lblTo = new Label();
Label lblHz = new Label();
Label lblRipple = new Label();
Label lblRippleUnit = new Label();
Label lblMinGain = new Label();
Label lblMinGainUnit = new Label();
Label lblMaxOrder = new Label();
CheckboxGroup cbgFilterType = new CheckboxGroup();
Checkbox cbLowPass = new Checkbox();
Checkbox cbBandPass = new Checkbox();
Checkbox cbHighPass = new Checkbox();
CheckboxGroup cbgPrototype = new CheckboxGroup();
Checkbox cbButterworth = new Checkbox();
Checkbox cbChebyshev = new Checkbox();
TextField tfOrder = new TextField("1", 3);
TextField tfFreq1 = new TextField("0", 5);
TextField tfFreq2 = new TextField("1000", 5);
TextField tfRipple = new TextField("1.0", 3);
Button btnResponse = new Button();
Button btnDesign = new Button();
Button btnCoeffs = new Button();
Button btnPoleZero = new Button();
Choice chMinGain = new Choice();
TextArea txtCoeffs = new TextArea();
Label lblcopy = new Label("(c)华南理工大学电子信息学院",Label.CENTER);
//Construct the applet
public IIRFilterDesign() {
}
//Initialize the applet
public void init() {
this.setLayout(borderLayout1);
pnlDisplay.setLayout(cardLayout1);
this.add("Center", pnlDisplay);
pnlDisplay.add("PZPlot", pnlPZPlot);
pnlDisplay.add("FRPlot", pnlFRPlot);
pnlDisplay.add("Coeffs", pnlCoeffs);
pnlCoeffs.setLayout(borderLayout4);
pnlButtons2.setLayout(flowLayout8);
pnlFRPlot.setLayout(borderLayout3);
frPlot.setPlotStyle(GraphPlot.SPECTRUM);
frPlot.setTracePlot(true);
frPlot.setLogScale(true);
frPlot.setBgColor(Color.lightGray);
frPlot.setPlotColor(Color.blue);
frPlot.setAxisColor(Color.darkGray);
frPlot.setGridColor(Color.darkGray);
pnlPZPlot.setLayout(borderLayout2);
pnlPZPlot.add("Center", pzPlot);
pnlFRPlot.add("Center", frPlot);
pnlCoeffs.add("Center", txtCoeffs);
this.add("North", pnlControls);
this.add("South", lblcopy);
Font f = new Font("Dialog", 0, 11);
lblFilterType.setFont(f);
lblFilterType.setText("滤波器类型:");
cbLowPass.setFont(f);
cbLowPass.setLabel("低通");
cbLowPass.setCheckboxGroup(cbgFilterType);
cbBandPass.setFont(f);
cbBandPass.setLabel("带通");
cbBandPass.setCheckboxGroup(cbgFilterType);
cbHighPass.setFont(f);
cbHighPass.setLabel("高通");
cbHighPass.setCheckboxGroup(cbgFilterType);
cbLowPass.setState(true);
lblPrototype.setFont(f);
lblPrototype.setText("原型:");
cbButterworth.setFont(f);
cbButterworth.setLabel("巴特沃兹");
cbButterworth.setCheckboxGroup(cbgPrototype);
cbChebyshev.setFont(f);
cbChebyshev.setLabel("切比雪夫");
cbChebyshev.setCheckboxGroup(cbgPrototype);
cbButterworth.setState(true);
lblOrder.setFont(f);
lblOrder.setText("滤波器阶数:");
tfOrder.setFont(f);
lblMaxOrder.setFont(new Font("Dialog", 2, 11));
lblMaxOrder.setText("(max " + String.valueOf(maxOrder) + ")" );
txtCoeffs.setText("滤波器系数\n\n");
btnCoeffs.disable();
btnCoeffs.setFont(f);
btnCoeffs.setLabel("系数");
lblPassband.setFont(f);
lblPassband.setText("通带:");
tfFreq1.setEditable(false);
tfFreq1.setFont(f);
lblTo.setFont(f);
lblTo.setText("to");
tfFreq2.setFont(f);
lblHz.setFont(f);
lblHz.setText("Hz");
lblRipple.disable();
lblRipple.setFont(f);
lblRipple.setText("通带波纹:");
tfRipple.disable();
tfRipple.setFont(f);
lblRippleUnit.disable();
lblRippleUnit.setFont(f);
lblRippleUnit.setText("dB");
btnResponse.disable();
btnResponse.setFont(f);
btnResponse.setLabel("频率响应");
btnDesign.setFont(f);
btnDesign.setLabel("设计");
lblMinGain.disable();
lblMinGain.setFont(f);
lblMinGain.setText("最小显示增益:");
lblMinGainUnit.disable();
lblMinGainUnit.setFont(f);
lblMinGainUnit.setText("dB");
btnPoleZero.setLabel("极点/零点");
btnPoleZero.disable();
btnPoleZero.setFont(f);
chMinGain.addItem("-10");
chMinGain.addItem("-50");
chMinGain.addItem("-100");
chMinGain.addItem("-200");
chMinGain.select("-100");
chMinGain.disable();
chMinGain.setFont(f);
pnlButtons1.setLayout(flowLayout7);
pnlMinGain.setLayout(flowLayout6);
pnlRipple.setLayout(flowLayout5);
pnlPassband.setLayout(flowLayout4);
pnlOrder.setLayout(flowLayout3);
pnlPrototype.setLayout(flowLayout2);
pnlFilterType.setLayout(flowLayout1);
pnlRightPanel.setLayout(gridLayout3);
pnlLeftPanel.setLayout(gridLayout2);
pnlControls.setLayout(gridLayout1);
pnlControls.add(pnlLeftPanel);
pnlLeftPanel.add(pnlFilterType);
pnlFilterType.add(lblFilterType);
pnlFilterType.add(cbLowPass);
pnlFilterType.add(cbBandPass);
pnlFilterType.add(cbHighPass);
pnlLeftPanel.add(pnlPrototype);
pnlPrototype.add(lblPrototype);
pnlPrototype.add(cbButterworth);
pnlPrototype.add(cbChebyshev);
pnlLeftPanel.add(pnlOrder);
pnlOrder.add(lblOrder);
pnlOrder.add(tfOrder);
pnlOrder.add(lblMaxOrder);
pnlLeftPanel.add(pnlPassband);
pnlPassband.add(lblPassband);
pnlPassband.add(tfFreq1);
pnlPassband.add(lblTo);
pnlPassband.add(tfFreq2);
pnlPassband.add(lblHz);
pnlControls.add(pnlRightPanel);
pnlRightPanel.add(pnlRipple);
pnlRipple.add(lblRipple);
pnlRipple.add(tfRipple);
pnlRipple.add(lblRippleUnit);
pnlRightPanel.add(pnlMinGain);
pnlMinGain.add(lblMinGain);
pnlMinGain.add(chMinGain);
pnlMinGain.add(lblMinGainUnit);
pnlRightPanel.add(pnlButtons1);
pnlButtons1.add(btnDesign);
pnlButtons1.add(btnPoleZero);
pnlRightPanel.add(pnlButtons2);
pnlButtons2.add(btnResponse);
pnlButtons2.add(btnCoeffs);
}
//Get Applet information
public String getAppletInfo() {
return "华南理工大学电子信息学院";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
private void setRippleState(boolean b) {
if (b) {
lblRipple.enable();
tfRipple.enable();
lblRippleUnit.enable();
}
else {
lblRipple.disable();
tfRipple.disable();
lblRippleUnit.disable();
}
}
private void setMinGainState(boolean b) {
if (b) {
lblMinGain.enable();
chMinGain.enable();
lblMinGainUnit.enable();
}
else {
lblMinGain.disable();
chMinGain.disable();
lblMinGainUnit.disable();
}
}
void designFilter() {
order = Integer.parseInt(tfOrder.getText());
if (order > maxOrder) {
showStatus("滤波器阶数过高 (max. "
+ String.valueOf(maxOrder) + ")");
}
else {
filterType = cbgFilterType.getCurrent().getLabel();
if ((filterType.equals("带通")) && ((order % 2) != 0)) {
showStatus("对于带通滤波器,阶数必须为偶数");
}
else {
filter.setFilterType(filterType);
prototype = cbgPrototype.getCurrent().getLabel();
filter.setPrototype(prototype);
filter.setRate(rate);
filter.setOrder(order);
filter.setFreq1(Float.valueOf(tfFreq1.getText()).floatValue());
filter.setFreq2(Float.valueOf(tfFreq2.getText()).floatValue());
if (cbChebyshev.getState())
filter.setRipple(Float.valueOf(tfRipple.getText()).floatValue());
filter.design();
btnResponse.enable();
btnPoleZero.enable();
}
}
}
void plotResponse() {
filter.setFreqPoints(freqPoints);
gain = filter.filterGain();
frPlot.setYmax(minPlotGain());
frPlot.setPlotValues(gain);
repaint();
cardLayout1.show(pnlDisplay, "FRPlot");
}
void plotPolesAndZeros() {
// pole and zero locations
// NB poles and zeros have indices 1 .. order
float[] pReal = new float[order + 1];
float[] pImag = new float[order + 1];
float[] z = new float[order + 1];
for (int i = 1; i <= order; i++) {
pReal[i] = filter.getPReal(i);
pImag[i] = filter.getPImag(i);
z[i] = filter.getZero(i);
}
pzPlot.setPolesAndZeros(pReal, pImag, z);
repaint();
cardLayout1.show(pnlDisplay, "PZPlot");
}
private float minPlotGain() {
return Math.abs(Float.valueOf(chMinGain.getSelectedItem()).floatValue());
}
void listCoeffs() {
txtCoeffs.setText(prototype + " IIR滤波器\n\n");
txtCoeffs.appendText("滤波器类型: " + filterType + "\n");
txtCoeffs.appendText("通带: " + tfFreq1.getText() + " - "
+ tfFreq2.getText() + " Hz\n");
if (cbChebyshev.getState())
txtCoeffs.appendText("通带波纹: " + tfRipple.getText() + " dB\n");
txtCoeffs.appendText("阶数: " + String.valueOf(order) + "\n\n");
txtCoeffs.appendText("系数\n\n");
for (int i = 0; i <= order; i++)
txtCoeffs.appendText("a[" + String.valueOf(i) + "] = "
+ String.valueOf(filter.getACoeff(i))
+ " \tb[" + String.valueOf(i) + "] = "
+ String.valueOf(filter.getBCoeff(i)) + "\n");
cardLayout1.show(pnlDisplay, "Coeffs");
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Checkbox) {
if (cbLowPass.getState()) {
tfFreq1.setText("0");
tfFreq1.setEditable(false);
tfFreq2.setEditable(true);
btnCoeffs.disable();
}
if (cbBandPass.getState()) {
tfFreq1.setText("");
tfFreq2.setText("");
tfFreq1.setEditable(true);
tfFreq2.setEditable(true);
btnCoeffs.disable();
}
if (cbHighPass.getState()) {
tfFreq1.setText("");
tfFreq2.setText(String.valueOf(rate/2).toString());
tfFreq1.setEditable(true);
tfFreq2.setEditable(false);
btnCoeffs.disable();
}
setRippleState(cbChebyshev.getState());
btnResponse.disable();
btnPoleZero.disable();
return true;
}
if (evt.target == btnDesign) {
designFilter();
return true;
}
if (evt.target == btnPoleZero) {
setMinGainState(false);
plotPolesAndZeros();
return true;
}
if (evt.target == btnResponse) {
setMinGainState(true);
plotResponse();
btnCoeffs.enable();
return true;
}
if (evt.target == btnCoeffs) {
listCoeffs();
return true;
}
if (evt.target == chMinGain) {
if (btnResponse.isEnabled()) {
plotResponse();
}
return true;
}
return false;
}
public boolean keyDown(Event evt, int key) {
btnResponse.disable();
btnPoleZero.disable();
btnCoeffs.disable();
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -