00001 00029 #include <itpp/srccode/vq.h> 00030 #include <itpp/base/array.h> 00031 #include <itpp/base/matfunc.h> 00032 #include <fstream> 00033 #include <iostream> 00034 #include <cstdlib> 00035 00037 00038 using std::ifstream; 00039 using std::ofstream; 00040 using std::cout; 00041 using std::endl; 00042 00043 namespace itpp 00044 { 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: 00084 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(); 00098 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: 00115 pos += Dim; 00116 } 00117 LatestDist = MinS[0]; 00118 return MinIndex; 00119 } 00120 00121 Array<vec> Vector_Quantizer::decode(const ivec &Index) const 00122 { 00123 Array<vec> Temp(Index.length()); 00124 00125 for (int i = 0;i < Temp.length();i++) { 00126 Temp(i) = get_codevector(Index(i)); 00127 } 00128 return Temp; 00129 } 00130 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 } 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 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 ; 00187 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); 00195 n++; 00196 CodeBookFile >> v ; 00197 } 00198 Size = n; 00199 Dim = d; 00200 CodeBook.set_length(Size*Dim); 00201 for (n = 0;n < CodeBook.length();n++) CodeBook(n) = Temp(n); 00202 cout << " size:" << size() << " dim:" << dim() << endl ; 00203 } 00204 00205 void Vector_Quantizer::save(const char *Name) const 00206 { 00207 ofstream CodeBookFile(Name); 00208 00209 cout << "Saving the codebook " << Name << endl ; 00210 for (int i = 0;i < Size;i++) { 00211 vec v = CodeBook.mid(i * Dim, Dim); 00212 for (int j = 0;j < v.length();j++) { 00213 CodeBookFile.operator << (v[j]); 00214 if (j < v.length() - 1) CodeBookFile.put(' ') ; 00215 } 00216 CodeBookFile << endl ; 00217 } 00218 CodeBookFile.close(); 00219 } 00220 00221 void Vector_Quantizer::modify_codevector(int no, double mul, const vec &add) 00222 { 00223 int pos = Dim * no; 00224 00225 for (int i = 0;i < Dim;i++) { 00226 CodeBook._elem(pos + i) *= mul; 00227 CodeBook._elem(pos + i) += add[i]; 00228 } 00229 } 00230 00231 vec Vector_Quantizer::get_codevector(int Index) const 00232 { 00233 return CodeBook.mid(Index*Dim, Dim); 00234 } 00235 00236 void Vector_Quantizer::set_codevector(int Index, const vec &v) 00237 { 00238 it_error_if(Dim != length(v), "Vector_Quantizer::set_codevector : Wrong dimension"); 00239 for (int i = 0;i < length(v);i++) { 00240 CodeBook._elem(Index*Dim + i) = v._elem(i); 00241 } 00242 } 00243 00244 void Vector_Quantizer::set_codebook(const mat &CB) 00245 { 00246 Size = CB.cols(); 00247 Dim = CB.rows(); 00248 CodeBook.set_length(Size*Dim); 00249 for (int i = 0;i < Size;i++) { 00250 for (int j = 0;j < Dim;j++) { 00251 CodeBook(i*Dim + j) = CB(j, i); 00252 } 00253 } 00254 } 00255 00256 mat Vector_Quantizer::get_codebook() const 00257 { 00258 mat CB(Dim, Size); 00259 00260 for (int i = 0;i < Size;i++) { 00261 for (int j = 0;i < Dim;i++) { 00262 CB(j, i) = CodeBook(i * Dim + j); 00263 } 00264 } 00265 return CB; 00266 } 00267 00268 //-------------------------------------------------------------------- 00269 // class SQ 00270 //-------------------------------------------------------------------- 00271 00272 Scalar_Quantizer::Scalar_Quantizer() 00273 { 00274 } 00275 00276 // SQ(const char *Name); 00277 00278 int Scalar_Quantizer::encode(double x) const 00279 { 00280 int il = 0, ih = Levels.length() - 1, im; 00281 00282 while (il < ih - 1) { 00283 im = (il + ih) / 2; 00284 if (x < Levels(im)) ih = im; 00285 else il = im; 00286 } 00287 if (Levels(ih) - x < x - Levels(il)) return ih; 00288 else return il; 00289 } 00290 00291 ivec Scalar_Quantizer::encode(const vec &x) const 00292 { 00293 int i; 00294 ivec Index(x.length()); 00295 00296 for (i = 0;i < x.length();i++) { 00297 Index(i) = encode(x(i)); 00298 } 00299 return Index; 00300 } 00301 00302 vec Scalar_Quantizer::decode(const ivec &Index) const 00303 { 00304 int i; 00305 vec y(Index.length()); 00306 00307 for (i = 0;i < Index.length();i++) { 00308 y(i) = decode(Index(i)); 00309 } 00310 return y; 00311 } 00312 00313 vec Scalar_Quantizer::Q(const vec &x) const 00314 { 00315 int i; 00316 vec y(x.length()); 00317 00318 for (i = 0;i < x.length();i++) { 00319 y(i) = Q(x(i)); 00320 } 00321 return y; 00322 } 00323 00324 // void load(const char *Name); 00325 // void save(const char *Name) const; 00326 00327 00328 //------------------------------------------------------------------------- 00329 00330 00331 int scalar_encode(double x, vec &Levels) 00332 { 00333 int il = 0, ih = Levels.length() - 1, im; 00334 00335 while (il < ih - 1) { 00336 im = (il + ih) / 2; 00337 if (x < Levels(im)) ih = im; 00338 else il = im; 00339 } 00340 if (Levels(ih) - x < x - Levels(il)) return ih; 00341 else return il; 00342 } 00343 00344 ivec scalar_encode(vec &x, vec &Levels) 00345 { 00346 ivec ind(x.length()); 00347 for (int i = 0;i < x.length();i++) ind(i) = scalar_encode(x(i), Levels); 00348 return ind; 00349 } 00350 00351 } // namespace itpp 00352
Generated on Sat Jul 9 2011 15:21:33 for IT++ by Doxygen 1.7.4