Go to the documentation of this file.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_FrameAccGF_cpp)
00056 #define ALIZE_FrameAccGF_cpp
00057
00058 #include "FrameAccGF.h"
00059 #include "Exception.h"
00060 #include "Feature.h"
00061 #include <new>
00062 #include <cmath>
00063
00064 using namespace alize;
00065 typedef FrameAccGF A;
00066
00067
00068 A::FrameAccGF()
00069 :FrameAcc() {}
00070
00071 A::FrameAccGF(const FrameAccGF& a)
00072 :FrameAcc() { copy(a); }
00073
00074 const FrameAccGF& A::operator=(const FrameAccGF& a)
00075 { copy(a); return *this; }
00076
00077 void A::copy(const FrameAccGF& a)
00078 {
00079 _vectSize = a._vectSize;
00080 _vectSizeDefined = a._vectSizeDefined;
00081 _count = a._count;
00082 _computed = a._computed;
00083 _stdComputed = a._stdComputed;
00084 _accVect = a._accVect;
00085 _meanVect = a._meanVect;
00086 _covMatrix = a._covMatrix;
00087 _stdMatrix = a._stdMatrix;
00088 _xaccMatrix = a._xaccMatrix;
00089 }
00090
00091 A& A::create()
00092 {
00093 A* p = new (std::nothrow) A();
00094 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00095 return *p;
00096 }
00097
00098 const DoubleSquareMatrix& A::getCovMatrix()
00099 {
00100 if (!_computed)
00101 computeAll();
00102 return _covMatrix;
00103 }
00104
00105 const DoubleSquareMatrix& A::getStdMatrix()
00106 {
00107 if (!_stdComputed)
00108 {
00109 if (!_computed)
00110 computeAll();
00111 _stdMatrix.setSize(_vectSize);
00112 double* stdMatrix = _stdMatrix.getArray();
00113 const double* covMatrix = _covMatrix.getArray();
00114 unsigned long i, j, ii;
00115 for (i=0; i<_vectSize; i++)
00116 for (j=i, ii = i*(_vectSize+1); j<_vectSize; j++, ii += _vectSize)
00117 stdMatrix[ii] = sqrt(covMatrix[ii]);
00118 _stdComputed = true;
00119 }
00120 return _stdMatrix;
00121 }
00122
00123 const DoubleSquareMatrix& A::getxAccMatrix() const { return _xaccMatrix; }
00124
00125 void A::accumulate(const Feature& f)
00126 {
00127 const unsigned long vectSize = f.getVectSize();
00128 if (!_vectSizeDefined)
00129 {
00130 _vectSize = vectSize;
00131 _accVect.setSize(_vectSize);
00132 _accVect.setAllValues(0.0);
00133 _xaccMatrix.setSize(_vectSize);
00134 _xaccMatrix.setAllValues(0.0);
00135 _vectSizeDefined = true;
00136 }
00137 else if (vectSize != _vectSize)
00138 throw Exception("Incompatible vectSize ("
00139 + String::valueOf(vectSize) + "/"
00140 + String::valueOf(_vectSize) + ")", __FILE__, __LINE__);
00141 const double* dataVect = f.getDataVector();
00142 double* xaccMatrix = _xaccMatrix.getArray();
00143 unsigned long i,j, ii;
00144 for (i=0; i<_vectSize; i++)
00145 {
00146 const double v = dataVect[i];
00147 _accVect[i] += v;
00148 for (j=i, ii=i*(_vectSize+1); j<_vectSize; j++, ii+=_vectSize)
00149 xaccMatrix[ii] += v*dataVect[j];
00150 }
00151 _count++;
00152 _computed = false;
00153 _stdComputed = false;
00154 }
00155
00156 void A::add(const FrameAccGF& f)
00157 {
00158 const unsigned long vectSize = f.getVectSize();
00159 if (!_vectSizeDefined)
00160 {
00161 _vectSize = vectSize;
00162 _accVect.setSize(_vectSize);
00163 _accVect.setAllValues(0.0);
00164 _xaccMatrix.setSize(_vectSize);
00165 _xaccMatrix.setAllValues(0.0);
00166 _vectSizeDefined = true;
00167 }
00168 else if (vectSize != _vectSize)
00169 throw Exception("Incompatible vectSize ("
00170 + String::valueOf(vectSize) + "/"
00171 + String::valueOf(_vectSize) + ")", __FILE__, __LINE__);
00172
00173 const DoubleVector& accVect = f.getAccVect();
00174 const DoubleSquareMatrix& xaccMatrix = f.getxAccMatrix();
00175
00176 double * matrixValues = xaccMatrix.getArray();
00177 double * _matrixValues = _xaccMatrix.getArray();
00178
00179 unsigned long i,j, ii;
00180 for (i=0; i<_vectSize; i++)
00181 {
00182 _accVect[i] += accVect[i];
00183 for (j=i, ii=i*(_vectSize+1); j<_vectSize; j++, ii+=_vectSize)
00184 _matrixValues[ii] += matrixValues[ii];
00185 }
00186 _count += f.getCount();
00187 _computed = false;
00188 _stdComputed = false;
00189 }
00190
00191 void A::deaccumulate(const Feature& f)
00192 {
00193 const unsigned long vectSize = f.getVectSize();
00194
00195 if (!_vectSizeDefined) {
00196 return;
00197 } else if (vectSize != _vectSize) {
00198 throw Exception("Incompatible vectSize ("
00199 + String::valueOf(vectSize) + "/"
00200 + String::valueOf(_vectSize) + ")", __FILE__, __LINE__);
00201 }
00202
00203 const double* dataVect = f.getDataVector();
00204 double* xaccMatrix = _xaccMatrix.getArray();
00205 unsigned long i,j, ii;
00206 for (i=0; i<_vectSize; i++)
00207 {
00208 const double v = dataVect[i];
00209 _accVect[i] -= v;
00210 for (j=i, ii=i*(_vectSize+1); j<_vectSize; j++, ii+=_vectSize)
00211 xaccMatrix[ii] -= v*dataVect[j];
00212 }
00213 _count--;
00214 _computed = false;
00215 _stdComputed = false;
00216 }
00217
00218 void A::computeAll()
00219 {
00220 if (_count == 0)
00221 throw Exception("No frame accumulated", __FILE__, __LINE__);
00222 const double invCount = 1.0/(double)_count;
00223 const unsigned long vectSize = getVectSize();
00224 unsigned long i, j, ii;
00225 _meanVect.setSize(_vectSize);
00226 _covMatrix.setSize(vectSize);
00227 _stdMatrix.setSize(vectSize);
00228 const double* accVect = _accVect.getArray();
00229 const double* xaccMatrix = _xaccMatrix.getArray();
00230 double* meanVect = _meanVect.getArray();
00231 double* covMatrix = _covMatrix.getArray();
00232 for (i=0; i<vectSize; i++)
00233 meanVect[i] = accVect[i]*invCount;
00234 for (i=0; i<vectSize; i++)
00235 {
00236 const double mean = meanVect[i];
00237 for (j=i, ii = i*(vectSize+1); j<vectSize; j++, ii += vectSize)
00238 covMatrix[ii] = xaccMatrix[ii]*invCount - mean*meanVect[j];
00239 }
00240
00241
00242
00243 DoubleSquareMatrix covInvMatrix(vectSize);
00244
00245 _det = _covMatrix.invert(covInvMatrix);
00246
00247
00248
00249 if (_det > EPS_LK)
00250 _cst = 1.0 / ( pow(_det, 0.5) * pow( PI2 , _vectSize/2.0 ) );
00251 else
00252 _cst = 1.0 / ( pow(EPS_LK, 0.5) * pow( PI2 , _vectSize/2.0 ) );
00253 _computed = true;
00254 }
00255
00256 String A::getClassName() const { return "FrameAccGF"; }
00257
00258 A::~FrameAccGF() {}
00259
00260
00261 #endif // !defined(ALIZE_FrameAccGF_cpp)
00262