// file: $isip_ifc/class/algo/Rps/Rps.h
// version: $Id: Rps.h 10484 2006-03-14 23:50:33Z srinivas $
//

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

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

// Rps: this class computes the reconstructed phase space (RPS)
// matrix from the input signal (time-series). two algorithms
// are supported: time delay embedding and SVD embedding. both
// the algorithms (for scalar time-series) are described in
//
// D. S. Broomhead and Gregory P. King,
// Extracting qualitative dynamics from experimental data,
// Physica D 20, pp. 217, 1986.
//
// time-delay embedding constructs points in RPS using 
// delayed-coordinate system. SVD embedding is achieved by first
// forming an RPS in a higher dimension with a delay of one
// sample and then reducing it, using SVD (singular value
// decomposition), to the desired dimension.
//
// a compute method to embed a vector time-series is also inclded
// in this class. presently, it cannot be used from isip_transform.
// but can only be called from a C++ program
//
// a zero column is appended at the end as a flag to indicate that
// each point in the RPS is continuous. this is useful, when
// concatenating several RPS matrices, to indicate points of
// discontinuities
//
class Rps : public AlgorithmBase {
  
  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

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

  // define the algorithm choices
  //
  enum ALGORITHM { TIME_DELAY_EMBEDDING = 0, SVD_EMBEDDING, DEF_ALGORITHM = TIME_DELAY_EMBEDDING };

  // define the implementation choices
  //
  enum IMPLEMENTATION { EMBEDDING = 0, DEF_IMPLEMENTATION = EMBEDDING };
  
  // 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_CMODE;
  static const String PARAM_EMBED_DIMENSION;
  static const String PARAM_DELAY;
  static const String PARAM_SVD_WINDOW_SIZE;
  static const String PARAM_SVD_THRESHOLD;
      
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define the default value(s) of the class data
  //
  static const long DEF_EMBED_DIMENSION = -1;
  static const float DEF_DELAY = -1.0;
  static const long DEF_SVD_WINDOW_SIZE = -1;
  static const float DEF_SVD_THRESHOLD = -1.0;
  
  // define default argument(s)
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE =
  AlgorithmData::SIGNAL;  
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 73400;
  static const long ERR_INP = 73401;
  static const long ERR_EMBEDDIM = 73402;
  static const long ERR_SVDDIM = 73403;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation name
  //
  IMPLEMENTATION implementation_d;

  // (global) embedding dimension
  //
  Long embed_dimension_d;

  // SVD window size (initial embedding dimension for SVD_EMBEDDING)
  //
  Long svd_window_size_d;

  // delay (tau) for embedding in sec
  //
  Float delay_d;

  // threshold value for embedding dimension calculation
  // using singular values
  //
  Float svd_threshold_d;

  // define a buffer for keeping track of all frames of data in ACCUMULATE
  // mode (using AlgorithmBase::frame_index_d to keep track of the frame
  // index)
  //
  Vector<VectorFloat> accum_buf_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
  //
  ~Rps() {}

  // method: default constructor
  //
  Rps(ALGORITHM algorithm = DEF_ALGORITHM,
      IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
      long embed_dimension = DEF_EMBED_DIMENSION,
      long svd_window_size = DEF_SVD_WINDOW_SIZE,
      float delay = DEF_DELAY,
      float svd_threshold = DEF_SVD_THRESHOLD) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    embed_dimension_d = embed_dimension;
    svd_window_size_d = svd_window_size;
    delay_d = delay;
    svd_threshold_d = svd_threshold;
    is_valid_d = false;
  }
  
  // method: copy constructor
  //
  Rps(const Rps& arg) {
    assign(arg);
  }

  // method: assign
  //
  boolean assign(const Rps& arg) {
    algorithm_d = arg.algorithm_d;
    implementation_d = arg.implementation_d;
    embed_dimension_d = arg.embed_dimension_d;
    svd_window_size_d = arg.svd_window_size_d;
    delay_d = arg.delay_d;
    svd_threshold_d = arg.svd_threshold_d;
    return AlgorithmBase::assign(arg);
  }
  
  // method: operator=
  //
  Rps& operator= (const Rps& 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;
  
  // method: eq
  //
  boolean eq(const Rps& arg) const {
    return ((algorithm_d == arg.algorithm_d) &&
	    (implementation_d == arg.implementation_d) &&
	    embed_dimension_d == arg.embed_dimension_d &&
	    svd_window_size_d == arg.svd_window_size_d &&
	    delay_d == arg.delay_d &&
	    svd_threshold_d == arg.svd_threshold_d);
  }
    
  // 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: setEmbedDimension
  //
  boolean setEmbedDimension(long embed_dimension) {
    embed_dimension_d = embed_dimension;
    is_valid_d = false;
    return true;  
  }

  // method: setSVDWindowSize
  //
  boolean setSVDWindowSize(long svd_window_size) {
    svd_window_size_d = svd_window_size;
    is_valid_d = false;
    return true;  
  }

  // method: setDelay
  //
  boolean setDelay(float delay ) {
    delay_d = delay;
    is_valid_d = false;
    return true;  
  }

  // method: setSVDThreshold
  //
  boolean setSVDThreshold(float svd_threshold) {
    svd_threshold_d = svd_threshold;
    is_valid_d = false;
    return true;  
  }  
  
  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	      long embed_dimension = DEF_EMBED_DIMENSION,
	      long svd_window_size = DEF_SVD_WINDOW_SIZE,
	      float delay = DEF_DELAY,
	      float svd_threshold = DEF_SVD_THRESHOLD) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    embed_dimension_d = embed_dimension;
    svd_window_size_d = svd_window_size;
    delay_d = delay;
    svd_threshold_d = svd_threshold;
    is_valid_d = false;
    return true;
  }

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

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

  long getSVDWindowSize() const {
    return svd_window_size_d;
  }

  float getDelay() const {
    return delay_d;
  }

  float getSVDThreshold() const {
    return svd_threshold_d;
  }  

  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation,
	      long& embed_dimension,
	      long& svd_window_size,
	      float& delay,
	      float& svd_threshold) const {
    algorithm = algorithm_d;
    implementation = implementation_d;
    embed_dimension = embed_dimension_d;
    svd_window_size = svd_window_size_d;
    delay = delay_d;
    svd_threshold = svd_threshold_d;
    return true;
  }

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

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

  // no initialization method required since none of the algorithms currently
  // supported require a history
  //
  //  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:

  // 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;

  // computation-mode-specific computation methods
  //

  // frame-based scalar embedding
  //
  boolean computeFrameInt(MatrixFloat& output,
			  const VectorFloat& input);


  // frame-based vector embedding
  //
  boolean computeFrameInt(MatrixFloat& rps,
			  const MatrixFloat& input);
  
  // Rps computation for the whole file by accumulating
  // frames or samples
  //
  boolean computeAccumulate(MatrixFloat& output,
		    const VectorFloat& input,
		    long chan_a);
  
  // algorithm-specific compute methods: TIME_DELAY_EMBEDDING
  // for scalar embedding
  //
  boolean computeTimeDelayEmbedding(MatrixFloat& output,
				    const VectorFloat& input);

  // algorithm-specific compute methods: SVD_EMBEDDING
  // for scalar embedding
  //
  boolean computeSVDEmbedding(MatrixFloat& output,
			      const VectorFloat& input);

  // algorithm-specific compute methods: TIME_DELAY_EMBEDDING
  // for vector embedding
  //
  boolean computeTimeDelayEmbedding(MatrixFloat& output,
				    const MatrixFloat& input);

  // algorithm-specific compute methods: SVD_EMBEDDING
  // for vector embedding
  //
  boolean computeSVDEmbedding(MatrixFloat& output,
			      const MatrixFloat& input);
  
};

// end of include file
// 
#endif
