## Copyright (C) 2004 Doug Stewart ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA % %function bode1w (n,d) %n= numerator %d=denominator % % This was written to help my students with Gain Margin and Phase Margin %GM and PM lines are added to the Bode plot and %GM and PM values are printed on the screen. ## bode1w ## Author: doug ## ## 2004-09-27 doug ## * Initial revision ## Rev #1 Nov 29 2004 Doug Stewart ## Rev #2 Sept 2005 Doug Stewart ## - Added the If for negative phase steps. ## - and the DO loop ## Rev #3 Sept 2005 Doug Stewart ## - used unwrap.m function bode1w (n,d); sys=tf2sys(n,d); [mag,ph,w]=bode(sys); # fix the phase wrap arownd phw=unwrap(ph*pi/180); ph=phw*180/pi; %do ll=length(ph); % [qm iqm]=max((diff(ph))); % if(qm>300) % ph(iqm+1:ll)=ph(iqm+1:ll)-360; % endif % [qm iqm]=min((diff(ph))); % if(qm<-300) % ph(iqm+1:ll)=ph(iqm+1:ll)+360; % endif %until (abs(qm)<300) # make a 180 deg line v=ones(ll,1)*(-180); db=20*log10(mag); # find the Gain Margin and its location [x ix]=min(abs(ph+180)); if (x>1) GM="INF" ix=length(ph); else Gm=20*log10(mag(ix)) endif # find the Phase Margin and its location [pmx ipmx]=min(abs(mag-1)); PM=180+ph(ipmx) #f=w/(2*pi); semilogx(w,db,1,0,w,ph,w,v,[w(ix) w(ix)],[30 -180] ,[w(ipmx) w(ipmx)],[0 -200]) #gset grid mxtics xtics ytics #replot endfunction