fix_functions.h
Go to the documentation of this file.
#ifndef FIX_FUNCTIONS_H
#define FIX_FUNCTIONS_H
#include <itpp/fixed/cfix.h>
#include <itpp/base/vec.h>
#include <itpp/base/mat.h>
#include <itpp/base/array.h>
#include <itpp/base/converters.h>
#include <itpp/itexports.h>
namespace itpp
{
template<class T> inline bool is_fix(const T &) {return false;}
template<> inline bool is_fix(const Fix &) {return true;}
template<> inline bool is_fix(const fixvec &) {return true;}
template<> inline bool is_fix(const fixmat &) {return true;}
template<> inline bool is_fix(const CFix &) {return true;}
template<> inline bool is_fix(const cfixvec &) {return true;}
template<> inline bool is_fix(const cfixmat &) {return true;}
template<class T> inline bool is_fix(const Array<T> &) {return is_fix(T());}
inline void set_fix(Fix &y, double x, int n) {y.set(x, n);}
inline void set_fix(Fix &y, double x, int n, q_mode q) {y.set(x, n, q);}
inline void set_fix(fixvec &y, const vec &x, int n)
{
y.set_size(x.length());
for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
}
inline void set_fix(fixvec &y, const vec &x, int n, q_mode q)
{
y.set_size(x.length());
for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
}
inline void set_fix(fixmat &y, const mat &x, int n)
{
y.set_size(x.rows(), x.cols());
for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
}
inline void set_fix(fixmat &y, const mat &x, int n, q_mode q)
{
y.set_size(x.rows(), x.cols());
for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
}
inline void set_fix(double &y, double x, int) {y = x;}
inline void set_fix(double &y, double x, int, q_mode) {y = x;}
inline void set_fix(vec &y, const vec &x, int) {y = x;}
inline void set_fix(vec &y, const vec &x, int, q_mode) {y = x;}
inline void set_fix(mat &y, const mat &x, int) {y = x;}
inline void set_fix(mat &y, const mat &x, int, q_mode) {y = x;}
inline void set_fix(CFix &y, std::complex<double> x, int n) {y.set(x, n);}
inline void set_fix(CFix &y, double real, double imag, int n) {y.set(real, imag, n);}
inline void set_fix(CFix &y, std::complex<double> x, int n, q_mode q) {y.set(x, n, q);}
inline void set_fix(CFix &y, double real, double imag, int n, q_mode q) {y.set(real, imag, n, q);}
inline void set_fix(cfixvec &y, const cvec &x, int n)
{
y.set_size(x.length());
for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
}
inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n)
{
it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size");
y.set_size(real.length());
for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n);
}
inline void set_fix(cfixvec &y, const cvec &x, int n, q_mode q)
{
y.set_size(x.length());
for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
}
inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n, q_mode q)
{
it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size");
y.set_size(real.length());
for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q);
}
inline void set_fix(cfixmat &y, const cmat &x, int n)
{
y.set_size(x.rows(), x.cols());
for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
}
inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n)
{
it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size");
y.set_size(real.rows(), real.cols());
for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n);
}
inline void set_fix(cfixmat &y, const cmat &x, int n, q_mode q)
{
y.set_size(x.rows(), x.cols());
for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
}
inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n, q_mode q)
{
it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size");
y.set_size(real.rows(), real.cols());
for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q);
}
inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int) {y = x;}
inline void set_fix(std::complex<double> &y, double real, double imag, int) {y = std::complex<double>(real, imag);}
inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int, q_mode) {y = x;}
inline void set_fix(std::complex<double> &y, double real, double imag, int, q_mode) {y = std::complex<double>(real, imag);}
inline void set_fix(cvec &y, const cvec &x, int) {y = x;}
inline void set_fix(cvec &y, const vec &real, const vec &imag, int) {y = to_cvec(real, imag);}
inline void set_fix(cvec &y, const cvec &x, int, q_mode) {y = x;}
inline void set_fix(cvec &y, const vec &real, const vec &imag, int, q_mode) {y = to_cvec(real, imag);}
inline void set_fix(cmat &y, const cmat &x, int) {y = x;}
inline void set_fix(cmat &y, const mat &real, const mat &imag, int) {y = to_cmat(real, imag);}
inline void set_fix(cmat &y, const cmat &x, int, q_mode) {y = x;}
inline void set_fix(cmat &y, const mat &real, const mat &imag, int, q_mode) {y = to_cmat(real, imag);}
template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n)
{
y.set_size(x.size());
for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n);
}
template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &real, const Array<T2> &imag, int n)
{
it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size");
y.set_size(real.size());
for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n);
}
template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n, q_mode q)
{
y.set_size(x.size());
for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n, q);
}
template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &real, const Array<T2> &imag, int n, q_mode q)
{
it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size");
y.set_size(real.size());
for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n, q);
}
inline void lshift_fix(Fix &y, int n) {y.lshift(n);}
inline void rshift_fix(Fix &y, int n) {y.rshift(n);}
inline void rshift_fix(Fix &y, int n, q_mode q) {y.rshift(n, q);}
inline void lshift_fix(fixvec &y, int n)
{for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
inline void rshift_fix(fixvec &y, int n)
{for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
inline void rshift_fix(fixvec &y, int n, q_mode q)
{for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
inline void lshift_fix(fixmat &y, int n)
{for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
inline void rshift_fix(fixmat &y, int n)
{for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
inline void rshift_fix(fixmat &y, int n, q_mode q)
{for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
inline void lshift_fix(double &, int) {}
inline void rshift_fix(double &, int) {}
inline void rshift_fix(double &, int, q_mode) {}
inline void lshift_fix(vec &, int) {}
inline void rshift_fix(vec &, int) {}
inline void rshift_fix(vec &, int, q_mode) {}
inline void lshift_fix(mat &, int) {}
inline void rshift_fix(mat &, int) {}
inline void rshift_fix(mat &, int, q_mode) {}
inline void lshift_fix(CFix &y, int n) {y.lshift(n);}
inline void rshift_fix(CFix &y, int n) {y.rshift(n);}
inline void rshift_fix(CFix &y, int n, q_mode q) {y.rshift(n, q);}
inline void lshift_fix(cfixvec &y, int n)
{for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
inline void rshift_fix(cfixvec &y, int n)
{for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
inline void rshift_fix(cfixvec &y, int n, q_mode q)
{for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
inline void lshift_fix(cfixmat &y, int n)
{for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
inline void rshift_fix(cfixmat &y, int n)
{for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
inline void rshift_fix(cfixmat &y, int n, q_mode q)
{for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
inline void lshift_fix(std::complex<double> &, int) {}
inline void rshift_fix(std::complex<double> &, int) {}
inline void rshift_fix(std::complex<double> &, int, q_mode) {}
inline void lshift_fix(cvec &, int) {}
inline void rshift_fix(cvec &, int) {}
inline void rshift_fix(cvec &, int, q_mode) {}
inline void lshift_fix(cmat &, int) {}
inline void rshift_fix(cmat &, int) {}
inline void rshift_fix(cmat &, int, q_mode) {}
template<class T> inline void lshift_fix(Array<T> &y, int n)
{for(int i = 0; i < y.size(); i++) lshift_fix(y(i), n);}
template<class T> inline void rshift_fix(Array<T> &y, int n)
{for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n);}
template<class T> inline void rshift_fix(Array<T> &y, int n, q_mode q)
{for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n, q);}
inline void assert_fixshift(double, int) {}
inline void assert_fixshift(const std::complex<double> &, int) {}
inline void assert_fixshift(const Fix &x, int shift)
{it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");}
inline void assert_fixshift(const CFix &x, int shift)
{it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");}
ITPP_EXPORT vec to_vec(const fixvec &v);
ITPP_EXPORT cvec to_cvec(const cfixvec &v);
ITPP_EXPORT mat to_mat(const fixmat &m);
ITPP_EXPORT cmat to_cmat(const cfixmat &m);
template<class T, class U>
class ConvertU2T
{
public:
typedef T result;
};
template<class T, class U>
class ConvertU2T<T, Array<U> >
{
public:
typedef Array<typename ConvertU2T<T, U>::result> result; // Recursive
};
template<class T, class U>
class ConvertU2T<T, Vec<U> >
{
public:
typedef Vec<T> result;
};
template<class T, class U>
class ConvertU2T<T, Mat<U> >
{
public:
typedef Mat<T> result;
};
template<class T> inline T to(double x) {return T(x);}
template<class T> inline T to(const Fix &x) {return T(x);}
template<class T> inline T to(const std::complex<double> &x) {return T(x);}
template<class T> inline T to(const CFix &x) {return T(x);}
template<class T> inline T to(double real, double imag) {return T(real, imag);}
template<class T> inline T to(const Fix &real, const Fix &imag) {return T(real, imag);}
template<class T, class U> Vec<T> to(const Vec<U> &x)
{
Vec<T> y(x.length());
for (int i = 0; i < x.length(); i++) {
y(i) = T(x(i));
}
return y;
}
template<> inline vec to<double>(const vec &x) {return x;}
template<> inline cvec to<std::complex<double> >(const cvec &x) {return x;}
template<> inline fixvec to<Fix>(const fixvec &x) {return x;}
template<> inline cfixvec to<CFix>(const cfixvec &x) {return x;}
template<class T, class U> Vec<T> to(const Vec<U> &real, const Vec<U> &imag)
{
it_assert_debug(real.length() == imag.length(), "to: real and imag should have the same size");
Vec<T> y(real.length());
for (int i = 0; i < real.length(); i++) {
y(i) = T(real(i), imag(i));
}
return y;
}
template<class T, class U> Mat<T> to(const Mat<U> &x)
{
Mat<T> y(x.rows(), x.cols());
for (int i = 0; i < x.rows(); i++) {
for (int j = 0; j < x.cols(); j++) {
y(i, j) = T(x(i, j));
}
}
return y;
}
template<> inline mat to<double>(const mat &x) {return x;}
template<> inline cmat to<std::complex<double> >(const cmat &x) {return x;}
template<> inline fixmat to<Fix>(const fixmat &x) {return x;}
template<> inline cfixmat to<CFix>(const cfixmat &x) {return x;}
template<class T, class U> Mat<T> to(const Mat<U> &real, const Mat<U> &imag)
{
it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "to: real and imag should have the same size");
Mat<T> y(real.rows(), real.cols());
for (int i = 0; i < real.rows(); i++) {
for (int j = 0; j < real.cols(); j++) {
y(i, j) = T(real(i, j), imag(i, j));
}
}
return y;
}
template<class T, class U>
Array<typename ConvertU2T<T, U>::result> to(const Array<U> &x)
{
Array<typename ConvertU2T<T, U>::result> y(x.size());
for (int i = 0; i < x.size(); i++) {
y(i) = to<T>(x(i));
}
return y;
}
template<class T, class U>
Array<typename ConvertU2T<T, U>::result> to(const Array<U> &real, const Array<U> &imag)
{
it_assert_debug(real.size() == imag.size(), "to: real and imag should have the same size");
Array<typename ConvertU2T<T, U>::result> y(real.size());
for (int i = 0; i < real.size(); i++) {
y(i) = to<T>(real(i), imag(i));
}
return y;
}
inline double unfix(const Fix &x) {return x.unfix();}
inline std::complex<double> unfix(const CFix &x) {return x.unfix();}
inline vec unfix(const fixvec &x) {return to_vec(x);}
inline cvec unfix(const cfixvec &x) {return to_cvec(x);}
inline mat unfix(const fixmat &x) {return to_mat(x);}
inline cmat unfix(const cfixmat &x) {return to_cmat(x);}
inline double unfix(double x) {return x;}
inline std::complex<double> unfix(const std::complex<double> &x) {return x;}
inline vec unfix(const vec &x) {return x;}
inline cvec unfix(const cvec &x) {return x;}
inline mat unfix(const mat &x) {return x;}
inline cmat unfix(const cmat &x) {return x;}
template<class T>
class Convert
{
public:
typedef double to_double;
};
template<>
class Convert<CFix>
{
public:
typedef std::complex<double> to_double;
};
template<class T>
class Convert<std::complex<T> >
{
public:
typedef std::complex<double> to_double;
};
template<class T>
class Convert<Array<T> >
{
public:
typedef Array<typename Convert<T>::to_double> to_double; // Recursive
};
template<class T>
class Convert<Vec<T> >
{
public:
typedef Vec<typename Convert<T>::to_double> to_double; // Recursive
};
template<class T>
class Convert<Mat<T> >
{
public:
typedef Mat<typename Convert<T>::to_double> to_double; // Recursive
};
template<class T>
Array<typename Convert<T>::to_double> unfix(const Array<T> &x)
{
Array<typename Convert<T>::to_double> y(x.size());
for (int i = 0; i < x.size(); i++) {
y(i) = unfix(x(i));
}
return y;
}
ITPP_EXPORT Fix abs(const Fix &x);
ITPP_EXPORT Fix real(const CFix &x);
ITPP_EXPORT Fix imag(const CFix &x);
ITPP_EXPORT CFix conj(const CFix &x);
} // namespace itpp
#endif // #ifndef FIX_FUNCTIONS_H
array.h
Definition of Array class (container).
cfix.h
Definitions of a complex fixed-point data type CFix.
itpp::Array
General array class.
Definition array.h:105
itpp::Array::size
int size() const
Returns the number of data elements in the array object.
Definition array.h:155
itpp::Array::set_size
void set_size(int n, bool copy=false)
Resizing an Array<T>.
Definition array.h:257
itpp::CFix
Complex fixed-point data type.
Definition cfix.h:52
itpp::CFix::lshift
void lshift(int n)
Left shift n bits.
Definition cfix.cpp:213
itpp::CFix::rshift
void rshift(int n)
Right shift n bits using quantization mode qmode (constructor argument).
Definition cfix.cpp:221
itpp::CFix::unfix
std::complex< double > unfix() const
Conversion to std::complex<double>.
Definition cfix.cpp:235
itpp::CFix::set
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
itpp::Fix_Base::get_shift
int get_shift() const
Get shift.
Definition fix_base.h:1001
itpp::Fix
Fixed-point data type.
Definition fix.h:52
itpp::Fix::lshift
void lshift(int n)
Left shift n bits.
Definition fix.cpp:138
itpp::Fix::rshift
void rshift(int n)
Right shift n bits using quantization mode qmode (constructor argument).
Definition fix.cpp:145
itpp::Fix::set
void set(double x, int n)
Set to x * pow2(n) using quantization mode qmode (constructor argument).
Definition fix.cpp:126
itpp::Fix::unfix
double unfix() const
Conversion to double.
Definition fix.cpp:157
itpp::Mat
Matrix Class (Templated).
Definition mat.h:202
itpp::Mat::set_size
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
itpp::Mat::set
void set(const std::string &str)
Set matrix equal to values in the string str.
Definition mat.h:762
itpp::Mat::rows
int rows() const
The number of rows.
Definition mat.h:237
itpp::Mat::cols
int cols() const
The number of columns.
Definition mat.h:235
itpp::Mat::size
int size() const
The number of elements.
Definition mat.h:239
itpp::Vec
Vector Class (Templated).
Definition vec.h:245
itpp::Vec::set
void set(const char *str)
Set the vector equal to the values in the str string.
Definition vec.h:814
itpp::Vec::size
int size() const
The size of the vector.
Definition vec.h:271
itpp::Vec::set_size
void set_size(int size, bool copy=false)
Set length of vector. if copy = true then keeping the old values.
Definition vec.h:663
itpp::Vec::length
int length() const
The size of the vector.
Definition vec.h:269
converters.h
Definitions of converters between different vector and matrix types.
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
itpp::cfixvec
Vec< CFix > cfixvec
Typedef for complex fixed-point vector type.
Definition cfix.h:178
itpp::set_fix
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
itpp::rshift_fix
void rshift_fix(Fix &y, int n)
Right shift n bits using the quantization mode of y.
Definition fix_functions.h:220
itpp::to< double >
vec to< double >(const vec &x)
Convert vec to vec.
Definition fix_functions.h:387
itpp::unfix
double unfix(const Fix &x)
Convert Fix to double by multiplying the bit representation with pow2(-shift).
Definition fix_functions.h:463
itpp::lshift_fix
void lshift_fix(Fix &y, int n)
Left shift n bits.
Definition fix_functions.h:218
itpp::cfixmat
Mat< CFix > cfixmat
Typedef for complex fixed-point matrix type.
Definition cfix.h:180
itpp::to
T to(double x)
Convert double to T.
Definition fix_functions.h:365
itpp::to< CFix >
cfixvec to< CFix >(const cfixvec &x)
Convert cfixvec to cfixvec.
Definition fix_functions.h:393
itpp::q_mode
q_mode
Quantization modes (aligned with SystemC).
Definition fix_base.h:957
itpp::fixmat
Mat< Fix > fixmat
Typedef for fixed-point matrix type.
Definition fix.h:153
itpp::assert_fixshift
void assert_fixshift(double, int)
If x is a fixed-point variable, assert that x has the specified shift value, otherwise do nothing.
Definition fix_functions.h:312
itpp::to< Fix >
fixvec to< Fix >(const fixvec &x)
Convert fixvec to fixvec.
Definition fix_functions.h:391
itpp::is_fix
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
itpp::fixvec
Vec< Fix > fixvec
Typedef for fixed-point vector type.
Definition fix.h:151
itpp::imag
vec imag(const cvec &data)
Imaginary part of complex values.
Definition elem_math.cpp:180
itpp::real
vec real(const cvec &data)
Real part of complex values.
Definition elem_math.cpp:157
itpp::conj
cvec conj(const cvec &x)
Conjugate of complex value.
Definition elem_math.h:226
mat.h
Matrix Class Definitions.
itpp
itpp namespace
Definition itmex.h:37
itpp::to_cvec
cvec to_cvec(const Vec< T > &v)
Converts a Vec<T> to cvec.
Definition converters.h:107
itpp::to_cmat
cmat to_cmat(const Mat< T > &m)
Converts a Mat<T> to cmat.
Definition converters.h:232
itpp::to_vec
vec to_vec(const Vec< T > &v)
Converts a Vec<T> to vec.
Definition converters.h:93
itpp::to_str
std::string to_str(const T &i)
Convert anything to string.
Definition converters.h:444
itpp::to_mat
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
Definition converters.h:216
itpp::abs
bin abs(const bin &inbin)
absolute value of bin
Definition binary.h:174
vec.h
Templated Vector Class Definitions.

Generated on for IT++ by Doxygen 1.15.0