/*---------------------------------------------------------------------------* * IT++ * *---------------------------------------------------------------------------* * Copyright (c) 1995-2003 by Tony Ottosson, Thomas Eriksson, Pål Frenger, * * Tobias Ringström, and Jonas Samuelsson. * * * * Permission to use, copy, modify, and distribute this software and its * * documentation under the terms of the GNU General Public License is hereby * * granted. No representations are made about the suitability of this * * software for any purpose. It is provided "as is" without expressed or * * implied warranty. See the GNU General Public License for more details. * *---------------------------------------------------------------------------*/ /*! \file \brief Conversion routines between IT++ and Matlab \author Tony Ottosson and Pål Frenger 1.7 2003/05/22 08:55:15 */ #ifndef __itmex_h #define __itmex_h #include #include "itbase.h" #ifndef DOXYGEN_SHOULD_SKIP_THIS #ifndef VERSION #ifdef V4_COMPAT #define VERSION "MATLAB 4 compatible" #else #define VERSION "MATLAB R11 native" #endif /* V4_COMPAT */ #endif /* VERSION */ #ifdef ARRAY_ACCESS_INLINING #define INLINE " (inlined)" #else #define INLINE #endif /* ARRAY_ACCESS_INLINING */ static const char *version = VERSION INLINE; #ifdef __cplusplus extern "C" { #endif const char *mexVersion () { /* mex version information */ return version; } #endif //DOXYGEN_SHOULD_SKIP_THIS #ifdef __cplusplus } #endif namespace itpp { //-------------------------------------------------------- // mex -> it++ //-------------------------------------------------------- /*! \defgroup mexfiles Writing Matlab Mex-files using IT++ \brief Conversions between IT++ and Matlab for writing mex-files. \author Tony Ottosson and Pål Frenger These routines are used to help writng mex-files for speeding up matlab simulations. A simple mex-file that performs QPSK modulation is given below. \code #include #include void mexFunction(int n_output, mxArray *output[], int n_input, const mxArray *input[]) { // Check the number of inputs and output arguments if(n_output!=1) mexErrMsgTxt("Wrong number of output variables!"); if(n_input!=1) mexErrMsgTxt("Wrong number of input variables!"); // Convert input variables to IT++ format bvec input_bits = mxArray2bvec(input[0]); // ------------------ Start of routine --------------------------- cvec output_symbols; QPSK_45 qpsk; output_symbols = qpsk.modulate_bits(input_bits); // ------------------ End of routine ----------------------------- // Create output vectors output[0] = mxCreateDoubleMatrix(1,output_symbols.size(), mxCOMPLEX); // Convert the IT++ format to Matlab format for output cvec2mxArray(output_symbols, output[0]); } \endcode */ //!\addtogroup mexfiles //!@{ //! Convert the matlab-format mxArray to bin bin mxArray2bin(const mxArray *in); //! Convert the matlab-format mxArray to short short mxArray2short(const mxArray *in); //! Convert the matlab-format mxArray to int int mxArray2int(const mxArray *in); //! Convert the matlab-format mxArray to double double mxArray2double(const mxArray *in); //! Convert the matlab-format mxArray to complex complex mxArray2double_complex(const mxArray *in); //string mxArray2string(const mxArray *in); //! Convert the matlab-format mxArray to bvec bvec mxArray2bvec(const mxArray *in); //! Convert the matlab-format mxArray to svec svec mxArray2svec(const mxArray *in); //! Convert the matlab-format mxArray to ivec ivec mxArray2ivec(const mxArray *in); //! Convert the matlab-format mxArray to vec vec mxArray2vec(const mxArray *in); //! Convert the matlab-format mxArray to cvec cvec mxArray2cvec(const mxArray *in); //! Convert the matlab-format mxArray to bmat bmat mxArray2bmat(const mxArray *in); //! Convert the matlab-format mxArray to smat smat mxArray2smat(const mxArray *in); //! Convert the matlab-format mxArray to imat imat mxArray2imat(const mxArray *in); //! Convert the matlab-format mxArray to mat mat mxArray2mat(const mxArray *in); //! Convert the matlab-format mxArray to cmat cmat mxArray2cmat(const mxArray *in); //-------------------------------------------------------- // it++ -> mex //-------------------------------------------------------- //! Convert bin to the matlab-format mxArray void bin2mxArray(const bin &in, mxArray *out); //! Convert short to the matlab-format mxArray void short2mxArray(const short &in, mxArray *out); //! Convert int to the matlab-format mxArray void int2mxArray(const int &in, mxArray *out); //! Convert double to the matlab-format mxArray void double2mxArray(const double &in, mxArray *out); //! Convert complex to the matlab-format mxArray void double_complex2mxArray(const complex &in, mxArray *out); //void string2mxArray(const string &in, mxArray *out); //! Convert bvec to the matlab-format mxArray void bvec2mxArray(const bvec &in, mxArray *out); //! Convert svec to the matlab-format mxArray void svec2mxArray(const svec &in, mxArray *out); //! Convert ivec to the matlab-format mxArray void ivec2mxArray(const ivec &in, mxArray *out); //! Convert vec to the matlab-format mxArray void vec2mxArray(const vec &in, mxArray *out); //! Convert cvec to the matlab-format mxArray void cvec2mxArray(const cvec &in, mxArray *out); //! Convert bmat to the matlab-format mxArray void bmat2mxArray(const bmat &in, mxArray *out); //! Convert smat to the matlab-format mxArray void smat2mxArray(const smat &in, mxArray *out); //! Convert imat to the matlab-format mxArray void imat2mxArray(const imat &in, mxArray *out); //! Convert mat to the matlab-format mxArray void mat2mxArray(const mat &in, mxArray *out); //! Convert cmat to the matlab-format mxArray void cmat2mxArray(const cmat &in, mxArray *out); //-------------------------------------------------------- // mex -> C //-------------------------------------------------------- //! Convert the matlab-format mxArray to C-format pointer to short void mxArray2Csvec(const mxArray *in, short *out); //! Convert the matlab-format mxArray to C-format pointer to int void mxArray2Civec(const mxArray *in, int *out); //! Convert the matlab-format mxArray to C-format pointer to double void mxArray2Cvec(const mxArray *in, double *out); //! Convert the matlab-format mxArray to C-format pointers to double (real and imaginary parts) void mxArray2Ccvec(const mxArray *in, double *out_real, double *out_imag); //! Convert the matlab-format mxArray to C-format pointer to pointer to short void mxArray2Csmat(const mxArray *in, short **out); //! Convert the matlab-format mxArray to C-format pointer to pointer to int void mxArray2Cimat(const mxArray *in, int **out); //! Convert the matlab-format mxArray to C-format pointer to pointer to double void mxArray2Cmat(const mxArray *in, double **out); //! Convert the matlab-format mxArray to C-format pointer to pointer to double (real and imaginary parts) void mxArray2Ccmat(const mxArray *in, double **out_real, double **out_imag); //-------------------------------------------------------- // C -> mex //-------------------------------------------------------- //! Convert C-format pointer to short to matlab-format mxArray void Csvec2mxArray(short *in, mxArray *out); //! Convert C-format pointer to int to matlab-format mxArray void Civec2mxArray(int *in, mxArray *out); //! Convert C-format pointer to double to matlab-format mxArray void Cvec2mxArray(double *in, mxArray *out); //! Convert C-format pointers to double (real and imaginary parts) to matlab-format mxArray void Ccvec2mxArray(double *in_real, double *in_imag, mxArray *out); //! Convert C-format pointer to pointer to short to matlab-format mxArray void Csmat2mxArray(short **in, mxArray *out); //! Convert C-format pointer to pointer to int to matlab-format mxArray void Cimat2mxArray(int **in, mxArray *out); //! Convert C-format pointer to pointer to double to matlab-format mxArray void Cmat2mxArray(double **in, mxArray *out); //! Convert C-format pointer to pointer to double (real and imaginary parts) to matlab-format mxArray void Ccmat2mxArray(double **in_real, double **in_imag, mxArray *out); //!@} //--------------------------------------------------------------- bin mxArray2bin(const mxArray *in) { int size; double* temp = (double*) mxGetPr(in); if (temp==0) mexErrMsgTxt("mxArray2bin: Pointer to data is NULL"); size = mxGetNumberOfElements(in); if (size!=1) mexErrMsgTxt("mxArray2bin: Size of data is not equal to one"); return ( ( (*temp) > 0.0 ) ? bin(1) : bin(0) ); } short mxArray2short(const mxArray *in) { int size; double* temp = (double*) mxGetPr(in); if (temp==0) mexErrMsgTxt("mxArray2short: Pointer to data is NULL"); size = mxGetNumberOfElements(in); if (size!=1) mexErrMsgTxt("mxArray2short: Size of data is not equal to one"); return (short) (*temp); } int mxArray2int(const mxArray *in) { int size; double* temp = (double*) mxGetPr(in); if (temp==0) mexErrMsgTxt("mxArray2int: Pointer to data is NULL"); size = mxGetNumberOfElements(in); if (size!=1) mexErrMsgTxt("mxArray2int: Size of data is not equal to one"); return (int) (*temp); } double mxArray2double(const mxArray *in) { int size; double* temp = (double*) mxGetPr(in); if (temp==0) mexErrMsgTxt("mxArray2double: Pointer to data is NULL"); size = mxGetNumberOfElements(in); if (size!=1) mexErrMsgTxt("mxArray2double: Size of data is not equal to one"); return (*temp); } complex mxArray2double_complex(const mxArray *in) { int size; double* tempR = (double*) mxGetPr(in); double* tempI = (double*) mxGetPi(in); if ((tempR==0)&&(tempI==0)) mexErrMsgTxt("mxArray2double_complex: Pointer to data is NULL"); size = mxGetNumberOfElements(in); if (size!=1) mexErrMsgTxt("mxArray2double_complex: Size of data is not equal to one"); if (tempR==0) { return complex( 0.0 , (*tempI) ); } else if (tempI==0) { return complex( (*tempR), 0.0 ); } else { return complex( (*tempR), (*tempI) ); } } bvec mxArray2bvec(const mxArray *in) { bvec out; int i, size; double* temp = (double*) mxGetPr(in); if (temp==0) mexErrMsgTxt("mxArray2bvec: Pointer to data is NULL"); size = mxGetNumberOfElements(in); if (size==0) mexErrMsgTxt("mxArray2bvec: Size of data is zero"); out.set_size(size,false); for (i=0; i1e-5) ? bin(1) : bin(0) ); } return out; } svec mxArray2svec(const mxArray *in) { svec out; int i, size; double* temp = (double*) mxGetPr(in); if (temp==0) mexErrMsgTxt("mxArray2svec: Pointer to data is NULL"); size = mxGetNumberOfElements(in); if (size==0) mexErrMsgTxt("mxArray2svec: Size of data is zero"); out.set_size(size,false); for (i=0; i( 0.0, (*tempI++)); } } else if (tempI==0) { for (i=0; i((*tempR++), 0.0 ); } } else { for (i=0; i((*tempR++), (*tempI++)); } } return out; } bmat mxArray2bmat(const mxArray *in) { bmat out; int r, c, rows, cols; double* temp = (double*) mxGetPr(in); if (temp==0) mexErrMsgTxt("mxArray2bmat: Pointer to data is NULL"); rows = mxGetM(in); if (rows==0) mexErrMsgTxt("mxArray2bmat: Data has zero rows"); cols = mxGetN(in); if (cols==0) mexErrMsgTxt("mxArray2bmat: Data has zero columns"); out.set_size(rows,cols,false); for (c=0; c 0.0) ? bin(1) : bin(0) ); } } return out; } smat mxArray2smat(const mxArray *in) { smat out; int r, c, rows, cols; double* temp = (double*) mxGetPr(in); if (temp==0) mexErrMsgTxt("mxArray2smat: Pointer to data is NULL"); rows = mxGetM(in); if (rows==0) mexErrMsgTxt("mxArray2smat: Data has zero rows"); cols = mxGetN(in); if (cols==0) mexErrMsgTxt("mxArray2smat: Data has zero columns"); out.set_size(rows,cols,false); for (c=0; c( 0.0 ,(*tempI++) ); } } } else if (tempI==0) { for (c=0; c( (*tempR++), 0.0 ); } } } else { for (c=0; c( (*tempR++),(*tempI++) ); } } } return out; } void bvec2mxArray(const bvec &in, mxArray *out) { double* temp = (double *) mxGetPr(out); if (temp==0) mexErrMsgTxt("bvec2mxArray: Pointer to data is NULL"); if (in.size()==0) mexErrMsgTxt("bvec2mxArray: Size of data is zero"); for(int i=0; i