57 Vector_Quantizer::Vector_Quantizer(
const char *Name) : CodeBook()
66 int Vector_Quantizer::encode(
const vec &x)
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;
90 ivec Vector_Quantizer::encode(
const vec &x,
int num)
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];
121 Array<vec> Vector_Quantizer::decode(
const ivec &Index)
const
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);
177 void Vector_Quantizer::load(
const char *Name)
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 ;
205 void Vector_Quantizer::save(
const char *Name)
const
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();
221 void Vector_Quantizer::modify_codevector(
int no,
double mul,
const vec &add)
225 for (
int i = 0;i < Dim;i++) {
226 CodeBook._elem(pos + i) *= mul;
227 CodeBook._elem(pos + i) += add[i];
231 vec Vector_Quantizer::get_codevector(
int Index)
const
233 return CodeBook.mid(Index*Dim, Dim);
236 void Vector_Quantizer::set_codevector(
int Index,
const vec &v)
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);
244 void Vector_Quantizer::set_codebook(
const mat &CB)
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);
256 mat Vector_Quantizer::get_codebook()
const
260 for (
int i = 0;i < Size;i++) {
261 for (
int j = 0;i < Dim;i++) {
262 CB(j, i) = CodeBook(i * Dim + j);
272 Scalar_Quantizer::Scalar_Quantizer()
278 int Scalar_Quantizer::encode(
double x)
const
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;
291 ivec Scalar_Quantizer::encode(
const vec &x)
const
294 ivec Index(x.length());
296 for (i = 0;i < x.length();i++) {
297 Index(i) = encode(x(i));
302 vec Scalar_Quantizer::decode(
const ivec &Index)
const
305 vec y(Index.length());
307 for (i = 0;i < Index.length();i++) {
308 y(i) = decode(Index(i));
313 vec Scalar_Quantizer::Q(
const vec &x)
const
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);
Definition of Array class (container)
std::istream & operator>>(std::istream &is, Array< T > &a)
Input stream for Array<T>. T must have istream operator>> defined.
Mat< double > mat
Default Matrix Type.
Vec< double > vec
Definition of double vector type.
Vec< int > ivec
Definition of integer vector type.
Vector_Quantizer()
Default constructor.
#define it_error_if(t, s)
Abort if t is true.
int size(const Vec< T > &v)
Length of vector.
int length(const Vec< T > &v)
Length of vector.
Various functions on vectors and matrices - header file.
ITPP_EXPORT int scalar_encode(double x, vec &Levels)
ADD DOCUMENTATION HERE.
Definition of a vector quantizer class (unconstrained)