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

  1. //------------------------------------------------------------------------
  2. //
  3. // stairs
  4. //
  5. // Syntax: A=stairs(x,y)
  6. //
  7. // This routine draws a stairstep graph of the elements in vector y
  8. // at the locations specified in vector x. A stiarstep graph is similar
  9. // to a bar graph, but the vertical lines dropping to the x-axis are
  10. // omitted. Stairstep plots are useful for drawing time history plots
  11. // of digital sampled-data systems.
  12. //
  13. // The routine can also be called as stairs(y), which draws a stairstep
  14. // graph of the elements of vector y.
  15. //
  16. // Note: If the routine is called as stairs(x,y), then the values in x
  17. //       must be evenly spaced in ascending order.
  18. //
  19. // Copyright (C), by Jeffrey B. Layton, 1994
  20. // Version JBL 940906
  21. //------------------------------------------------------------------------
  22.  
  23. stairs = function(x,y)
  24. {
  25.    local(xp,yp,i,nargs,n,ip)
  26.  
  27. // Count number of inputs
  28.    nargs=0;
  29.    if (exist(x)) {nargs=nargs+1;}
  30.    if (exist(y)) {nargs=nargs+1;}
  31.  
  32. // Check dimensions
  33.    if (nargs == 1) {
  34.        n=length(x);
  35.    else
  36.        n=length(x);
  37.        if (length(y) != n) {
  38.            error("STAIRS:  Length(y) != Length(x)");
  39.        }
  40.    }
  41.    ip=(n*2)-1;
  42.    xp=zeros(ip,1);
  43.    yp=zeros(ip,1);
  44.  
  45. // Convert vectors x and y into arrays for plotting
  46.    if (nargs == 2) {
  47.        for (i in 1:n) {
  48.             ip=(i-1)*2 + 1;
  49.             xp[ip]=x[i];
  50.             if (i != n) {
  51.                 xp[ip+1]=x[i+1];
  52.             }
  53.             yp[ip]=y[i];
  54.             if (i != n) {
  55.                 yp[ip+1]=y[i];
  56.             }
  57.        }
  58.    else
  59.        for (i in 1:n) {
  60.             ip=(i-1)*2 + 1;
  61.             xp[ip]=i;
  62.             if (i != n) {
  63.                 xp[ip+1]=i+1;
  64.             }
  65.             yp[ip]=x[i];
  66.             if (i != n) {
  67.                 yp[ip+1]=x[i];
  68.             }
  69.        }
  70.    }
  71.  
  72. // Make the plot
  73.    plot([xp,yp]);
  74.  
  75.    return << xp=xp; yp=yp >>
  76. };
  77.