📄 idealplt.m
字号:
function idealplt
% IDEALPLT Plot control for IDEALGUI.
% Author: Ashok Ambardar <akambard@mtu.edu>
% Date: 2/2/96 11:32AM
% Copyright (c) 1997 by Prentice-Hall, Inc.
%Revised Aug 12,96
% Revised Sep 22, 96
f = gcf;
ui = get(f,'userdata');
filter_pop = ui(4);
window_pop = ui(13);
n_edt = ui(6);
%cutoff_uis = ui(8:10);
cutoff_edt = ui(9);
beta_edt = ui(15);
ripple_edt = ui(18);
harris_edt = ui(27);
autoplot = ui(21);
plot_btn = ui(22);
axs = ui(23);
ph_axs = ui(36);
ph_line = ui(37);
win_axs = ui(38);
win_line = ui([39,40]);
wind_axs = ui(41);
wind_line = ui([42,43]);
line1 = ui(24);
line2 = ui(25);
%%%%%%%
if get(ui(5),'userdata')==1
set(f,'currentaxes',ui(23));
zoom out,zoom reset
set(f,'currentaxes',ui(36));
zoom out,zoom reset
set(f,'currentaxes',ui(38));
zoom out,zoom reset
set(f,'currentaxes',ui(41));
zoom out,zoom reset
end
%%%%%%%%%
min_dB=eval(get(ui(35),'string'));
if min_dB >= 0
errordlg('Minmum dB Scale must be less than zero.','Input Error');
return
end
filter_type = get(filter_pop,'value');
window_type = get(window_pop,'value');
n = eval(['[',get(n_edt,'string'),']'],'[]');
if isempty(n),
errordlg('MATLAB syntax error.','Input Error');
return;
elseif n ~= round(n),
errordlg('Length must be an integer.','Input Error');
return;
elseif n <2,
errordlg('Length must be >= 2.','Input Error');
return;
end
cutoff = eval(['[',get(cutoff_edt,'string'),']'],'[]');
beta = eval(['[',get(beta_edt,'string'),']'],'[]');
ripple = eval(['[',get(ripple_edt,'string'),']'],'[]');
harris = eval(['[',get(harris_edt,'string'),']'],'[]');
% verify lengths
if any(filter_type == [1:2]),
if length(cutoff) == 2,
errordlg('Only one cutoff frequency needed.','Input Error');
return;
elseif cutoff < 0 | cutoff >= 0.5,
errordlg('Cutoffs must be 0 <= F < 0.5.','Input Error');
return;
end
else
if length(cutoff) == 1,
errordlg('Two cutoff frequencies needed.','Input Error');
return;
elseif any(cutoff < 0) | any(cutoff >= 0.5),
errordlg('Cutoffs must be 0 <= F < 0.5.','Input Error');
return;
elseif diff(cutoff) <= 0,
errordlg('Cutoffs must be in increasing order.','Input Error');
return;
end
end
if any(filter_type == [2, 4])
if ~rem(n,2),
errordlg('HP and BS filters require ODD length.','Input Error');
return
end
end
% compute window
if window_type == 1,
win = ones(1,n);
elseif any(window_type == [(2:4)]),
win = winwind(window_type-1,n);
elseif window_type == 5,
win = winwind(window_type-1,n,beta);
elseif window_type == 6, % Cheby
if ~rem(n,2),
errordlg('Cheby requires ODD integer length.','Input Error');
return
elseif ripple <= 0
errordlg('Ripple value must be positive.','Input Error');
return
end
win = winwind(window_type-1,n,ripple);
elseif window_type == 7,
if harris<0|harris>7|rem(harris,1)~=0,
errordlg('Enter integer type from 0 to 7.','Input Error');
return
end
win = winwind(window_type-1,n,harris);
end
% PLOTS
set(win_axs,'xlim',[0 n-1],'ylim',[0, 1.1*max(win)]);
dtstem(win_line,0:n-1,win);
set(ui(44),'userdata',win);
fac=max([4,fix(400/n)]);
if window_type==1
df=0.5/n/fac;
else
df=0.5/(n-1)/fac;
end
f = 0:df:0.5;
if length(cutoff)==1,
fc=cutoff;
%if filter_type == 2,fc=0.5-fc;end
else
f0=0.5*sum(cutoff);fc=max(cutoff)-f0;
end
n0 = fix(n/2);
n1 = 1:n0;
if rem(n,2) == 0, n1 = n1 - 0.5; end
h = 2*fc*sinc(2*n1*fc);
if rem(n,2) == 1,
h = [fliplr(h) 2*fc h];
else
h = [fliplr(h) h];
end
hw = h .* win;
if filter_type == 1,
hwn=hw;
end
if rem(n,2)==1,
nn=-n0:n0;
else,
nn=[-n0+.5:-.5 .5:n0-.5];
end
if filter_type == 2,
hwn = -hw;
% hwn=hw.*((-1).^nn);
hwn(n0+1)=1+hwn(n0+1);
end
if filter_type == 3
hwn=2*cos(2*pi*nn*f0).*hw;
end
if filter_type == 4,
hwn = -2*cos(2*pi*nn*f0).*hw;
hwn(n0+1)=1+hwn(n0+1);
end
hwn=hwn.*(abs(hwn)>10*eps);
set(ui(45),'userdata',hwn);
dy=0.05*max(hwn);
set(wind_axs,'xlim',[0 n-1],'ylim',[min(hwn)-dy max(hwn)+dy]);
dtstem(wind_line,0:n-1,hwn);
HW = abs(freqz(win,1,2*pi*f));
HWdB = 20*log10(HW+(HW==0))-5000*(HW==0);
H=HWdB-max(HWdB);
% Figures of merit
d=diff(H);
i=find(d>=0);
if ~isempty(i)
iw=i(1);
WM=f(iw);
H2=H(iw:length(H));
PSL=max(H2);
iP=find(H<=PSL);
iP=iP(1);
WS=f(iP);
else
WM=[];PSL=[];WS=[];
end
set(ui(54),'string',num2str(PSL));
set(ui(56),'string',num2str(WM));
set(ui(58),'string',num2str(WS));
if get(ui(32),'value')==1
set(ph_axs,'ylim',[0,max(HW)]);
set(ph_line,'xdata',f,'ydata',HW);
set(ui(46),'userdata',[f(:) HW(:)]);
else
set(ph_axs,'ylim',[min_dB,0]);
set(ph_line,'xdata',f,'ydata',H);
set(ui(46),'userdata',[f(:) H(:)]);
end
set(ph_axs,'xlim',[0,0.5]);
HF = freqz(hwn,1,2*pi*f);
tf = abs(HF);
HdB = 20*log10(tf+(tf==0))-5000*(tf==0);
HdB=HdB-20*log10(max(tf));
if get(ui(32),'value')==1
if filter_type == 1, % LP
F=[0 cutoff cutoff 0.5];
M=[1 1 0 0];
elseif filter_type == 2, % HP
F=[0 cutoff cutoff 0.5];
M=[0 0 1 1];
elseif filter_type == 3, % BP
F=[0 cutoff(1) cutoff(1) cutoff(2) cutoff(2) 0.5];
M=[0 0 1 1 0 0];
else, % BS
F=[0 cutoff(1) cutoff(1) cutoff(2) cutoff(2) 0.5];
M=[1 1 0 0 1 1];
end
set(line1,'xdata',f,'ydata',tf);
set(ui(47),'userdata',tf(:));
set(line2,'xdata',F,'ydata',M);
max1=max(tf);maxy=max([1,max1]);
set(axs,'ylim',[0,max1]);
else
%L=min_dB-5;
L=-100;
if filter_type == 1, % LP
F=[0 cutoff cutoff 0.5];
M=[0 0 L L];
elseif filter_type == 2, % HP
F=[0 cutoff cutoff 0.5];
M=[L L 0 0];
elseif filter_type == 3, % BP
F=[0 cutoff(1) cutoff(1) cutoff(2) cutoff(2) 0.5];
M=[L L 0 0 L L];
else, % BS
F=[0 cutoff(1) cutoff(1) cutoff(2) cutoff(2) 0.5];
M=[0 0 L L 0 0];
end
set(axs,'ylim',[min_dB max(HdB)]);
set(line1,'xdata',f,'ydata',HdB);
set(ui(47),'userdata',HdB(:));
set(line2,'xdata',F,'ydata',M);
end
set(axs,'xlim',[0,0.5]);
AP=[];AS=[];TW=[];
mx=max(tf)-1;
if mx>0,AP=-20*log10((1-mx)/(1+mx));end
if filter_type==1
i=find(f>=fc);H=HdB(i);d=diff(H);
i=find(d>=0);
if ~isempty(i)
iw=i(1);
H2=H(iw:length(H));
AS=-max(H2);
end
elseif filter_type==2
i=find(f<=fc);H=HdB(i);H=fliplr(H);d=diff(H);
i=find(d>=0);
if ~isempty(i)
iw=i(1);
H2=H(iw:length(H));
AS=-max(H2);
end
elseif filter_type==3
i=find(f>cutoff(2));H=HdB(i);
d=diff(H);i=find(d>=0);
if ~isempty(i),iw=i(1);H2=H(iw:length(H));AS1=-max(H2);
else
AS1=[];%AS1=-H(length(H));
end
i=find(f<cutoff(1));H=HdB(i);H=fliplr(H);
d=diff(H);i=find(d>=0);
if ~isempty(i),iw=i(1);H2=H(iw:length(H));AS2=-max(H2);
else
AS2=[];%AS2=-H(length(H));
end
if ~isempty(AS1) | ~isempty(AS2)
AS=min([AS1,AS2]);
end
else
i=find(f>cutoff(1) & f<f0);H=HdB(i);
d=diff(H);i=find(d>=0);
if ~isempty(i),
iw=i(1);H2=H(iw:length(H));AS1=-max(H2);
else
AS1=[];%AS1=-H(length(H));
end
i=find(f>f0 & f<cutoff(2));H=HdB(i);H=fliplr(H);
d=diff(H);i=find(d>=0);
if ~isempty(i),
iw=i(1);H2=H(iw:length(H));AS2=-max(H2);
else
AS2=[];%AS2=-H(length(H));
end
if ~isempty(AS1) | ~isempty(AS2)
AS=min([AS1,AS2]);
end
end
set(ui(61),'string',num2str(AP));
set(ui(63),'string',num2str(AS));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -