#!../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().

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

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

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

/* 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(2, 40, .002, 2); // 2 - 40 Hz, .002 dt, 2 oct/min

/* Fields on standard Nastran TABLED1 cares 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)
      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)

      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
   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 >>

