FIR數字濾波器的GUI設計
1. 窗函式法設計FIR數字濾波器的GUI設計
程式清單:
function run_Callback(hObject,eventdata,handles)
fp1=str2double(get(handles.edit_fp1,'String'));
fs1=str2double(get(handles.edit_fs1,'String'));
fp2=str2double(get(handles.edit_fp2,'String'));
fs2=str2double(get(handles.edit_fs2,'String'));
Fs=100;%取樣頻率
global filter_value;
filter_value=get(handles.filter,'value');
switch filter_value %選擇濾波器型別
case 1 %低通濾波器
wp1=2*pi*fp1/Fs; %將模擬通帶截止頻率轉換為數字濾波器頻率
ws1=2*pi*fs1/Fs; %將模擬阻帶起始頻率轉換為數字濾波器頻率
wn=(wp1+ws1)/2/pi; %標準化的截止頻率響應
Bt=ws1-wp1;
if fs1<fp1
msgbox('錯誤:fs1應該大於fp1,請重新輸入...','提示','error');
elseif fs1==fp1
msgbox('警告:存在問題,請檢查!','提示','warn');
end
N=ceil(6.2*pi/Bt);%濾波器長度
M=N-1;
set(handles.text_M,'String',M);
windows_value=get(handles.windows,'value');
switch windows_value %窗函式選擇
case 1 %使用boxcar窗函式
b1=fir1(M,wn,boxcar(N));
[H1,W1]=freqz(b1,1,512,2);
stem(handles.axes1,b1,'filled');
grid on;
plot(handles.axes2,W1,20*log10(abs(H1)));%繪頻率響應圖形
set(handles.text_title1,'String','低通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','低通boxcar窗的頻率響應圖形');
set(handles.text_x2,'String','頻(Hz)');
set(handles.text_y2,'String','幅值');
case 2
window2=bartlett(N);
b2=fir1(M,wn,window2);
[H2,W2]=freqz(b2,1,512,2);
stem(handles.axes1,b2,'filled');
grid on;
plot(handles.axes2,W2,20*log10(abs(H2)));%繪頻率響應圖形
set(handles.text_title1,'String','低通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','低通bartlett窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 3
window3=hanning(N);
b3=fir1(N-1,wn,window3);
[H3,W3]=freqz(b3,1,512,2);
stem(handles.axes1,b3,'filled');
grid on;
plot(handles.axes2,W3,20*log10(abs(H3)));%繪頻率響應圖形
set(handles.text_title1,'String','低通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','低通hanning窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 4
window4=hamming(N);
b4=fir1(N-1,wn,window4);
[H4,W4]=freqz(b4,1,512,2);
stem(handles.axes1,b4,'filled');
plot(handles.axes2,W4,20*log10(abs(H4)));%繪頻率響應圖形
set(handles.text_title1,'String','低通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','低通hamming窗頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 5
window5=blackman(N);
b5=fir1(N-1,wn,window5);
[H5,W5]=freqz(b5,1,512,2);
stem(handles.axes1,b5,'filled');
grid on;
plot(handles.axes2,W5,20*log10(abs(H5)));%繪頻率響應圖形
set(handles.text_title1,'String','低通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','低通blackman窗頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 6
[n,Wn,beta,~]=kaiserord([fp1fs1],[1 0],[0.01 0.01],100);
window6=kaiser(n+1,beta);%使用kaiser窗函式
b6=fir1(n,Wn,window6 ,'noscale');
[H6,W6]=freqz(b6,1,512,2);
stem(handles.axes1,b6,'filled');
grid on;
plot(handles.axes2,W6,20*log10(abs(H6)));%繪頻率響應圖形
set(handles.text_title1,'String','低通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','低通kaiser窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
end
case 2 %高通濾波器
ws1=2*pi*fs1/Fs;
wp1=2*pi*fp1/Fs;
wn=(wp1+ws1)/2/pi;
Bt=wp1-ws1;
N0=ceil(6.2*pi/Bt);
N=N0+mod(N0+1,2);
M=N-1;
set(handles.text_M,'String',M);
if fs1>fp1
msgbox('錯誤:輸入資料有誤,請重新輸入...','提示','error');
elseif fs1==fp1
msgbox('警告:存在問題,請檢查!','提示','warn');
end
windows_value=get(handles.windows,'value');
switch windows_value %窗函式選擇
case 1 %使用boxcar窗函式
window1=boxcar(N);
b1=fir1(N-1,wn,'high',window1);
[H1,W1]=freqz(b1,1,512,2);
stem(handles.axes1,b1,'filled');
grid on;
plot(handles.axes2,W1,20*log10(abs(H1)));%繪頻率響應圖形
set(handles.text_title1,'String','高通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','高通boxcar窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 2
window2=bartlett(N);
b2=fir1(N-1,wn,'high',window2);
[H2,W2]=freqz(b2,1,512,2);
stem(handles.axes1,b2,'filled');
grid on;
plot(handles.axes2,W2,20*log10(abs(H2)));%繪頻率響應圖形
set(handles.text_title1,'String','高通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','高通bartlett窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 3
window3=hanning(N);
b3=fir1(N-1,wn,'high',window3);
[H3,W3]=freqz(b3,1,512,2);
stem(handles.axes1,b3,'filled');
grid on;
plot(handles.axes2,W3,20*log10(abs(H3)));%繪頻率響應圖形
set(handles.text_title1,'String','高通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','高通hanning窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 4
window4=hamming(N);
b4=fir1(N-1,wn,'high',window4);
[H4,W4]=freqz(b4,1,512,2);
stem(handles.axes1,b4,'filled');
grid on;
plot(handles.axes2,W4,20*log10(abs(H4)));%繪頻率響應圖形
set(handles.text_title1,'String','高通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','高通hamming窗頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 5
window5=blackman(N);
b5=fir1(N-1,wn,'high',window5);
[H5,W5]=freqz(b5,1,512,2);
stem(handles.axes1,b5,'filled');
grid on;
plot(handles.axes2,W5,20*log10(abs(H5)));%繪頻率響應圖形
set(handles.text_title1,'String','高通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','高通blackman窗頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 6
[n,Wn,beta,~]=kaiserord([fs1fp1],[0 1],[0.01 0.01],100);
window6=kaiser(n+1,beta);%使用kaiser窗函式
b6=fir1(n,Wn,'high',window6 ,'noscale');
[H6,W6]=freqz(b6,1,512,2);
stem(handles.axes1,b6,'filled');
grid on;
plot(handles.axes2,W6,20*log10(abs(H6)));%繪頻率響應圖形
set(handles.text_title1,'String','高通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','高通kaiser窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
end
case 3 %帶通濾波器
wp1=2*pi*fp1/Fs;%將通帶下限截止頻率轉換為數字濾波器頻率
wp2=2*pi*fp2/Fs;%將通帶上限截止頻率轉換為數字濾波器頻率
ws1=2*pi*fs1/Fs;%將通帶下限截止頻率轉換為數字濾波器頻率
ws2=2*pi*fs2/Fs;%將通帶上限截止頻率轉換為數字濾波器頻率
Bt=wp1-ws1;
N=ceil(12*pi/Bt);
M=N-1;
set(handles.text_M,'String',M);
if (fs1>fp1 || fs1>fp2 || fs1>fs2 || fp1>fp2 || fp1>fs2|| fp2>fs2)
msgbox('錯誤:資料輸入有誤,請重新輸入...','提示','error');
elseif (fs1==fp1 || fs1==fp2 ||fs1==fs2 || fs2==fp1 || fs2==fp2 ||fp1==fp2)
msgbox('警告:存在問題,請檢查!','提示','warn');
end
wn=[(wp1+ws1)/2/pi,(wp2+ws2)/2/pi];
windows_value=get(handles.windows,'value');
switch windows_value %窗函式選擇
case 1 %使用boxcar窗函式
window1=boxcar(N);
b1=fir1(N-1,wn,window1);
[H1,W1]=freqz(b1,1,512,2);
stem(handles.axes1,b1,'filled');
grid on;
plot(handles.axes2,W1,20*log10(abs(H1)));%繪頻率響應圖形
set(handles.text_title1,'String','帶通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶通boxcar窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 2
window2=bartlett(N);
b2=fir1(N-1,wn,window2);
[H2,W2]=freqz(b2,1,512,2);
stem(handles.axes1,b2,'filled');
grid on;
plot(handles.axes2,W2,20*log10(abs(H2)));%繪頻率響應圖形
set(handles.text_title1,'String','帶通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶通bartlett窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 3
window3=hanning(N);
b3=fir1(N-1,wn,window3);
[H3,W3]=freqz(b3,1,512,2);
stem(handles.axes1,b3,'filled');
grid on;
plot(handles.axes2,W3,20*log10(abs(H3)));%繪頻率響應圖形
set(handles.text_title1,'String','帶通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶通hanning窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 4
window4=hamming(N);
b4=fir1(N-1,wn,window4);
[H4,W4]=freqz(b4,1,512,2);
stem(handles.axes1,b4,'filled');
grid on;
plot(handles.axes2,W4,20*log10(abs(H4)));%繪頻率響應圖形
set(handles.text_title1,'String','帶通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶通hamming窗頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 5
window5=blackman(N);
b5=fir1(N-1,wn,window5);
[H5,W5]=freqz(b5,1,512,2);
stem(handles.axes1,b5,'filled');
grid on;
plot(handles.axes2,W5,20*log10(abs(H5)));%繪頻率響應圖形
set(handles.text_title1,'String','帶通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶通blackman窗頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 6
[n,Wn,beta,~]=kaiserord([fs1fp1 fp2 fs2],
[0 1 0],[0.01 0.01 0.01],100);%求階數n以及引數beta
window6=kaiser(n+1,beta);%使用kaiser窗函式
b6=fir1(n,Wn,window6,'noscale');
[H6,W6]=freqz(b6,1,512,2);
stem(handles.axes1,b6,'filled');
grid on;
plot(handles.axes2,W6,20*log10(abs(H6)));%繪頻率響應圖形
set(handles.text_title1,'String','帶通FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶通kaiser窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
end
case 4 %帶阻濾波器
wp1=2*pi*fp1/Fs;%將通帶下限截止頻率轉換為數字濾波器頻率
wp2=2*pi*fp2/Fs;%將通帶上限截止頻率轉換為數字濾波器頻率
ws1=2*pi*fs1/Fs;%將通帶下限截止頻率轉換為數字濾波器頻率
ws2=2*pi*fs2/Fs;%將通帶上限截止頻率轉換為數字濾波器頻率
Bt=ws1-wp1;
N0=ceil(6.2*pi/Bt);
N=N0+mod(N0+1,2);
M=N-1;
wn=[(wp1+ws1)/2/pi,(wp2+ws2)/2/pi];
set(handles.text_M,'String',M);
if (fp1>fs1 || fp1>fs2 || fp1>fp2 || fs1>fs2 || fs1>fp2|| fs2>fp2)
msgbox('錯誤:資料輸入有誤,請重新輸入...','提示','error');
elseif (fs1==fp1 || fs1==fp2 ||fs1==fs2 || fs2==fp1 || fs2==fp2 ||fp1==fp2)
msgbox('警告:存在問題,請檢查!','提示','warn');
end
windows_value=get(handles.windows,'value');
switch windows_value %窗函式選擇
case 1 %使用boxcar窗函式
window1=boxcar(N);
b1=fir1(N-1,wn,'stop',window1);
[H1,W1]=freqz(b1,1,512,2);
stem(handles.axes1,b1,'filled');
grid on;
plot(handles.axes2,W1,20*log10(abs(H1)));%繪頻率響應圖形
set(handles.text_title1,'String','帶阻FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶阻boxcar窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 2
window2=bartlett(N);
b2=fir1(N-1,wn,'stop',window2);
[H2,W2]=freqz(b2,1,512,2);
stem(handles.axes1,b2,'filled');
grid on;
plot(handles.axes2,W2,20*log10(abs(H2)));%繪頻率響應圖形
set(handles.text_title1,'String','帶阻FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶阻bartlett窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 3
window3=hanning(N);
b3=fir1(N-1,wn,'stop',window3);
[H3,W3]=freqz(b3,1,512,2);
stem(handles.axes1,b3,'filled');
grid on;
plot(handles.axes2,W3,20*log10(abs(H3)));%繪頻率響應圖形
set(handles.text_title1,'String','帶阻FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶阻hanning窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 4
window4=hamming(N);
b4=fir1(N-1,wn,'stop',window4);
[H4,W4]=freqz(b4,1,512,2);
stem(handles.axes1,b4,'filled');
grid on;
plot(handles.axes2,W4,20*log10(abs(H4)));%繪頻率響應圖形
set(handles.text_title1,'String','帶阻FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶阻hamming窗頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 5
window5=blackman(N);
b5=fir1(N-1,wn,'stop',window5);
[H5,W5]=freqz(b5,1,512,2);
stem(handles.axes1,b5,'filled');
grid on;
plot(handles.axes2,W5,20*log10(abs(H5)));%繪頻率響應圖形
set(handles.text_title1,'String','帶阻FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶阻blackman窗頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
case 6
[n,Wn,beta,~]=kaiserord([fp1fs1 fs2 fp2],
[1 0 1],[0.01 0.01 0.01],100);%求階數n以及引數beta
window6=kaiser(n+1,beta);%使用kaiser窗函式
b6=fir1(n,Wn,'stop',window6,'noscale');
[H6,W6]=freqz(b6,1,512,100);
stem(handles.axes1,b6,'filled');
grid on;
plot(handles.axes2,W6,20*log10(abs(H6)));%繪頻率響應圖形
set(handles.text_title1,'String','帶阻FIR數字濾波器h(n)波形');
set(handles.text_x1,'String','n');
set(handles.text_y1,'String','h(n)');
set(handles.text_title2,'String','帶阻kaiser窗的頻率響應圖形');
set(handles.text_x2,'String','頻率(Hz)');
set(handles.text_y2,'String','幅值');
end
end