// file: $isip/class/algo/Statistics/Statistics.h
// version: $Id: Statistics.h 8314 2002-07-09 02:19:50Z picone $
//

// make sure definitions are only made once
//
#ifndef ISIP_STATISTICS
#define ISIP_STATISTICS

// isip include files
//
#ifndef ISIP_ALGORITHM_BASE
#include <AlgorithmBase.h>
#endif

// Statistics : a class that calculates various statistical parameters
// of the input data such as the maximum, mean, and variance.  two types
// of implementations have been supported - FRAME_INTERNAL and
// ACCUMULATE. FRAME_INTERNAL computes the requested feature
// over one frame of data. this is useful for computing the average
// value of a signal on a frame-by-frame basis.
//
// ACCUMULATE computes the requested feature across all frames, and
// can be used to output a value for the entire file. this is useful
// when computing the mean value of a signal for the entire file.
//
class Statistics : public AlgorithmBase {

  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:

  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------

  // define algorithm choices
  //
  enum ALGORITHM { MINIMUM = 0, MINIMUM_MAG, MAXIMUM, MAXIMUM_MAG,
		   MEAN, MEDIAN, VARIANCE, STDEV, SKEW, KURTOSIS,
		   DEF_ALGORITHM = MINIMUM };

  // define implementation choices
  //
  enum IMPLEMENTATION { LINEAR = 0, DEF_IMPLEMENTATION = LINEAR };  
  
  // define static NameMap objects
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------
  
  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;
  static const String PARAM_CMODE;
  static const String PARAM_DMODE;  

  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define the default value(s) of the class data
  //
  static const long DEF_DELAY = 0;
  static const long DEF_NUM_FRAMES = 0;

  // define default argument(s)
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE =
  AlgorithmData::DEF_CTYPE;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  

  static const long ERR = 71800;

  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation type
  //
  IMPLEMENTATION implementation_d;

  // static memory manager
  //
  static MemoryManager mgr_d;
  
  // define a variety of accumulators for input vectors:
  //  note that the median operation is rather expensive since
  //  it requires keeping track of all prior data members.
  //
  Vector<AlgorithmData> accum_sum_d;
  Vector<AlgorithmData> accum_sumsqr_d;
  Vector<AlgorithmData> accum_result_d;
  Vector<AlgorithmData> accum_frame_data_d;
  Vector<Vector<AlgorithmData> > accum_frame_buff_d;
  
  // define some useful counters
  //
  long accum_samples_d;
  long accum_frames_d;

  // only return a value once it has seen all data
  //
  boolean is_calculated_d;
  
  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
    
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }

  // other static methods
  //
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // debug methods:
  //  setDebug is inherited from the AlgorithmBase class
  //
  boolean debug(const unichar* msg) const;

  // method: destructor
  //
  ~Statistics() {}

  // method: default constructor
  //
  Statistics(ALGORITHM algorithm = DEF_ALGORITHM,
	     IMPLEMENTATION implementation = DEF_IMPLEMENTATION);

  // method: copy constructor
  //
  Statistics(const Statistics& arg) {
    is_calculated_d = false;
    assign(arg);
  }
  
  // assign methods
  //
  boolean assign(const Statistics& arg);

  // method: operator=
  //
  Statistics& operator= (const Statistics& arg) {
    assign(arg);
    return *this;
  }

  // i/o methods
  //
  long sofSize() const;

  boolean read(Sof& sof, long tag, const String& name = CLASS_NAME);
  boolean write(Sof& sof, long tag, const String& name = CLASS_NAME) const;
  
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
                   long size = SofParser::FULL_OBJECT,
                   boolean param = true,
		   boolean nested = false);
  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const;

  // equality methods
  //
  boolean eq(const Statistics& arg) const;
  
  // method: new
  //
  static void* operator new(size_t size) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t size) {
    return mgr_d.getBlock(size);
  }

  // method: delete
  //
  static void operator delete(void* ptr) {
    mgr_d.release(ptr);
  }

  // method: delete[]
  //
  static void operator delete[](void* ptr) {
    mgr_d.releaseBlock(ptr);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long grow_size) {
    return mgr_d.setGrow(grow_size);
  }

  // method: clear
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //  set methods
  //
  //---------------------------------------------------------------------------

  // method: setAlgorithm
  //
  boolean setAlgorithm(ALGORITHM algorithm) {
    algorithm_d = algorithm;
    is_calculated_d = false;
    is_valid_d = false;
    return true;
  }

  // method: setImplementation
  //
  boolean setImplementation(IMPLEMENTATION implementation) {
    implementation_d = implementation;
    is_calculated_d = false;
    is_valid_d = false;
    return true;
  }

  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    is_valid_d = false;
    return true;
  }  
  
  // other set methods
  //
  boolean setAccumulateVar(long num_channel, long dimension);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //  get methods
  //
  //---------------------------------------------------------------------------
  
  // method: getAlgorithm
  //
  ALGORITHM getAlgorithm() const {
    return algorithm_d;
  }

  // method: getImplementation
  //
  IMPLEMENTATION getImplementation() const {
    return implementation_d;
  }  

  //
  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation) const {
    algorithm = algorithm_d;
    implementation = implementation_d;
    return true;
  }
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------

  // compute methods
  //
  boolean compute(VectorFloat& output, const VectorFloat& input,
		  AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  public methods required by the AlgorithmBase interface contract
  //
  //---------------------------------------------------------------------------

  // assign method
  //
  boolean assign(const AlgorithmBase& arg);

  // equality method
  //
  boolean eq(const AlgorithmBase& arg) const;

  // method: className
  //
  const String& className() const {
    return CLASS_NAME;
  }

  // method: init
  //
  boolean init() {
    is_calculated_d = false;
    return true;
  }

  // apply methods
  //
  boolean apply(Vector<AlgorithmData>& output,
		const Vector< CircularBuffer<AlgorithmData> >& input);

  // method to set the parser
  //
  boolean setParser(SofParser* parser);

  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // common i/o methods
  //
  boolean readDataCommon(Sof& sof, const String& pname,
			 long size = SofParser::FULL_OBJECT,
			 boolean param = true, boolean nested = false);
  boolean writeDataCommon(Sof& sof, const String& pname) const;

  // mode and computation mode-specific computation methods
  //

  boolean computeFrameInt(VectorFloat& output,
			  const VectorFloat& input,
			  AlgorithmData::COEF_TYPE
			  input_coef_type = DEF_COEF_TYPE,
			  long index = DEF_CHANNEL_INDEX);
  boolean computeFrameAccumulate(VectorFloat& output,
				 const VectorFloat& input,
				 AlgorithmData::COEF_TYPE
				 input_coef_type = DEF_COEF_TYPE,
				 long index = DEF_CHANNEL_INDEX);
  boolean computeSampleAccumulate(VectorFloat& output,
				  const VectorFloat& input,
				  AlgorithmData::COEF_TYPE
				  input_coef_type = DEF_COEF_TYPE,
				  long index = DEF_CHANNEL_INDEX);    

  // algorithm specific computation methods
  //
  boolean computeMin(VectorFloat& output, const VectorFloat& input);
  boolean computeMax(VectorFloat& output, const VectorFloat& input);
  boolean computeMinMag(VectorFloat& output, const VectorFloat& input);
  boolean computeMaxMag(VectorFloat& output, const VectorFloat& input);  
  boolean computeMean(VectorFloat& output, const VectorFloat& input);
  boolean computeMedian(VectorFloat& output, const VectorFloat& input);
  boolean computeVar(VectorFloat& output, const VectorFloat& input);
  boolean computeStdDev(VectorFloat& output, const VectorFloat& input);
};

// end of include file
//
#endif
