Feature.cpp

Go to the documentation of this file.
00001 /*
00002         This file is part of ALIZE which is an open-source tool for 
00003         speaker recognition.
00004 
00005     ALIZE is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU Lesser General Public License as 
00007     published by the Free Software Foundation, either version 3 of 
00008     the License, or any later version.
00009 
00010     ALIZE is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public 
00016     License along with ALIZE.
00017     If not, see <http://www.gnu.org/licenses/>.
00018         
00019         ALIZE is a development project initiated by the ELISA consortium
00020         [alize.univ-avignon.fr/] and funded by the French Research 
00021         Ministry in the framework of the TECHNOLANGUE program 
00022         [www.technolangue.net]
00023 
00024         The ALIZE project team wants to highlight the limits of voice
00025         authentication in a forensic context.
00026         The "Person  Authentification by Voice: A Need of Caution" paper 
00027         proposes a good overview of this point (cf. "Person  
00028         Authentification by Voice: A Need of Caution", Bonastre J.F., 
00029         Bimbot F., Boe L.J., Campbell J.P., Douglas D.A., Magrin-
00030         chagnolleau I., Eurospeech 2003, Genova].
00031         The conclusion of the paper of the paper is proposed bellow:
00032         [Currently, it is not possible to completely determine whether the 
00033         similarity between two recordings is due to the speaker or to other 
00034         factors, especially when: (a) the speaker does not cooperate, (b) there 
00035         is no control over recording equipment, (c) recording conditions are not 
00036         known, (d) one does not know whether the voice was disguised and, to a 
00037         lesser extent, (e) the linguistic content of the message is not 
00038         controlled. Caution and judgment must be exercised when applying speaker 
00039         recognition techniques, whether human or automatic, to account for these 
00040         uncontrolled factors. Under more constrained or calibrated situations, 
00041         or as an aid for investigative purposes, judicious application of these 
00042         techniques may be suitable, provided they are not considered as infallible.
00043         At the present time, there is no scientific process that enables one to 
00044         uniquely characterize a person=92s voice or to identify with absolute 
00045         certainty an individual from his or her voice.]
00046         Contact Jean-Francois Bonastre for more information about the licence or
00047         the use of ALIZE
00048 
00049         Copyright (C) 2003-2010
00050         Laboratoire d'informatique d'Avignon [lia.univ-avignon.fr]
00051         ALIZE admin [alize@univ-avignon.fr]
00052         Jean-Francois Bonastre [jean-francois.bonastre@univ-avignon.fr]
00053 */
00054 
00055 #if !defined(ALIZE_Feature_cpp)
00056 #define ALIZE_Feature_cpp
00057 
00058 #include <new>
00059 #include <memory.h>
00060 #include "Feature.h"
00061 #include "alizeString.h"
00062 #include "Exception.h"
00063 #include "Config.h"
00064 #include "ULongVector.h"
00065 
00066 using namespace alize;
00067 
00068 //-------------------------------------------------------------------------
00069 Feature::Feature(unsigned long vectSize)
00070 :Object(), _vectSize(vectSize), _isValid(false), _labelCode(0),
00071  _dataVector(createVector())
00072 {
00073   //for (unsigned long i=0; i<_vectSize; i++)
00074   //  _dataVector[i] = 0;
00075 }
00076 //-------------------------------------------------------------------------
00077 Feature::Feature(const Config& config)
00078 :Object(), _vectSize(config.getParam_vectSize()), _isValid(false),
00079  _labelCode(0), _dataVector(createVector())
00080 {
00081   //for (unsigned long i=0; i<_vectSize; i++)
00082   //  _dataVector[i] = 0;
00083 }
00084 //-------------------------------------------------------------------------
00085 Feature& Feature::create(unsigned long vectSize)
00086 {
00087   Feature* p = new (std::nothrow) Feature(vectSize);
00088   assertMemoryIsAllocated(p, __FILE__, __LINE__);
00089   return *p;
00090 }
00091 //-------------------------------------------------------------------------
00092 Feature::Feature(const Feature& f)
00093 :Object(), _vectSize(f._vectSize), _isValid(f._isValid),
00094  _labelCode(f._labelCode), _dataVector(createVector())
00095 {
00096   memcpy(_dataVector, f._dataVector, _vectSize*sizeof(_dataVector[0]));
00097 }
00098 //-------------------------------------------------------------------------
00099 const Feature& Feature::operator=(const Feature& f)
00100 {
00101   if (_vectSize != f._vectSize)
00102     throw Exception("Feature copy : source vectSize ("
00103         + String::valueOf(f._vectSize)
00104         + ") does not match target vectSize ("
00105         + String::valueOf(_vectSize) + ")", __FILE__, __LINE__);
00106   memcpy(_dataVector, f._dataVector, _vectSize*sizeof(_dataVector[0]));
00107   _isValid   = f._isValid;
00108   _labelCode = f._labelCode;
00109   return *this;
00110 }
00111 //-------------------------------------------------------------------------
00112 bool Feature::operator==(const Feature& f) const
00113 {
00114   if (_vectSize != f._vectSize || _isValid != f._isValid)
00115     return false;
00116   for (unsigned long i=0; i<_vectSize; i++)
00117     if (_dataVector[i] != f._dataVector[i])
00118       return false;
00119   return true;
00120 }  
00121 //-------------------------------------------------------------------------
00122 bool Feature::operator!=(const Feature& c) const { return !(*this == c); }
00123 //-------------------------------------------------------------------------
00124 Feature::data_t* Feature::createVector() const // private
00125 {
00126   data_t* p = new (std::nothrow) data_t[_vectSize==0?1:_vectSize];
00127   assertMemoryIsAllocated(p, __FILE__, __LINE__);
00128   return p;
00129 }
00130 //-------------------------------------------------------------------------
00131 void Feature::reset()
00132 {
00133   for (unsigned long i=0; i<_vectSize; i++)
00134   {
00135     _dataVector[i] = 0.0;
00136   }
00137   _labelCode = 0;
00138   _isValid   = false;
00139 }
00140 //-------------------------------------------------------------------------
00141 bool Feature::isValid() const { return _isValid; }
00142 //-------------------------------------------------------------------------
00143 void Feature::setValidity(bool v) { _isValid = v; }
00144 //-------------------------------------------------------------------------
00145 unsigned long Feature::getLabelCode() const { return _labelCode; }
00146 //-------------------------------------------------------------------------
00147 void Feature::setLabelCode(unsigned long v) { _labelCode = v; }
00148 //-------------------------------------------------------------------------
00149 Feature::data_t& Feature::operator[](unsigned long i)
00150 {
00151   assertIsInBounds(__FILE__, __LINE__, i, _vectSize);
00152   assert(_dataVector != NULL);
00153   return _dataVector[i];
00154 }
00155 //-------------------------------------------------------------------------
00156 Feature::data_t Feature::operator[](unsigned long i) const
00157 {
00158   assertIsInBounds(__FILE__, __LINE__, i, _vectSize);
00159   assert(_dataVector != NULL);
00160   return _dataVector[i];
00161 }
00162 //-------------------------------------------------------------------------
00163 unsigned long Feature::getVectSize() const { return _vectSize; }
00164 //-------------------------------------------------------------------------
00165 void Feature::setVectSize(const K&, unsigned long s)
00166 {
00167   if (s != _vectSize)
00168   {
00169     // all data are lost
00170     assert(_dataVector != NULL);
00171     delete [] _dataVector;
00172     _vectSize = s;
00173     _dataVector = createVector();
00174   }
00175 }
00176 //-------------------------------------------------------------------------
00177 void Feature::setData(const FloatVector& v, unsigned long start)
00178 {
00179   if (_vectSize + start> v.size())
00180     throw Exception("Lack of data to fill the feature", __FILE__, __LINE__);
00181   const float* source = v.getArray();
00182   for (unsigned long i=0; i<_vectSize; i++)
00183     _dataVector[i] = (data_t)source[i+start];
00184 }
00185 //-------------------------------------------------------------------------
00186 void Feature::setData(const DoubleVector& v, unsigned long start)
00187 {
00188   if (_vectSize + start> v.size())
00189     throw Exception("Lack of data to fill the feature", __FILE__, __LINE__);
00190   const double* source = v.getArray();
00191   for (unsigned long i=0; i<_vectSize; i++)
00192     _dataVector[i] = (data_t)source[i+start];
00193 }
00194 //-------------------------------------------------------------------------
00195 void Feature::copySelectedData(const Feature& f, const ULongVector& selection)
00196 {
00197   const unsigned long selectionSize = selection.size();
00198   if (selectionSize > _vectSize)
00199       throw Exception("", __FILE__, __LINE__);
00200   const unsigned long sourceSize = f._vectSize;
00201   unsigned long* selectionVect = selection.getArray();
00202   double* sourceVect = f._dataVector;
00203   unsigned long sourceIdx;
00204   for (unsigned long i=0; i<selectionSize; i++)
00205   {
00206     if ( (sourceIdx = selectionVect[i]) >= sourceSize)
00207       throw Exception("", __FILE__, __LINE__);
00208     _dataVector[i] = sourceVect[sourceIdx];
00209   }
00210   // copy validity and labelCode too
00211   _labelCode = f._labelCode;
00212   _isValid = f._isValid;
00213 }
00214 //-------------------------------------------------------------------------
00215 Feature::data_t* Feature::getDataVector() const { return _dataVector; }
00216 //-------------------------------------------------------------------------
00217 String Feature::getClassName() const { return "Feature"; }
00218 //-------------------------------------------------------------------------
00219 String Feature::toString() const
00220 {
00221   String s = Object::toString()
00222     + "\n  vectSize   = " + String::valueOf(_vectSize)
00223     + "\n  label code = " + String::valueOf(_labelCode)
00224     + "\n  is valid   = " + String::valueOf(_isValid);
00225   for (unsigned long i=0; i<_vectSize; i++)
00226     s += "\n  data[" + String::valueOf(i) + "] = "
00227       + String::valueOf(operator[](i)); 
00228   return s;
00229 }
00230 //-------------------------------------------------------------------------
00231 Feature::~Feature()
00232 {
00233   assert(_dataVector != NULL);
00234   delete[] _dataVector;
00235 }
00236 //-------------------------------------------------------------------------
00237 
00238 #endif // !defined(ALIZE_Feature_cpp)
00239