69 double S, MinS = 1.0E30F;
74 for (i = 0;i <
Size;i++) {
76 for (j = 0;j <
Dim;j++) {
77 a = x._elem(j) -
CodeBook._elem(pos + j);
79 if (S >= MinS)
goto sune;
95 int i, j, index, pos = 0;
100 for (i = 0;i <
Size;i++) {
102 for (j = 0;j <
Dim;j++) {
103 a = x._elem(j) -
CodeBook._elem(pos + j);
105 if (S >= MinS[num-1])
goto sune;
107 for (index = num - 2;(index >= 0) && (S < MinS[index]);index--);
108 for (j = MinS.length() - 2;j > index;j--) {
110 MinIndex[j+1] = MinIndex[j];
113 MinIndex[index+1] = i;
123 Array<vec> Temp(Index.length());
125 for (
int i = 0;i < Temp.length();i++) {
139 for (i = 0;i <
length(v);i++) {
140 ifs.operator >> (v[i]) ;
145 ifs.getline(str, 2000);
146 if (strlen(str) == 0) ifs.getline(str, 2000);
151 ptr = strchr(ptr,
' ');
152 while (ptr == ptr_old) {
155 ptr = strchr(ptr,
' ');
158 if (i >= v.length()) v.set_length(2*v.length(),
true);
162 ptr = strchr(ptr,
' ');
163 while (ptr == ptr_old) {
166 ptr = strchr(ptr,
' ');
171 v.set_length(i,
true);
180 ifstream CodeBookFile(Name);
185 it_error_if(!CodeBookFile, std::string(
"Vector_Quantizer::load : cannot open file ") + Name);
186 cout <<
"Reading the codebook " << Name ;
190 Temp.set_length(d*16);
192 while (!CodeBookFile.eof()) {
193 if (n*d >= Temp.length()) Temp.set_length(2*Temp.length(),
true);
194 Temp.replace_mid(n*d, v);
202 cout <<
" size:" <<
size() <<
" dim:" <<
dim() << endl ;
207 ofstream CodeBookFile(Name);
209 cout <<
"Saving the codebook " << Name << endl ;
210 for (
int i = 0;i <
Size;i++) {
212 for (
int j = 0;j < v.length();j++) {
213 CodeBookFile.operator << (v[j]);
214 if (j < v.length() - 1) CodeBookFile.put(
' ') ;
216 CodeBookFile << endl ;
218 CodeBookFile.close();
225 for (
int i = 0;i <
Dim;i++) {
233 return CodeBook.mid(Index*Dim, Dim);
238 it_error_if(Dim !=
length(v),
"Vector_Quantizer::set_codevector : Wrong dimension");
239 for (
int i = 0;i <
length(v);i++) {
240 CodeBook._elem(Index*Dim + i) = v._elem(i);
249 for (
int i = 0;i <
Size;i++) {
250 for (
int j = 0;j <
Dim;j++) {
260 for (
int i = 0;i <
Size;i++) {
261 for (
int j = 0;i <
Dim;i++) {
280 int il = 0, ih =
Levels.length() - 1, im;
282 while (il < ih - 1) {
284 if (x <
Levels(im)) ih = im;
294 ivec Index(x.length());
296 for (i = 0;i < x.length();i++) {
305 vec y(Index.length());
307 for (i = 0;i < Index.length();i++) {
318 for (i = 0;i < x.length();i++) {
333 int il = 0, ih = Levels.length() - 1, im;
335 while (il < ih - 1) {
337 if (x < Levels(im)) ih = im;
340 if (Levels(ih) - x < x - Levels(il))
return ih;
346 ivec ind(x.length());
347 for (
int i = 0;i < x.length();i++) ind(i) =
scalar_encode(x(i), Levels);