📄 dfdpzp4.m
字号:
function dfdpzp4(mode)
% DFDPZP4 Interactive digital filter design by pole-zero placement
%
% DFDPZP4 invokes the digital filter design environment for Matlab
% versions 4.x and 5.x.
%
% Push Button Scheme:
%
% Add Poles - sets the Add Poles mode for the mouse. Adding poles
% is accomplished by selecting a point within the
% Pole-Zero Diagram [the z-plane and unit circle]
%
% Add Zeros - sets the Add Zeros mode for the mouse. Adding zeros
% is accomplished by selecting a point within the
% Pole-Zero Diagram
%
% Del Poles - sets the Delete mode for the mouse. Deleting poles
% is accomplished by selecting the desired pole to be
% deleted
%
% Del Zeros - sets the Delete mode for the mouse. Deleting zeros
% is accomplished by selecting the desired zero to be
% deleted
%
% Update - updates all plots
%
% To Work - To Workspace allows coefficients of H(z) to be
% transferred to the workspace in descending powers
% of z and numerator/denominator format
%
% Notes: 1) In the add modes, the pointer is a crosshair.
% 2) In the delete modes, the pointer is a bullseye (circle).
% 3) The xlabel of the Pole-Zero Diagram displays the current mode.
% 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
j=sqrt(-1);
if nargin==0, % setup environment
if exist('version')~=5,
disp('This routine is not for MATLAB v3')
return
end
clf
axs_han(1) = subplot(2,2,1);
xn=exp(j*(pi/6:pi/6:2*pi));
ellipse(1)
axis('square')
set(gca,'Xtick',[],'Ytick',[])
hold on
ellipse(0.25)
ellipse(0.5)
ellipse(0.75)
zn=zeros(length(xn));
plot([zn;real(xn)],[zn;imag(xn)])
title('Pole-Zero Diagram');
%polar(2*pi,1),hold on,axis(axis),grid on,
mode = 0;
btn(1) = uicontrol('style','push','units','norm','pos',[0.0080,0.8907,0.1400,0.0533],...
'string','Add Poles','callback','dfdpzp4(1)');
btn(2) = uicontrol('style','push','units','norm','pos',[0.0080,0.8240,0.1400,0.0533],...
'string','Add Zeros','callback','dfdpzp4(2)');
btn(3) = uicontrol('style','push','units','norm','pos',[0.0080,0.7573,0.1400,0.0533],...
'string','Del Poles','callback','dfdpzp4(3)');
btn(4) = uicontrol('style','push','units','norm','pos',[0.0080,0.6907,0.1400,0.0533],...
'string','Del Zeros','callback','dfdpzp4(4)');
btn(5) = uicontrol('style','push','units','norm','pos',[0.0080,0.6240,0.1400,0.0533],...
'string','Plot','callback','dfdpzp4(9)');
btn(6) = uicontrol('style','push','units','norm','pos',[0.0080,0.5573,0.1400,0.0533],...
'string','To Work','callback','pzpvar(0)');
set(btn(3),'userdata',axs_han(1));
if matverch < 5,typ='yes';else,typ='on';end
set(gcf,'userdata',btn,'interruptible',typ);
dfdpzp4(1);
else
f = gcf;
btn = get(f,'userdata');
axs_han = get(btn(3),'userdata');
subplot(2,2,1);
ax = gca;
deleted = 0;
end
if any(mode == [1,5]), % add poles
if mode == 1,
xlabel('Mode: Add Poles');
set(f,'pointer','crosshair','windowbuttondownfcn','dfdpzp4(5)');
% refresh pole/zero axis
set(axs_han,'color',get(axs_han,'color'));
else
pt = get(ax,'currentpoint');
xx = pt(1,1); yy = pt(1,2);
if abs(xx+j*yy) < 0.02, % snap to origin
xx=0; yy=0;
end
if abs(yy) < 0.02, % snap to real axis
yy=0;
end
if yy ~= 0,
xx=[xx,xx]; yy=[yy,-yy];
end
if matverch < 5,typ1='none';else,typ1='xor';end
%typ1='xor';
pole = plot(xx,yy,'rx','erase',typ1);
all_poles = get(btn(1),'userdata');
all_poles = [all_poles,pole];
set(btn(1),'userdata',all_poles);
end
elseif any(mode == [2,6]), % add zeros
if mode == 2,
xlabel('Mode: Add Zeros');
set(f,'pointer','crosshair','windowbuttondownfcn','dfdpzp4(6)');
% refresh pole/zero axis
set(axs_han,'color',get(axs_han,'color'));
else
pt = get(ax,'currentpoint');
xx = pt(1,1); yy = pt(1,2);
if abs(xx+j*yy) < 0.02, % snap to origin
xx=0; yy=0;
end
if abs(yy) < 0.02, % snap to real axis
yy=0;
end
if yy ~= 0,
xx=[xx,xx]; yy=[yy,-yy];
end
zz=xx+j*yy;
r=abs(zz);
th=angle(zz);
if abs(1-r(1)) < 0.02,
xx=cos(th);
yy=sin(th);
end
if matverch < 5,typ1='none';else,typ1='xor';end
%typ1='xor';
zero = plot(xx,yy,'go','erase',typ1);
all_zeros = get(btn(2),'userdata');
all_zeros = [all_zeros,zero];
set(btn(2),'userdata',all_zeros);
end
elseif any(mode == [3,7]), % delete pole
if mode == 3,
xlabel('Mode: Delete Poles');
set(f,'pointer','circle','windowbuttondownfcn','dfdpzp4(7)');
else
pt = get(ax,'currentpoint');
sel_pt = pt(1,1)+j*pt(1,2);
% had to make a switch to using the currentpoint instead of the currentobject
% because matlab seems to scramble the stacking order or the x's and o's and
% the lines that make up the circular grid lines. this caused the selection
% of a grid line instead of the desired x or o.
all_poles = get(btn(1),'userdata');
deleted = 0;
for k = 1:length(all_poles),
xdata = get(all_poles(k),'xdata');
ydata = get(all_poles(k),'ydata');
pol_pt = xdata+j*ydata;
if any(abs(sel_pt - pol_pt)<0.05),
deleted = 1;
delete(all_poles(k));
all_poles(k) = [];
break;
end
end
if deleted,
set(btn(1),'userdata',all_poles);
end
% refresh pole/zero axis
set(axs_han,'color',get(axs_han,'color'));
end
elseif any(mode == [4,8]), % delete zero
if mode == 4,
xlabel('Mode: Delete Zeros');
set(f,'pointer','circle','windowbuttondownfcn','dfdpzp4(8)');
else
pt = get(ax,'currentpoint');
sel_pt = pt(1,1)+j*pt(1,2);
% had to make a switch to using the currentpoint instead of the currentobject
% because matlab seems to scramble the stacking order or the x's and o's and
% the lines that make up the circular grid lines. this caused the selection
% of a grid line instead of the desired x or o.
all_zeros = get(btn(2),'userdata');
deleted = 0;
for k = 1:length(all_zeros),
xdata = get(all_zeros(k),'xdata');
ydata = get(all_zeros(k),'ydata');
zer_pt = xdata+j*ydata;
if any(abs(sel_pt - zer_pt)<0.05),
deleted = 1;
delete(all_zeros(k));
all_zeros(k) = [];
break;
end
end
if deleted,
set(btn(2),'userdata',all_zeros);
end
% refresh pole/zero axis
set(axs_han,'color',get(axs_han,'color'));
end
elseif mode==9,
all_poles = get(btn(1),'userdata');
all_zeros = get(btn(2),'userdata');
if isempty(all_zeros),
num=1;
else
zer=[];
for k=1:length(all_zeros),
xdata = get(all_zeros(k),'xdata');
ydata = get(all_zeros(k),'ydata');
zeros_loc = xdata + ydata*i;
zer = [zeros_loc.';zer];
end
num=real(poly(zer.'));
end
if isempty(all_poles),
den=1;
else
pol=[];
for k=1:length(all_poles),
xdata = get(all_poles(k),'xdata');
ydata = get(all_poles(k),'ydata');
poles_loc = xdata + ydata*i;
pol = [poles_loc.';pol];
end
den=real(poly(pol.'));
end
n=0:100;
n0=0*n';
n0(1)=1;
y1=filter(num,den,n0);
y2=filter(num,den,0*n+1);
n=n(1:2:101);
y1=y1(1:2:101);
y2=y2(1:2:101);
w=0:pi/200:pi;
zzz=exp(sqrt(-1)*w);
h=polyval(num,zzz)./polyval(den,zzz);
%hold off,axis('normal');
%eval('figure(2)')
subplot(2,2,2);
cla
plot(w/2/pi,abs(h))
xlabel('Frequency Response vs F')
subplot(2,2,3);
cla
dtplot(n,y1,'.');
xlabel('Impulse response n=0:100')
subplot(2,2,4);
cla
dtplot(n,y2,'.');
xlabel('Step response n=0:100')
% refresh pole/zero axis
set(axs_han,'color',get(axs_han,'color'));
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -