00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_DistribGF_cpp)
00056 #define ALIZE_DistribGF_cpp
00057
00058
00059 #if defined(_WIN32)
00060 #include <cfloat>
00061 #define ISNAN(x) _isnan(x)
00062
00063
00064 #elif defined(linux) || defined(__linux) || defined(__CYGWIN__) || defined(__APPLE__)
00065 #define ISNAN(x) isnan(x)
00066 #else
00067 #error "Unsupported OS\n"
00068 #endif
00069
00070 #include <new>
00071 #include <cmath>
00072 #include <cstdlib>
00073 #include <memory.h>
00074 #include "DistribGF.h"
00075 #include "alizeString.h"
00076 #include "Feature.h"
00077 #include "Exception.h"
00078 #include "Config.h"
00079
00080
00081 using namespace alize;
00082 using namespace std;
00083
00084
00085 DistribGF::DistribGF(const unsigned long vectSize)
00086 :Distrib(vectSize), _covInvMatr(_vectSize),
00087 _cst(0.0), _tmpVect(_vectSize) {}
00088
00089 DistribGF::DistribGF(const Config& c)
00090 :Distrib(c.getParam_vectSize()>0?c.getParam_vectSize():1),
00091 _covInvMatr(_vectSize), _cst(0.0), _tmpVect(_vectSize) {}
00092
00093 void DistribGF::reset()
00094 {
00095 throw Exception("Do not use this method temporary", __FILE__, __LINE__);
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 }
00112
00113 DistribGF& DistribGF::create(const K&, const unsigned long vectSize)
00114 {
00115 DistribGF* p = new (std::nothrow) DistribGF(vectSize);
00116 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00117 return *p;
00118 }
00119
00120 DistribGF& DistribGF::create(const K&, const Config& c)
00121 { return create(K::k, c.getParam_vectSize()); }
00122
00123 DistribGF::DistribGF(const DistribGF& d)
00124 :Distrib(d._vectSize), _covMatr(d._covMatr), _covInvMatr(d._covInvMatr),
00125 _cst(d._cst), _tmpVect(d._vectSize)
00126 {
00127 _meanVect = d._meanVect;
00128 _det = d._det;
00129 }
00130
00131 const Distrib& DistribGF::operator=(const Distrib& d)
00132 {
00133 const DistribGF* p = dynamic_cast<const DistribGF*>(&d);
00134 if (p == NULL)
00135 throw Exception("incompatible distrib", __FILE__, __LINE__);
00136 return operator=(*p);
00137 }
00138
00139 const DistribGF& DistribGF::operator=(const DistribGF& d)
00140 {
00141 if (_vectSize != d._vectSize)
00142 throw Exception("target distrib vectSize ("
00143 + String::valueOf(_vectSize) + ") != source distrib vectSize ("
00144 + String::valueOf(d._vectSize) + ")", __FILE__, __LINE__);
00145 _meanVect = d._meanVect;
00146 _covInvMatr = d._covInvMatr;
00147 _covMatr = d._covMatr;
00148 _det = d._det;
00149 _cst = d._cst;
00150 return *this;
00151 }
00152
00153 bool DistribGF::operator==(const Distrib& d) const
00154 {
00155 const DistribGF* p = dynamic_cast<const DistribGF*>(&d);
00156 if (p == NULL || _meanVect != p->_meanVect ||
00157 (_covMatr.size() == 0 && p->_covMatr.size() != 0) ||
00158 (_covMatr.size() != 0 && p->_covMatr.size() == 0))
00159 return false;
00160 if (_covMatr.size() == 0)
00161 return (_covInvMatr == p->_covInvMatr);
00162 return _covMatr == p->_covMatr;
00163 }
00164
00165 DistribGF& DistribGF::duplicate(const K&) const
00166 { return static_cast<DistribGF&>(clone()); }
00167
00168 Distrib& DistribGF::clone() const
00169 {
00170 DistribGF* p = new (std::nothrow) DistribGF(*this);
00171 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00172 return *p;
00173 }
00174
00175
00176
00177 lk_t DistribGF::computeLK(const Feature& frame) const
00178 {
00179 if (frame.getVectSize() != _vectSize)
00180 throw Exception("distrib vectSize ("
00181 + String::valueOf(_vectSize) + ") != feature vectSize ("
00182 + String::valueOf(frame.getVectSize()) + ")", __FILE__, __LINE__);
00183
00184 real_t tmp = 0.0;
00185 real_t tmp2;
00186 unsigned long i, j, ii;
00187 real_t* m = _meanVect.getArray();
00188 real_t* x = _tmpVect.getArray();
00189 real_t* c = _covInvMatr.getArray();
00190 Feature::data_t* f = frame.getDataVector();
00191
00192 for (j=0; j<_vectSize; j++)
00193 x[j] = f[j] - m[j];
00194 for (i=0; i<_vectSize; i++)
00195 {
00196 tmp2 = 0.0;
00197 ii = i*_vectSize;
00198 for (j=0; j<_vectSize; j++)
00199 tmp2 += x[j] * c[j+ii];
00200 tmp += tmp2 * x[i];
00201 }
00202
00203 tmp = _cst * exp(-0.5*tmp);
00204 if (ISNAN(tmp))
00205 return EPS_LK;
00206 return tmp;
00207 }
00208
00209 lk_t DistribGF::computeLK(const Feature& frame, unsigned long idx) const
00210 {
00211 real_t x = frame[idx] - _meanVect[idx];
00212 real_t tmp = _cst * exp(-0.5 * x * x * _covInvMatr(idx, idx) );
00213 if (ISNAN(tmp))
00214 return EPS_LK;
00215 return tmp;
00216 }
00217
00218 void DistribGF::computeAll()
00219 {
00220
00221
00222 _det = _covMatr.invert(_covInvMatr);
00223
00224
00225
00226 if (_det > EPS_LK)
00227 _cst = 1.0 / ( pow(_det, 0.5) * pow( PI2 , _vectSize/2.0 ) );
00228 else
00229 _cst = 1.0 / ( pow(EPS_LK, 0.5) * pow( PI2 , _vectSize/2.0 ) );
00230
00231
00232 _covMatr.setSize(0, true);
00233 }
00234
00235 void DistribGF::setCov(real_t v, unsigned long col, unsigned long row)
00236 {
00237 _covMatr.setSize(_vectSize);
00238 if (v < MIN_COV)
00239 v = MIN_COV;
00240 _covMatr(col, row) = v;
00241 }
00242
00243 void DistribGF::setCovInv(const K&, const real_t v, const unsigned long col,
00244 const unsigned long row)
00245 { _covInvMatr(col, row) = v; }
00246
00247 real_t DistribGF::getCov(unsigned long col, unsigned long row) const
00248 {
00249 _covMatr.setSize(_vectSize);
00250 return _covMatr(col, row);
00251 }
00252
00253 real_t DistribGF::getCovInv(const unsigned long col,
00254 const unsigned long row) const
00255 { return _covInvMatr(col, row); }
00256
00257 DoubleSquareMatrix& DistribGF::getCovInvMatrix() {return _covInvMatr;}
00258
00259 const DoubleSquareMatrix& DistribGF::getCovInvMatrix() const {return _covInvMatr;}
00260
00261 DoubleSquareMatrix& DistribGF::getCovMatrix() { return _covMatr; }
00262
00263 const DoubleSquareMatrix& DistribGF::getCovMatrix() const { return _covMatr; }
00264
00265 String DistribGF::getClassName() const { return "DistribGF"; }
00266
00267 String DistribGF::toString() const
00268 {
00269 String s = Object::toString()
00270 + "\n vectSize = " + String::valueOf(_vectSize)
00271 + "\n det = " + String::valueOf(_det)
00272 + "\n cst = " + String::valueOf(_cst);
00273
00274
00275 if (_covMatr.size() != 0)
00276 for (unsigned long i=0; i<_vectSize; i++)
00277 for (unsigned long j=0; j<_vectSize; j++)
00278 s += "\n cov["+String::valueOf(i)+","+String::valueOf(j)+"] = "
00279 + String::valueOf(_covMatr(i,j))
00280 + " covInv["+String::valueOf(i)+","+String::valueOf(j)+"] = "
00281 + String::valueOf(_covInvMatr(i,j));
00282 else
00283 for (unsigned long i=0; i<_vectSize; i++)
00284 for (unsigned long j=0; j<_vectSize; j++)
00285 s += "\n covInv["+String::valueOf(i)+","+String::valueOf(j)+"] = "
00286 + String::valueOf(_covInvMatr(i,j));
00287 for (unsigned long i=0; i<_vectSize; i++)
00288 s += "\n mean[" + String::valueOf(i) + "] = "
00289 + String::valueOf(_meanVect[i]);
00290 return s;
00291 }
00292
00293 DistribGF::~DistribGF() {}
00294
00295 #endif // !defined(ALIZE_DistribGF_cpp)
00296