// file: $isip/class/algo/Energy/Energy.h
// version: $Id: Energy.h 7609 2002-01-04 20:55:18Z gao $
//

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

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

#ifndef ISIP_CIRCULAR_DELAY_LINE
#include <CircularDelayLine.h>
#endif

// Energy: a class that computes the energy of a signal. currently,
// two modes are supported: SUM - a sum of squares; FILTER - a linear
// filtering of the squares of the samples of a signal. a number
// of scaling options are available including dB, rms, and power.
//
class Energy : public AlgorithmBase {

  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define the algorithm choices
  //
  enum ALGORITHM { SUM = 0, FILTER, DEF_ALGORITHM = SUM };

  // define the implementation choices
  //
  enum IMPLEMENTATION { IDENTITY = 0, LOG, DB, 
			POWER, LOG_POWER, DB_POWER,
			RMS, LOG_RMS, DB_RMS,
			DEF_IMPLEMENTATION = IDENTITY };

  // define the 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_FLOOR;
  static const String PARAM_MA_COEF;
  static const String PARAM_AR_COEF;

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

  // define a default value for the energy floor
  //
  static const float DEF_FLOOR = 0.0;

  // define default values for the filter coefficients
  //
  static const VectorFloat DEF_MA_COEF;
  static const VectorFloat DEF_AR_COEF;

  // define default argument(s)
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE =
  AlgorithmData::DEF_CTYPE;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 70500;
  static const long ERR_UNSUPM = 70510;  

  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:
  
  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation name
  //
  IMPLEMENTATION implementation_d;
  
  // energy floor (limits the minimum value of energy):
  //  extremely useful when performing log operations
  //
  Float floor_d;
  
  // static memory manager
  //
  static MemoryManager mgr_d;

  // this section contains data for a specific algorithm
  //

  // algorithm: FILTER
  // implementation: all
  //
  // filter coefficients
  //
  VectorFloat ma_coef_d;
  VectorFloat ar_coef_d;
  
  // filter memory
  //
  Vector< CircularDelayLine<Float> > ma_mem_d;
  Vector< CircularDelayLine<Float> > ar_mem_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 base class
  //
  boolean debug(const unichar* msg) const;

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

  // method: default constructor
  //
  Energy(ALGORITHM algorithm = DEF_ALGORITHM,
	 IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	 float floor = DEF_FLOOR) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    floor_d = floor;
    is_valid_d = false;
  }

  // method: copy constructor
  //
  Energy(const Energy& arg) {
    assign(arg);
  }

  // assign methods
  //
  boolean assign(const Energy& arg);

  // method: operator=
  //
  Energy& operator= (const Energy& 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 Energy& 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);
  }

  // other memory management methods
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);

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

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

  // method: setImplementation
  //
  boolean setImplementation(IMPLEMENTATION implementation) {
    implementation_d = implementation;
    is_valid_d = false;
    return true;  
  }
  
  // method: setFloor
  //
  boolean setFloor(float floor = DEF_FLOOR) {
    floor_d = floor;
    return true;
  }

  // method: setFilter
  //
  boolean setFilter(VectorFloat& ma_coef, VectorFloat& ar_coef) {
    ma_coef_d.assign(ma_coef);
    ar_coef_d.assign(ar_coef);
    is_valid_d = false;
    return true;
  }

  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	      float floor = DEF_FLOOR) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    floor_d = floor;
    is_valid_d = false;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  get methods
  //
  //---------------------------------------------------------------------------

  // method: getAlgorithm
  //
  ALGORITHM getAlgorithm() const {
    return algorithm_d;
  }  

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

  // method: getFilter
  //
  boolean getFilter(VectorFloat& ma_coef, VectorFloat& ar_coef) const {
    ma_coef.assign(ma_coef_d);
    return ar_coef.assign(ar_coef_d);
  }

  // method: get
  //
  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation,
	      float& floor) {
    algorithm = algorithm_d;
    implementation = implementation_d;
    floor = floor_d;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------

  boolean compute(VectorFloat& output, const VectorFloat& input,
		  AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);
  boolean compute(VectorFloat& output, const VectorComplexFloat& 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;
  }

  // initialization method
  //
  boolean init();

  // apply method
  //
  boolean apply(Vector<AlgorithmData>& output,
		const Vector< CircularBuffer<AlgorithmData> >& input);
  
  // method to set the parser
  //
  boolean setParser(SofParser* parser);

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

  // summation i/o methods
  //
  boolean readDataSum(Sof& sof, const String& pname,
		      long size, boolean param, boolean nested);
  boolean writeDataSum(Sof& sof, const String& pname) const;

  // filter i/o methods
  //
  boolean readDataFilt(Sof& sof, const String& pname,
		       long size, boolean param, boolean nested);
  boolean writeDataFilt(Sof& sof, const String& pname) const;

  // summation compute methods
  //
  boolean computeSum(VectorFloat& output, const VectorFloat& input);

  // filter compute methods
  //
  boolean computeFilt(VectorFloat& output, const VectorFloat& input,
		      long channel_index);

  // implementation-specific scaling method
  // 
  float scale(const Float& value, long len);
};

// end of include file
// 
#endif
