#!../src/tops  -i -s ../sys  -u ../usr
/* File test/make_sweep  March 2007

Using the infix parser.
Making Nastran TABLED1 cards of function A(t), a sine sweep.
Shows setting the field format in make_tabled1().

To run this file interactively, use word psource to parse this infix
file and then source and run the resulting postfix words:

   [dale@kaffia] /opt/tops/tops/test > tops
            Tops 3.2.0
   Sat Oct 27 10:22:05 PDT 2012
   [tops@kaffia] ready > 'make_sweep' psource

Updates:
   Sat Oct 27 10:26:01 PDT 2012.  Show sweep frequency versus time when
      running interactively.

----------------------------------------------------------------------*/

   if(!X11) exit(<< "make_sweep: require X11 graphics" . nl >>);

   catmsg(no);
   if(missing("make_tabled1")) source("nas.v");
   if(missing("sine_sweep")) source("signal.v");
   if(missing("plot")) source("plot.v");

/*--------------------------------------------------------------------*/

   f1 = 2;  // starting frequency
   f2 = 40; // ending frequency
   oct = 2; // sweep rate, octaves per minute

/* Function sine_sweep() makes a unit amplitude sine sweep transient;
   the amplitude of returned A(t) is one, such as one in/sec^2: */
   (A, t) = sine_sweep(f1, f2, .002, oct);// 2-40 Hz, .002 dt, 2 oct/min

/* Fields on standard Nastran TABLED1 cards have 8 characters.

   The default format in make_tabled1() allows 5 digits to the right 
   of the decimal, so the value -9.99999 has the maximum number of 
   characters for a field of 8.

   Below, the default formats in make_tabled1() are modified for values
   of time that exceed 100 seconds and amplitudes that exceed 10.

   The formats in quotes are as specified in the C programming language
   (check a C reference for e formats and others; "man formatset" shows
   some examples). */

// Setting the 8 character field formats in make_tabled1():
   make_tabled1.X_FORMAT = "%8.4f"; // 8 field times like 120.1299 sec
   make_tabled1.Y_FORMAT = "%8.3f"; // accels like -256.345 in/sec^2

   g = 386.;
   T = make_tabled1(141, A*g, t, "SW"); // TABLED1 141, 1g sine sweep

   File = "tabled1_sine_sweep.dat";
   save(T, File);

   A = T = t = purged;

/*--------------------------------------------------------------------*/

/* If interactive, read the TABLED1 cards, make a PSD and some plots: */

   IF(keys?) // if interactive
      (A, t) = read_tabled1(File); // read A(t)
      delete(File);
      dt = @(t[2] - t[1]); // get uniform time step from t

   // Sat Oct 27 10:06:24 PDT 2012.  Plot the sweep frequency vs. t:
      macro("e^", "exp"); // make exp() that will work for infix
      r = oct/60; // octaves per second
      fsweep = f1*exp(r*t*ln(2));
      nl(dot(" make_sweep: sweep frequency (2 to 40 Hz), vs. time"));
      plot(fsweep, t);
      pause;

      S = 1/dt; // sample rate
      (PSD, f) = wPSD(A/g, S, rows(A)); // make PSD of A(t)

      IF(exists?("_engOpen")) 
         if(missing("mplot")) source("matlab.v");
         mplot(A, t); mgrid();
         TITLE = "Sine sweep, 2 to 40 Hz at 2 oct/min";
         mlabel(TITLE, "time, sec", "acceleration, in/sec^2");
         mloglog(PSD, f); mgrid();
         mlabel(TITLE, "frequency, Hz", "PSD, g^2/Hz");
      ELSE
         plot(A[1:3000], t[1:3000]);
         pause;
         plot(log10(PSD[2:rows(PSD)]), log10(f[2:rows(PSD)]));
      THEN

      halt; 

   THEN

/*--------------------------------------------------------------------*/

// Wrapping up the batch demo:

   IF(file?(File))
      (A, t) = read_tabled1(File); // read A(t)
      delete(File);
      
      dt = @(t[2] - t[1]); // get uniform time step from t
      S = 1/dt; // sample rate
      (PSD, f) = wPSD(A/g, S, rows(A)); // make PSD of A(t)

      nl(dot(" make_sweep: plot of sine sweep PSD"));
      plot(log10(PSD[2:rows(PSD)]), log10(f[2:rows(PSD)]));

      WAIT_INIT(2, "false"); // delay the ending of this script
      WAIT_BEGIN;

      << " Ok make_sweep" . >>
   ELSE 
      << " Errors in make_sweep" . >>
   THEN nl

   << 2 idle "*" 72 cats . nl >>

