home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 7.ddi / ROBUST.DI$ / FITD.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  5.4 KB  |  193 lines

  1. function [aw,bw,cw,dw,magfit] = fitd(threl, freq, n, blksz,flag)
  2. % [SS_D,LOGDFIT] = FITD(LOGD,W,N,BLKSZ,FLAG)
  3. % [AD,BD,CD,DD,LOGDFIT] = FITD(LOGD,W,N,BLKSZ,FLAG) produces a stable,
  4. %   minimum-phase state-space realization SS_D of a diagonal transfer
  5. %   function matrix such that the diagonal elements' magnitude bode plots 
  6. %   approximately fit Bode magnitude plot data given in the rows of the
  7. %   matrix LOGD.  Uses the the numerically stable routine "YULEWALK.M".
  8. %
  9. %   Input:     LOGD  matrix whose rows are logs of magnitude bode plots;
  10. %              W frequency vector.
  11. %   Optional:  N vector of desired orders of the approximants (default=0);
  12. %              BLKSZ vector of sizes diagonal blocks in SS_D (default = 1);
  13. %              FLAG (default: display Bode plot; 0: no Bode plot);
  14. %              Bode plot of the fit will be displayed 4 at a time
  15. %              with a "pause" in between every full screen display.
  16. %   Output:    SS_D is a state-space realization of the diagonal transfer
  17. %              function matrix  D(s) =diag(d1(s)I1,...,dn(s)In) where
  18. %              di(s) is the fit to the i-th row of LOGD and I1,...,In are
  19. %              identity matrices of dimensions given in the n-vector BLKSZ.
  20. %              LOGDFIT contains the fit to LOGD, i.e., SS_D's bode plot.
  21.  
  22. % by Weizheng Wang (University of Southern California)
  23. % with enhancements by
  24. % R. Y. Chiang & M. G. Safonov 
  25. % Copyright (c) 1988 by the MathWorks, Inc.
  26. % All Rights Reserved.
  27. % ------------------------------------------------------------------------
  28.  
  29. [mmm,nnn] = size(threl);
  30. if mmm > nnn
  31.    threl = threl';
  32.    [mmm,nnn] = size(threl);
  33. end
  34. thre=exp(threl);  
  35. threl=threl/log(10);     % from log to log10
  36. freql = log10(freq);
  37.  
  38. bodeflag=0;
  39. magfit=[];
  40. if nargout==2 | nargout==5, bodeflag == 1; end
  41.  
  42. nfreq=max(size(freq));
  43. if nnn ~= nfreq,
  44.    error('MAG and W must have the same number of points')
  45. end
  46.  
  47. if nargin<3, n=zeros(1,mmm); end
  48. if max(size(n))==1, n=n*ones(1,mmm); end
  49.  
  50. if nargin <5
  51.    bodeflag=1;% compute bode plot of fit
  52.    flag = 1;  % display bode plot of fit
  53.    if nargin<4,blksz=ones(1,mmm);end
  54. end
  55. if max(size(n))==0, n=zeros(1,mmm); end
  56. if max(size(blksz))==0, blksz=ones(1,mmm); end
  57. if max(size(flag))==0,
  58.    bodeflag=1;% compute bode plot of fit
  59.    flag = 1;  % display bode plot of fit
  60. end
  61.  
  62.  
  63. % if scalar blksz, make it a vector
  64. if max(size(blksz))==1, blksz=blksz*ones(1,mmm);end
  65.  
  66. % Find the best sampling time "ff so that the frequency mapping from "s"
  67. % to "z" is approximately linear between 0 and Pi:
  68. %
  69. mx=max(freql);  mn=min(freql);  
  70. ff=10^(mn+2);
  71. ff=ff/3^(4-mx+mn); % ff is approx. sqrt(max(freq)/min(freq));
  72.  
  73. %
  74. % Bilinear mapping of the frequency axis from "z" to "s":
  75. %
  76. z=[0:126]/126*pi;
  77. j=sqrt(-1);
  78. w=imag(ff*(exp(j*z)-1)./(exp(j*z)+1));
  79. wl(1)=log10(freq(1));
  80. [x,y]=size(freq);
  81. x=max(x,y);
  82. wl(127)=log10(freq(x));
  83. wl(2:126)=log10(w(2:126));
  84. for i=2:126, if wl(i)>wl(127), wl(i)=wl(127); end; end;
  85. for i=2:126, if wl(i)<wl(1),  wl(i)=wl(1); end; end;
  86. z=z/pi;
  87.  
  88.  
  89. % Curve fit the continuous THREL magnitude data, one row at a time:
  90. %
  91. % Now prepare to curve fit THREL magnitude data, one row at a time:
  92.  
  93. loop = mmm;
  94.  
  95. if loop ~= max(size(n)),
  96.    error('Number of rows of MAG must equal the dimension of the vector N')
  97. end
  98. count = 0;
  99.  
  100. for i = 1:loop
  101.   mag =thre(i,:);
  102.   magl = threl(i,:);
  103.   nl = n(1,i);
  104.   count = count + 1;
  105. %      
  106.  
  107.   if nl==0
  108.     ad=1;
  109.     bd = exp(log(10)*polyfit(freql, magl, 0));
  110.   else
  111.   
  112.   % First do a high order least squares polynomial curve fit to the
  113.   % loglog bode magnitude plot data in ith row of THREL:
  114.     y = nl*2;
  115.     if y < 8, y = 8; end
  116.     if y>nfreq/2, y=round(nfreq/2); end
  117.     if y<1, y=1; end; if nfreq>100, y=10; end;
  118.     p = polyfit(freql, magl, y);
  119.  
  120.   % 
  121.   % Now obtain a lower order approximation in z-plane via YULEWALK.M:
  122.   %
  123.     dth = polyval(p, wl);
  124.     dth = exp(log(10)*dth);
  125.     dth(1)=mag(1);
  126.     dth(127)=mag(x);    
  127.     [bd,ad]=yulewalk(nl,z,dth);
  128.     % make sure the fitting is minimum phase
  129.     bd = polystab(bd);
  130.   end
  131.  
  132. % Discrete time state-space (az,bz,cz,dz):
  133. %
  134.   [az,bz,cz,dz]=tf2ss(bd,ad);
  135. %
  136. % Convert the discrete state-space to W-plane:
  137. %
  138.   if nl>0,
  139.     [aw0,bw0,cw0,dw0]=bilin(az,bz,cz,dz,-1,'Tustin',2/ff);
  140.   else
  141.     aw0=[];bw0=[];cw0=[];dw0=dz;
  142.   end
  143.   for j=1:blksz(i);
  144.      if i == 1 & j==1
  145.         aw = aw0; bw = bw0; cw = cw0; dw = dw0;
  146.      else
  147.         [aw,bw,cw,dw] = append(aw,bw,cw,dw,aw0,bw0,cw0,dw0);
  148.      end
  149.   end
  150.  
  151. %
  152. % Compute and display (if flag==1) the frequency response and fit
  153. %
  154.   if bodeflag==1,
  155.      if nl==0,
  156.          aw0=-1;bw0=0; cw0=0;
  157.      end
  158.      [ga_w0,ph0] = bode(aw0,bw0,cw0,dw0,1,freq);
  159.      magfit=[magfit; log(ga_w0')];
  160.      if flag==1,
  161.         if loop > 1
  162.            if count==1, clg; subplot(221); end;
  163.        if count==2, subplot(222); end;
  164.            if count==3, subplot(223); end;
  165.            if count==4, subplot(224); end;
  166.         end;
  167.         loglog(freq,mag,'x',freq,ga_w0);
  168.         title([ 'YULE-WALKER ORDER ' num2str(n(i)) ' FIT']);
  169.         xlabel('R/S (x: data; solid: fit)')
  170.         ylabel(['Mag D' num2str(i)]);
  171.      end
  172.   end
  173.   if count == 4     % reset the counter after four loops
  174.      count = 0;
  175.      pause          % pause the 4-window plot on the screen
  176.      clg
  177.   end
  178. %
  179. end  % of the loop
  180. %
  181. if nargout <3,
  182.     ss_w = mksys(aw,bw,cw,dw);
  183.     aw = ss_w;
  184.     bw=magfit;
  185. end
  186. %
  187. % -------- End of FITD.M % WW/RYC/MGS %
  188.  
  189.  
  190.  
  191.  
  192.