// file: $isip/class/algo/Calculus/Calculus.h // version: $Id: Calculus.h 8165 2002-05-31 21:57:33Z picone $ // // make sure definitions are only made once // #ifndef ISIP_CALCULUS #define ISIP_CALCULUS #ifndef ISIP_ALGORITHM_BASE #include #endif // Calculus: a class that performs basic calculus operations such as // differentiation (typically implemented via regression) and integration. // this class operates on elements of vectors or time series data delivered // as sequences of vectors. // class Calculus : public AlgorithmBase { //--------------------------------------------------------------------------- // // public constants // //--------------------------------------------------------------------------- public: // define the class name // static const String CLASS_NAME; //---------------------------------------- // // other important constants // //---------------------------------------- // define algorithm choices // enum ALGORITHM { DIFFERENTIATION = 0, INTEGRATION, DEF_ALGORITHM = DIFFERENTIATION }; // define implementation choices // enum IMPLEMENTATION { REGRESSION = 0, CENTRAL_DIFFERENCE, BACKWARD_DIFFERENCE, DEF_IMPLEMENTATION = REGRESSION }; // 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_ORDER; static const String PARAM_DELTAWIN; //---------------------------------------- // // default values and arguments // //---------------------------------------- // define the default value(s) of the class data // static const int32 DEF_ORDER = 1; static const int32 DEF_DELTAWIN = 2; // define default argument(s) // static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE = AlgorithmData::GENERIC; //---------------------------------------- // // error codes // //---------------------------------------- static const int32 ERR = 70200; static const int32 ERR_ZERODENOM = 70201; //--------------------------------------------------------------------------- // // protected data // //--------------------------------------------------------------------------- protected: // algorithm name // ALGORITHM algorithm_d; // implementation name // IMPLEMENTATION implementation_d; // specify an order for the operation (for example, a 1st-order derivative) // Long order_d; // static memory manager // static MemoryManager mgr_d; // this section contains data for a specific algorithm // // algorithm: differentiation // implementation: regression/central difference/backward difference // description: window length in frames // notes: these implementations interpret this differently. // Long delta_win_d; // algorithm: differentiation // implementation: regression, central difference, or regression // description: precomputed scaling factor // double denom_d; //--------------------------------------------------------------------------- // // required public methods // //--------------------------------------------------------------------------- public: // method: name // static const String& name() { return CLASS_NAME; } // other static methods // static bool8 diagnose(Integral::DEBUG debug_level); // method: setDebug // this method is inherited from the AlgorithmBase class // other debug methods // bool8 debug(const unichar* msg) const; // method: destructor // ~Calculus() {} // method: default constructor // note that we can't include all arguments because that would create // ambiguous default values. // Calculus(ALGORITHM algorithm = DEF_ALGORITHM, IMPLEMENTATION implementation = DEF_IMPLEMENTATION, int32 order = DEF_ORDER, int32 delta_win = DEF_DELTAWIN) { algorithm_d = algorithm; implementation_d = implementation; order_d = order; delta_win_d = DEF_DELTAWIN; denom_d = 0; is_valid_d = false; } // method: copy constructor // Calculus(const Calculus& arg) { assign(arg); } // assign methods // bool8 assign(const Calculus& arg); // method: operator= // Calculus& operator= (const Calculus& arg) { assign(arg); return *this; } // i/o methods // int32 sofSize() const; bool8 read(Sof& sof, int32 tag, const String& name = CLASS_NAME); bool8 write(Sof& sof, int32 tag, const String& name = CLASS_NAME) const; bool8 readData(Sof& sof, const String& pname = DEF_PARAM, int32 size = SofParser::FULL_OBJECT, bool8 param = true, bool8 nested = false); bool8 writeData(Sof& sof, const String& pname = DEF_PARAM) const; // equality methods // bool8 eq(const Calculus& 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 bool8 setGrowSize(int32 grow_size) { return mgr_d.setGrow(grow_size); } // other memory management methods // bool8 clear(Integral::CMODE ctype = Integral::DEF_CMODE); //--------------------------------------------------------------------------- // // class-specific public methods // set methods // //--------------------------------------------------------------------------- // method: setAlgorithm // bool8 setAlgorithm(ALGORITHM algorithm) { algorithm_d = algorithm; is_valid_d = false; return true; } // method: setImplementation // bool8 setImplementation(IMPLEMENTATION implementation) { implementation_d = implementation; is_valid_d = false; return true; } // method: setOrder // bool8 setOrder(int32 order) { order_d = order; is_valid_d = false; return true; } // method: setDeltaWindow // bool8 setDeltaWindow(int32 delta_win) { delta_win_d = delta_win; is_valid_d = false; return true; } // method: set // bool8 set(ALGORITHM algorithm = DEF_ALGORITHM, IMPLEMENTATION implementation = DEF_IMPLEMENTATION, int32 order = DEF_ORDER, int32 delta_win = DEF_DELTAWIN) { algorithm_d = algorithm; implementation_d = implementation; order_d = order; delta_win_d = delta_win; denom_d = 0; 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: getOrder // int32 getOrder() const { return order_d; } // method: getDeltaWindow // int32 getDeltaWindow() const { return delta_win_d; } // method: get // bool8 get(ALGORITHM& algorithm, IMPLEMENTATION& implementation, int32& order, int32& delta_win) const { algorithm = algorithm_d; implementation = implementation_d; order = order_d; delta_win = delta_win_d; return true; } //--------------------------------------------------------------------------- // // class-specific public methods: // computational methods // //--------------------------------------------------------------------------- bool8 compute(VectorFloat& output, const VectorFloat& input, AlgorithmData::COEF_TYPE coef_type = DEF_COEF_TYPE, int32 index = DEF_CHANNEL_INDEX); bool8 compute(VectorComplexFloat& output, const VectorComplexFloat& input, AlgorithmData::COEF_TYPE coef_type = DEF_COEF_TYPE, int32 index = DEF_CHANNEL_INDEX); //--------------------------------------------------------------------------- // // class-specific public methods: // public methods required by the AlgorithmBase interface contract // //--------------------------------------------------------------------------- // assign method // bool8 assign(const AlgorithmBase& arg); // equality method // bool8 eq(const AlgorithmBase& arg) const; // method: className // const String& className() const { return CLASS_NAME; } // initialization method // bool8 init(); // apply method // bool8 apply(Vector& output, const Vector< CircularBuffer >& input); // method: getLeadingPad // int32 getLeadingPad() const { return delta_win_d; } // method: getTrailingPad // int32 getTrailingPad() const { return delta_win_d; } // method to set the parser // bool8 setParser(SofParser* parser); //--------------------------------------------------------------------------- // // private methods // //--------------------------------------------------------------------------- private: // common i/o methods // bool8 readDataCommon(Sof& sof, const String& pname, int32 size = SofParser::FULL_OBJECT, bool8 param = true, bool8 nested = false); bool8 writeDataCommon(Sof& sof, const String& pname) const; // general compute methods: // The compute method in this class is different with most of other // classes, it is easier to operate directly on the circular buffer // of data. hence, we provide two additional compute methods for // FRAME_INTERNAL and CROSS_FRAME mode to use. // bool8 computeInternal(VectorFloat& output, const CircularBuffer& input, AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE, int32 channel_index = DEF_CHANNEL_INDEX); bool8 computeCross(VectorFloat& output, const CircularBuffer& input, AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE, int32 channel_index = DEF_CHANNEL_INDEX); // algorithm-specific compute methods: Differentiation // bool8 computeDifRegression(VectorFloat& output, const VectorFloat& input); bool8 computeDifCentral(VectorFloat& output, const VectorFloat& input); bool8 computeDifBackward(VectorFloat& output, const VectorFloat& input); }; // end of include file // #endif