IT++ Logo
fix_functions.h
Go to the documentation of this file.
1
30#ifndef FIX_FUNCTIONS_H
31#define FIX_FUNCTIONS_H
32
33#include <itpp/fixed/cfix.h>
34#include <itpp/base/vec.h>
35#include <itpp/base/mat.h>
36#include <itpp/base/array.h>
38#include <itpp/itexports.h>
39
40
41namespace itpp
42{
43
46
48template<class T> inline bool is_fix(const T &) {return false;}
50template<> inline bool is_fix(const Fix &) {return true;}
52template<> inline bool is_fix(const fixvec &) {return true;}
54template<> inline bool is_fix(const fixmat &) {return true;}
56template<> inline bool is_fix(const CFix &) {return true;}
58template<> inline bool is_fix(const cfixvec &) {return true;}
60template<> inline bool is_fix(const cfixmat &) {return true;}
62template<class T> inline bool is_fix(const Array<T> &) {return is_fix(T());}
63
65inline void set_fix(Fix &y, double x, int n) {y.set(x, n);}
67inline void set_fix(Fix &y, double x, int n, q_mode q) {y.set(x, n, q);}
69inline void set_fix(fixvec &y, const vec &x, int n)
70{
71 y.set_size(x.length());
72 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
73}
75inline void set_fix(fixvec &y, const vec &x, int n, q_mode q)
76{
77 y.set_size(x.length());
78 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
79}
81inline void set_fix(fixmat &y, const mat &x, int n)
82{
83 y.set_size(x.rows(), x.cols());
84 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
85}
87inline void set_fix(fixmat &y, const mat &x, int n, q_mode q)
88{
89 y.set_size(x.rows(), x.cols());
90 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
91}
93inline void set_fix(double &y, double x, int) {y = x;}
95inline void set_fix(double &y, double x, int, q_mode) {y = x;}
97inline void set_fix(vec &y, const vec &x, int) {y = x;}
99inline void set_fix(vec &y, const vec &x, int, q_mode) {y = x;}
101inline void set_fix(mat &y, const mat &x, int) {y = x;}
103inline void set_fix(mat &y, const mat &x, int, q_mode) {y = x;}
104
106inline void set_fix(CFix &y, std::complex<double> x, int n) {y.set(x, n);}
108inline void set_fix(CFix &y, double real, double imag, int n) {y.set(real, imag, n);}
110inline void set_fix(CFix &y, std::complex<double> x, int n, q_mode q) {y.set(x, n, q);}
112inline void set_fix(CFix &y, double real, double imag, int n, q_mode q) {y.set(real, imag, n, q);}
114inline void set_fix(cfixvec &y, const cvec &x, int n)
115{
116 y.set_size(x.length());
117 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
118}
120inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n)
121{
122 it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size");
123 y.set_size(real.length());
124 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n);
125}
127inline void set_fix(cfixvec &y, const cvec &x, int n, q_mode q)
128{
129 y.set_size(x.length());
130 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
131}
133inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n, q_mode q)
134{
135 it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size");
136 y.set_size(real.length());
137 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q);
138}
140inline void set_fix(cfixmat &y, const cmat &x, int n)
141{
142 y.set_size(x.rows(), x.cols());
143 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
144}
146inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n)
147{
148 it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size");
149 y.set_size(real.rows(), real.cols());
150 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n);
151}
153inline void set_fix(cfixmat &y, const cmat &x, int n, q_mode q)
154{
155 y.set_size(x.rows(), x.cols());
156 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
157}
159inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n, q_mode q)
160{
161 it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size");
162 y.set_size(real.rows(), real.cols());
163 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q);
164}
166inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int) {y = x;}
168inline void set_fix(std::complex<double> &y, double real, double imag, int) {y = std::complex<double>(real, imag);}
170inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int, q_mode) {y = x;}
172inline void set_fix(std::complex<double> &y, double real, double imag, int, q_mode) {y = std::complex<double>(real, imag);}
174inline void set_fix(cvec &y, const cvec &x, int) {y = x;}
176inline void set_fix(cvec &y, const vec &real, const vec &imag, int) {y = to_cvec(real, imag);}
178inline void set_fix(cvec &y, const cvec &x, int, q_mode) {y = x;}
180inline void set_fix(cvec &y, const vec &real, const vec &imag, int, q_mode) {y = to_cvec(real, imag);}
182inline void set_fix(cmat &y, const cmat &x, int) {y = x;}
184inline void set_fix(cmat &y, const mat &real, const mat &imag, int) {y = to_cmat(real, imag);}
186inline void set_fix(cmat &y, const cmat &x, int, q_mode) {y = x;}
188inline void set_fix(cmat &y, const mat &real, const mat &imag, int, q_mode) {y = to_cmat(real, imag);}
189
191template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n)
192{
193 y.set_size(x.size());
194 for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n);
195}
197template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &real, const Array<T2> &imag, int n)
198{
199 it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size");
200 y.set_size(real.size());
201 for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n);
202}
204template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n, q_mode q)
205{
206 y.set_size(x.size());
207 for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n, q);
208}
210template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &real, const Array<T2> &imag, int n, q_mode q)
211{
212 it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size");
213 y.set_size(real.size());
214 for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n, q);
215}
216
218inline void lshift_fix(Fix &y, int n) {y.lshift(n);}
220inline void rshift_fix(Fix &y, int n) {y.rshift(n);}
222inline void rshift_fix(Fix &y, int n, q_mode q) {y.rshift(n, q);}
224inline void lshift_fix(fixvec &y, int n)
225{for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
227inline void rshift_fix(fixvec &y, int n)
228{for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
230inline void rshift_fix(fixvec &y, int n, q_mode q)
231{for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
233inline void lshift_fix(fixmat &y, int n)
234{for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
236inline void rshift_fix(fixmat &y, int n)
237{for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
239inline void rshift_fix(fixmat &y, int n, q_mode q)
240{for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
242inline void lshift_fix(double &, int) {}
244inline void rshift_fix(double &, int) {}
246inline void rshift_fix(double &, int, q_mode) {}
248inline void lshift_fix(vec &, int) {}
250inline void rshift_fix(vec &, int) {}
252inline void rshift_fix(vec &, int, q_mode) {}
254inline void lshift_fix(mat &, int) {}
256inline void rshift_fix(mat &, int) {}
258inline void rshift_fix(mat &, int, q_mode) {}
260inline void lshift_fix(CFix &y, int n) {y.lshift(n);}
262inline void rshift_fix(CFix &y, int n) {y.rshift(n);}
264inline void rshift_fix(CFix &y, int n, q_mode q) {y.rshift(n, q);}
266inline void lshift_fix(cfixvec &y, int n)
267{for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
269inline void rshift_fix(cfixvec &y, int n)
270{for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
272inline void rshift_fix(cfixvec &y, int n, q_mode q)
273{for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
275inline void lshift_fix(cfixmat &y, int n)
276{for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
278inline void rshift_fix(cfixmat &y, int n)
279{for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
281inline void rshift_fix(cfixmat &y, int n, q_mode q)
282{for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
284inline void lshift_fix(std::complex<double> &, int) {}
286inline void rshift_fix(std::complex<double> &, int) {}
288inline void rshift_fix(std::complex<double> &, int, q_mode) {}
290inline void lshift_fix(cvec &, int) {}
292inline void rshift_fix(cvec &, int) {}
294inline void rshift_fix(cvec &, int, q_mode) {}
296inline void lshift_fix(cmat &, int) {}
298inline void rshift_fix(cmat &, int) {}
300inline void rshift_fix(cmat &, int, q_mode) {}
302template<class T> inline void lshift_fix(Array<T> &y, int n)
303{for(int i = 0; i < y.size(); i++) lshift_fix(y(i), n);}
305template<class T> inline void rshift_fix(Array<T> &y, int n)
306{for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n);}
308template<class T> inline void rshift_fix(Array<T> &y, int n, q_mode q)
309{for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n, q);}
310
312inline void assert_fixshift(double, int) {}
314inline void assert_fixshift(const std::complex<double> &, int) {}
316inline void assert_fixshift(const Fix &x, int shift)
317{it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");}
319inline void assert_fixshift(const CFix &x, int shift)
320{it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");}
321
323ITPP_EXPORT vec to_vec(const fixvec &v);
325ITPP_EXPORT cvec to_cvec(const cfixvec &v);
327ITPP_EXPORT mat to_mat(const fixmat &m);
329ITPP_EXPORT cmat to_cmat(const cfixmat &m);
330
332
334template<class T, class U>
335class ConvertU2T
336{
337public:
338 typedef T result;
339};
341template<class T, class U>
342class ConvertU2T<T, Array<U> >
343{
344public:
345 typedef Array<typename ConvertU2T<T, U>::result> result; // Recursive
346};
348template<class T, class U>
349class ConvertU2T<T, Vec<U> >
350{
351public:
352 typedef Vec<T> result;
353};
355template<class T, class U>
356class ConvertU2T<T, Mat<U> >
357{
358public:
359 typedef Mat<T> result;
360};
361
363
365template<class T> inline T to(double x) {return T(x);}
367template<class T> inline T to(const Fix &x) {return T(x);}
369template<class T> inline T to(const std::complex<double> &x) {return T(x);}
371template<class T> inline T to(const CFix &x) {return T(x);}
373template<class T> inline T to(double real, double imag) {return T(real, imag);}
375template<class T> inline T to(const Fix &real, const Fix &imag) {return T(real, imag);}
376
378template<class T, class U> Vec<T> to(const Vec<U> &x)
379{
380 Vec<T> y(x.length());
381 for (int i = 0; i < x.length(); i++) {
382 y(i) = T(x(i));
383 }
384 return y;
385}
387template<> inline vec to<double>(const vec &x) {return x;}
389template<> inline cvec to<std::complex<double> >(const cvec &x) {return x;}
391template<> inline fixvec to<Fix>(const fixvec &x) {return x;}
393template<> inline cfixvec to<CFix>(const cfixvec &x) {return x;}
394
396template<class T, class U> Vec<T> to(const Vec<U> &real, const Vec<U> &imag)
397{
398 it_assert_debug(real.length() == imag.length(), "to: real and imag should have the same size");
399 Vec<T> y(real.length());
400 for (int i = 0; i < real.length(); i++) {
401 y(i) = T(real(i), imag(i));
402 }
403 return y;
404}
405
407template<class T, class U> Mat<T> to(const Mat<U> &x)
408{
409 Mat<T> y(x.rows(), x.cols());
410 for (int i = 0; i < x.rows(); i++) {
411 for (int j = 0; j < x.cols(); j++) {
412 y(i, j) = T(x(i, j));
413 }
414 }
415 return y;
416}
418template<> inline mat to<double>(const mat &x) {return x;}
420template<> inline cmat to<std::complex<double> >(const cmat &x) {return x;}
422template<> inline fixmat to<Fix>(const fixmat &x) {return x;}
424template<> inline cfixmat to<CFix>(const cfixmat &x) {return x;}
425
427template<class T, class U> Mat<T> to(const Mat<U> &real, const Mat<U> &imag)
428{
429 it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "to: real and imag should have the same size");
430 Mat<T> y(real.rows(), real.cols());
431 for (int i = 0; i < real.rows(); i++) {
432 for (int j = 0; j < real.cols(); j++) {
433 y(i, j) = T(real(i, j), imag(i, j));
434 }
435 }
436 return y;
437}
438
440template<class T, class U>
442{
444 for (int i = 0; i < x.size(); i++) {
445 y(i) = to<T>(x(i));
446 }
447 return y;
448}
449
451template<class T, class U>
453{
454 it_assert_debug(real.size() == imag.size(), "to: real and imag should have the same size");
456 for (int i = 0; i < real.size(); i++) {
457 y(i) = to<T>(real(i), imag(i));
458 }
459 return y;
460}
461
463inline double unfix(const Fix &x) {return x.unfix();}
465inline std::complex<double> unfix(const CFix &x) {return x.unfix();}
467inline vec unfix(const fixvec &x) {return to_vec(x);}
469inline cvec unfix(const cfixvec &x) {return to_cvec(x);}
471inline mat unfix(const fixmat &x) {return to_mat(x);}
473inline cmat unfix(const cfixmat &x) {return to_cmat(x);}
474
476inline double unfix(double x) {return x;}
478inline std::complex<double> unfix(const std::complex<double> &x) {return x;}
480inline vec unfix(const vec &x) {return x;}
482inline cvec unfix(const cvec &x) {return x;}
484inline mat unfix(const mat &x) {return x;}
486inline cmat unfix(const cmat &x) {return x;}
487
489
491template<class T>
492class Convert
493{
494public:
495 typedef double to_double;
496};
498template<>
499class Convert<CFix>
500{
501public:
502 typedef std::complex<double> to_double;
503};
505template<class T>
506class Convert<std::complex<T> >
507{
508public:
509 typedef std::complex<double> to_double;
510};
512template<class T>
513class Convert<Array<T> >
514{
515public:
516 typedef Array<typename Convert<T>::to_double> to_double; // Recursive
517};
519template<class T>
520class Convert<Vec<T> >
521{
522public:
523 typedef Vec<typename Convert<T>::to_double> to_double; // Recursive
524};
526template<class T>
527class Convert<Mat<T> >
528{
529public:
530 typedef Mat<typename Convert<T>::to_double> to_double; // Recursive
531};
532
534
536template<class T>
538{
540 for (int i = 0; i < x.size(); i++) {
541 y(i) = unfix(x(i));
542 }
543 return y;
544}
545
547ITPP_EXPORT Fix abs(const Fix &x);
549ITPP_EXPORT Fix real(const CFix &x);
551ITPP_EXPORT Fix imag(const CFix &x);
553ITPP_EXPORT CFix conj(const CFix &x);
554
556
557} // namespace itpp
558
559#endif // #ifndef FIX_FUNCTIONS_H
Definition of Array class (container)
Definitions of a complex fixed-point data type CFix.
General array class.
Definition: array.h:105
int size() const
Returns the number of data elements in the array object.
Definition: array.h:155
void set_size(int n, bool copy=false)
Resizing an Array<T>.
Definition: array.h:257
Complex fixed-point data type.
Definition: cfix.h:52
void lshift(int n)
Left shift n bits.
Definition: cfix.cpp:213
void rshift(int n)
Right shift n bits using quantization mode qmode (constructor argument)
Definition: cfix.cpp:221
std::complex< double > unfix() const
Conversion to std::complex<double>
Definition: cfix.cpp:235
void set(double real, double imag, int n)
Set to (real + i*imag) * pow2(n) using quantization mode qmode (constructor argument)
Definition: cfix.cpp:185
int get_shift() const
Get shift.
Definition: fix_base.h:1001
Fixed-point data type.
Definition: fix.h:52
void lshift(int n)
Left shift n bits.
Definition: fix.cpp:138
void rshift(int n)
Right shift n bits using quantization mode qmode (constructor argument)
Definition: fix.cpp:145
void set(double x, int n)
Set to x * pow2(n) using quantization mode qmode (constructor argument)
Definition: fix.cpp:126
double unfix() const
Conversion to double.
Definition: fix.cpp:157
Matrix Class (Templated)
Definition: mat.h:202
void set_size(int rows, int cols, bool copy=false)
Set size of matrix. If copy = true then keep the data before resizing.
Definition: mat.h:647
void set(const std::string &str)
Set matrix equal to values in the string str.
Definition: mat.h:762
int rows() const
The number of rows.
Definition: mat.h:237
int cols() const
The number of columns.
Definition: mat.h:235
int size() const
The number of elements.
Definition: mat.h:239
Vector Class (Templated)
Definition: vec.h:245
int size() const
The size of the vector.
Definition: vec.h:271
void set_size(int size, bool copy=false)
Set length of vector. if copy = true then keeping the old values.
Definition: vec.h:663
int length() const
The size of the vector.
Definition: vec.h:269
Definitions of converters between different vector and matrix types.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
Vec< CFix > cfixvec
Typedef for complex fixed-point vector type.
Definition: cfix.h:178
void set_fix(Fix &y, double x, int n)
Set y = x * pow2(n) using the quantization mode of y.
Definition: fix_functions.h:65
void rshift_fix(Fix &y, int n)
Right shift n bits using the quantization mode of y.
vec to< double >(const vec &x)
Convert vec to vec.
double unfix(const Fix &x)
Convert Fix to double by multiplying the bit representation with pow2(-shift)
void lshift_fix(Fix &y, int n)
Left shift n bits.
Mat< CFix > cfixmat
Typedef for complex fixed-point matrix type.
Definition: cfix.h:180
T to(double x)
Convert double to T.
cfixvec to< CFix >(const cfixvec &x)
Convert cfixvec to cfixvec.
void set(const char *str)
Set the vector equal to the values in the str string.
Definition: vec.h:814
q_mode
Quantization modes (aligned with SystemC)
Definition: fix_base.h:957
Mat< Fix > fixmat
Typedef for fixed-point matrix type.
Definition: fix.h:153
void assert_fixshift(double, int)
If x is a fixed-point variable, assert that x has the specified shift value, otherwise do nothing.
fixvec to< Fix >(const fixvec &x)
Convert fixvec to fixvec.
bool is_fix(const T &)
Return true only if argument is of type Fix or CFix (or an Array/Vec/Mat of Fix or CFix)
Definition: fix_functions.h:48
Vec< Fix > fixvec
Typedef for fixed-point vector type.
Definition: fix.h:151
vec imag(const cvec &data)
Imaginary part of complex values.
Definition: elem_math.cpp:180
vec real(const cvec &data)
Real part of complex values.
Definition: elem_math.cpp:157
cvec conj(const cvec &x)
Conjugate of complex value.
Definition: elem_math.h:226
Matrix Class Definitions.
itpp namespace
Definition: itmex.h:37
cvec to_cvec(const Vec< T > &v)
Converts a Vec<T> to cvec.
Definition: converters.h:107
cmat to_cmat(const Mat< T > &m)
Converts a Mat<T> to cmat.
Definition: converters.h:232
vec to_vec(const Vec< T > &v)
Converts a Vec<T> to vec.
Definition: converters.h:93
std::string to_str(const T &i)
Convert anything to string.
Definition: converters.h:444
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
Definition: converters.h:216
bin abs(const bin &inbin)
absolute value of bin
Definition: binary.h:174
STL namespace.
Templated Vector Class Definitions.

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