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 //-------------------------------------------------------------------- 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: 00085 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(); 00099 MinS += 1.0E30F; 00100 MinIndex.clear(); 00101 for (i = 0;i < Size;i++) { 00102 S = 0; 00103 for (j = 0;j < Dim;j++) { 00104 a = x._elem(j) - CodeBook._elem(pos + j); 00105 S += a * a; 00106 if (S >= MinS[num-1]) goto sune; 00107 } 00108 for (index = num - 2;(index >= 0) && (S < MinS[index]);index--); 00109 for (j = MinS.length() - 2;j > index;j--) { 00110 MinS[j+1] = MinS[j];// memcpy, memmov 00111 MinIndex[j+1] = MinIndex[j]; 00112 } 00113 MinS[index+1] = S; 00114 MinIndex[index+1] = i; 00115 sune: 00116 pos += Dim; 00117 } 00118 LatestDist = MinS[0]; 00119 return MinIndex; 00120 } 00121 00122 Array<vec> Vector_Quantizer::decode(const ivec &Index) const 00123 { 00124 Array<vec> Temp(Index.length()); 00125 00126 for (int i = 0;i < Temp.length();i++) { 00127 Temp(i) = get_codevector(Index(i)); 00128 } 00129 return Temp; 00130 } 00131 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 } 00144 else { 00145 v.set_length(50); 00146 ifs.getline(str, 2000); 00147 if (strlen(str) == 0) ifs.getline(str, 2000); 00148 i = 0; 00149 v[i++] = atof(str); 00150 ptr = str; 00151 ptr_old = ptr; 00152 ptr = strchr(ptr, ' '); 00153 while (ptr == ptr_old) { 00154 ptr++; 00155 ptr_old = ptr; 00156 ptr = strchr(ptr, ' '); 00157 } 00158 while (ptr) { 00159 if (i >= v.length()) v.set_length(2*v.length(), true); 00160 v[i++] = atof(ptr); 00161 00162 ptr_old = ptr; 00163 ptr = strchr(ptr, ' '); 00164 while (ptr == ptr_old) { 00165 ptr++; 00166 ptr_old = ptr; 00167 ptr = strchr(ptr, ' '); 00168 } 00169 } 00170 flag = true; 00171 flag = false; 00172 v.set_length(i, true); 00173 } 00174 return ifs; 00175 } 00176 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 ; 00188 cout.flush() ; 00189 CodeBookFile >> v ; 00190 d = length(v); 00191 Temp.set_length(d*16); 00192 n = 0; 00193 while (!CodeBookFile.eof()) { 00194 if (n*d >= Temp.length()) Temp.set_length(2*Temp.length(), true); 00195 Temp.replace_mid(n*d, v); 00196 n++; 00197 CodeBookFile >> v ; 00198 } 00199 Size = n; 00200 Dim = d; 00201 CodeBook.set_length(Size*Dim); 00202 for (n = 0;n < CodeBook.length();n++) CodeBook(n) = Temp(n); 00203 cout << " size:" << size() << " dim:" << dim() << endl ; 00204 } 00205 00206 void Vector_Quantizer::save(const char *Name) const 00207 { 00208 ofstream CodeBookFile(Name); 00209 00210 cout << "Saving the codebook " << Name << endl ; 00211 for (int i = 0;i < Size;i++) { 00212 vec v = CodeBook.mid(i * Dim, Dim); 00213 for (int j = 0;j < v.length();j++) { 00214 CodeBookFile.operator << (v[j]); 00215 if (j < v.length() - 1) CodeBookFile.put(' ') ; 00216 } 00217 CodeBookFile << endl ; 00218 } 00219 CodeBookFile.close(); 00220 } 00221 00222 void Vector_Quantizer::modify_codevector(int no, double mul, const vec &add) 00223 { 00224 int pos = Dim * no; 00225 00226 for (int i = 0;i < Dim;i++) { 00227 CodeBook._elem(pos + i) *= mul; 00228 CodeBook._elem(pos + i) += add[i]; 00229 } 00230 } 00231 00232 vec Vector_Quantizer::get_codevector(int Index) const 00233 { 00234 return CodeBook.mid(Index*Dim, Dim); 00235 } 00236 00237 void Vector_Quantizer::set_codevector(int Index, const vec &v) 00238 { 00239 it_error_if(Dim != length(v), "Vector_Quantizer::set_codevector : Wrong dimension"); 00240 for (int i = 0;i < length(v);i++) { 00241 CodeBook._elem(Index*Dim + i) = v._elem(i); 00242 } 00243 } 00244 00245 void Vector_Quantizer::set_codebook(const mat &CB) 00246 { 00247 Size = CB.cols(); 00248 Dim = CB.rows(); 00249 CodeBook.set_length(Size*Dim); 00250 for (int i = 0;i < Size;i++) { 00251 for (int j = 0;j < Dim;j++) { 00252 CodeBook(i*Dim + j) = CB(j, i); 00253 } 00254 } 00255 } 00256 00257 mat Vector_Quantizer::get_codebook() const 00258 { 00259 mat CB(Dim, Size); 00260 00261 for (int i = 0;i < Size;i++) { 00262 for (int j = 0;i < Dim;i++) { 00263 CB(j, i) = CodeBook(i * Dim + j); 00264 } 00265 } 00266 return CB; 00267 } 00268 00269 //-------------------------------------------------------------------- 00270 // class SQ 00271 //-------------------------------------------------------------------- 00272 00273 Scalar_Quantizer::Scalar_Quantizer() 00274 { 00275 } 00276 00277 // SQ(const char *Name); 00278 00279 int Scalar_Quantizer::encode(double x) const 00280 { 00281 int il = 0, ih = Levels.length() - 1, im; 00282 00283 while (il < ih - 1) { 00284 im = (il + ih) / 2; 00285 if (x < Levels(im)) ih = im; 00286 else il = im; 00287 } 00288 if (Levels(ih) - x < x - Levels(il)) return ih; 00289 else return il; 00290 } 00291 00292 ivec Scalar_Quantizer::encode(const vec &x) const 00293 { 00294 int i; 00295 ivec Index(x.length()); 00296 00297 for (i = 0;i < x.length();i++) { 00298 Index(i) = encode(x(i)); 00299 } 00300 return Index; 00301 } 00302 00303 vec Scalar_Quantizer::decode(const ivec &Index) const 00304 { 00305 int i; 00306 vec y(Index.length()); 00307 00308 for (i = 0;i < Index.length();i++) { 00309 y(i) = decode(Index(i)); 00310 } 00311 return y; 00312 } 00313 00314 vec Scalar_Quantizer::Q(const vec &x) const 00315 { 00316 int i; 00317 vec y(x.length()); 00318 00319 for (i = 0;i < x.length();i++) { 00320 y(i) = Q(x(i)); 00321 } 00322 return y; 00323 } 00324 00325 // void load(const char *Name); 00326 // void save(const char *Name) const; 00327 00328 00329 //------------------------------------------------------------------------- 00330 00331 00332 int scalar_encode(double x, vec &Levels) 00333 { 00334 int il = 0, ih = Levels.length() - 1, im; 00335 00336 while (il < ih - 1) { 00337 im = (il + ih) / 2; 00338 if (x < Levels(im)) ih = im; 00339 else il = im; 00340 } 00341 if (Levels(ih) - x < x - Levels(il)) return ih; 00342 else return il; 00343 } 00344 00345 ivec scalar_encode(vec &x, vec &Levels) 00346 { 00347 ivec ind(x.length()); 00348 for (int i = 0;i < x.length();i++) ind(i) = scalar_encode(x(i), Levels); 00349 return ind; 00350 } 00351 00352 } // namespace itpp 00353
Generated on Thu Apr 23 20:06:44 2009 for IT++ by Doxygen 1.5.8