// file: $isip/class/algo/Spectrum/Spectrum.h
// version: $Id: Spectrum.h 8348 2002-07-12 03:51:03Z parihar $
//

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

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

#ifndef ISIP_FOURIER_TRANSFORM
#include <FourierTransform.h>
#endif

#ifndef ISIP_MEMORY_MANAGER
#include <MemoryManager.h>
#endif

#ifndef ISIP_PREDICTION
#include <Prediction.h>
#endif

#ifndef ISIP_CORRELATION
#include <Correlation.h>
#endif

#ifndef ISIP_REFLECTION
#include <Reflection.h>
#endif

// Spectrum: a class that is used to calculate the spectrum of given
// data. the input data can be sampled data, linear prediction
// coefficients or autocorrelation coefficients. the output spectrum
// can be in complex spectrum.
//
class Spectrum : public AlgorithmBase {
  
  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define algorithm choices
  //
  enum ALGORITHM { FOURIER, MAXIMUM_ENTROPY, DEF_ALGORITHM = FOURIER };

  // define implementation choices
  //  
  enum IMPLEMENTATION { MAGNITUDE = 0, COMPLEX,
			DEF_IMPLEMENTATION = MAGNITUDE };

  // define static NameMap objects for the enumerated values
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;

  // define maximum entropy spectrum scale
  //
  static const Float MAGNITUDE_SCALE;
  static const ComplexFloat COMPLEX_SCALE;  
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------
  
  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;
  static const String PARAM_DYN_RANGE;
  static const String PARAM_FT;
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // default arguments to methods
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE = AlgorithmData::SIGNAL;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 71600;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:
  
  // algorithm name
  //
  ALGORITHM algorithm_d;      

  // implementation name
  //
  IMPLEMENTATION implementation_d;

  // parameters related to the alogorithm specification
  //
  Float dyn_range_d;

  // FourierTransform object
  //
  FourierTransform ft_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);
  
  // method: setDebug
  //  this method is inherited from the AlgorithmBase class

  // other debug methods
  //
  boolean debug(const unichar* msg) const;

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

  // method: default constructor
  //
  Spectrum(ALGORITHM algorithm = DEF_ALGORITHM,
	   IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	   float dyn_range = Prediction::DEF_DYN_RANGE) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    dyn_range_d = dyn_range;
  }

  // method: copy consturctor
  //
  Spectrum(const Spectrum& arg) {
    assign(arg);
  }

  // method: assign
  //
  boolean assign(const Spectrum& arg) {
    algorithm_d = arg.algorithm_d;
    implementation_d = arg.implementation_d;
    dyn_range_d = arg.dyn_range_d;
    ft_d.assign(arg.ft_d);
    return true;
  }
  
  // method: operator=
  //
  Spectrum& operator= (const Spectrum& copy_node) {
    assign(copy_node);
    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 Spectrum& arg) const {
    return ((algorithm_d == arg.algorithm_d) &&
	    (implementation_d == arg.implementation_d) &&
	    (dyn_range_d == arg.dyn_range_d) &&
	    (ft_d.eq(arg.ft_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);
  }

  // method: clear
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE) {
    if (ctype != Integral::RETAIN) {
      ft_d.clear(ctype);
      algorithm_d = DEF_ALGORITHM;
      implementation_d = DEF_IMPLEMENTATION;
      dyn_range_d = Prediction::DEF_DYN_RANGE;
    }
    return AlgorithmBase::clear(ctype);
  }

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

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

  // method: setImplementation
  //
  boolean setImplementation(IMPLEMENTATION implementation) {
    implementation_d = implementation;
    is_valid_d = false;
    return true;  
  }
  
  // method: setDynRange
  //
  boolean setDynRange(float dyn_range) {
    dyn_range_d = dyn_range;
    return true;
  }
  
  // method: setFtAlgorithm
  //
  boolean setFtAlgorithm(FourierTransform::ALGORITHM algorithm) {
    ft_d.setAlgorithm(algorithm);
    return true;
  }

  // method: setFtImplementation
  //
  boolean setFtImplementation(FourierTransform::IMPLEMENTATION
			      implementation) {
    ft_d.setImplementation(implementation);
    return true;
  }

  // method: setFtOrder
  //
  boolean setFtOrder(long order) {
    ft_d.setOrder(order);
    return true;
  }

  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	      float dyn_range = Prediction::DEF_DYN_RANGE,
	      FourierTransform::ALGORITHM ft_algorithm =
	      FourierTransform::DEF_ALGORITHM,
	      FourierTransform::IMPLEMENTATION ft_implementation =
	      FourierTransform::DEF_IMPLEMENTATION,
	      long order = FourierTransform::DEF_ORDER) {
    setAlgorithm(algorithm);
    setImplementation(implementation);
    setDynRange(dyn_range);
    setFtAlgorithm(ft_algorithm);
    setFtImplementation(ft_implementation);    
    setFtOrder(order);
    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: getDynRange
  //
  float getDynRange() const {
    return dyn_range_d;
  }  
  
  // method: getFtAlgorithm
  //
  FourierTransform::ALGORITHM getFtAlgorithm() const {
    return ft_d.getAlgorithm();
  }

  // method: getFtImplementation
  //
  FourierTransform::IMPLEMENTATION getFtImplementation() const {
    return ft_d.getImplementation();
  }

  // method: getFtOrder
  //
  long getFtOrder() const {
    return ft_d.getOrder();
  }

  // method: get
  //
  boolean get(ALGORITHM& algorithm, IMPLEMENTATION& implementation,
	      float& dyn_range, FourierTransform::ALGORITHM& ft_algorithm,
	      FourierTransform::IMPLEMENTATION& ft_implementation,
	      float& order) const {
    algorithm = algorithm_d;
    implementation = implementation_d;
    dyn_range = dyn_range_d;
    ft_algorithm = ft_d.getAlgorithm();
    ft_implementation = ft_d.getImplementation();
    order = ft_d.getOrder();
    return true;
  }

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

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

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  interface contract methods
  //
  //--------------------------------------------------------------------------

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

  // equality method
  //  
  boolean eq(const AlgorithmBase& arg) const;
  
  // method: className
  //
  const String& className() const {
    return CLASS_NAME;
  }

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

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

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

  // algorithm-specific compute methods:
  //  algorithm: FOURIER
  //  implementation: MAGNITUDE
  // 
  boolean computeFourierMag(VectorFloat& output, const VectorFloat& input);

  // algorithm-specific compute methods:
  //  algorithm: FOURIER
  //  implementation: COMPLEX
  // 
  boolean computeFourierComplex(VectorComplexFloat& output,
				const VectorFloat& input);

  // algorithm-specific compute methods:
  //  algorithm: MAXIMUM_ENTROPY
  //  implementation: MAGNITUDE
  // 
  boolean computeReflectionMag(VectorFloat& output,
			       const VectorFloat& input,
			       double range = Prediction::DEF_DYN_RANGE);
  boolean computePredictionMag(VectorFloat& output, const VectorFloat& input);
  boolean computeCorrelationMag(VectorFloat& output,
				const VectorFloat& input,
				double range = Prediction::DEF_DYN_RANGE); 

  // algorithm-specific compute methods:
  //  algorithm: MAXIMUM_ENTROPY
  //  implementation: COMPLEX
  // 
  boolean computeReflectionComplex(VectorComplexFloat& output,
				   const VectorFloat& input,
				   double range = Prediction::DEF_DYN_RANGE);
  boolean computePredictionComplex(VectorComplexFloat& output,
				   const VectorFloat& input);
  boolean computeCorrelationComplex(VectorComplexFloat& output,
				    const VectorFloat& input,
				    double range = Prediction::DEF_DYN_RANGE);
};

// end of include file
// 
#endif
