www.gusucode.com > 声音的处理有:LPC,FFT,共振峰,频谱源码程序 > siganlandsystemusingMatlab/SSUM/complex/complexpofn.m

    function complexpofn(action,datastruct)
% This code borrows from MAD polezero demonstration
    if nargin < 1
        action='init';
    end

	name = mfilename;
	figname = [name(1:end-2) '_fig'];
	f=findobj('Tag',figname);
    handles = get(f,'UserData');

    switch action
		case 'help'
			display_help(figname);
        case 'init'
			setdefaults;
            movegui(f,'center');
			reset(handles.pointplot);
			makePlot(handles);
			set(handles.x_point1,'String',num2str(rand));
            set(handles.y_point1,'String',num2str(rand));
			set(handles.x_point2,'String',num2str(rand));
            set(handles.y_point2,'String',num2str(rand));
			complexpofn('update','x1');
			complexpofn('update','x2');
			h_arrow = findobj(gcf,'Tag','Arrow2');
			delete(h_arrow);
			plotPoints(handles);
		case 'update'
			contents = get(handles.plotmenu,'String');
			switch datastruct
				case {'x1','y1'}
					x = str2num(get(handles.x_point1,'String'));
					y = str2num(get(handles.y_point1,'String'));
    				point=findobj(handles.pointplot,'Tag','Point1');
					[t,r] = cart2pol(x,y);
					t = 180*t/pi;
					set(handles.r_point1,'String',num2str(r));
					set(handles.t_point1,'String',num2str(t));
					h_arrow = findobj(gcf,'Tag','Arrow1');
					delete(h_arrow);
					h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
					set(h,'Tag','Arrow1');
					set(point,'Position',[x y]);
					switch contents{get(handles.plotmenu,'Value')};
						case {'Point 1', 'Point 2'}
						otherwise
							complexpofn('update','result');
							x = str2num(get(handles.x_result,'String'));
							y = str2num(get(handles.y_result,'String'));
							h_arrow = findobj(gcf,'Tag','Arrow3');
							delete(h_arrow);
							h = arrow('Start',[0 0],'Stop',[x y],'Length',10,...
								'Width',2);
							set(h,'Tag','Arrow3','FaceColor',[0 0 1],'EdgeColor',[0 0 1]);
					end
				case {'r1','t1'}
					r = str2num(get(handles.r_point1,'String'));
					t = str2num(get(handles.t_point1,'String'));
					t = t*pi/180;
					[x,y] = pol2cart(t,r);
    				point=findobj(handles.pointplot,'Tag','Point1');
					set(handles.x_point1,'String',num2str(x));
					set(handles.y_point1,'String',num2str(y));
					h_arrow = findobj(gcf,'Tag','Arrow1');
					delete(h_arrow);
					h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
					set(h,'Tag','Arrow1');
					set(point,'Position',[x y]);
					switch contents{get(handles.plotmenu,'Value')};
						case {'Point 1', 'Point 2'}
						otherwise
							complexpofn('update','result');
							x = str2num(get(handles.x_result,'String'));
							y = str2num(get(handles.y_result,'String'));
							h_arrow = findobj(gcf,'Tag','Arrow3');
							delete(h_arrow);
							h = arrow('Start',[0 0],'Stop',[x y],'Length',10,...
								'Width',2);
							set(h,'Tag','Arrow3','FaceColor',[0 0 1],'EdgeColor',[0 0 1]);
					end
				case {'x2','y2'}
					x = str2num(get(handles.x_point2,'String'));
					y = str2num(get(handles.y_point2,'String'));
    				point=findobj(handles.pointplot,'Tag','Point2');
					[t,r] = cart2pol(x,y);
					t = 180*t/pi;
					set(handles.r_point2,'String',num2str(r));
					set(handles.t_point2,'String',num2str(t));
					h_arrow = findobj(gcf,'Tag','Arrow2');
					delete(h_arrow);
					h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
					set(h,'Tag','Arrow2');
					set(point,'Position',[x y]);
					switch contents{get(handles.plotmenu,'Value')};
						case {'Point 1', 'Point 2'}
						otherwise
							complexpofn('update','result');
							x = str2num(get(handles.x_result,'String'));
							y = str2num(get(handles.y_result,'String'));
							h_arrow = findobj(gcf,'Tag','Arrow3');
							delete(h_arrow);
							h = arrow('Start',[0 0],'Stop',[x y],'Length',10,...
								'Width',2);
							set(h,'Tag','Arrow3','FaceColor',[0 0 1],'EdgeColor',[0 0 1]);
					end
				case {'r2','t2'}
					r = str2num(get(handles.r_point2,'String'));
					t = str2num(get(handles.t_point2,'String'));
					t = t*pi/180;
					[x,y] = pol2cart(t,r);
    				point=findobj(handles.pointplot,'Tag','Point2');
					set(handles.x_point2,'String',num2str(x));
					set(handles.y_point2,'String',num2str(y));
					h_arrow = findobj(gcf,'Tag','Arrow2');
					delete(h_arrow);
					h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
					set(h,'Tag','Arrow2');
					set(point,'Position',[x y]);
					switch contents{get(handles.plotmenu,'Value')};
						case {'Point 1', 'Point 2'}
						otherwise
							complexpofn('update','result');
							x = str2num(get(handles.x_result,'String'));
							y = str2num(get(handles.y_result,'String'));
							h_arrow = findobj(gcf,'Tag','Arrow3');
							delete(h_arrow);
							h = arrow('Start',[0 0],'Stop',[x y],'Length',10,...
								'Width',2);
							set(h,'Tag','Arrow3','FaceColor',[0 0 1],'EdgeColor',[0 0 1]);
					end
				case {'result'}
					xp(1) = str2num(get(handles.x_point1,'String'));
					yp(1) = str2num(get(handles.y_point1,'String'));
					xp(2) = str2num(get(handles.x_point2,'String'));
					yp(2) = str2num(get(handles.y_point2,'String'));

					switch contents{get(handles.plotmenu,'Value')};
						case 'Point 1 + Point 2'
							x = xp(1) + xp(2);
							y = yp(1) + yp(2);
						case 'Point 1 - Point 2'
							x = xp(1) - xp(2);
							y = yp(1) - yp(2);
						case 'Point 2 - Point 1'
							x = xp(2) - xp(1);
							y = yp(2) - yp(1);
					end
					set(handles.x_result,'String',num2str(x));
					set(handles.y_result,'String',num2str(y));
					[t,r] = cart2pol(x,y);
					t = 180*t/pi;
					set(handles.r_result,'String',num2str(r));
					set(handles.t_result,'String',num2str(t));
					point=findobj(handles.pointplot,'Tag','Point3');
					set(point,'Position',[x y]);
			end
		case 'plotmenu'
			reset(handles.pointplot);
			makePlot(handles);
			plotPoints(handles);
		case 'gridcart'
			if get(handles.grid_cart,'Value')
				set(handles.grid_polar,'Value',0);
			else
				set(handles.grid_polar,'Value',1);
			end
			makePlot(handles);
			plotPoints(handles);
		case 'gridpolar'
			if get(handles.grid_polar,'Value')
				set(handles.grid_cart,'Value',0);
			else
				set(handles.grid_cart,'Value',1);
			end
			makePlot(handles);
			plotPoints(handles);
		case 'selectpoint'
			handles = select_point(handles);
		case 'movepoint'
			move_point(handles);
			point = findobj(gcf,'Selected','on');
			switch get(point,'Tag')
				case 'Point1'
					xy = get(point,'Position');
					x = xy(1); y = xy(2);
					set(handles.x_point1,'String',num2str(x));
					set(handles.y_point1,'String',num2str(y));
					complexpofn('update','x1');
					h_arrow = findobj(gcf,'Tag','Arrow1');
					delete(h_arrow);
					h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
					set(h,'Tag','Arrow1');
				case 'Point2'
					xy = get(point,'Position');
					x = xy(1); y = xy(2);
					set(handles.x_point2,'String',num2str(x));
					set(handles.y_point2,'String',num2str(y));
					complexpofn('update','x2');
					h_arrow = findobj(gcf,'Tag','Arrow2');
					delete(h_arrow);
					h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
					set(h,'Tag','Arrow2');
			end

			contents = get(handles.plotmenu,'String');
			switch contents{get(handles.plotmenu,'Value')};
				case {'Point 1', 'Point 2'}
				otherwise
					x = str2num(get(handles.x_result,'String'));
					y = str2num(get(handles.y_result,'String'));
					complexpofn('update','result');
					h_arrow = findobj(gcf,'Tag','Arrow3');
                    delete(h_arrow);
                    h = arrow('Start',[0 0],'Stop',[x y],'Length',10,...
						'Width',2);
            		set(h,'Tag','Arrow3','FaceColor',[0 0 1],'EdgeColor',[0 0 1]);
			end
		case 'releaseobject'
			set(gcf,'WindowButtonMotionFcn','');
			set(gcf,'WindowButtonUpFcn','');
		case 'unselectall'
			findobj(gcf,'Selected','on');
			set(findobj(gcf,'Selected','on'),'Selected','off');
		case 'print'
			print_axes(handles.pointplot);
		case 'close'
			close_figure(f, figname(1:end-4));
			return;
	end
	set(f,'UserData',handles);


% --------------------------------------------------------------------
function makePlot(handles);
	axes(handles.pointplot);
	set(handles.pointplot, 'DrawMode','fast', 'NextPlot','replacechildren',...
		'Box','on', 'XLim', [-2.1 2.1], 'YLim', [-2.1 2.1], ...
		'ButtonDownFcn','complexpofn unselectall');

	if get(handles.grid_cart, 'Value')
		cla;
		axis on;
		line([-3 3],[0 0]);
		line([0 0],[-3 3]);
		grid on;
	else
		cla;
		% define a circle
		th = 0:pi/50:2*pi;
		xunit = [0.0:0.2381:1]'*cos(th);
		yunit = [0.0:0.2381:1]'*sin(th);
		rmax=2.1;
		tc = get(handles.pointplot,'xcolor');

		% now really force points on x/y axes to lie on them exactly
		%inds = 1:(length(th)-1)/4:length(th);
		%xunit(inds(2:2:4)) = zeros(2,1);
		%yunit(inds(1:2:5)) = zeros(3,1);

		patch('xdata',xunit(end,:).*rmax,'ydata',yunit(end,:).*rmax, ...
			'facecolor',get(gca,'color'),'ButtonDownFcn','complexpofn unselectall');
		hold on;
		for i=1:size(xunit,1),
			plot(yunit(i,:).*rmax, xunit(i,:).*rmax, 'k:')
		end

		line([-3 3],[0 0]);
		line([0 0],[-3 3]);
		axis off;
		%axis(rmax*[-1.1 1.1 -1.15 1.15]);
		%axis off;
	end
	axis image;
	set(handles.pointplot, 'XLim', [-2.1 2.1], 'YLim', [-2.1 2.1]);

	
function plotPoints(handles)
	contents = get(handles.plotmenu,'String');
	switch contents{get(handles.plotmenu,'Value')};
		case 'Point 1'
			x = str2num(get(handles.x_point1,'String'));
			y = str2num(get(handles.y_point1,'String'));
			p1 = text('Parent',handles.pointplot,'Position',[x y],...
				'String','o','ButtonDownFcn','complexpofn selectpoint',...
				'EraseMode','xor','Tag','Point1','HorizontalAlignment','center');
			h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
			set(h,'Tag','Arrow1');
		case 'Point 2'
			x = str2num(get(handles.x_point2,'String'));
			y = str2num(get(handles.y_point2,'String'));
			p1 = text('Parent',handles.pointplot,'Position',[x y],...
				'String','o','ButtonDownFcn','complexpofn selectpoint',...
				'EraseMode','xor','Tag','Point2','HorizontalAlignment','center');
			h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
			set(h,'Tag','Arrow2');
		otherwise
			x = str2num(get(handles.x_point1,'String'));
			y = str2num(get(handles.y_point1,'String'));
			p1 = text('Parent',handles.pointplot,'Position',[x y],...
				'String','o','ButtonDownFcn','complexpofn selectpoint',...
				'EraseMode','xor','Tag','Point1','HorizontalAlignment','center');
			h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
			set(h,'Tag','Arrow1');
			xp(1) = x;
			yp(1) = y;

			x = str2num(get(handles.x_point2,'String'));
			y = str2num(get(handles.y_point2,'String'));
			p1 = text('Parent',handles.pointplot,'Position',[x y],...
				'String','o','ButtonDownFcn','complexpofn selectpoint',...
				'EraseMode','xor','Tag','Point2','HorizontalAlignment','center');
			h = arrow('Start',[0 0],'Stop',[x y],'Length',10);
			set(h,'Tag','Arrow2');
			xp(2) = x;
			yp(2) = y;

			switch contents{get(handles.plotmenu,'Value')};
				case 'Point 1 + Point 2'
					x = xp(1) + xp(2);
					y = yp(1) + yp(2);
				case 'Point 1 - Point 2'
					x = xp(1) - xp(2);
					y = yp(1) - yp(2);
				case 'Point 2 - Point 1'
					x = xp(2) - xp(1);
					y = yp(2) - yp(1);
			end

			set(handles.x_result,'String',num2str(x));
			set(handles.y_result,'String',num2str(y));
			[t,r] = cart2pol(x,y);
			t = 180*t/pi;
			set(handles.r_result,'String',num2str(r));
			set(handles.t_result,'String',num2str(t));
			p1 = text('Parent',handles.pointplot,'Position',[x y],...
                'String','o','ButtonDownFcn','',...
                'EraseMode','xor','Tag','Point3','HorizontalAlignment','center');
            h = arrow('Start',[0 0],'Stop',[x y],'Length',10,'Width',2);
            set(h,'Tag','Arrow3','FaceColor',[0 0 1],'EdgeColor',[0 0 1]);
	end

% --------------------------------------------------------------------
function ud = updatePlots(ud)
    polepos=findobj(ud.pz_plot,'Tag','Pole');
    zeropos=findobj(ud.pz_plot,'Tag','Zero');
    len=max(length(polepos),length(zeropos));
    pp=zeros(1,len);  % pp=1e-10*rand(1,len);
    for p=1:length(polepos)
        ppos=get(polepos(p),'Position');
        pp(p)=ppos(1)+ppos(2)*i;
    end
    zz=zeros(1,len);  % zz=1e-10*rand(1,len);
    for z=1:length(zeropos)
        zp=get(zeropos(z),'Position');
        zz(z)=zp(1)+zp(2)*i;
    end
    ud.zz=zz;ud.pp=pp;
    [ud.num,ud.den]=zp2tf(zz',pp',1);
    [h,w]=freqz(ud.num,ud.den,ud.npoints,'whole');
	if get(ud.dB,'Value')
		% normalise so max = 0 dB
		logmag=20*log10(abs(h));
		mag=logmag-max(logmag);
	else
		mag = abs(h)./max(abs(h));
	end
    set(ud.freqline,'YData',mag);
    set(ud.phaseline,'YData',angle(h));
	imp = filter(ud.num, ud.den, [1, zeros(1,499)]);	
    set(ud.implot(1),'YData',imp(1:500));
	temp = get(ud.implot(2),'YData');
	temp(2:3:end) = imp(1:500);
    set(ud.implot(2),'YData',temp);
	set(ud.impulse_plot,'YLim',[1.1*min(imp) 1.1*max(imp)]);
	

function ud = select_point(ud)
	s=get(gcbo,'UserData');
	set(gcf,'WindowButtonMotionFcn', 'complexpofn movepoint');
	set(gcf,'WindowButtonUpFcn','complexpofn releaseobject');
	ud.currentcoef=gcbo;
	ud.partnercoef=s;
	set([findobj(gca,'Tag','Point1'), findobj(gca,'Tag','Point2')],'Selected','off');
	set([ud.currentcoef ud.partnercoef],'Selected','on');


function ud = move_point(ud)
	cp=get(ud.pointplot,'CurrentPoint');
	if (cp(1)*cp(1)+cp(3)*cp(3)) < 16.0
	  set(ud.currentcoef,'Position',[cp(1) cp(3)]);
	end