//
//	Analysis unit structures
//
//        Copyright (c) 2002 Jim Peters.  Released under the GNU 
//        GPL version 2.  See the file COPYING for details.
//

typedef struct BWAnal BWAnal;
typedef struct BWSetup BWSetup;

//
//	This describes the setup of the analysis engine.  It is used
//	in two ways -- one to show what the current setup is, and the
//	other to request a new setup.
//

struct BWSetup {
   int off;		// Offset within input file (in samples counting from 0)
   int chan;		// Channel to display (counting from 0)
   int tbase;		// Time-base (i.e. samples per data point horizontally)
   int sx;		// Number of columns to calculate (size-X)
   int sy;		// Number of lines to calculate (size-Y)
   double freq0, freq1;	// Top+bottom frequencies (see note)
   double wwrat;	// Ratio of window width to the centre-frequency wavelength
};

// Note on top+bottom frequencies.  The number of lines 'sy' will be
// arranged as a number of equally-spaced (in log-freq-space) bands
// between freq0 and freq1.  This means that the top band will have a
// centre frequency just below freq0 and the bottom one will have a
// frequency just above freq1.  So, to get 6 bands in one octave from
// 128Hz to 256Hz, for example, set sy=6, freq0=128, freq1=256.

struct BWAnal {
   BWFile *file;
   BWBlock **blk;	// List of blocks loaded
   int n_blk;		// Number of blocks in list
   int bsiz;		// Block size
   int bnum;		// Number of block at front of list

   fftw_plan *plan;	// Big list of FFTW plans (see note below for ordering)
   int m_plan;		// Maximum plans (i.e. allocated size of plan[])

   int inp_siz;		// Size of data in inp[], or 0 if not valid
   fftw_real *inp;	// FFT'd input data (half-complex)
   fftw_real *wav;	// FFT'd wavelet (real)
   fftw_real *tmp;	// General workspace (complex)
   fftw_real *out;	// Output (complex)

   // Publically readable unchanging information
   int n_chan;		// Number of channels in input file
   double rate;		// Sample rate in input file

   // Publically readable changing information
   BWSetup c;		// Current setup
   float *sig;		// Signal mid-point values: sig[x], or NAN for sync errors
   float *sig0;		// Signal minimum values: sig0[x], or NAN for sync errors
   float *sig1;		// Signal maximum values: sig1[x], or NAN for sync errors
   float *mag;		// Magnitude information: mag[x+y*sx]
   float *est;		// Estimated nearby peak frequencies: est[x+y*sx], or NAN if can't calc
   float *freq;		// Centre-frequency of each line (Hz): freq[y]
   float *wwid;		// Width of window in samples: wwid[y]
   int *fftp;		// FFT plan to use (index into ->plan[], fftp[y]%3==0)
   int yy;		// Number of lines currently correctly calculated in arrays
   int sig_wind;	// Are the ->sig arrays windowed ? 0 no, 1 yes
   
   // Publically writable information
   BWSetup req;		// Requested setup
};

// Storage of plans in aa->plan[]: For index 'a', a%3 gives the type
// of the plan: 0: real->complex, 1: complex->real, 2:
// complex->complex.  (a/3%2 ? 3 : 2) << (a/6) gives the size of the
// plan.  This means they go in the order (2,3,4,6,8,12,16,24,etc).

#define PLAN_SIZE(n) ((n)/3%2 ? 3 : 2) << ((n)/6)


// END //
