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++) {
239 for (
int i = 0;i <
length(v);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);
#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 LatestDist
The distortion at the latest time a vector was encoded.
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)
vec Levels
The vector containing the code book.
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.
void modify_codevector(int no, double mul, const vec &add)
Rescale and translate a codevector.
int dim() const
Returns the dimension of the VQ.
vec get_codevector(int Index) const
Returns the codevector at the given index.
vec CodeBook
The vector containing the code book.
int Size
The size of the code book.
int size() const
Returns the size (number of codevectors) of the VQ.
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.
int Dim
The dimension of the code book.