IT++ Logo
vq.h
Go to the documentation of this file.
1
29#ifndef VQ_H
30#define VQ_H
31
32#include <itpp/base/vec.h>
33#include <itpp/base/sort.h>
35#include <itpp/itexports.h>
36
37namespace itpp
38{
39
62class ITPP_EXPORT Vector_Quantizer
63{
64public:
68 Vector_Quantizer(const char *Name);
70 int encode(const vec &x);
72 ivec encode(const vec &x, int num);
74 vec decode(int Index) const;
76 Array<vec> decode(const ivec &Index) const;
78 vec Q(const vec &x);
80 vec operator()(const vec &x);
82 void set_codebook(const mat &CB);
84 mat get_codebook() const;
86 void set_codevector(int Index, const vec &indata);
88 vec get_codevector(int Index) const;
90 void modify_codevector(int no, double mul, const vec &add);
92 int size() const;
94 int dim() const;
96 int nobits() const;
103 void load(const char *Name);
110 void save(const char *Name) const;
112 double latest_distortion();
113protected:
117 int Size;
119 int Dim;
122};
123
124// INLINE FUNCTIONS
125
126inline int Vector_Quantizer::size() const { return Size; }
127inline int Vector_Quantizer::nobits() const { return levels2bits(Size); }
128inline int Vector_Quantizer::dim() const { return Dim; }
130inline vec Vector_Quantizer::decode(int Index) const { return get_codevector(Index); }
131inline vec Vector_Quantizer::Q(const vec &x) { return decode(encode(x)); }
132inline vec Vector_Quantizer::operator()(const vec &x) { return Q(x); }
133
152class ITPP_EXPORT Scalar_Quantizer
153{
154public:
158 Scalar_Quantizer(const char *Name);
160 int encode(double x) const;
162 ivec encode(const vec &x) const;
164 double decode(int Index) const;
166 vec decode(const ivec &Index) const;
168 double Q(double x) const;
170 vec Q(const vec &x) const;
172 double operator()(double x) const;
174 vec operator()(const vec &x) const;
176 void set_levels(const vec &L);
178 vec get_levels() const;
180 int size() const;
181protected:
186};
187
188inline int Scalar_Quantizer::size() const { return Levels.length(); }
189inline double Scalar_Quantizer::decode(int Index) const { return Levels(Index); }
190inline double Scalar_Quantizer::Q(double x) const { return decode(encode(x)); }
191inline double Scalar_Quantizer::operator()(double x) const { return Q(x); }
192inline vec Scalar_Quantizer::operator()(const vec &x) const { return Q(x); }
193inline void Scalar_Quantizer::set_levels(const vec &L) {Levels = L;sort(Levels); }
194inline vec Scalar_Quantizer::get_levels() const {return Levels; }
195
197ITPP_EXPORT int scalar_encode(double x, vec &Levels) ;
199ITPP_EXPORT ivec scalar_encode(vec &x, vec &Levels);
201inline double scalar_quantize(double x, vec &Levels) { return Levels(scalar_encode(x, Levels)); }
203inline vec scalar_quantize(vec &x, vec &Levels) { return Levels(scalar_encode(x, Levels)); }
204
205
206} // namespace itpp
207
208#endif // #ifndef VQ_H
General array class.
Definition factory.h:40
Automatic naming when saving.
Definition itfile.h:429
Class for vector quantization.
Definition vq.h:153
int size() const
Returns the size (number of codevectors) of the VQ.
Definition vq.h:188
ivec encode(const vec &x) const
Encode the input vector.
double LatestDist
The distortion at the latest time a vector was encoded.
Definition vq.h:185
int encode(double x) const
Encode.
vec Q(const vec &x) const
Quantize the input vector.
void set_levels(const vec &L)
Initialize the codebook by a matrix.
Definition vq.h:193
double Q(double x) const
Quantize.
Definition vq.h:190
double decode(int Index) const
Decode the index.
Definition vq.h:189
vec decode(const ivec &Index) const
Decode the indices.
Scalar_Quantizer(const char *Name)
Create a VQ from a VQ file.
vec Levels
The vector containing the code book.
Definition vq.h:183
double operator()(double x) const
Quantize.
Definition vq.h:191
Scalar_Quantizer()
Default constructor.
vec get_levels() const
Returns the codebook.
Definition vq.h:194
void sort(Vec< T > &data, SORTING_METHOD method=INTROSORT)
Sort the data vector in increasing order.
Definition sort.h:187
Class for vector quantization.
Definition vq.h:63
void modify_codevector(int no, double mul, const vec &add)
Rescale and translate a codevector.
int dim() const
Returns the dimension of the VQ.
Definition vq.h:128
Array< vec > decode(const ivec &Index) const
Decode the indices.
vec operator()(const vec &x)
Quantize the input vector.
Definition vq.h:132
vec get_codevector(int Index) const
Returns the codevector at the given index.
int nobits() const
Returns the number of bits of the VQ [log2(size)/dim].
Definition vq.h:127
vec decode(int Index) const
Decode the index.
Definition vq.h:130
void set_codebook(const mat &CB)
Initialize the codebook by a matrix.
int encode(const vec &x)
Encode the input vector.
ivec encode(const vec &x, int num)
Encode the input vector, and return the num best indices.
vec Q(const vec &x)
Quantize the input vector.
Definition vq.h:131
void set_codevector(int Index, const vec &indata)
Set a codevector in the codebook.
int Size
The size of the code book.
Definition vq.h:117
void load(const char *Name)
Load the codebook from a file.
double latest_distortion()
Returns the distortion at the latest time a vector was encoded.
Definition vq.h:129
void save(const char *Name) const
Save the codebook to a file.
Vector_Quantizer(const char *Name)
Create a VQ from a VQ file.
mat get_codebook() const
Returns the codebook.
Vector_Quantizer()
Default constructor.
int size() const
Returns the size (number of codevectors) of the VQ.
Definition vq.h:126
vec CodeBook
The vector containing the code book.
Definition vq.h:115
double LatestDist
The distortion at the latest time a vector was encoded.
Definition vq.h:121
int Dim
The dimension of the code book.
Definition vq.h:119
int levels2bits(int n)
Calculate the number of bits needed to represent n different values (levels).
Definition log_exp.h:92
int size(const Vec< T > &v)
Length of vector.
Definition matfunc.h:55
Logarithmic and exponenential functions - header file.
itpp namespace
Definition itmex.h:37
ITPP_EXPORT int scalar_encode(double x, vec &Levels)
ADD DOCUMENTATION HERE.
double scalar_quantize(double x, vec &Levels)
ADD DOCUMENTATION HERE.
Definition vq.h:201
Sorting functions.
Templated Vector Class Definitions.

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.12.0