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

📄 diirplt.m

📁 ADSP TOOLBOX: Version 2.0 and gui m-files
💻 M
字号:
function diirplt
%DIIRPLT Support file for DIIRGUI


% 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



f = gcf;
ui = get(f,'userdata');
filter_uis  = ui(3:4);
proto_uis   = ui(5:6);
method_uis  = ui(7:8);
edge_edt   = ui(11);
attn_edt   = ui(13);
samp_uis    = ui(19:20);
order_uis   = ui(32);
autoplot    = ui(46);
plot_btn    = ui(47);
ap_des      = ui(50);
as_des      = ui(52);
phase_axs    = ui(17);
phase_line  = ui(18);
ord_edt     = ui(68);
autodes     = ui(71);
mandes      = ui(72);
resp_axs    = ui(73);
resp_line   = ui(74);
resp_line2  = ui(75);
resp_line3  = ui(76);
resp_line4  = ui(77);
pz_axes   = ui(85);
uc_line   = ui(88);
z_line   = ui(86);
p_line   = ui(87);
sig_axs     = ui(90);
sig_line    = ui([91,92]);
lin_btn     = ui(22);
an_ord_des = ui(78);
an_ap_des  = ui(79);
an_as_des  = ui(80);

%%%%%%
if get(ui(10),'userdata')==1
set(f,'currentaxes',ui(73));
zoom out,zoom reset
set(f,'currentaxes',ui(17));
zoom out,zoom reset
set(f,'currentaxes',ui(85));
zoom out,zoom reset
set(f,'currentaxes',ui(90));
zoom out,zoom reset
end
%%%%%%%



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

%order_n  = str2num(get(order_uis(2),'string'));
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

if filter_type == 1
edgep=edge(1);edges=edge(2);ty1='lp';
elseif filter_type == 2
edgep=edge(2);edges=edge(1);ty1='hp';
elseif filter_type == 3
edgep=edge(2:3);edges=[edge(1) edge(4)];ty1='bp';
elseif filter_type == 4
edges=edge(2:3);edgep=[edge(1) edge(4)];ty1='bs';
end

sampf    = str2num(get(samp_uis(2),'string'));
emax=max([edgep edges]);
if sampf <= 2*emax
   errordlg('Sampling Frequency not high enough.','Input Error');
   return;
end

if proto_type==1, ty='bw';
elseif proto_type==2, ty='c1';
elseif proto_type==3, ty='c2';
elseif proto_type==4, ty='el';
end
tym=[];if method_type==1,ty2='bili';
elseif method_type==2,ty2='impu';
elseif method_type==3,ty2='mat0';tym='0';
elseif method_type==4,ty2='mat1';tym='1';
elseif method_type==5,ty2='mat2';tym='2';
elseif method_type==6,ty2='back';
end

if get(autodes,'value')
[num,den,np,dp,na,da]=dfdiir(ty,ty1,ty2,[attnp,attns],sampf,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

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

if ty2=='bili'
[num,den]=lp2iir(ty1,'a',np,dp,1,edgep/sampf);
else
if ty2=='impu',[dtfn,dtfd]=s2zinvar(np,dp,1,ty2,0);
elseif ty2(1:3)=='mat',
[dtfn,dtfd]=s2zmatch(np,dp,1,tym,0);
elseif ty2=='back'
[dtfn,dtfd]=s2zni(np,dp,1,ty2,0);
end
[num,den]=lp2iir(ty1,'d',dtfn,dtfd,1,edgep/sampf,1/2/pi);
end
end

set(ui(25),'userdata',num);
set(ui(26),'userdata',den);

df = 1/400;
f = 0:df:0.5; % frequency array
j = sqrt(-1);
z = exp(j*2*pi*f);                           % to find H(z)
HD = polyval(num,z)./polyval(den,z);          % spectrum
H=abs(HD);
s = j*2*pi*f;                         % to find H(s)
HA = abs(polyval(na,s)./polyval(da,s));          % spectrum

if H == 0,
   errordlg('Sampling frequency too low.','Input Error');
   return;
end

fa=f*sampf;


f1 = [edgep edges]/sampf;
z = exp(j*2*pi*f1);
H1 = abs(polyval(num,z)./polyval(den,z));
s=j*2*pi*f1;
HA1 = abs(polyval(na,s)./polyval(da,s));
fa1=[edgep edges];

if get(lin_btn,'value')==1

set(resp_axs,'xlim',[0 0.5*sampf],'ylim',[0,max([H,HA])]);
set(resp_line,'xdata',fa,'ydata',H);
set(resp_line3,'xdata',fa,'ydata',HA);
set(ui(27),'userdata',[fa(:) H(:)]);
set(resp_line2,'xdata',fa1,'ydata',H1);
set(resp_line4,'xdata',fa1,'ydata',HA1);

else

%%%% Avoid warning divide by zero
HdB=H;
i=find(HdB==0);
HdB(i)=10*eps*ones(size(i));
HdB=20*log10(HdB);
HAdB=HA;
i=find(HAdB==0);
HAdB(i)=10*eps*ones(size(i));
HAdB=20*log10(HAdB);
%%%%%%%%

set(resp_axs,'xlim',[0 0.5*sampf],'ylim',[-2*attns,0]);
set(resp_line,'xdata',fa,'ydata',HdB);
set(ui(27),'userdata',[fa(:) HdB(:)]);

set(resp_line3,'xdata',fa,'ydata',HAdB);
set(resp_line2,'xdata',fa1,'ydata',20*log10(H1));
set(resp_line4,'xdata',fa1,'ydata',20*log10(HA1));

end

%display phase
th=180*angle(HD)/pi;th=th.*(abs(th)>1e-6);Pmin=min(th);Pmax=max(th);
set(phase_axs,'xlim',[0 0.5*sampf],'ylim',[Pmin Pmax]);
set(phase_line,'xdata',fa,'ydata',th);
set(ui(28),'userdata',th(:));

if get(autodes,'value')
set(ui([67:68]),'string',int2str(length(da)-1));
end

%digital order
set(order_uis,'string',int2str(length(den)-1));

H1dB = -20*log10(H1);
if length(H1) == 2,
   set(ap_des,'string',num2str(H1dB(1)));
   set(as_des,'string',num2str(H1dB(2)));
else
   set(ap_des,'string',num2str(max(H1dB(1:2))));
   set(as_des,'string',num2str(min(H1dB(3:4))));
end

% analog order 
set(ui(78),'string',int2str(length(da)-1));

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(max(HA1dB(1:2)),4));
    set(ui(80),'string',num2str(min(HA1dB(3:4)),5));
end

%Pole-zero plot
hn=num;hd=den;
while hn(1)==0,hn(1)=[];end
while hd(1)==0,hd(1)=[];end
ln=length(hn);ld=length(hd);
dif=ln-ld;if dif<0,dif=0;end

fr=0:1/360:1;
phi=fr*2*pi;
cir=exp(sqrt(-1)*phi);
set(uc_line,'xdata',real(cir),'ydata',imag(cir))

zz=roots(hn);pp=roots(hd);
i=find(max(abs(zz))>1e4);zz(i)=[];
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]);
%set(pz_axes,'xlim',[rmin, rmax],'ylim',[imin, imax]);
maxx=max(abs([rmax,imax,rmin,imin]));
set(pz_axes,'xlim',[-maxx, maxx],'ylim',[-maxx, maxx]);
set(p_line,'xdata',real(pp),'ydata',imag(pp));
set(z_line,'xdata',real(zz),'ydata',imag(zz));

%tdd=get(ui(47),'value');
n_lim=[0-dif,10*ld];n=n_lim(1):n_lim(2);
tdd=1;

%impulse response
if tdd==1
h_impn=izt('tf',hn,hd);tit='Impulse Response h[n]';
elseif tdd==2
h_impn=izt('tf',[hn 0],conv(hd,[1 -1]));tit='Step Response s[n]';
end
if ~isempty(h_impn)
h_imp=eval(h_impn);
h_imp=h_imp.*(abs(h_imp)>1e-6);
else
h_imp=0*n;
end
set(ui(29),'userdata',h_impn);

%Impulse Response
hmin=min(h_imp);hmax=max(h_imp);
if hmax==hmin
hmax=hmax+0.5;hmin=hmin-0.5;
end
set(sig_axs,'xlim',[n_lim(1),n_lim(2)],'ylim',1.2*[hmin,hmax]);
dtstem(sig_line,n,h_imp);
ptt=get(sig_axs,'title');
set(ptt,'string',tit);
set(sig_axs,'userdata',tit);

⌨️ 快捷键说明

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