// file: $isip/class/algo/Covariance/Covariance.h // version: $Id: Covariance.h 8267 2002-07-02 12:55:31Z picone $ // // make sure definitions are only made once // #ifndef ISIP_COVARIANCE #define ISIP_COVARIANCE // isip include files // #ifndef ISIP_ALGORITHM_BASE #include #endif // Covariance: a class that computes the covariance matrix from the // input signal. two implementations are supported: factored and // unfactored. unfactored implementation is described in: // // J.D. Markel and A.H. Gray, Jr., // Linear Prediction of Speech, Springer-Verlag Berlin Heidelberg, // New York, New York, USA, pp. 51, 1976. // // the key equation is Eq. 3.37: // // N-1 // c[i,j] = sum x(n-i) x(n-j) // n=M // // factored is a computationally efficient approach described in: // // J.D. Markel and A.H. Gray, Jr., // Linear Prediction of Speech, Springer-Verlag Berlin Heidelberg, // New York, New York, USA, pp. 220, 1976. // // that produces the same result as the unfactored approach. // // the key equation is Eq. 9.10: // // c[i,j] = c[i-1,j-1] + x[M-i]*x[M-j] - x[N-i]x[N-j] // // this class has one other complication. in FRAME_INTERNAL mode, // it computes the covariance of the data within the current frame. // this is typically used in linear prediction analysis (CROSS_FRAME // also allows data outside the frame to be used in the standard // windowed covariance analysis). // // in contrast, in ACCUMULATE mode, it computes the covariance across // successive frames of data and returns the global covariance matrix // for the file. // // the key equation for ACCUMUlate mode is: // // if it is not the last frame:: c(i, j) += x(i) x(j), u(i) += x(i) // // if it is the last frame: C(i, j) = 1/N * sum{c(i,j)} - u(i) u(i)' // class Covariance : public AlgorithmBase { //--------------------------------------------------------------------------- // // public constants // //--------------------------------------------------------------------------- public: // define the class name // static const String CLASS_NAME; //---------------------------------------- // // other important constants // //---------------------------------------- // define the algorithm choices // enum ALGORITHM { NORMAL = 0, DEF_ALGORITHM = NORMAL }; // define the implementation choices // enum IMPLEMENTATION { FACTORED = 0, UNFACTORED, DEF_IMPLEMENTATION = FACTORED }; // define normalization choices // enum NORMALIZATION { NONE = 0, LENGTH, UNIT_ENERGY, DEF_NORMALIZATION = NONE }; // define the 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 int32 DEF_ORDER = -1; // define default argument(s) // static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE = AlgorithmData::SIGNAL; //---------------------------------------- // // error codes // //---------------------------------------- static const int32 ERR = 70400; //--------------------------------------------------------------------------- // // protected data // //--------------------------------------------------------------------------- protected: // algorithm name // ALGORITHM algorithm_d; // implementation name // IMPLEMENTATION implementation_d; // normalization name // NORMALIZATION normalization_d; // specify a Covariance order // Long order_d; // specific variables for accumulation update // Vector accum_cov_d; // covariance accumulation Vector accum_sum_d; // signal amplitude accumulation Long accum_frame_d; // number of frames received // static memory manager // static MemoryManager mgr_d; //--------------------------------------------------------------------------- // // required public methods // //--------------------------------------------------------------------------- public: // method: name // static const String& name() { return CLASS_NAME; } // other static methods // static bool8 diagnose(Integral::DEBUG debug_level); // debug methods: // setDebug is inherited from the AlgorithmBase class // bool8 debug(const unichar* msg) const; // method: destructor // ~Covariance() {} // method: default constructor // Covariance(ALGORITHM algorithm = DEF_ALGORITHM, IMPLEMENTATION implementation = DEF_IMPLEMENTATION, NORMALIZATION normalization = DEF_NORMALIZATION, int32 order = DEF_ORDER) { algorithm_d = algorithm; implementation_d = implementation; normalization_d = normalization; order_d = order; is_valid_d = false; } // method: copy constructor // Covariance(const Covariance& arg) { assign(arg); } // method: assign // bool8 assign(const Covariance& arg) { algorithm_d = arg.algorithm_d; implementation_d = arg.implementation_d; order_d = arg.order_d; return AlgorithmBase::assign(arg); } // method: operator= // Covariance& operator= (const Covariance& 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; // method: eq // bool8 eq(const Covariance& arg) const { return ((algorithm_d == arg.algorithm_d) && (implementation_d == arg.implementation_d) && order_d.eq(arg.order_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 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: setNormalization // bool8 setNormalization(NORMALIZATION normalization) { normalization_d = normalization; is_valid_d = false; return true; } // method: setOrder // bool8 setOrder(int32 order) { order_d = order; is_valid_d = false; return true; } // method: set // bool8 set(ALGORITHM algorithm = DEF_ALGORITHM, IMPLEMENTATION implementation = DEF_IMPLEMENTATION, NORMALIZATION normalization = DEF_NORMALIZATION, int32 order = DEF_ORDER) { algorithm_d = algorithm; implementation_d = implementation; normalization_d = normalization; order_d = order; return true; } // method: setAccumulateVar // bool8 setAccumulateVar(int32 num_channel, int32 dimension) { accum_cov_d.setLength(num_channel); accum_sum_d.setLength(num_channel); accum_frame_d = 0; for (int32 i = 0; i < num_channel; i++) { accum_cov_d(i).setDimensions(dimension, dimension); accum_cov_d(i).assign(0); accum_sum_d(i).setLength(dimension); accum_sum_d(i).assign((float32)0); } 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 // int32 getOrder() const { return order_d; } // method: get // bool8 get(ALGORITHM& algorithm, IMPLEMENTATION& implementation, NORMALIZATION& normalization, int32& order) const { algorithm = algorithm_d; implementation = implementation_d; normalization = normalization_d; order = order_d; return true; } //--------------------------------------------------------------------------- // // class-specific public methods: // computational methods // //--------------------------------------------------------------------------- bool8 compute(MatrixFloat& output, const VectorFloat& input, AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE, int32 index = DEF_CHANNEL_INDEX); bool8 compute(MatrixComplexFloat& output, const VectorComplexFloat& input, AlgorithmData::COEF_TYPE input_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() { return true; } // apply method // bool8 apply(Vector& output, const Vector< CircularBuffer >& input); // 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; // algorithm-specific compute methods: Normal (FRAME_INTERNAL) // bool8 computeNormalFactored(MatrixFloat& output, const VectorFloat& input); bool8 computeNormalUnFactored(MatrixFloat& output, const VectorFloat& input); // algorithm-specific compute methods: Normal (ACCUMULATE) // bool8 computeAccumulate(MatrixFloat& output, const VectorFloat& input, int32 chan); }; // end of include file // #endif