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_NormFeatWindowMode_cpp)
00056 #define ALIZE_NormFeatWindowMode_cpp
00057
00058 #include <iostream>
00059 #include <fstream>
00060 #include <cstdio>
00061 #include <cassert>
00062 #include <cmath>
00063 #include <liatools.h>
00064 #include "NormFeatWindowMode.h"
00065
00066 using namespace alize;
00067 using namespace std;
00068
00069
00070
00071
00072 void computeCMVnorm(Feature &f, DoubleVector &cMean, DoubleVector &cCov ){
00073
00074
00075 for (unsigned long c = 0; c < cMean.size(); c++)
00076 {
00077 f[c] = (f[c] - cMean[c])/cCov[c];
00078 }
00079 }
00080
00081
00082
00083
00084
00085 void loadMeanAndCovParam(RefVector <Feature> &windowFrameAcc, DoubleVector &cMean,DoubleVector &cCov, long &windowDuration )
00086 {
00087 FrameAccGD local;
00088
00089 Feature f(cMean.size());
00090 int i = 0;
00091
00092 if(windowFrameAcc.size() == (unsigned long)windowDuration)
00093 {
00094 while(i < windowDuration)
00095 {
00096 f = windowFrameAcc[i];
00097 local.accumulate(f);
00098 i++;
00099 }
00100 cMean = local.getMeanVect();
00101 cCov = local.getStdVect();
00102 local.reset();
00103
00104 }
00105 else
00106 {
00107 cout << "Pb with buffer length" <<endl;
00108 }
00109
00110 }
00111
00112
00113
00114 void updateMeanAndCovParam(Feature &f, DoubleVector &cMean,DoubleVector &cCov, long &windowDuration, long &frameCount, long &lookHead )
00115 {
00116
00117
00118
00119 double Beta;
00120
00121 if(frameCount < lookHead)
00122 {
00123 Beta = 1.0;
00124 }
00125
00126 else
00127 {
00128 Beta = (((double)windowDuration - 1) / (double)windowDuration) ;
00129 }
00130
00131 for (unsigned long i = 0; i < cMean.size(); i++)
00132 {
00133 cMean[i] = Beta * cMean[i] + (1 - Beta)*f[i];
00134 cCov[i] = sqrt( cCov[i] * cCov[i] * Beta + (1 - Beta) * (f[i]*f[i]));
00135
00136 }
00137
00138
00139 }
00140
00141
00142
00143 void computeCMVparameters(Feature &f, long &windowDuration, RefVector <Feature> &windowFrameAcc, DoubleVector &cMean, DoubleVector &cCov, long &frameCount )
00144 {
00145
00146
00147 loadMeanAndCovParam(windowFrameAcc,cMean,cCov,windowDuration );
00148
00149 windowFrameAcc.removeObject(0);
00150 windowFrameAcc.addObject(f);
00151
00152
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 int normFeatOnlineMode (Config & config) {
00166
00167 String inputFeatureFileName =config.getParam("inputFeatureFilename");
00168 String labelSelectedFrames =config.getParam("labelSelectedFrames");
00169
00170 long windowDuration = 300,
00171 frameCount = 0,
00172 lookHead = 0;
00173 bool initWithDelay = false;
00174
00175 if (config.existsParam("initWithDelay"))
00176 {
00177 lookHead = config.getParam("initWithDelay").toLong();
00178 initWithDelay = true;
00179
00180 }
00181
00182 if(lookHead > windowDuration)
00183 {
00184 cout << "BAD INIT DELAY, SET TO WINDOW DURATION"<<endl;
00185 lookHead = windowDuration;
00186 }
00187
00188 bool writeAllFeature = true;
00189 if (config.existsParam("writeAllFeatures"))
00190 {
00191 writeAllFeature=config.getParam("writeAllFeatures").toBool();
00192 }
00193
00194 if (verbose){
00195 cout << "NormFeatWindowMode";
00196 cout << " Window mode, Window Duration["<<windowDuration<<"]"<<endl;
00197 cout << "var and mean normalisation"<< endl;
00198 }
00199 double frameLength = 0.01;
00200
00201 if (config.existsParam("frameLength")) frameLength=config.getParam("frameLength").toDouble();
00202
00203
00204
00205 RefVector <Feature> memFrameAcc;
00206 RefVector <Feature> windowFrameAcc;
00207
00208 XLine inputFeatureFileNameList;
00209 if ( inputFeatureFileName.endsWith(".lst")){
00210 XList inputFileNameXList(inputFeatureFileName,config);
00211 inputFeatureFileNameList=inputFileNameXList.getAllElements();
00212 }
00213 else {
00214 inputFeatureFileNameList.addElement(inputFeatureFileName);
00215 }
00216 try{
00217 String *file;
00218
00219
00220 while ((file=inputFeatureFileNameList.getElement())!= NULL)
00221 {
00222 String & featureFileName=(*file);
00223 FeatureFileReader fr(featureFileName, config);
00224
00225 if (!(config.existsParam("featureFlags")))
00226 config.setParam("featureFlags",fr.getFeatureFlags().getString());
00227
00228 if (config.existsParam("windowDuration"))
00229 {
00230 windowDuration = config.getParam("windowDuration").toLong();
00231 }
00232
00233 cout << "Normalizing " << featureFileName << endl;
00234 FeatureFileWriter w(featureFileName, config);
00235
00236
00237
00238 DoubleVector cMean(fr.getVectSize(),fr.getVectSize()),cCov(fr.getVectSize(),fr.getVectSize());
00239 frameCount = 0;
00240
00241
00242
00243 if(initWithDelay)
00244 {
00245 while(frameCount < windowDuration)
00246 {
00247 Feature &f = *new Feature(fr.getVectSize());
00248
00249
00250 if ( frameCount < (windowDuration - lookHead))
00251 {
00252 f.reset();
00253 }
00254 else
00255 {
00256 if(!fr.readFeature(f))
00257 {
00258
00259 cout << "Reach end of file, new window size : " << frameCount<<endl;
00260 windowDuration=frameCount;
00261 break;
00262 }
00263 }
00264
00265 windowFrameAcc.addObject(f);
00266 frameCount++;
00267 }
00268
00269
00270 loadMeanAndCovParam(windowFrameAcc,cMean,cCov,windowDuration);
00271
00272
00273 }
00274
00275 else
00276 {
00277
00278 }
00279
00280 fr.seekFeature(0);
00281 frameCount = 0;
00282
00283 Feature ftmp;
00284
00285 while(fr.readFeature(ftmp))
00286 {
00287 Feature &f = *new Feature(ftmp);
00288 frameCount++;
00289
00290 updateMeanAndCovParam(f, cMean,cCov, windowDuration, frameCount, lookHead );
00291
00292
00293 computeCMVnorm(f, cMean, cCov);
00294
00295 w.writeFeature(f);
00296 delete &f;
00297
00298 }
00299 windowFrameAcc.deleteAllObjects();
00300
00301
00302 }
00303 }
00304
00305
00306 catch (Exception & e)
00307 {
00308 cout << e.toString ().c_str () << endl;
00309 }
00310 return 0;
00311 }
00312
00313
00314
00315 #endif // !defined(ALIZE_NormFeatWindowMode_cpp)