GeneralTools.h

Go to the documentation of this file.
00001 /*
00002 This file is part of LIA_RAL which is a set of software based on ALIZE
00003 toolkit for speaker recognition. ALIZE toolkit is required to use LIA_RAL.
00004 
00005 LIA_RAL project is a development project was initiated by the computer
00006 science laboratory of Avignon / France (Laboratoire Informatique d'Avignon -
00007 LIA) [http://lia.univ-avignon.fr <http://lia.univ-avignon.fr/>]. Then it
00008 was supported by two national projects of the French Research Ministry:
00009         - TECHNOLANGUE program [http://www.technolangue.net]
00010         - MISTRAL program [http://mistral.univ-avignon.fr]
00011 
00012 LIA_RAL is free software: you can redistribute it and/or modify
00013 it under the terms of the GNU Lesser General Public License as
00014 published by the Free Software Foundation, either version 3 of
00015 the License, or any later version.
00016 
00017 LIA_RAL is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00020 GNU Lesser General Public License for more details.
00021 
00022 You should have received a copy of the GNU Lesser General Public
00023 License along with LIA_RAL.
00024 If not, see [http://www.gnu.org/licenses/].
00025 
00026 The LIA team as well as the LIA_RAL project team wants to highlight the
00027 limits of voice authentication in a forensic context.
00028 The "Person Authentification by Voice: A Need of Caution" paper
00029 proposes a good overview of this point (cf. "Person
00030 Authentification by Voice: A Need of Caution", Bonastre J.F.,
00031 Bimbot F., Boe L.J., Campbell J.P., Douglas D.A., Magrin-
00032 chagnolleau I., Eurospeech 2003, Genova].
00033 The conclusion of the paper of the paper is proposed bellow:
00034 [Currently, it is not possible to completely determine whether the
00035 similarity between two recordings is due to the speaker or to other
00036 factors, especially when: (a) the speaker does not cooperate, (b) there
00037 is no control over recording equipment, (c) recording conditions are not
00038 known, (d) one does not know whether the voice was disguised and, to a
00039 lesser extent, (e) the linguistic content of the message is not
00040 controlled. Caution and judgment must be exercised when applying speaker
00041 recognition techniques, whether human or automatic, to account for these
00042 uncontrolled factors. Under more constrained or calibrated situations,
00043 or as an aid for investigative purposes, judicious application of these
00044 techniques may be suitable, provided they are not considered as infallible.
00045 At the present time, there is no scientific process that enables one to
00046 uniquely characterize a persones voice or to identify with absolute
00047 certainty an individual from his or her voice.]
00048 
00049 Copyright (C) 2004-2010
00050 Laboratoire d'informatique d'Avignon [http://lia.univ-avignon.fr]
00051 LIA_RAL admin [alize@univ-avignon.fr]
00052 Jean-Francois Bonastre [jean-francois.bonastre@univ-avignon.fr]
00053 */
00054 
00055 #if !defined(ALIZE_GeneralTools_h)
00056 #define ALIZE_GeneralTools_h
00057 
00058 #if defined(_WIN32)
00059 #if defined(LIA_SPKTOOLS_EXPORTS)
00060 #define LIA_SPKTOOLS_API __declspec(dllexport)
00061 #else
00062 #define LIA_SPKTOOLS_API __declspec(dllimport)
00063 #endif
00064 #else
00065 #define LIA_SPKTOOLS_API
00066 #endif
00067 
00068 #include "alize.h"
00069 #include "liatools.h"
00070 
00071 using namespace alize;
00072 using namespace std;
00073 
00074 //--------------------------------------------------------------------------------
00075 //----------------------         ComputeTest stuff    ----------------------------
00076 const unsigned long CST_MAX_CLIENT_LINE=500;
00077 
00078 class LIA_SPKTOOLS_API TabClientLine{
00079   unsigned long nbMaxModelLine;
00080   MixtureServer *ms;
00081   Config *conf;
00082   MixtureGD ** tabModel;
00083   XLine *pline;
00084   unsigned long nbModelsLine;
00085   
00086 public:
00087   unsigned long nbClientLine();
00088   String& getClientName(unsigned long);
00089   MixtureGD& getClientModel(unsigned long);
00090   unsigned long loadLine(XLine*,String="",bool=true,bool=false);  
00091   TabClientLine(MixtureServer &, Config &, unsigned long=CST_MAX_CLIENT_LINE);
00092   ~TabClientLine();
00093 };
00094 
00095 //---------------------------------------------------------------------------------
00096 //-----------------------  TabHisto definition ----------------------------------
00097 class LIA_SPKTOOLS_API TabHisto{
00098   unsigned long _nb;                        // Nb of histo
00099   ObjectRefVector _tabHisto;                            // histo tabs
00100   XLine _id;                                // the client ID;
00101 public:
00102   TabHisto(unsigned long nbBins, unsigned long maxTargetLine){_nb=0;for (unsigned int i=0;i<maxTargetLine;i++) _tabHisto.addObject(*(new Histo(nbBins)));}// build a default histo numbers and default nbBins
00103   ~TabHisto(){_tabHisto.deleteAllObjects();}                           // Verify if the memory is freezen for ...Vector and the XLine - TODO
00104   unsigned long getSize(){return _nb;}
00105   void accumulateValueInTab(const String &id,double score);
00106   Histo & getHisto(const String &id);
00107   void computeHistoInTab(const String &id);
00108   String & getId(unsigned long n);
00109   unsigned long getIndex(const String &id);
00110   Histo & getHistoFromVect(unsigned long n);
00111 };
00112 
00113  
00114 //-----------------------------------------------------------------------------------
00115 // Compute the entropy from an Histo
00116 LIA_SPKTOOLS_API double computeEntropy(Histo & hist);
00117 
00118 //-----------------------------------------------------------------------------------
00119 // Compute mean from an Histo
00120 LIA_SPKTOOLS_API double computeMean(Histo & hist);
00121 
00122 
00123 LIA_SPKTOOLS_API long setDecision(double LLRClient, double decisionThreshold);
00124 
00125 //---------------------------------------------------------------------------------
00126 //-----------------------  ScoreAccum definition ----------------------------------
00127 // JFB
00128 class LIA_SPKTOOLS_API ScoreAccum{
00129   unsigned long _nb;                        // Nb of  clients
00130   DoubleVector _score;                      // The scores
00131   ULongVector _nbFrame;                     // The accumulated value
00132   XLine _id;                                // the client ID;
00133 public:
00134   ScoreAccum(){_nb=0;}
00135   ~ScoreAccum(){}                           // Verify if the memory is freezen for ...Vector and the XLine - TODO
00136   unsigned long getSize(){return _nb;}
00137   void addAndAccumulate(const String &id,double score, unsigned long nbFrames);
00138   double getScore(const String &id);
00139   double getScore(unsigned long n);
00140   String & getId(unsigned long n);
00141   unsigned long getIndex(const String &id);
00142 };
00143 
00144 // Class for component selection
00145 struct TabWeightElem{
00146   double weight;
00147   unsigned long distrib; 
00148   DistribGD *distribP;
00149 };
00150 
00151 LIA_SPKTOOLS_API int _compF(const void * op1, const void *op2);
00152 
00153 class LIA_SPKTOOLS_API TabWeight{
00154   unsigned long _size;
00155   TabWeightElem *_tab;
00156   unsigned long _nbTop;
00157   void _sortByWeight(const MixtureGD &data){
00158     for(unsigned i=0;i<_size;i++){
00159       _tab[i].weight=data.weight(i);
00160       _tab[i].distrib=i;
00161       _tab[i].distribP=&(data.getDistrib(i));
00162     }
00163     qsort ( (void *) _tab,_size, sizeof(TabWeightElem),_compF);
00164   }
00165   void _nbTopDyn(double threshold){
00166     _nbTop=0;
00167     for (double tot=0;(_nbTop<_size)&&(tot<threshold);_nbTop++) tot+=_tab[_nbTop].weight;
00168     if (verbose) cout <<"Dyn nb top["<<_nbTop<<"]"<<endl;
00169   }
00170 public:
00171   TabWeight(){};
00172   TabWeight(const MixtureGD &);
00173   TabWeight(const MixtureGD &,unsigned long);
00174   TabWeight(const MixtureGD &,double);
00175   void init(const MixtureGD &,unsigned long);
00176   void init(const MixtureGD &,double);
00177   unsigned long getNbTop(){return _nbTop;}
00178   unsigned long getDistrib(unsigned long id){return _tab[id].distrib;}
00179   double getWeight(unsigned long id){return _tab[id].weight;}
00180   void setDistrib(unsigned long id,unsigned long distrib,double weight){_tab[id].distrib=distrib;_tab[id].weight=weight;}
00181   TabWeightElem *getArray(){return _tab;}
00182   ~TabWeight(){delete []_tab;}
00183 };
00184 
00185 
00186 // Random picking of frames (bagging) functions, based on segment/cluster processing
00187 // It is independent of the segment length
00188 // Try to decrease the number of segments for fasting the world model training
00189 // Used mainly in TrainTools.cpp 
00190 // Author: JFB
00191 LIA_SPKTOOLS_API bool baggedFrame(double baggedFrameProbability);
00192 LIA_SPKTOOLS_API void baggedSegments(SegCluster &,SegCluster &,double,unsigned long=3,unsigned long=7);
00193 LIA_SPKTOOLS_API void baggedSegmentsConstraint(SegCluster &,SegCluster &,double,unsigned long=3,unsigned long=7);
00194 // Works on a set of bagged clusters - only one reading of the
00195 // segments and multiple selections, one by bagged cluster
00196 LIA_SPKTOOLS_API void baggedSegments(SegCluster &selectedSegments,SegCluster &baggedSeg,unsigned long nbBagged,double & baggedProbability,
00197                     unsigned long minimumLength,unsigned long maximumLength);
00198 // Same but returns both selected and unselected clusters
00199 // Take care, both clusters should be created in the same server - NOT TESTED
00200 LIA_SPKTOOLS_API void baggedSegments(SegCluster &selectedSegments,SegCluster &baggedSelected,SegCluster &baggedUnselected,double baggedProbability,unsigned long minimumLength,unsigned long maximumLength);
00201 //-------------------------------------------------------------------------
00202 //-- Compute the mean log likelihood for the Selected frames and a given model
00203 LIA_SPKTOOLS_API double meanLikelihood(StatServer &ss,FeatureServer &fs,MixtureGD &model,unsigned long idxBeginFrame,unsigned long nbFrames,Config &config);
00204 // one a cluster
00205 LIA_SPKTOOLS_API double meanLikelihood(StatServer &ss,FeatureServer &fs,MixtureGD &model,SegCluster &selectedSegments,Config &config);
00206 // on a set of input streams - OLD and NEw, TODO FIRST SHOULD BE DELETED
00207 LIA_SPKTOOLS_API double meanLikelihood(StatServer &ss,FeatureServer **fsTab,SegCluster **segTab,unsigned long nbStream,MixtureGD &model,Config &config);
00208 LIA_SPKTOOLS_API double meanLikelihood(StatServer &ss,ObjectRefVector &FeatServ,ObjectRefVector &ClusterSeg,MixtureGD &model,DoubleVector &decision,Config &config);
00209 
00210 //-------------------------------------------------------------------------
00211 //-- Compute the mean and cov of selected the data using segmental mode
00212 LIA_SPKTOOLS_API void globalMeanCov (FeatureServer &fs,SegCluster &selectedSegments,FrameAcc & globalFrameAcc,Config &config);  
00213 // On a complete feature stream
00214 LIA_SPKTOOLS_API void globalMeanCov (FeatureServer &fs,FrameAcc & globalFrameAcc,Config &config);  
00215 
00216 //-------------------------------------------------------------------------
00217 //-- accumulate the statistic on the frames raw distribution of each coefficient using a current 
00218 //-- CAUTION: 
00219 //         *THE ACCUMULATOR SHOULD BE INITIALIZED BEFORE THE FIRST CALL
00220 //          initHistoTab()
00221 //         *THE HISTO SHOULD BE COMPUTED BEFORE TO USE THE STAT
00222 //          computeHistoTab()
00223 //         *The histoTab should be freezen after use
00224 //          freezeHistoTab();
00225 //      
00226 // Init the Histo array (one by coeff)
00227 LIA_SPKTOOLS_API double areaHisto(const Histo & histo,unsigned long bin);
00228 LIA_SPKTOOLS_API double areaHisto(const Histo & histo,unsigned long bin, double nonObserved);
00229 LIA_SPKTOOLS_API double linearInterpolation(double val,double lower,double higher);
00230 LIA_SPKTOOLS_API void freezeHistoTab(Histo* &histoT);
00231 LIA_SPKTOOLS_API void initHistoTab(Histo* &histoT,unsigned long size, unsigned long nbBins);
00232 LIA_SPKTOOLS_API void computeHistoTab(Histo* histoT,unsigned long size);
00233 LIA_SPKTOOLS_API void accumulateHistoFrame(Histo  *histoT,FeatureServer &fs,
00234                           unsigned long idxBeginFrame,unsigned long nbFrames,Config &config);
00235 // one a Segment
00236 LIA_SPKTOOLS_API void accumulateHistoFrame(Histo *histoT,FeatureServer &fs,Seg* seg,Config &config);
00237 // One on Cluster
00238 LIA_SPKTOOLS_API void accumulateHistoFrame(Histo *histoT,FeatureServer &fs,SegCluster &selectedSegments,Config &config);
00239 
00240 
00241 
00242 // ----------------------------------------------------------------------------------------------------------
00243 // Feature Warping giving a source(tab of histo, one by coeff) and a target distribution 
00244 // for a segment and cluster (segment is considerred to be the minimum time unit to perform this)
00245 LIA_SPKTOOLS_API void computeWarp(Histo *histoT,Histo &destH,FeatureServer & fs,unsigned long begin, unsigned long length,Config &config);
00246 // on a segment
00247 LIA_SPKTOOLS_API void computeWarp(Histo *histoT,Histo &destH, FeatureServer & fs, Seg* seg, Config & config);
00248 // on a cluster
00249 LIA_SPKTOOLS_API void computeWarp(Histo *histoT,Histo &destH, FeatureServer & fs, SegCluster & selectedSegments, Config & config);
00250  
00251 // ----------------------------------------------------------------------------------------------------------
00252 // Feature Mean subtraction and Cov reduction for a segment; cluster
00253 
00254 LIA_SPKTOOLS_API void cms(String &,FeatureServer &,Config &);
00255 
00256 LIA_SPKTOOLS_API void computeZeroOne(FrameAccGD &frameAccu,FeatureServer & fs,unsigned long begin, unsigned long length,Config &config) ;
00257 // on a segment
00258 LIA_SPKTOOLS_API void computeZeroOne(FrameAccGD & frameAccu, FeatureServer & fs, Seg* seg, Config & config);
00259 // on a cluster
00260 LIA_SPKTOOLS_API void computeZeroOne(FrameAccGD & frameAccu, FeatureServer & fs, SegCluster & selectedSegments, Config & config);
00261 // 
00262 LIA_SPKTOOLS_API void computeZeroOne(const DoubleVector &mean,const DoubleVector& cov,FeatureServer & fs,unsigned long begin, unsigned long length,Config &config) ;
00263 // on a segment
00264 LIA_SPKTOOLS_API void computeZeroOne(const DoubleVector &mean,const DoubleVector& cov, FeatureServer & fs, Seg* seg, Config & config);
00265 // on a cluster
00266 LIA_SPKTOOLS_API void computeZeroOne(const DoubleVector &mean,const DoubleVector& cov, FeatureServer & fs, SegCluster & selectedSegments, Config & config);
00267 
00268 // Feature writing in an output stream w - could be used for multiple segmen,ts from multiple files to one file
00269 LIA_SPKTOOLS_API void outputFeatureFile(Config &config, FeatureServer &fs, Feature & f, FeatureFileWriter &w);
00270 // on a segment
00271 LIA_SPKTOOLS_API void outputFeatureFile(Config &config, FeatureServer &fs, Seg * seg, FeatureFileWriter &w);
00272 // on a cluster
00273 LIA_SPKTOOLS_API void outputFeatureFile(Config &config, FeatureServer &fs, SegCluster & selectedSegments, FeatureFileWriter &w);
00274 // on a part of a file
00275 LIA_SPKTOOLS_API void outputFeatureFile(Config &config, FeatureServer &fs, unsigned long begin,unsigned long length, FeatureFileWriter &w);
00276 
00277 // Feature Mapping related
00278 LIA_SPKTOOLS_API unsigned long getBestGaussian(Mixture & M, Feature & f);
00279 LIA_SPKTOOLS_API void mapDataToDistrib(double & data, const double meanData, const double covData, const double meanMap, const double covMap);
00280 
00281 LIA_SPKTOOLS_API void featureMapping(MixtureServer & ms, FeatureServer & fs,unsigned long begin, unsigned long length,Config &config);
00282 // on a segment
00283 LIA_SPKTOOLS_API void featureMapping(MixtureServer & ms, FeatureServer & fs,Seg * seg,Config &config);
00284 
00285 // on a cluster
00286 LIA_SPKTOOLS_API void featureMapping(MixtureServer & ms, FeatureServer & fs,SegCluster & selectedSegments,Config &config);
00287 
00288 
00289 // Model based likelihood computation 
00290 LIA_SPKTOOLS_API double likelihoodGD(const DistribGD&,const DistribGD &);           
00291 LIA_SPKTOOLS_API double likelihoodGD(const MixtureGD&,const MixtureGD &);           
00292 LIA_SPKTOOLS_API double likelihoodGD(const MixtureGD&,const MixtureGD &,TabWeight&);
00293 LIA_SPKTOOLS_API double likelihoodGD(const MixtureGD& ,const MixtureGD &,TabWeight &,TabWeight &);
00294 
00295 #endif //!defined(ALIZE_GeneralTools_h)