#! /usr/local/bin/pvtool

#define USAGE_MESSAGE "\
\tmakeenv <pv input file> <pv output envelope file> [<threshold scalar>]\n\
\n\
This program extracts a spectral envelope of sorts from an fft file. This\n\
envelope can be used with commands such as mergeenv and applyenv. The\n\
envelope is found by taking local maxima in a frame above a certain\n\
threshold when compared to the maximum for the frame. The threshold can be\n\
changed for particularly noisy or clean signals to produce a more useful\n\
envelope. The default value is 0.1\n"

#define DEFAULT_THRESHOLD_SCALAR 0.1

/*****************************************************************************/

int
pvaction (void)
{
  int i, j;
  float threshold_scalar;

  if (parameter_count >= 1)
    {
      threshold_scalar = fparameter[0];
      printf ("Using threshold value %f.\n", threshold_scalar);
    }
  else
    threshold_scalar = DEFAULT_THRESHOLD_SCALAR;

  for (i = 0; i < OUTPUT_FRAMES; i++)
    {
      for (j = 0; j < bins; j++)
	{
	  OUTPUT_PHIDOT (i, j) = bin_to_freq (j);
	  OUTPUT_MAG (i, j) =
	    spectral_envelope_component (1, i, j, threshold_scalar);
	}
    }
  return (0);
}
