DoubleSquareMatrix.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_DoubleSquareMatrix_cpp)
00056 #define ALIZE_DoubleSquareMatrix_cpp
00057 
00058 #include <new>
00059 #include <math.h>
00060 #include <memory.h>
00061 #include <cstdlib>
00062 #include "DoubleSquareMatrix.h"
00063 #include "alizeString.h"
00064 #include "Exception.h"
00065 
00066 using namespace alize;
00067 typedef DoubleSquareMatrix M;
00068 
00069 //-------------------------------------------------------------------------
00070 M::DoubleSquareMatrix(unsigned long size)
00071 :Object(), _size(size), _array(size*size, size*size) {}
00072 //-------------------------------------------------------------------------
00073 M::DoubleSquareMatrix(const DoubleSquareMatrix& v)
00074 :Object(), _size(v._size), _array(v._array) {}
00075 //-------------------------------------------------------------------------
00076 const DoubleSquareMatrix& M::operator=(const DoubleSquareMatrix& v)
00077 {
00078   _array = v._array;
00079   _size = v._size;
00080   return *this;
00081 }
00082 //-------------------------------------------------------------------------
00083 bool M::operator==(const DoubleSquareMatrix& v) const
00084 { return (_array == v._array); }
00085 //-------------------------------------------------------------------------
00086 bool M::operator!=(const DoubleSquareMatrix& v) const
00087 { return !(*this == v); }
00088 //-------------------------------------------------------------------------
00089 M::_type& M::operator()(unsigned long i, unsigned long j)
00090 {
00091   assertIsInBounds(__FILE__, __LINE__, i, _size);
00092   assertIsInBounds(__FILE__, __LINE__, j, _size);
00093   return _array[i+j*_size];
00094 }
00095 //-------------------------------------------------------------------------
00096 M::_type M::operator()(unsigned long i, unsigned long j) const
00097 {
00098   assertIsInBounds(__FILE__, __LINE__, i, _size);
00099   assertIsInBounds(__FILE__, __LINE__, j, _size);
00100   return _array[i+j*_size];
00101 }
00102 //-------------------------------------------------------------------------
00103 void M::setSize(const unsigned long size, bool saveMemory)
00104 {
00105   _size = size;
00106   _array.setSize(size*size, saveMemory);
00107 }
00108 //-------------------------------------------------------------------------
00109 real_t M::invert(DoubleSquareMatrix& m)
00110 {
00111   long size = m._size; // unsigned long -> long
00112   if (size == 0)
00113     throw Exception("Cannot invert matrix : dimension = 0",__FILE__, __LINE__);
00114   if (size !=_size)
00115           throw Exception("Cannot return the invert matrix : dimension not compatible",__FILE__, __LINE__);
00116 
00117 
00118   long j, k;
00119   real_t det;
00120 
00121   DoubleVector diag(_size, _size);
00122   DoubleVector b(_size, _size);
00123   DoubleSquareMatrix tmp(*this);
00124   real_t* pMatrix = m.getArray();
00125   real_t* pTmp = tmp.getArray();
00126   real_t* pDiag = diag.getArray();
00127   real_t* pB = b.getArray();
00128 
00129   // Cholesky decomposition: A = L*trans(L)
00130   choleskyDecomp(pTmp, pDiag, _size);
00131   
00132   det = pDiag[0]*pDiag[0];
00133   for (k=1; k<size; k++)
00134     det *= pDiag[k]*pDiag[k];
00135 
00136   // Solve inverse of the matrix by forward and backward substitution.
00137   // The right hand side is a unit matrix, the solution is thus the
00138   // inverse of the matrix.
00139   for (j=0; j<size; j++)
00140   {
00141     // one column at a time
00142     for (k=0; k<size; k++)
00143       pB[k] = 0.0;
00144     pB[j] = 1.0;
00145     choleskySolve(pTmp, pDiag, pB, size);
00146     unsigned long jsize = j*size;
00147     for (k=0; k<size; k++)
00148        pMatrix[k + jsize] = pB[k];
00149   }
00150   return det;
00151 }
00152 //-------------------------------------------------------------------------
00153 void M::choleskyDecomp(real_t* pMatrix, real_t* pDiag, long n) // static private
00154 {
00155   // This function performs Cholesky decomposition.
00156   // A is a positive-definite symmetric matrix. Only the upper triangle of
00157   // A is needed on input. On output, the lower triangle of A contains the
00158   // Cholesky factor L.  The diagonal elements of L are returned in vector
00159   // diag.
00160 
00161   long i, j, k;
00162   real_t sum;
00163 
00164   // Cholesky decompose A = L*trans(L)
00165   for (i=0; i<n; i++)
00166   {
00167     unsigned long in = i*n;
00168     for(j=i; j<n; j++)
00169     {
00170       sum = pMatrix[i + j*n];
00171       for (k=(i-1)*n; k>=0; k-=n)
00172         sum -= pMatrix[i + k] * pMatrix[j + k];
00173       if (i == j)
00174       {
00175         if (sum < 0.0)
00176                 throw Exception("Matrix is not positive definite",
00177                   __FILE__, __LINE__);
00178         pDiag[i] = sqrt(sum);
00179       }
00180       else
00181               pMatrix[j + in] = sum/pDiag[i];
00182     }
00183   }
00184 }
00185 //-------------------------------------------------------------------------
00186 void M::choleskySolve(real_t* pMatrix, real_t* pDiag, real_t* pB, long n) // static private
00187 {
00188   // Solve linear equation A*x = b, where A positive-definite symmetric.
00189   // On input, A contains Cholesky factor L in its low triangle except the
00190   // diagonal elements which are in vector diag.  On return x contains the
00191   // solution.  b and x can be the same vector to save memory space.
00192 
00193   int i, k;
00194   real_t sum;
00195 
00196   // solve by forward and backward substitution.  
00197   // L*y = b
00198   for (i=0; i<n; i++)
00199   {
00200     sum = pB[i];
00201     for (k=i-1; k>=0; k--)
00202       sum -= pMatrix[i + k*n] * pB[k];
00203     pB[i] = sum / pDiag[i];
00204   }
00205   // trans(L)*x = y
00206   for (i=n-1; i>=0; i--)
00207   {
00208     sum = pB[i];
00209     for (k=i+1; k<n; k++)
00210       sum -= pMatrix[k + i*n] * pB[k];
00211     pB[i] = sum / pDiag[i];
00212   }
00213 }
00214 //-------------------------------------------------------------------------
00215 void M::setAllValues(_type v) { _array.setAllValues(v); }
00216 //-------------------------------------------------------------------------
00217 M::_type* M::getArray() const { return _array.getArray(); }
00218 //-------------------------------------------------------------------------
00219 unsigned long M::size() const { return _size; }
00220 //-------------------------------------------------------------------------
00221 String M::getClassName() const { return "DoubleSquareMatrix"; }
00222 //-------------------------------------------------------------------------
00223 String M::toString() const
00224 {
00225   String s = Object::toString()
00226     + "\n  size  = " + String::valueOf(_size)+"x"+String::valueOf(_size);
00227   for (unsigned long i=0; i<_size; i++)
00228   {
00229     for (unsigned long j=0; j<_size; j++)
00230       s += "\n  [" + String::valueOf(j)
00231         + "," + String::valueOf(i)
00232         + "] = " + String::valueOf((*this)(j, i));
00233     s += "\n";
00234   }
00235   return s;
00236 }
00237 //-------------------------------------------------------------------------
00238 M::~DoubleSquareMatrix() {}
00239 //-------------------------------------------------------------------------
00240 
00241 #endif  // ALIZE_DoubleSquareMatrix_cpp