% function [dsysl,dsysr] = musynfiw(pre_dsysl,Ddata,sens,blk,nmeas,ncntrl,wt)
%
%   Fits the magnitude curve obtained by multiplying the old D 
%   frequency response (from PRE_DSYSL) with the DDATA data. 
%   Returns stable, minimum phase system matrices DSYSL and 
%   DSYSR, which can be absorbed into the original interconnection 
%   structure with multiplications (using MMULT), and one inverse 
%   (using MINV). Once absorbed, a H_infinity design can be 
%   performed, completing another iteration of MU-SYNTHESIS.
%
%   For the first MU-SYNTHESIS iteration, the variable
%   PRE_DSYSL should be set to the string  'first'
%   In subsequent iterations, PRE_DSYSL should be the previous
%   (left) rational D-scaling system matrix, DSYSL.
%
%   See also: FITMAG, FITSYS, MAGFIT, and MUFTBTCH and MUSYNFLP.

%
% Wes Wang changed the format of fitmag.m for demo. 4/5/92
%
function [dsysl,dsysr] = musynfiw(osysl,magdata,sens,blk,nmeas,ncntrl,wt)
if nargin < 6
 disp('usage: [dsysl,dsysr] = musynfiw(pre_dsysl,Ddata,sens,blk,nmeas,ncntrl)')
 return
end
%
[nblk,x] = size(blk);
leftdim = nmeas;
 for i=1:nblk
  if blk(i,2) == 0
    leftdim = leftdim + blk(i,1);
    if blk(i,1) > 1
      disp('full Ds not implemented yet')
      return
    end
  else
    leftdim = leftdim + blk(i,2);
  end
 end
%
 [mtype,mrows,mcols,mnum] = minfo(magdata);
 omega = magdata(1:mnum,mcols+1);
 [stype,srows,scols,snum] = minfo(sens);
 if mnum ~= snum
   error('SENS & MAG data have diff # of points - rerun VMU')
   return
 end
 if nargin == 7
   [wtype,wrows,wcols,wnum] = minfo(wt);
   if wnum ~= mnum
    error('WT & MAG data have diff # of points - rerun VMU')
    return
   end
   supwt = wt(1:mnum,1);
 else
   supwt = ones(mnum,1);
 end
%
%  NORMALIZE THE DATA BY THE LAST DSCALE
%
 magdata(1:mnum,1:mcols-1) = ...
    inv(diag(magdata(1:mnum,mcols)))*magdata(1:mnum,1:mcols-1);
%
% MODIFICATION START %%%%%%%%%%%%%%
 if isstr(osysl)
   if strcmp(osysl,'first');
     osysl_g = eye(leftdim);
   else
     error('First argument should be the string   first   or a SYSTEM matrix')
     return  
   end       
 else        
   [dum1,dum2,dum3,dum4] = minfo(osysl);
   if strcmp(dum1,'syst')
     osysl_g = frsp(osysl,omega);
   elseif strcmp(dum1,'cons')
     osysl_g = osysl;
   else      
     error('First argument should be the string   first   or a SYSTEM matrix')
     return  
   end       
 end         
% MODIFICATION END %%%%%%%%%%%%%%
 loc = 1;
 dsysl = [];
 dsysr = [];
 for i=1:nblk-1
   wt = vpck((abs(sens(1:mnum,i) .* supwt)),omega);
   osysl_g_loc = sel(osysl_g,loc,loc);
   newdata = vabs(mmult(osysl_g_loc,sel(magdata,1,i)));
   heading = ['FITTING D SCALING #' int2str(i)];
   scalard = fitmagw(newdata,wt,heading,osysl_g_loc,3);
   tmpsys = [];
   for j=1:min([blk(i,1) blk(i,2)])
     tmpsys = daug(tmpsys,scalard);
   end
   dsysl = daug(dsysl,tmpsys);
   dsysr = daug(dsysr,tmpsys);
   if blk(i,1) < blk(i,2)
     for j=1:(blk(i,2)-blk(i,1))
       dsysl = daug(dsysl,scalard);
     end
   elseif blk(i,2) < blk(i,1)
     for j=1:(blk(i,1)-blk(i,2))
       dsysr = daug(dsysr,scalard);
     end
   end
   loc = loc + blk(i,2);
 end
 dsysl = daug(dsysl,eye(blk(nblk,2)+nmeas));
 dsysr = daug(dsysr,eye(blk(nblk,1)+ncntrl));
%	Copyright (c) 1990-94 by The MathWorks, Inc.
%
