home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 2.ddi / MUTOOLS1.DI$ / SIGGEN.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  3.5 KB  |  125 lines

  1. % function y = siggen('function(t)',t)
  2. %
  3. %   SIGGEN creates VARYING matrices as a function
  4. %   of time.  If 'function()' is in terms of an argument,
  5. %   that argument must be 't'.
  6. %
  7. %   Examples:
  8. %
  9. %       y = siggen('sin(t)',[0:pi/100:2*pi]);
  10. %       y = siggen('rand(2,2)',[0:20]);
  11. %
  12. %   See also: COS_TR, SIN_TR, and STEP_TR.
  13.  
  14. function y = siggen(function,t)
  15.  
  16. if nargin ~= 2,
  17.     disp('usage: y = siggen(function(t),t)')
  18.     return
  19.     end
  20.  
  21. if ~isstr(function),
  22.     error('first argument must be a string')
  23.     return
  24.     end
  25.  
  26. %       t is extracted as a column vector.    
  27.  
  28. [ttype,nr,nc,npts] = minfo(t);
  29. if ttype == 'vary',
  30.     [tdat,tptr,tt] = vunpck(t);
  31.     t = tt;
  32. elseif ttype == 'cons',
  33.     if nr == 1,
  34.         npts = nc;
  35.         t = t.';
  36.     elseif nc == 1,
  37.         npts = nr;
  38.     else
  39.         error('second argument does specify a vector')
  40.         return
  41.         end
  42. else
  43.     error('second argument must be VARYING or CONSTANT')
  44.     return
  45.     end
  46.  
  47. %       perform a single evaluation of the function.
  48. %       If there is more than one time point and the eval
  49. %       didn't generate a matrix of the right size it can
  50. %       be detected by repeating the eval with two elements
  51. %       of t.  In this case loop over t to generate ydat.
  52. %       Two elements are used because some MATLAB functions
  53. %       behave differently when given a scalar (eg: rand,
  54. %       ones, zeros).  This means that the npts = 2 case
  55. %       has to be done separately.
  56.  
  57. %       If a loop is not necessary, yet the number
  58. %       of rows of the output varying matrix is more than
  59. %       one, the eval() has created the data in the wrong
  60. %       order.  It must be rearranged.
  61.  
  62. eval(['ydat = ' function ';']);
  63. if npts > 2,
  64.     [nr,nc] = size(ydat);
  65.     nrr = round(nr/npts);
  66.     timebase = t;
  67.     t = timebase(1:2);
  68.     eval(['ltest = ' function ';']);
  69.     t = timebase;
  70.     [nrltest,ncltest] = size(ltest);
  71.     if nrltest == nr,
  72.         nrr = nr;
  73.         y = zeros(nrr*npts+1,ncltest+1);
  74.         y(1:nrr,1:nc) = ydat;
  75.         for i = 2:npts,
  76.             eval(['nxtev = ' function ';']);
  77.             y((i-1)*nrr+1:i*nrr,1:nc) = nxtev;
  78.             end
  79.     elseif  nrr > 1,
  80.         y = zeros(nr+1,nc+1);
  81.         ydat = reshape(ydat,npts*nrr*nc,1);
  82.         index = ([0:nrr-1]'*npts+1)*ones(1,npts) + ones(nrr,1)*[0:npts-1];
  83.         index = reshape(index,nrr*npts,1); 
  84.         for i = 1:nc,
  85.             y(1:nr,i) = ydat(index + (i-1)*nrr*npts);
  86.             end
  87.     else
  88.         y(1:nr,1:nc) = ydat;
  89.         end
  90.     y(1:npts,nc+1) = timebase;
  91.     y(nrr*npts+1,nc+1) = inf;
  92.     y(nrr*npts+1,nc) = npts;
  93. elseif npts == 2,
  94.     [nr,nc] = size(ydat);
  95.     nrr = round(nr/npts);
  96.     if nrr*npts ~= nr,
  97.         eval(['ydat = [ydat; ' function '];']);
  98.     elseif  nrr > 1,
  99.         y = zeros(nr+1,nc+1);
  100.         ydat = reshape(ydat,npts*nrr*nc,1);
  101.         index = ([0:nrr-1]'*npts+1)*ones(1,npts) + ones(nrr,1)*[0:npts-1];
  102.         index = reshape(index,nrr*npts,1);
  103.         for i = 1:nc,
  104.             y(1:nr,i) = ydat(index + (i-1)*nrr*npts);
  105.             end
  106.         end
  107.     y(1:npts,nc+1) = timebase;
  108.     y(nrr*npts+1,nc+1) = inf;
  109.     y(nrr*npts+1,nc) = npts;
  110. else
  111.     [nr,nc] = size(ydat);
  112.     y = zeros(nr+1,nc+1);
  113.     y(1:nr,1:nc) = ydat;
  114.     y(1:npts,nc+1) = timebase;
  115.     y(nr+1,nc+1) = inf;
  116.     y(nr+1,nc) = npts;
  117.     end
  118.  
  119.  
  120. %------------------------------------------------------------------
  121.  
  122.  
  123. %
  124. % Copyright MUSYN INC 1991,  All Rights Reserved
  125.