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