home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / languages / rlab1_23a / CTB / tf2ss < prev    next >
Text File  |  1995-11-14  |  5KB  |  198 lines

  1. //----------------------------------------------------------------------------
  2. //
  3. // tf2ss
  4. //
  5. // Syntax: A=tf2ss(num,den,meth)
  6. //
  7. // This routine calculates the state-space representation:
  8. //     .
  9. //     x = Ax + Bu
  10. //     y = Cx + Du
  11. //
  12. // from the system:
  13. //
  14. //             NUM(s) 
  15. //     H(s) = --------
  16. //             DEN(s)
  17. //
  18. // for a single input.  Vector den must contain the coefficients of
  19. // the denominator in descending powers of s.  Matrix num must 
  20. // contain the numerator coefficients with as many rows as there are
  21. // outputs y.
  22. //
  23. // There are 3 choices to perform the conversion:
  24. //
  25. //     meth = 1: Returns A,B,C,D in Phase-Variable Form (default)
  26. //     meth = 2: Returns A,B,C,D in Controller Canonical Form
  27. //               (MATLAB uses this method)
  28. //     meth = 3: Returns A,B,C,D in Rectangular Form
  29. //
  30. // This calculation also works for discrete systems. To avoid confusion
  31. // when using this function with discrete systems, always use a numerator
  32. // polynomial that has been padded with zeros to make it the same length
  33. // as the denominator.
  34. //
  35. // The state-space matrices are returned in a list. For example:
  36. //
  37. //      A=tf2ss(num,den,meth);
  38. //
  39. //      A.a = a matrix
  40. //      A.b = b matrix
  41. //      A.c = c matrix
  42. //      A.d = d matrix
  43. //
  44. // Ref: (1) Skelton, R. "Dynamic Systems Control Linear System Analysis and
  45. //          Synthesis," John Wiley and Sons, 1988.
  46. //      (2) Kailath, "Linear Systems," Prentice-Hall, 1980
  47. //
  48. // Copyright (C), by Jeffrey B. Layton, 1994
  49. // Version JBL 940106
  50. //----------------------------------------------------------------------------
  51.  
  52. tf2ss = function(num,den,meth)
  53. {
  54.    local(mnum,nnum,nden,con,inz,denl,numl,a,b,c,d,meth,lower,upper)
  55.  
  56.    mnum=num.nr;
  57.    nnum=num.nc;
  58.    nden=den.nc;
  59.  
  60. // Check for meth
  61.    if (! exist(meth)) {meth = 1;}
  62.  
  63. // Define local version of den and num
  64.    denl=den;
  65.    numl=num;
  66.  
  67. // Check for null systems
  68.    if ((nden == 0) && (nnum == 0)) {
  69.         a=[];
  70.         b=[];
  71.         c=[];
  72.         d=[];
  73.         return << a=a; b=b; c=c; d=d >>
  74.    }
  75.  
  76. // Strip leading zeros from denominator
  77.    inz=find(den != 0);
  78.    denl=denl[inz[1]:nden];
  79.    nden=den.nc;
  80.  
  81. // Check for proper numerator
  82.    if (nnum > nden) {
  83. // Try to strip leading zeros to make proper
  84.        if (all(all(numl[;1:(nnum-1)] == 0))) {
  85.            numl=numl[;(nnum-nden+1):nnum];
  86.            mnum=numl.nr;
  87.            nnum=numl.nc;
  88.        else
  89.            error("TF2SS: Denominator must be higher or equal order than numerator.");
  90.        }
  91.    }
  92.  
  93. // Pad numerator with leading zeros, to make it have the same number of
  94. // columns as the denominator, and normalize it to den[1]
  95.    numl=[zeros(mnum,nden-nnum),numl]./denl[1];
  96.    nnum=numl.nc;
  97.  
  98. //
  99. // ===================
  100. // Phase-Variable Form
  101. // ===================
  102. //
  103.    if (meth == 1) {
  104.  
  105. // Now do the rest, starting by normalizing den to den[1],
  106.        denl=denl[2:nden]./denl[1];
  107.  
  108. // Define constant term for Phase-Variable Form
  109.        con=numl[;1];
  110.  
  111. // Switch Order of Coefficients in num and den
  112.        denl=denl[nden-1:1:-1];
  113.        numl=numl[numl.nc:1:-1];
  114.  
  115. // Create the State-Space Matrices
  116.        a=[zeros(nden-2,1), eye(nden-2,nden-2)];
  117.        a=[a;-denl];
  118.        b=[zeros(nden-2,1);1];
  119.        numl
  120.        nnum
  121.        con*denl
  122.        c=numl[1:nnum-1]-con*denl;
  123.        d=con;
  124.  
  125.        return << a=a; b=b; c=c; d=d >>
  126.  
  127. //
  128. // =============================================
  129. // Controller Canonical Form (MATLAB compatible)
  130. // =============================================
  131. //
  132.    else if (meth == 2) {
  133.  
  134. // Do the D-matrix first
  135.        if (length(numl)) {
  136.            d=numl[;1];
  137.        else
  138.            d=[];
  139.        }
  140.  
  141. // Handle special constant case:
  142.    if (nden == 1) {
  143.        a=[];
  144.        b=[];
  145.        c=[];
  146.        return << a=a; b=b; c=c; d=d >>
  147.    }
  148.  
  149. // Now do the rest, starting by normalizing den to den[1],
  150.        denl=denl[2:nden]./denl[1];
  151.        a=[-denl;eye(nden-2,nden-1)];
  152.        b=eye(nden-1,1);
  153.  
  154.        if (mnum > 0) {
  155.            c=numl[;2:nden]-numl[;1]*denl;
  156.        else
  157.            c=[];
  158.        }
  159.  
  160.        return << a=a; b=b; c=c; d=d >>
  161.  
  162. //
  163. // ================
  164. // Rectangular Form
  165. // ================
  166. //
  167.    else if (meth == 3) {
  168.  
  169. // First define the constant con[1]
  170.        con=numl[1];
  171.  
  172.        numl=numl[2:nnum]';
  173.        denl=denl[2:nden]';
  174.        numl
  175.        denl
  176.  
  177. // Compute a,b,c,d
  178.        b=numl-con*denl;
  179.        b
  180.        c=zeros(1,nnum-1);
  181.        c[1]=1;
  182.        c
  183.  
  184.        d=con;
  185.        d
  186.  
  187.        if (nnum == 2) {
  188.            a=[-numl];
  189.        else
  190.            a=[eye(nnum-2,nnum-2);
  191.               zeros(1,nnum-2)];
  192.            a=[-numl,a];
  193.        }
  194.  
  195.        return << a=a; b=b; c=c; d=d >>
  196.     }}}
  197. };
  198.