RealVector.h

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_RealVector_h)
00056 #define ALIZE_RealVector_h
00057 
00058 #if defined(_WIN32)
00059 #if defined(ALIZE_EXPORTS)
00060 #define ALIZE_API __declspec(dllexport)
00061 #else
00062 #define ALIZE_API __declspec(dllimport)
00063 #endif
00064 #else
00065 #define ALIZE_API
00066 #endif
00067 
00068 #include <new>
00069 #include <math.h>
00070 #include <memory.h>
00071 #include <cstdlib>
00072 #include "alizeString.h"
00073 #include "Exception.h"
00074 
00075 namespace alize
00076 {
00082 
00083   template <class T> class ALIZE_API RealVector : public Object
00084   {
00085           ;
00086 
00087     friend class TestDoubleVector;
00088     friend class TestFloatVector;
00089     friend class TestRealVector;
00090 
00091   public:
00092   
00099     explicit RealVector(unsigned long capacity = 0, unsigned long size = 0)
00100     :Object(), _size(0), _capacity(capacity!=0?capacity:1), 
00101     _array(createArray())
00102     {
00103       setSize(size);
00104     }
00105 
00106     RealVector(const RealVector<T>& v)
00107     :Object(), _size(v._size), _capacity(v._size!=0?v._size:1),
00108     _array(createArray())
00109     {
00110       memcpy(_array, v._array, _size*sizeof(_array[0]));
00111     }
00112 
00113     static RealVector<T>& create(unsigned long capacity = 0,
00114       unsigned long size = 0)
00115     {
00116       RealVector<T>* p = new (std::nothrow) RealVector<T>(capacity, size);
00117       assertMemoryIsAllocated(p, __FILE__, __LINE__);
00118       return *p;
00119     }
00120 
00121     const RealVector<T>& operator=(const RealVector<T>& v)
00122     {
00123       if (this->isSameObject(v))
00124         return v;
00125       assert(_array != NULL);
00126       _size = v._size;
00127       if (_capacity < _size)
00128       {
00129         delete[] _array;
00130         _capacity = _size!=0?_size:1;
00131         _array = createArray();
00132       }
00133       memcpy(_array, v._array, _size*sizeof(_array[0]));
00134       return *this;
00135     }
00136 
00137     const RealVector<T>& operator+=(const RealVector<T>& v)
00138     {
00139       if (_size != v._size)
00140         throw Exception("Mismatch vector sizes", __FILE__, __LINE__);
00141       for (unsigned long i=0; i<_size; i++)
00142         _array[i] += v._array[i];
00143       return *this;
00144     }
00145     const RealVector<T>& operator-=(const RealVector<T>& v)
00146     {
00147       if (_size != v._size)
00148         throw Exception("Mismatch vector sizes", __FILE__, __LINE__);
00149       for (unsigned long i=0; i<_size; i++)
00150         _array[i] -= v._array[i];
00151       return *this;
00152     }
00153 
00154     bool operator==(const RealVector<T>& v) const
00155     {
00156       if (_size != v._size)
00157         return false;
00158       for (unsigned long i=0; i<_size; i++)
00159       {
00160         if (_array[i] != v._array[i])
00161           return false;
00162       }
00163       return true;
00164     }
00165 
00166     bool operator!=(const RealVector<T>& v) const
00167     {
00168       return !(*this == v);
00169     }
00170 
00171     virtual ~RealVector()
00172     {
00173       delete[] _array;
00174     }
00175 
00176     unsigned long size() const
00177     {
00178       return _size;
00179     }
00180 
00181     void clear()
00182     {
00183       _size = 0;
00184     }
00185   
00191     void setSize(const unsigned long size,
00192                  const bool updateCapacity = false)
00193     {
00194       assert(_array != NULL);
00195       if ((size > _capacity) || (size < _capacity && updateCapacity))
00196       {
00197         unsigned long oldSize = _size;
00198         _size = size;
00199         _capacity = _size;
00200         if (_capacity == 0)
00201           _capacity = 1;
00202         T* oldArray = _array;
00203         _array = createArray(); // can throw OutOfMemoryException
00204         memcpy(_array, oldArray, (size>oldSize?oldSize:size)*sizeof(_array[0]));
00205         delete[] oldArray;
00206         //for (unsigned long i=oldSize; i<_size; i++)
00207         //  _array[i] = 0.0;
00208       }
00209       else
00210         _size = size;
00211     }
00212 
00216     void addValue(T v)
00217     {
00218       assert(_array != NULL);
00219       if (_size == _capacity)
00220       {
00221         _capacity += _capacity;
00222         T* oldArray = _array;
00223         _array = createArray(); // can throw OutOfMemoryException
00224         memcpy(_array, oldArray, _size*sizeof(_array[0]));
00225         delete[] oldArray;
00226       }
00227       _array[_size] = v;
00228       _size++;
00229     }
00230   
00234     void addValue(const RealVector<T>& v)
00235     {
00236       for (unsigned long i=0; i<v._size; i++)
00237       {
00238         addValue(v._array[i]);
00239       }
00240       // TODO : methode a optimiser
00241     }
00242 
00247     void setValues(const RealVector<T>& v)
00248     {
00249       if (_size != v._size)
00250       throw Exception("Cannot set values : vector size mismatch ",
00251                        __FILE__, __LINE__);
00252       memcpy(_array, v._array, _size*sizeof(_array[0]));
00253     }
00254   
00258     void setAllValues(T v)
00259     {
00260       for (unsigned long i=0; i< _size; i++)
00261         _array[i] = v;
00262     }
00263 
00267     T computeSum() const
00268     {
00269        T s = 0.0;
00270       for (unsigned long i=0; i<_size; i++)
00271         s += _array[i];
00272       return s;
00273     }
00274 
00279     const RealVector<T>& operator*=(double s)
00280     {
00281       for (unsigned long i=0; i<_size; i++)
00282         _array[i] *= (T)s;
00283        return *this;
00284     }
00285     
00291     T& operator[](unsigned long i)
00292     {
00293       assertIsInBounds(__FILE__, __LINE__, i, _size);
00294       assert(_array != NULL);
00295       return _array[i];
00296     }
00297 
00300     T  operator[](unsigned long i) const
00301     {
00302       assertIsInBounds(__FILE__, __LINE__, i, _size);
00303       assert(_array != NULL);
00304       return _array[i];
00305     }
00306 
00309     void ascendingSort() const
00310     {
00311       assert(_array != NULL);
00312       qsort(_array, _size, sizeof(T), compare);
00313     }
00314 
00319     unsigned long getIndexOfLargestValue() //const
00320     {
00321       if (_size == 0)
00322         throw Exception("Empty vector : cannot find the largest value", __FILE__, __LINE__);
00323       unsigned long i, maxI = 0;
00324       T v = _array[maxI];
00325 
00326       for (i=1; i<_size; i++)
00327         if (_array[i] > v)
00328         {
00329           maxI = i;
00330           v = _array[maxI];
00331         }
00332       return maxI;
00333     }
00334 
00339     T* getArray() const
00340     {
00341       return _array;
00342     }
00343 
00344     virtual String getClassName() const
00345     {
00346       return "RealVector";
00347     }
00348 
00349     virtual String toString() const
00350     {
00351       String s = Object::toString()
00352         + "\n  size = " + String::valueOf(size());
00353       for (unsigned long i=0; i<size(); i++)
00354         s += "\n  value[" + String::valueOf(i) + "] = "
00355           + String::valueOf(operator[](i)); 
00356       return s;
00357     }
00358 
00359   private:
00360 
00361     unsigned long _size;
00362     unsigned long _capacity;
00363     T*            _array;
00364 
00365     T* createArray() const
00366     {
00367       assert(_capacity != 0);
00368       T* p = new (std::nothrow) T[_capacity];
00369       assertMemoryIsAllocated(p, __FILE__, __LINE__);
00370       return p;
00371     }
00372     static int compare(const void* s1, const void* s2)
00373     {
00374       if (*((T*)s1) > *((T*)s2))
00375         return 1;
00376       if (*((T*)s1) < *((T*)s2))
00377         return -1;
00378       return 0;
00379     }
00380 
00381         
00382   };
00383 
00384   typedef RealVector<double> DoubleVector;
00385   typedef RealVector<float> FloatVector;
00386 
00387 #if defined(_WIN32)
00388   template class RealVector<double>;
00389   template class RealVector<float>;
00390 #endif
00391 
00392 } // end namespace alize
00393 
00394 #endif  // ALIZE_RealVector_h