www.gusucode.com > 利用MATLAB GUI设计滤波器界面,可以设计IIR滤波器 > AFD/GuiListTransferFunction.m
function varargout = GuiListTransferFunction(varargin) % GUILISTTRANSFERFUNCTION M-file for guiListTransferFunction.fig % GUILISTTRANSFERFUNCTION, by itself, creates a new GUILISTTRANSFERFUNCTION or raises the existing % singleton*. % % H = GUILISTTRANSFERFUNCTION returns the handle to a new GUILISTTRANSFERFUNCTION or the handle to % the existing singleton*. % % GUILISTTRANSFERFUNCTION('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUILISTTRANSFERFUNCTION.M with the given input arguments. % % GUILISTTRANSFERFUNCTION('Property','Value',...) creates a new GUILISTTRANSFERFUNCTION or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before guiListTransferFunction_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to guiListTransferFunction_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help guiListTransferFunction % Last Modified by GUIDE v2.5 27-Dec-2003 12:04:25 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @GuiListTransferFunction_OpeningFcn, ... 'gui_OutputFcn', @GuiListTransferFunction_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin & isstr(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before guiListTransferFunction is made visible. function GuiListTransferFunction_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to guiListTransferFunction (see VARARGIN) global strFilterObject if isempty(strFilterObject) % debug t = load('matlab.mat'); strFilterObject = t.strFilterObject; disp('Debug mode') end strFilterObject = Utility_zpk(strFilterObject); % create the numerator polynomial string figure(hObject) %so if called a second time from main entry screen while this window preexists will write text to this window, not main screen top = 45; %pixels from bottom of uiax to bottom of numerator text bot = 10; %pixels from bottom of uiax to bottom of denomenator text Font.FontUnits='points'; Font.FontSize=12; Font.FontName='Arial'; hax = handles.uiax; pixFigSize = get(hax,'position'); sNum=PolyString(strFilterObject.vZeros,'a'); hTextNum=text(pixFigSize(3)/2,top,sNum,Font,'Units','pixels','HorizontalAlignment','center'); handles.hTextNum = hTextNum; % create the denomenator polynomial string sDen=PolyString(strFilterObject.vPoles,'b'); hTextDen=text(pixFigSize(3)/2,bot,sDen,Font,'Units','pixels','HorizontalAlignment','center'); handles.hTextDen = hTextDen; % move the dividing line FixLineWidth(handles) % add the text PolyText(handles) % and fix the status bar and title bar sStatus=strFilterObject.sTitle; set(handles.uitxStatus,'string','Close the window to return to the main screen') set(handles.uiListTransferFunction,'Name',sStatus) % Choose default command line output for guiListTransferFunction handles.output = hObject; guidata(hObject, handles); % --- Outputs from this function are returned to the command line. function varargout = GuiListTransferFunction_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in uirbPolynomial. function uirbPolynomial_Callback(hObject, eventdata, handles) global strFilterObject set(handles.uirbPolynomial, 'Value', 1); set(handles.uirbPoleZero, 'Value', 0); set(handles.hTextNum,'string',PolyString(strFilterObject.vZeros*strFilterObject.fK,'a')) set(handles.hTextDen,'string',PolyString(strFilterObject.vPoles,'b')) FixLineWidth(handles) PolyText(handles) % --- Executes on button press in uirbPoleZero. function uirbPoleZero_Callback(hObject, eventdata, handles) global strFilterObject set(handles.uirbPoleZero, 'Value', 1); set(handles.uirbPolynomial, 'Value', 0); set(handles.hTextNum,'string',RootString(strFilterObject.vZeros*strFilterObject.fK,'z')) set(handles.hTextDen,'string',RootString(strFilterObject.vPoles,'p')) FixLineWidth(handles) RootText(handles) %%%%%%%%%%%%%%%%%%%%%%%% Helper Functions %%%%%%%%%%%%%%%%%%%%%%%%%%% % Returns a string in polynomial form function sOut = PolyString(vRoots, sChar) len=length(vRoots)+1; sOut=['']; for i=len:-1:3 sOut = sprintf('%s + %s_%g s^%g',sOut,sChar,i-1,i-1); end if len>=2 sOut = sprintf('%s + %s_1 s',sOut,sChar); end sOut = [sOut ' + ' sChar '_0']; sOut = sOut(4:end); % Returns a string in factored form function sOut = RootString(vRoots, sChar) sOut=''; if sChar=='z', sOut = 'k'; end for i=1:length(vRoots) sOut = sprintf('%s (s-%s_%g)',sOut,sChar,i); end % Fixes the length of the divider line function FixLineWidth(handles) hTextDen = handles.hTextDen; uiLine = handles.uiLine; pixWidth = get(hTextDen,'Extent'); pixWidth = pixWidth(3); pixPos = get(uiLine,'position'); pixMidPoint = pixPos(1)+pixPos(3)/2; pixPos(3) = pixWidth; pixPos(1) = pixMidPoint - pixWidth/2; set(uiLine,'position',pixPos) % Fills out the variable list in polynomial form function PolyText(handles) global strFilterObject % fix k set(handles.uiK,'visible','off'); % show numerator vNum = poly(strFilterObject.vZeros)*strFilterObject.fK; len = length(vNum); for i=1:len h=findobj('Tag',['uia' num2str(i-1)]); set(h,'visible','on') set(h,'string',sprintf('a%g = %g',len-i,vNum(i))) end for i=length(vNum)+1:9 h=findobj('Tag',['uia' num2str(i-1)]); set(h,'visible','off') end % show denomenator vDen = poly(strFilterObject.vPoles); len = length(vDen); for i=1:len h=findobj('Tag',['uib' num2str(i-1)]); set(h,'visible','on') set(h,'string',sprintf('b%g = %g',len-i,vDen(i))) end for i=length(vDen)+1:9 h=findobj('Tag',['uib' num2str(i-1)]); set(h,'visible','off') end % Fills out the variable list in root factored form function RootText(handles) global strFilterObject % show k set(handles.uiK,'visible','on') set(handles.uiK,'string',sprintf('k = %g',strFilterObject.fK)) % show zeros vZeros = strFilterObject.vZeros; for i=1:length(vZeros) h=findobj('Tag',['uia' num2str(i-1)]); set(h,'visible','on') fZero = vZeros(i); if isreal(fZero) set(h,'string',sprintf('z%g = %g',i,fZero)) elseif imag(fZero)>0 set(h,'string',sprintf('z%g = %g + j%g',i,real(fZero), imag(fZero))) else set(h,'string',sprintf('z%g = %g - j%g',i,real(fZero), -imag(fZero))) end end for i=length(vZeros)+1:9 h=findobj('Tag',['uia' num2str(i-1)]); set(h,'visible','off') end % show poles vPoles = strFilterObject.vPoles; for i=1:length(vPoles) h=findobj('Tag',['uib' num2str(i-1)]); set(h,'visible','on') fPole = vPoles(i); if isreal(fPole) set(h,'string',sprintf('p%g = %g',i,fPole)) elseif imag(fPole)>0 set(h,'string',sprintf('p%g = %g + j%g',i,real(fPole), imag(fPole))) else set(h,'string',sprintf('p%g = %g - j%g',i,real(fPole), -imag(fPole))) end end for i=length(vPoles)+1:9 h=findobj('Tag',['uib' num2str(i-1)]); set(h,'visible','off') end