IT++ Logo

vq.cpp

Go to the documentation of this file.
00001 
00030 #include <itpp/srccode/vq.h>
00031 #include <itpp/base/array.h>
00032 #include <itpp/base/matfunc.h>
00033 #include <fstream>
00034 #include <iostream>
00035 #include <cstdlib>
00036 
00038 
00039 using std::ifstream;
00040 using std::ofstream;
00041 using std::cout;
00042 using std::endl;
00043 
00044 namespace itpp {
00045 
00046 //--------------------------------------------------------------------
00047 //    class VQ
00048 //--------------------------------------------------------------------
00049 
00050 Vector_Quantizer::Vector_Quantizer() : CodeBook()
00051 {
00052   LatestDist=0;
00053   Size=0;
00054   Dim=0;
00055 }
00056 
00057 Vector_Quantizer::Vector_Quantizer(const char *Name) : CodeBook()
00058 {
00059   LatestDist=0;
00060   Size=0;
00061   Dim=0;
00062   load(Name);
00063 }
00064 
00065 
00066 int Vector_Quantizer::encode(const vec &x)
00067 {
00068   int i;
00069   double  S,MinS=1.0E30F;
00070   int MinIndex=0;
00071   int j,pos=0;
00072   double  a;
00073 
00074   for (i=0;i<Size;i++) {
00075     S=0;
00076     for (j=0;j<Dim;j++) {
00077       a=x._elem(j)-CodeBook._elem(pos+j);
00078       S+=a*a;
00079       if (S>=MinS) goto sune;
00080     }
00081     MinS=S;
00082     MinIndex=i;
00083 sune:   pos+=Dim;
00084   }
00085   LatestDist=MinS;
00086   return MinIndex;
00087 }
00088 
00089 ivec Vector_Quantizer::encode(const vec &x, int num)
00090 {
00091   double  S,a;
00092   vec   MinS(num);
00093   ivec  MinIndex(num);
00094   int i,j,index,pos=0;
00095 
00096   MinS.clear();MinS+=1.0E30F;
00097   MinIndex.clear();
00098   for (i=0;i<Size;i++) {
00099     S=0;
00100     for (j=0;j<Dim;j++) {
00101       a=x._elem(j)-CodeBook._elem(pos+j);
00102       S+=a*a;
00103       if (S>=MinS[num-1]) goto sune;
00104     }
00105     for (index=num-2;(index>=0) && (S<MinS[index]);index--);
00106     for (j=MinS.length()-2;j>index;j--) {
00107       MinS[j+1]=MinS[j];// memcpy, memmov
00108       MinIndex[j+1]=MinIndex[j];
00109     }
00110     MinS[index+1]=S;
00111     MinIndex[index+1]=i;
00112 sune:   pos+=Dim;
00113   }
00114   LatestDist=MinS[0];
00115   return MinIndex;
00116 }
00117 
00118 Array<vec> Vector_Quantizer::decode(const ivec &Index) const
00119 {
00120   Array<vec>  Temp(Index.length());
00121 
00122   for (int i=0;i<Temp.length();i++) {
00123     Temp(i)=get_codevector(Index(i));
00124   }
00125   return Temp;
00126 }
00127 
00128 
00129 ifstream &operator>>( ifstream &ifs, vec &v)
00130 {
00131   int    i;
00132   char    str[2000];
00133   char    *ptr,*ptr_old;
00134   bool flag;
00135   if (length(v)!=0) {
00136     for (i=0;i<length(v);i++) {
00137       ifs.operator>>(v[i]) ;
00138     }
00139   } else {
00140     v.set_length(50);
00141     ifs.getline(str,2000);
00142     if (strlen(str)==0) ifs.getline(str,2000);
00143     i=0;
00144     v[i++]=atof(str);
00145     ptr=str;
00146     ptr_old=ptr;
00147     ptr=strchr(ptr,' ');
00148     while (ptr==ptr_old) {
00149       ptr++;
00150       ptr_old=ptr;
00151       ptr=strchr(ptr,' ');
00152     }
00153     while (ptr) {
00154       if (i>=v.length()) v.set_length(2*v.length(),true);
00155       v[i++]=atof(ptr);
00156 
00157       ptr_old=ptr;
00158       ptr=strchr(ptr,' ');
00159       while (ptr==ptr_old) {
00160         ptr++;
00161         ptr_old=ptr;
00162         ptr=strchr(ptr,' ');
00163       }
00164     }
00165     flag=true;
00166     flag=false;
00167     v.set_length(i,true);
00168   }
00169   return ifs;
00170 }
00171 
00172 
00173 void Vector_Quantizer::load(const char *Name)
00174 {
00175   vec     Temp;
00176   ifstream  CodeBookFile(Name);
00177   vec     v;
00178   int     n;
00179   int     d;
00180 
00181   it_error_if(!CodeBookFile,std::string("Vector_Quantizer::load : cannot open file ")+Name);
00182   cout << "Reading the codebook " << Name ; cout.flush() ;
00183   CodeBookFile >> v ;
00184   d=length(v);
00185   Temp.set_length(d*16);
00186   n=0;
00187   while (!CodeBookFile.eof()) {
00188     if (n*d>=Temp.length()) Temp.set_length(2*Temp.length(),true);
00189     Temp.replace_mid(n*d,v);n++;
00190     CodeBookFile >> v ;
00191   }
00192   Size=n;
00193   Dim=d;
00194   CodeBook.set_length(Size*Dim);
00195   for (n=0;n<CodeBook.length();n++) CodeBook(n)=Temp(n);
00196   cout << "  size:" << size() << "  dim:" << dim() << endl ;
00197 }
00198 
00199 void Vector_Quantizer::save(const char *Name) const
00200 {
00201   ofstream  CodeBookFile(Name);
00202 
00203   cout << "Saving the codebook " << Name << endl ;
00204   for (int i=0;i<Size;i++) {
00205     vec v=CodeBook.mid(i*Dim,Dim);
00206     for (int j=0;j<v.length();j++) {
00207       CodeBookFile.operator<<(v[j]);
00208       if (j<v.length()-1) CodeBookFile.put(' ') ;
00209     }
00210     CodeBookFile << endl ;
00211   }
00212   CodeBookFile.close();
00213 }
00214 
00215 void Vector_Quantizer::modify_codevector(int no, double mul, const vec &add)
00216 {
00217   int    pos=Dim*no;
00218 
00219   for (int i=0;i<Dim;i++) {
00220     CodeBook._elem(pos+i)*=mul;
00221     CodeBook._elem(pos+i)+=add[i];
00222   }
00223 }
00224 
00225 vec Vector_Quantizer::get_codevector(int Index) const
00226 {
00227   return CodeBook.mid(Index*Dim,Dim);
00228 }
00229 
00230 void Vector_Quantizer::set_codevector(int Index, const vec &v)
00231 {
00232   it_error_if(Dim!=length(v),"Vector_Quantizer::set_codevector : Wrong dimension");
00233   for (int i=0;i<length(v);i++) {
00234     CodeBook._elem(Index*Dim+i)=v._elem(i);
00235   }
00236 }
00237 
00238 void Vector_Quantizer::set_codebook(const mat &CB)
00239 {
00240   Size=CB.cols();
00241   Dim=CB.rows();
00242   CodeBook.set_length(Size*Dim);
00243   for (int i=0;i<Size;i++) {
00244     for (int j=0;j<Dim;j++) {
00245       CodeBook(i*Dim+j)=CB(j,i);
00246     }
00247   }
00248 }
00249 
00250 mat Vector_Quantizer::get_codebook() const
00251 {
00252   mat CB(Dim,Size);
00253 
00254   for (int i=0;i<Size;i++) {
00255     for (int j=0;i<Dim;i++) {
00256       CB(j,i)=CodeBook(i*Dim+j);
00257     }
00258   }
00259   return CB;
00260 }
00261 
00262 //--------------------------------------------------------------------
00263 //    class SQ
00264 //--------------------------------------------------------------------
00265 
00266 Scalar_Quantizer::Scalar_Quantizer()
00267 {
00268 }
00269 
00270 //  SQ(const char *Name);
00271 
00272 int Scalar_Quantizer::encode(double x) const
00273 {
00274    int il=0, ih=Levels.length()-1, im;
00275 
00276     while (il < ih-1) {
00277     im = (il + ih) / 2;
00278     if (x < Levels(im)) ih = im;
00279     else il = im;
00280   }
00281   if (Levels(ih)-x<x-Levels(il)) return ih;
00282   else return il;
00283 }
00284 
00285 ivec Scalar_Quantizer::encode(const vec &x) const
00286 {
00287   int   i;
00288   ivec  Index(x.length());
00289 
00290   for (i=0;i<x.length();i++) {
00291     Index(i)=encode(x(i));
00292   }
00293   return Index;
00294 }
00295 
00296 vec Scalar_Quantizer::decode(const ivec &Index) const
00297 {
00298   int i;
00299   vec y(Index.length());
00300 
00301   for (i=0;i<Index.length();i++) {
00302     y(i)=decode(Index(i));
00303   }
00304   return y;
00305 }
00306 
00307 vec Scalar_Quantizer::Q(const vec &x) const
00308 {
00309   int i;
00310   vec y(x.length());
00311 
00312   for (i=0;i<x.length();i++) {
00313     y(i)=Q(x(i));
00314   }
00315   return y;
00316 }
00317 
00318 //  void load(const char *Name);
00319 //  void save(const char *Name) const;
00320 
00321 
00322 //-------------------------------------------------------------------------
00323 
00324 
00325 int scalar_encode(double x, vec &Levels)
00326 {
00327    int il=0, ih=Levels.length()-1, im;
00328 
00329     while (il < ih-1) {
00330     im = (il + ih) / 2;
00331     if (x < Levels(im)) ih = im;
00332     else il = im;
00333   }
00334   if (Levels(ih)-x<x-Levels(il)) return ih;
00335   else return il;
00336 }
00337 
00338 ivec scalar_encode(vec &x, vec &Levels)
00339 {
00340   ivec  ind(x.length());
00341   for (int i=0;i<x.length();i++) ind(i)=scalar_encode(x(i),Levels);
00342   return ind;
00343 }
00344 
00345 } // namespace itpp
00346 
SourceForge Logo

Generated on Sun Dec 9 17:38:50 2007 for IT++ by Doxygen 1.5.4