/*---------------------------------------------------------------------------* * IT++ * *---------------------------------------------------------------------------* * Copyright (c) 1995-2003 by Tony Ottosson, Thomas Eriksson, Pål Frenger, * * Tobias Ringström, and Jonas Samuelsson. * * * * Permission to use, copy, modify, and distribute this software and its * * documentation under the terms of the GNU General Public License is hereby * * granted. No representations are made about the suitability of this * * software for any purpose. It is provided "as is" without expressed or * * implied warranty. See the GNU General Public License for more details. * *---------------------------------------------------------------------------*/ /*! \file \brief Vector quantizer training \author Thomas Eriksson */ #include "srccode/vqtrain.h" #include "base/random.h" #include "base/timing.h" #include "base/elmatfunc.h" #include "base/stat.h" #include "base/matfunc.h" namespace itpp { // the cols contains codevectors double kmeansiter(Array &DB, mat &codebook) { int DIM=DB(0).length(),SIZE=codebook.cols(),T=DB.length(); vec x,xnum(SIZE); mat xsum(DIM,SIZE); int n,MinIndex,i,j,k; double MinS,S,D,Dold,*xp,*cp; xsum.clear(); xnum.clear(); n=0; D=1E20; Dold=D; D=0; for (k=0;k=MinS) goto sune; } MinS=S; MinIndex=i; sune: i=i; } D+=MinS; cp=&xsum(0,MinIndex); for (j=0;j &DB, int SIZE, int NOITER, bool VERBOSE) { int DIM=DB(0).length(),T=DB.length(); mat codebook(DIM,SIZE); int n,i,j; double D,Dold; ivec ind(SIZE); for (i=0;i &DB, int SIZE, int NOITER, bool VERBOSE) { int S=1,DIM=DB(0).length(),T=DB.length(),i,n; mat cb; vec delta=0.001*randn(DIM),x; double D,Dold; x=zeros(DIM); for (i=0;i &DB, int SIZE, int NOITER, double STARTSTEP, bool VERBOSE) { int DIM=DB(0).length(); vec x; vec codebook(DIM*SIZE); int n,MinIndex,i,j; double MinS,S,D,step,*xp,*cp; for (i=0;i=MinS) goto sune; } MinS=S; MinIndex=i; sune: i=i; } D+=MinS; cp=&codebook(MinIndex*DIM); for (j=0;j1)) { if (VERBOSE) cout << n << ": " << D/20000 << " "; D=0; } } // checking training result vec dist(SIZE),num(SIZE); dist.clear();num.clear(); for (n=0;n=MinS) goto sune2; } MinS=S; MinIndex=i; sune2: i=i; } dist(MinIndex)+=MinS; num(MinIndex)+=1; } dist=10*log10(dist*length(dist)/sum(dist)); if (VERBOSE) cout << endl << "Distortion contribution: " << dist << endl ; if (VERBOSE) cout << "Num spread: " << num/DB.length()*100 << " %" << endl << endl ; if (min(dist)<-30) { cout << "Points without entries! Retraining" << endl ; j=min_index(dist); i=max_index(num); codebook.replace_mid(j*DIM,codebook.mid(i*DIM,DIM)); goto res; } mat cb(DIM,SIZE); for (i=0;i