LKVector.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_LKVector_cpp)
00056 #define ALIZE_LKVector_cpp
00057 
00058 #include <new>
00059 #include <math.h>
00060 #include <memory.h>
00061 #include <cstdlib>
00062 #include "LKVector.h"
00063 #include "alizeString.h"
00064 #include "Exception.h"
00065 
00066 using namespace alize;
00067 
00068 //-------------------------------------------------------------------------
00069 LKVector::LKVector(unsigned long capacity, unsigned long size)
00070 :Object(), _capacity(capacity!=0?capacity:1), _size(0),
00071 _array(createArray()) { setSize(size); }
00072 
00073 //-------------------------------------------------------------------------
00074 //LIUM, sylvain
00075 LKVector::LKVector(const LKVector& obj){
00076   _capacity = obj._capacity;
00077   _size = 0;
00078   if (_capacity > 0) {
00079           _array = createArray();
00080           //memcpy(_array, obj._array, _capacity*sizeof(_array[0]));
00081           for(unsigned long i = 0; i < obj._size; i++) {
00082                   addValue(obj[i]);
00083       }
00084   }
00085   sumNonTopDistribWeights = obj.sumNonTopDistribWeights;
00086   sumNonTopDistribLK = obj.sumNonTopDistribLK;
00087   topDistribsCount = obj.topDistribsCount;
00088 }
00089 
00090 //LIUM, sylvain
00091 const LKVector& LKVector::operator=(const LKVector& obj){
00092   _capacity = obj._capacity;
00093   _size = 0;
00094   if (_capacity > 0) {
00095           _array = createArray();
00096           for(unsigned long i = 0; i < obj._size; i++) {
00097                   addValue(obj[i]);
00098       }
00099   }
00100   sumNonTopDistribWeights = obj.sumNonTopDistribWeights;
00101   sumNonTopDistribLK = obj.sumNonTopDistribLK;
00102   topDistribsCount = obj.topDistribsCount;
00103   return *this;
00104 }
00105 //-------------------------------------------------------------------------
00106 LKVector::type* LKVector::createArray() const // private
00107 {
00108   assert(_capacity != 0);
00109   type* p = new (std::nothrow) type[_capacity];
00110   assertMemoryIsAllocated(p, __FILE__, __LINE__);
00111   return p;
00112 }
00113 //-------------------------------------------------------------------------
00114 LKVector::type& LKVector::operator[](unsigned long i)
00115 {
00116   assertIsInBounds(__FILE__, __LINE__, i, _size);
00117   assert(_array != NULL);
00118   return _array[i];
00119 }
00120 //-------------------------------------------------------------------------
00121 const LKVector::type& LKVector::operator[](unsigned long i) const
00122 {
00123   assertIsInBounds(__FILE__, __LINE__, i, _size);
00124   assert(_array != NULL);
00125   return _array[i];
00126 }
00127 //-------------------------------------------------------------------------
00128 void LKVector::addValue(type element)
00129 {
00130   assert(_array != NULL);
00131   if (_size == _capacity)
00132   {
00133     _capacity += _capacity;
00134     type* oldArray = _array;
00135     _array = createArray(); // can throw OutOfMemoryException
00136     memcpy(_array, oldArray, _size*sizeof(_array[0]));
00137     delete[] oldArray;
00138   }
00139   _array[_size] = element;
00140   _size++;
00141 }
00142 //-------------------------------------------------------------------------
00143 void LKVector::pack(unsigned long size)
00144 {
00145     unsigned long oldSize = _size;
00146     _size = size;
00147     _capacity = _size;
00148     type* oldArray = _array;
00149     _array = createArray(); // can throw OutOfMemoryException
00150         unsigned long s = oldSize;
00151         if (oldSize > size) {
00152                 s = size;
00153         }
00154     memcpy(_array, oldArray, s*sizeof(_array[0]));
00155     delete[] oldArray;
00156 }
00157 //-------------------------------------------------------------------------
00158 void LKVector::setSize(unsigned long size)
00159 {
00160   assert(_array != NULL);
00161   if (size > _capacity)
00162   {
00163     unsigned long oldSize = _size;
00164     _size = size;
00165     _capacity = _size;
00166     type* oldArray = _array;
00167     _array = createArray(); // can throw OutOfMemoryException
00168     memcpy(_array, oldArray, oldSize*sizeof(_array[0]));
00169     delete[] oldArray;
00170   }
00171   else
00172     _size = size;
00173 }
00174 //-------------------------------------------------------------------------
00175 int LKVector::compare(const void *s1, const void *s2) // static method
00176 {
00177   if (((type*)s1)->lk > ((type*)s2)->lk)
00178     return -1;
00179   if (((type*)s1)->lk < ((type*)s2)->lk)
00180     return 1;
00181   return 0;
00182 }
00183 //-------------------------------------------------------------------------
00184 void LKVector::descendingSort() const
00185 {
00186   assert(_array != NULL);
00187   qsort(_array, _size, sizeof(type), compare);
00188 }
00189 //-------------------------------------------------------------------------
00190 LKVector::type* LKVector::getArray() const { return _array; }
00191 //-------------------------------------------------------------------------
00192 void LKVector::clear() { _size = 0; }
00193 //-------------------------------------------------------------------------
00194 unsigned long LKVector::size() const { return _size; }
00195 //-------------------------------------------------------------------------
00196 String LKVector::getClassName() const { return "LKVector"; }
00197 //-------------------------------------------------------------------------
00198 String LKVector::toString() const
00199 {
00200   String s = Object::toString() + "Size = " + String::valueOf(_size);
00201   for (unsigned long i=0; i<_size; i++)
00202   {
00203     s += "\n  idx = " + String::valueOf(_array[i].idx)
00204       +  " lk = " + String::valueOf(_array[i].lk);
00205   }
00206 
00207   return s;
00208 }
00209 //-------------------------------------------------------------------------
00210 LKVector::~LKVector() { delete[] _array; }
00211 //-------------------------------------------------------------------------
00212 
00213 #endif  // ALIZE_LKVector_cpp