IT++ Logo
vq.cpp
Go to the documentation of this file.
1
29#include <itpp/srccode/vq.h>
30#include <itpp/base/array.h>
31#include <itpp/base/matfunc.h>
32#include <fstream>
33#include <iostream>
34#include <cstdlib>
35
37
38using std::ifstream;
39using std::ofstream;
40using std::cout;
41using std::endl;
42
43namespace itpp
44{
45
46//--------------------------------------------------------------------
47// class VQ
48//--------------------------------------------------------------------
49
51{
52 LatestDist = 0;
53 Size = 0;
54 Dim = 0;
55}
56
57Vector_Quantizer::Vector_Quantizer(const char *Name) : CodeBook()
58{
59 LatestDist = 0;
60 Size = 0;
61 Dim = 0;
62 load(Name);
63}
64
65
66int Vector_Quantizer::encode(const vec &x)
67{
68 int i;
69 double S, MinS = 1.0E30F;
70 int MinIndex = 0;
71 int j, pos = 0;
72 double a;
73
74 for (i = 0;i < Size;i++) {
75 S = 0;
76 for (j = 0;j < Dim;j++) {
77 a = x._elem(j) - CodeBook._elem(pos + j);
78 S += a * a;
79 if (S >= MinS) goto sune;
80 }
81 MinS = S;
82 MinIndex = i;
83 sune:
84 pos += Dim;
85 }
86 LatestDist = MinS;
87 return MinIndex;
88}
89
90ivec Vector_Quantizer::encode(const vec &x, int num)
91{
92 double S, a;
93 vec MinS(num);
94 ivec MinIndex(num);
95 int i, j, index, pos = 0;
96
97 MinS.clear();
98 MinS += 1.0E30F;
99 MinIndex.clear();
100 for (i = 0;i < Size;i++) {
101 S = 0;
102 for (j = 0;j < Dim;j++) {
103 a = x._elem(j) - CodeBook._elem(pos + j);
104 S += a * a;
105 if (S >= MinS[num-1]) goto sune;
106 }
107 for (index = num - 2;(index >= 0) && (S < MinS[index]);index--);
108 for (j = MinS.length() - 2;j > index;j--) {
109 MinS[j+1] = MinS[j];// memcpy, memmov
110 MinIndex[j+1] = MinIndex[j];
111 }
112 MinS[index+1] = S;
113 MinIndex[index+1] = i;
114 sune:
115 pos += Dim;
116 }
117 LatestDist = MinS[0];
118 return MinIndex;
119}
120
121Array<vec> Vector_Quantizer::decode(const ivec &Index) const
122{
123 Array<vec> Temp(Index.length());
124
125 for (int i = 0;i < Temp.length();i++) {
126 Temp(i) = get_codevector(Index(i));
127 }
128 return Temp;
129}
130
131
132ifstream &operator>>(ifstream &ifs, vec &v)
133{
134 int i;
135 char str[2000];
136 char *ptr, *ptr_old;
137 bool flag;
138 if (length(v) != 0) {
139 for (i = 0;i < length(v);i++) {
140 ifs.operator >> (v[i]) ;
141 }
142 }
143 else {
144 v.set_length(50);
145 ifs.getline(str, 2000);
146 if (strlen(str) == 0) ifs.getline(str, 2000);
147 i = 0;
148 v[i++] = atof(str);
149 ptr = str;
150 ptr_old = ptr;
151 ptr = strchr(ptr, ' ');
152 while (ptr == ptr_old) {
153 ptr++;
154 ptr_old = ptr;
155 ptr = strchr(ptr, ' ');
156 }
157 while (ptr) {
158 if (i >= v.length()) v.set_length(2*v.length(), true);
159 v[i++] = atof(ptr);
160
161 ptr_old = ptr;
162 ptr = strchr(ptr, ' ');
163 while (ptr == ptr_old) {
164 ptr++;
165 ptr_old = ptr;
166 ptr = strchr(ptr, ' ');
167 }
168 }
169 flag = true;
170 flag = false;
171 v.set_length(i, true);
172 }
173 return ifs;
174}
175
176
177void Vector_Quantizer::load(const char *Name)
178{
179 vec Temp;
180 ifstream CodeBookFile(Name);
181 vec v;
182 int n;
183 int d;
184
185 it_error_if(!CodeBookFile, std::string("Vector_Quantizer::load : cannot open file ") + Name);
186 cout << "Reading the codebook " << Name ;
187 cout.flush() ;
188 CodeBookFile >> v ;
189 d = length(v);
190 Temp.set_length(d*16);
191 n = 0;
192 while (!CodeBookFile.eof()) {
193 if (n*d >= Temp.length()) Temp.set_length(2*Temp.length(), true);
194 Temp.replace_mid(n*d, v);
195 n++;
196 CodeBookFile >> v ;
197 }
198 Size = n;
199 Dim = d;
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 ;
203}
204
205void Vector_Quantizer::save(const char *Name) const
206{
207 ofstream CodeBookFile(Name);
208
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(' ') ;
215 }
216 CodeBookFile << endl ;
217 }
218 CodeBookFile.close();
219}
220
221void Vector_Quantizer::modify_codevector(int no, double mul, const vec &add)
222{
223 int pos = Dim * no;
224
225 for (int i = 0;i < Dim;i++) {
226 CodeBook._elem(pos + i) *= mul;
227 CodeBook._elem(pos + i) += add[i];
228 }
229}
230
231vec Vector_Quantizer::get_codevector(int Index) const
232{
233 return CodeBook.mid(Index*Dim, Dim);
234}
235
236void Vector_Quantizer::set_codevector(int Index, const vec &v)
237{
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);
241 }
242}
243
244void Vector_Quantizer::set_codebook(const mat &CB)
245{
246 Size = CB.cols();
247 Dim = CB.rows();
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);
252 }
253 }
254}
255
256mat Vector_Quantizer::get_codebook() const
257{
258 mat CB(Dim, Size);
259
260 for (int i = 0;i < Size;i++) {
261 for (int j = 0;i < Dim;i++) {
262 CB(j, i) = CodeBook(i * Dim + j);
263 }
264 }
265 return CB;
266}
267
268//--------------------------------------------------------------------
269// class SQ
270//--------------------------------------------------------------------
271
272Scalar_Quantizer::Scalar_Quantizer()
273{
274}
275
276// SQ(const char *Name);
277
278int Scalar_Quantizer::encode(double x) const
279{
280 int il = 0, ih = Levels.length() - 1, im;
281
282 while (il < ih - 1) {
283 im = (il + ih) / 2;
284 if (x < Levels(im)) ih = im;
285 else il = im;
286 }
287 if (Levels(ih) - x < x - Levels(il)) return ih;
288 else return il;
289}
290
291ivec Scalar_Quantizer::encode(const vec &x) const
292{
293 int i;
294 ivec Index(x.length());
295
296 for (i = 0;i < x.length();i++) {
297 Index(i) = encode(x(i));
298 }
299 return Index;
300}
301
302vec Scalar_Quantizer::decode(const ivec &Index) const
303{
304 int i;
305 vec y(Index.length());
306
307 for (i = 0;i < Index.length();i++) {
308 y(i) = decode(Index(i));
309 }
310 return y;
311}
312
313vec Scalar_Quantizer::Q(const vec &x) const
314{
315 int i;
316 vec y(x.length());
317
318 for (i = 0;i < x.length();i++) {
319 y(i) = Q(x(i));
320 }
321 return y;
322}
323
324// void load(const char *Name);
325// void save(const char *Name) const;
326
327
328//-------------------------------------------------------------------------
329
330
331int scalar_encode(double x, vec &Levels)
332{
333 int il = 0, ih = Levels.length() - 1, im;
334
335 while (il < ih - 1) {
336 im = (il + ih) / 2;
337 if (x < Levels(im)) ih = im;
338 else il = im;
339 }
340 if (Levels(ih) - x < x - Levels(il)) return ih;
341 else return il;
342}
343
344ivec scalar_encode(vec &x, vec &Levels)
345{
346 ivec ind(x.length());
347 for (int i = 0;i < x.length();i++) ind(i) = scalar_encode(x(i), Levels);
348 return ind;
349}
350
351} // namespace itpp
352
Definition of Array class (container)
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
Vector_Quantizer()
Default constructor.
#define it_error_if(t, s)
Abort if t is true.
Definition itassert.h:117
int size(const Vec< T > &v)
Length of vector.
Definition matfunc.h:55
int length(const Vec< T > &v)
Length of vector.
Definition matfunc.h:51
Various functions on vectors and matrices - header file.
itpp namespace
Definition itmex.h:37
ITPP_EXPORT int scalar_encode(double x, vec &Levels)
ADD DOCUMENTATION HERE.
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Definition binary.cpp:42
Definition of a vector quantizer class (unconstrained)

Generated on Tue Mar 26 2024 19:08:31 for IT++ by Doxygen 1.9.8