// file: $isip/class/algo/Correlation/Correlation.h
// version: $Id: Correlation.h 8165 2002-05-31 21:57:33Z picone $
//

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

#ifndef ISIP_ALGORITHM_BASE
#include <AlgorithmBase.h>
#endif

// Correlation: a class that computes the correlation between two vectors.
// this class currently supports three algorithms: autocorrelation,
// crosscorrelation and convolution. three implementation methods
// are supported: factored, unfactored, and circular (where appropriate).
//
class Correlation : public AlgorithmBase {
  
  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define algorithm choices
  //
  enum ALGORITHM { AUTO = 0, AUTO_TAPERED, CROSS, CONV, DEF_ALGORITHM = AUTO };

  // define implementation choices
  //
  enum IMPLEMENTATION { FACTORED = 0, UNFACTORED, CIRCULAR,
			DEF_IMPLEMENTATION = FACTORED };

  // define mode choices:
  //  the implementation choices are specified by DMODE in the base class.
  //
  
  // define normalization choices
  //
  enum NORMALIZATION { NONE = 0, LENGTH, UNIT_ENERGY,
		       DEF_NORMALIZATION = NONE };

  // define static NameMap objects
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;
  static const NameMap NORM_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_NORMALIZATION;  
  static const String PARAM_ORDER;
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define the default value(s) of the class data
  //
  static const long DEF_ORDER = -1;

  // define default argument(s)
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE = AlgorithmData::SIGNAL;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 70100;
  static const long ERR_INPUT = 70101;
  static const long ERR_DATA = 70102;
  static const long ERR_STABLE = 70103;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation type
  //
  IMPLEMENTATION implementation_d;

  // normalization type
  //
  NORMALIZATION normalization_d;
  
  // specify the analysis order
  //
  Long order_d;

  // static memory manager
  //
  static MemoryManager mgr_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
  //
  ~Correlation() {}

  // default constructor
  //
  Correlation(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	      NORMALIZATION normalization = DEF_NORMALIZATION,
	      long order = DEF_ORDER) {
    algorithm_d = DEF_ALGORITHM;
    implementation_d = DEF_IMPLEMENTATION;
    normalization_d = DEF_NORMALIZATION;
    order_d = order;
    is_valid_d = false;
  }

  // method: copy constructor
  //
  Correlation(const Correlation& arg) {
    assign(arg);
  }
  
  // assign methods
  //
  boolean assign(const Correlation& arg);
  
  // method: operator=
  //
  Correlation& operator= (const Correlation& 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 Correlation& 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: setNormalization
  //
  boolean setNormalization(NORMALIZATION normalization) {
    normalization_d = normalization;
    is_valid_d = false;
    return true;  
  }  

  // method: setOrder
  //
  boolean setOrder(long order) {
    order_d = order;
    is_valid_d = false;
    return true;
  }

  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	      NORMALIZATION normalization = DEF_NORMALIZATION,
	      long order = DEF_ORDER) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    normalization_d = normalization;
    order_d = order;
    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: getNormalization
  //
  NORMALIZATION getNormalization() const {
    return normalization_d;
  }  

  // method: getOrder
  //
  long getOrder() const {
    return order_d;
  }

  // method: get
  //
  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation,
	      NORMALIZATION& normalization,
	      long& order) {
    algorithm = algorithm_d;
    implementation = implementation_d;
    normalization = normalization_d;
    order = order_d;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computation 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 VectorFloat& input1, const VectorFloat& input2,
		  AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);
  boolean compute(VectorComplexFloat& output,
		  const VectorComplexFloat& input,
		  AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);
  boolean compute(VectorComplexFloat& output,
		  const VectorComplexFloat& input1,
		  const VectorComplexFloat& input2,
		  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;
  }

  // no initialization method is needed since none of the
  // algorithms currently supported require a history

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

  // pad time methods:
  //  cross-frame processing requires non-zero pad times
  //
  long getLeadingPad() const;
  long getTrailingPad() const;
  
  // 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;

  // algorithm-specific computation methods: correlation of signals
  //
  boolean computeAutoFactoredFromSignal(VectorFloat& output,
					const VectorFloat& input);
  boolean computeAutoUnfactoredFromSignal(VectorFloat& output,
					  const VectorFloat& input);

  // algorithm-specific computation methods: correlation from lp models
  //
  boolean computeAutoFromPrediction(VectorFloat& output,
				    const VectorFloat& input);
  boolean computeAutoFromReflection(VectorFloat& output,
				    const VectorFloat& input);
  boolean convertFromPrediction(VectorFloat& output, const VectorFloat& input);

  // algorithm-specific computation methods: crosscorrelation and convolution
  //
  boolean computeCross(VectorFloat& output,
		       const VectorFloat& input1,
		       const VectorFloat& input2);
  boolean computeConv(VectorFloat& output,
		      const VectorFloat& input1,
		      const VectorFloat& input2);
  boolean compute(VectorFloat& output_a,
		  const CircularBuffer<AlgorithmData>& input_a,
		  AlgorithmData::COEF_TYPE coef_type_a,
		  long channel_index_a);
};

// end of include file
// 
#endif

