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