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;
117 LatestDist = MinS[0];
123 Array<vec> Temp(Index.length());
125 for (
int i = 0;i < Temp.length();i++) {
126 Temp(i) = get_codevector(Index(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);
200 CodeBook.set_length(Size*Dim);
201 for (n = 0;n < CodeBook.length();n++) CodeBook(n) = Temp(n);
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++) {
211 vec v = CodeBook.mid(i * Dim, Dim);
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++) {
226 CodeBook._elem(pos + i) *= mul;
227 CodeBook._elem(pos + i) += add[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);
248 CodeBook.set_length(Size*Dim);
249 for (
int i = 0;i < Size;i++) {
250 for (
int j = 0;j < Dim;j++) {
251 CodeBook(i*Dim + j) = CB(j, i);
260 for (
int i = 0;i < Size;i++) {
261 for (
int j = 0;i < Dim;i++) {
262 CB(j, i) = CodeBook(i * Dim + j);
280 int il = 0, ih = Levels.length() - 1, im;
282 while (il < ih - 1) {
284 if (x < Levels(im)) ih = im;
287 if (Levels(ih) - x < x - Levels(il))
return ih;
294 ivec Index(x.length());
296 for (i = 0;i < x.length();i++) {
297 Index(i) = encode(x(i));
305 vec y(Index.length());
307 for (i = 0;i < Index.length();i++) {
308 y(i) = decode(Index(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);
#define it_error_if(t, s)
Abort if t is true.
Various functions on vectors and matrices - header file.
vec decode(int Index) const
Decode the index.
double Q(double x) const
Quantize.
void load(const char *Name)
Load the codebook from a file.
Vector_Quantizer()
Default constructor.
void save(const char *Name) const
Save the codebook to a file.
int length(const Vec< T > &v)
Length of vector.
Scalar_Quantizer()
Default constructor.
Definition of Array class (container)
void set_codevector(int Index, const vec &indata)
Set a codevector in the codebook.
ITPP_EXPORT int scalar_encode(double x, vec &Levels)
ADD DOCUMENTATION HERE.
int encode(double x) const
Encode.
int encode(const vec &x)
Encode the input vector.
int size(const Vec< T > &v)
Length of vector.
void modify_codevector(int no, double mul, const vec &add)
Rescale and translate a codevector.
vec get_codevector(int Index) const
Returns the codevector at the given index.
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
void set_codebook(const mat &CB)
Initialize the codebook by a matrix.
Definition of a vector quantizer class (unconstrained)
double decode(int Index) const
Decode the index.
mat get_codebook() const
Returns the codebook.