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

📄 afdplt.m

📁 ADSP TOOLBOX: Version 2.0 and gui m-files
💻 M
字号:
function afdplt
% AFDPLT  Support file for AFDGUI


% ADSP Toolbox: Version 2.0 
% For use with "Analog and Digital Signal Processing", 2nd Ed.
% Published by PWS Publishing Co.
%
% Ashok Ambardar, EE Dept. MTU, Houghton, MI 49931, USA
% http://www.ee.mtu/faculty/akambard.html
% e-mail: akambard@mtu.edu
% Copyright (c) 1998



% obtain necessary handles
f = gcf;
ui = get(f,'userdata');
filter_uis  = ui(3:4);
proto_uis   = ui(5:6);
edge_edt   = ui(11);
attn_edt   = ui(13);
autoplot    = ui(46);
plot_btn    = ui(47);
lin_btn     = ui(50);
curs_btn    = ui(20);
ord_edt     = ui(68);
autodes     = ui(71);
mandes      = ui(72);
resp_axs    = ui(73);
phase_axs    = ui(7);
phase_line  = ui(8);
resp_line   = ui(74);
resp_line2  = ui(75);
resp_line3  = ui(76);
resp_line4  = ui(77);
freq_pop = ui(82);
freq_edt = ui(84);
log_pop   = ui(91);
sig_axs     = ui(89);
sig_line    = ui(90);
pz_axes   = ui(85);
uc_line   = ui(88);
z_line   = ui(86);
p_line   = ui(87);
an_ord_des = ui(78);
an_ap_des  = ui(79);
an_as_des  = ui(80);
%%%%%%%%%%%%%%
if get(ui(20),'userdata')==1
set(f,'currentaxes',ui(73));
zoom out,zoom reset
set(f,'currentaxes',ui(7));
zoom out,zoom reset
set(f,'currentaxes',ui(85));
zoom out,zoom reset
set(f,'currentaxes',ui(89));
zoom out,zoom reset
end
%%%%%%%%%%%%%%

f_axis = get(freq_pop,'value');
f_lim = eval(['[',get(freq_edt,'string'),']']);
if length(f_lim)<2
   errordlg('Limits must be in the form Min,Max.','Input Error');
   return
end
if f_lim(1)>=f_lim(2)
   errordlg('Limits must be in the form Min,Max.','Input Error');
   return
end

log_lin = get(log_pop,'value');
if log_lin == 2
   if f_lim(1)<=0
      set(curs_btn,'value',0);afdgui(9);
      errordlg('Log scale requires positive frequencies.','Input Error');
      return;
   end
end


% get the values
filter_type = get(filter_uis(2),'value');
proto_type = get(proto_uis(2),'value');

attn    = eval(['[',get(attn_edt,'string'),']'],'[]');
if length(attn)~=2
      errordlg('Need 2 values for attenuation.','Input Error');
      return;
end
if diff(attn)<=0
     errordlg('Attenuation values must be in increasing order.','Input Error');
      return;
end
attnp=attn(1);attns=attn(2);


% verify lengths
edge    = eval(['[',get(edge_edt,'string'),']'],'[]');

if any(filter_type == [1:2]),
   if length(edge) ~= 2,
      errordlg('LP & HP filters need 2 band edges.','Input Error');
      return;
   end
else
   if length(edge) ~= 4,
      errordlg('BP & BS filters need 4 band edges.','Input Error');
      return;
   end
end
%Check increasing order
if any(diff(edge)<=0)
      errordlg('Band edges must be in increasing order.','Input Error');
      return;
end
if filter_type == 1
edgep=edge(1);edges=edge(2);ty='lp';
elseif filter_type == 2
edgep=edge(2);edges=edge(1);ty='hp';
elseif filter_type == 3
edgep=edge(2:3);edges=[edge(1) edge(4)];ty='bp';
elseif filter_type == 4
edges=edge(2:3);edgep=[edge(1) edge(4)];ty='bs';
end

if proto_type==1, ty1='bw';
elseif proto_type ==2, ty1='c1';
elseif proto_type ==3, ty1='c2';
elseif proto_type ==4, ty1='el';
end

% if all checks out, do the plot
if get(autodes,'value')
[na,da] = afd(ty1,ty,[attnp,attns],edgep,edges);
else
ord = eval(['[',get(ord_edt,'string'),']'],'[]');
if ~length(ord)
      errordlg('Order not specified.','Input Error');
      return;
end
if ~length(ord)
      errordlg('Order not specified.','Input Error');
      return;
end
if filter_type >2 & rem(ord,2)
      errordlg('BP and BS filters require even order.','Input Error');
      return;
end

if filter_type>2,ord=ord/2;end
if proto_type==4
if ord > 10
errordlg('Require n<=10 (lp,hp) or n<=20 (bp,bs) for ELLIPTIC.','Input Error');
return;
end
end

[n1,d1]=lpp(ty1,ord,[attnp,attns]);
if filter_type>2,
bw=2*pi*abs(diff(edgep));fc=2*pi*sqrt(prod(edgep));
[na,da]=lp2af(ty,n1,d1,fc,bw);
else
bw=2*pi*edgep;[na,da]=lp2af(ty,n1,d1,bw);
end
end

%%%%%%%% PLOT %%%%%%%%%
fmin=f_lim(1);fmax=f_lim(2);
if log_lin==2
set([resp_axs,phase_axs],'xscale','log');
f=logspace(log10(fmin),log10(fmax),501);
else
set([resp_axs,phase_axs],'xscale','linear');
f=linspace(f_lim(1),f_lim(2),501);
end

if f_axis == 2
s=j*f;f_label = 'Frequency omega   [rad/s]'; 
else
s=j*2*pi*f;f_label='Frequency f   [Hz]';
end
H0 = polyval(na,s)./polyval(da,s);          % spectrum
HA=abs(H0);
fa1 = [edgep edges];
s=j*2*pi*fa1;
if f_axis==2,fa1=fa1*2*pi;end
HA1 = abs(polyval(na,s)./polyval(da,s));
gainp=10^(-attnp/20);
gains=10^(-attns/20);

if get(lin_btn,'value')==1
set(resp_axs,'xlim',[fmin fmax],'ylim',[0,max(HA)]);
set(resp_line3,'xdata',f,'ydata',HA);
set(ui(10),'userdata',[f(:) HA(:)]);

set(resp_line4,'xdata',fa1,'ydata',HA1);
set(resp_line,'xdata',[fmin fmax],'ydata',[gainp gainp]);
set(resp_line2,'xdata',[fmin fmax],'ydata',[gains gains]);
else
set(resp_axs,'xlim',[fmin fmax],'ylim',[-2*attns,0]);
HdB=HA;
i=find(HdB==0);
HdB(i)=10*eps*ones(size(i));
HdB=20*log10(HdB);
set(resp_line3,'xdata',f,'ydata',HdB);
set(resp_line4,'xdata',fa1,'ydata',20*log10(HA1));
set(resp_line,'xdata',[fmin fmax],'ydata',-[attnp attnp]);
set(resp_line2,'xdata',[fmin fmax],'ydata',-[attns attns]);
set(ui(10),'userdata',[f(:) HdB(:)]);
end

ptt=get(resp_axs,'xlabel');
set(ptt,'string',f_label);

%phase
th=180*angle(H0)/pi;th=th.*(abs(th)>1e-6);Pmin=min(th);Pmax=max(th);
set(phase_axs,'xlim',[fmin fmax],'ylim',[Pmin Pmax]);
set(phase_line,'xdata',f,'ydata',th);
%axs_pos=get(resp_axs,'pos');
mtt=get(phase_axs,'xlabel');
set(mtt,'string',f_label);
set(ui(11),'userdata',[f(:) th(:)]);

%Ap and As
if get(autodes,'value')
set(ui([67:68]),'string',int2str(length(da)-1));
end
HA1dB = -20*log10(HA1);
if length(HA1) == 2,
    set(ui(79),'string',num2str(HA1dB(1),4));
    set(ui(80),'string',num2str(HA1dB(2),5));
else
    set(ui(79),'string',[num2str(HA1dB(1),4),', ',num2str(HA1dB(2),5)]);
    set(ui(80),'string',[num2str(HA1dB(3),5),', ',num2str(HA1dB(4),5)]);
end


%PZ Plot 
while na(1)==0,na(1)=[];end
while da(1)==0,da(1)=[];end
ln=length(na);ld=length(da);
zz=roots(na);pp=roots(da);
rmin=min([real(pp);real(zz);-1]);rmax=max([real(pp);real(zz);1]);
imin=min([imag(pp);imag(zz);-1]);imax=max([imag(pp);imag(zz);1]);

if filter_type > 2, rmin=4*rmin;end
set(pz_axes,'xlim',1.1*[rmin, rmax],'ylim',1.1*[imin, imax]);
set(p_line,'xdata',real(pp),'ydata',imag(pp));
set(z_line,'xdata',real(zz),'ydata',imag(zz));
tmax=4/min(abs(real(pp)));
n_lim=[0,tmax];
t=linspace(n_lim(1),n_lim(2),501);

%Step response
%tdd=get(ui(47),'value');
tdd=2; % Step response

if tdd==1
[h_impf,h_impn]=ilt('tf',na,da);tit='Impulse Response h(t)';
elseif tdd==2
[h_impf,h_impn]=ilt('tf',na,[da 0]);tit='Step Response s(t)';
end
if ~isempty(h_impn),
h_imp=eval(h_impn);h_imp=h_imp.*(abs(h_imp)>1e-6);
else
h_imp=0*t;
end
td_resp=[tit ' = ' h_impf];

hmin=min(h_imp);hmax=max(h_imp);
if hmin>0,hmin=0;end
if hmax<0,hmax=0;end
if hmax==hmin
hmax=hmax+0.5;hmin=hmin-0.5;
end
set(sig_line,'xdata',t,'ydata',h_imp);
set(sig_axs,'xlim',[n_lim(1),n_lim(2)],'ylim',[hmin,hmax]);
mtt=get(sig_axs,'title');
set(mtt,'string',tit);
set(sig_axs,'userdata',tit);

%Save variables
set(ui(1),'userdata',na);
set(ui(2),'userdata',da);
set(ui(12),'userdata',h_impf);

%set(ui(2),'userdata',[t(:) h_imp(:)]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -