28#define MSG_STR_LENGTH ( 2048 )
33typedef enum _MEX_DATA_TYPES{
51void checkNumInOutArgs(
int nInputs,
int nOutputs,
int nInputs_expected,
int nOutputs_expected)
53 if(nInputs != nInputs_expected){
55 mexErrMsgIdAndTxt(
"MyToolbox:arrayProduct:nrhs",
message);
57 if(nOutputs != nOutputs_expected){
59 mexErrMsgIdAndTxt(
"MyToolbox:arrayProduct:nlhs",
message);
67 mwSize nDims, true_nDims;
70 for(i=0; i<nArgs; i++){
72 nDims = mxGetNumberOfDimensions(hData[i]);
73 pDims = mxGetDimensions(hData[i]);
75 for(j=0; j<nDims; j++)
76 true_nDims = pDims[j]!=1 ? true_nDims+1 : true_nDims;
81 if (mxIsComplex(hData[i]) || mxGetNumberOfElements(hData[i])!=1){
83 mexErrMsgIdAndTxt(
"MyToolbox:arrayProduct:notScalar",
message);
87 if (!mxIsDouble(hData[i]) || mxIsComplex(hData[i]) || true_nDims!=0){
88 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a real-valued double-precision scalar: %d", i+1);
89 mexErrMsgIdAndTxt(
"MyToolbox:arrayProduct:notScalar",
message);
93 if (!mxIsDouble(hData[i]) || !mxIsComplex(hData[i]) || true_nDims!=0){
94 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a complex-valued double-precision scalar: %d", i+1);
95 mexErrMsgIdAndTxt(
"MyToolbox:arrayProduct:notScalar",
message);
99 if (!mxIsDouble(hData[i]) || mxIsComplex(hData[i]) || true_nDims!=1){
100 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a real-valued double-precision 1-D vector: %d", i+1);
101 mexErrMsgIdAndTxt(
"MyToolbox:inputError",
message);
105 if (!mxIsDouble(hData[i]) || !mxIsComplex(hData[i]) || true_nDims!=1){
106 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a complex-valued double-precision 1-D vector: %d", i+1);
107 mexErrMsgIdAndTxt(
"MyToolbox:inputError",
message);
111 if (!mxIsDouble(hData[i]) || mxIsComplex(hData[i]) || true_nDims==0 || true_nDims>2){
112 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a real-valued double-precision 1-D vector or 2-D matrix: %d", i+1);
113 mexErrMsgIdAndTxt(
"MyToolbox:inputError",
message);
117 if (!mxIsDouble(hData[i]) || !mxIsComplex(hData[i]) || true_nDims==0 || true_nDims>2){
118 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a complex-valued double-precision 1-D vector or 2-D matrix: %d", i+1);
119 mexErrMsgIdAndTxt(
"MyToolbox:inputError",
message);
123 if( !mxIsDouble(hData[i]) || mxIsComplex(hData[i]) || true_nDims!=2) {
124 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a real-valued double-precision 2-D matrix: %d", i+1);
125 mexErrMsgIdAndTxt(
"MyToolbox:inputError",
message);
129 if( !mxIsDouble(hData[i]) || !mxIsComplex(hData[i]) || true_nDims!=2) {
130 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a complex-valued double-precision 2-D matrix: %d", i+1);
131 mexErrMsgIdAndTxt(
"MyToolbox:inputError",
message);
135 if( !mxIsDouble(hData[i]) || mxIsComplex(hData[i]) || nDims!=3) {
136 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a real-valued double-precision 3-D matrix: %d", i+1);
137 mexErrMsgIdAndTxt(
"MyToolbox:inputError",
message);
141 if( !mxIsDouble(hData[i]) || !mxIsComplex(hData[i]) || nDims!=3) {
142 snprintf(
message,
MSG_STR_LENGTH,
"The following input argument must be a complex-valued double-precision 3-D matrix: %d", i+1);
143 mexErrMsgIdAndTxt(
"MyToolbox:inputError",
message);
153 int i, j, numElements;
156 const mwSize *pDims_mx;
159 nDims_mx = mxGetNumberOfDimensions(in);
160 pDims_mx = mxGetDimensions(in);
163 (*nDims) = (int)nDims_mx;
164 (*pDims) =
realloc1d((*pDims), (*nDims)*
sizeof(
int));
165 for(i=0; i<(*nDims); i++)
166 (*pDims)[i] = (int)pDims_mx[i];
170 for(i=0; i<(*nDims); i++)
171 numElements *= (*pDims)[i];
174#if MX_HAS_INTERLEAVED_COMPLEX
177 inMatrix = mxGetData(in);
179 (*out) =
malloc1d(numElements*
sizeof(
float));
185 for(i=0; i<(*pDims)[0]; i++)
186 (*out)[i] = (float)inMatrix[i];
189 for(i=0; i<(*pDims)[0]; i++)
190 for(j=0; j<(*pDims)[1]; j++)
191 (*out)[i*(*pDims)[1]+j] = (float)inMatrix[j*(*pDims)[0]+i];
194 default: assert(0);
break;
201 int i, j, k, numElements;
202 #if MX_HAS_INTERLEAVED_COMPLEX
203 mxComplexDouble* inMatrix;
209 const mwSize *pDims_mx;
212 nDims_mx = mxGetNumberOfDimensions(in);
213 pDims_mx = mxGetDimensions(in);
216 (*nDims) = (int)nDims_mx;
217 (*pDims) =
malloc1d(*nDims*
sizeof(
int));
218 for(i=0; i<(*nDims); i++)
219 (*pDims)[i] = (int)pDims_mx[i];
223 for(i=0; i<(*nDims); i++)
224 numElements *= (*pDims)[i];
227#if MX_HAS_INTERLEAVED_COMPLEX
228 inMatrix = mxGetData(in);
230 inMatrix_r = mxGetPr(in);
231 inMatrix_i = mxGetPi(in);
234 (*out) =
malloc1d(numElements*
sizeof(float_complex));
239 case 1: assert(0);
break;
241#if MX_HAS_INTERLEAVED_COMPLEX
244 for(i=0; i<(*pDims)[0]; i++)
245 for(j=0; j<(*pDims)[1]; j++)
246 (*out)[i*(*pDims)[1]+j] = cmplxf((
float)inMatrix_r[j*(*pDims)[0]+i], (
float)inMatrix_i[j*(*pDims)[0]+i]);
250#if MX_HAS_INTERLEAVED_COMPLEX
253 for(i=0; i<(*pDims)[0]; i++)
254 for(j=0; j<(*pDims)[1]; j++)
255 for(k=0; k<(*pDims)[2]; k++)
256 (*out)[i*(*pDims)[1]*(*pDims)[2]+j*(*pDims)[2]+k] = cmplxf((
float)inMatrix_r[k*(*pDims)[1]*(*pDims)[0]+ j*(*pDims)[0]+i], (
float)inMatrix_i[k*(*pDims)[1]*(*pDims)[0]+ j*(*pDims)[0]+i]);
259 default: assert(0);
break;
266 int i, j, numElements;
269 const mwSize *pDims_mx;
272 nDims_mx = mxGetNumberOfDimensions(in);
273 pDims_mx = mxGetDimensions(in);
276 (*nDims) = (int)nDims_mx;
277 (*pDims) =
malloc1d((*nDims)*
sizeof(
int));
278 for(i=0; i<(*nDims); i++)
279 (*pDims)[i] = (int)pDims_mx[i];
283 for(i=0; i<(*nDims); i++)
284 numElements *= (*pDims)[i];
287 inMatrix = mxGetData(in);
289 (*out) =
malloc1d(numElements*
sizeof(
int));
295 for(i=0; i<(*pDims)[0]; i++)
296 (*out)[i] = (int)inMatrix[i];
299 for(i=0; i<(*pDims)[0]; i++)
300 for(j=0; j<(*pDims)[1]; j++)
301 (*out)[i*(*pDims)[1]+j] = (int)inMatrix[j*(*pDims)[0]+i];
304 default: assert(0);
break;
315 nDims_mx = (mwSize)nDims;
318 pDims_mx =
malloc1d(nDims*
sizeof(mwSize));
319 for(i=0; i<nDims; i++)
320 pDims_mx[i] = (mwSize)dims[i];
323 *out = mxCreateNumericArray(nDims_mx, pDims_mx, mxDOUBLE_CLASS, mxREAL);
324 pData = mxGetData(*out);
329 case 1: assert(0);
break;
331 for(i=0; i<dims[0]; i++)
332 for(j=0; j<dims[1]; j++)
333 pData[j*dims[0]+i] = (
double)in[i*dims[1]+j];
335 default: assert(0);
break;
346#if MX_HAS_INTERLEAVED_COMPLEX
347 mxComplexDouble* pData;
349 double *pData_r, *pData_i;
353 nDims_mx = (mwSize)nDims;
356 pDims_mx =
malloc1d(nDims*
sizeof(mwSize));
357 for(i=0; i<nDims; i++)
358 pDims_mx[i] = (mwSize)dims[i];
361 *out = mxCreateNumericArray(nDims_mx, pDims_mx, mxDOUBLE_CLASS, mxCOMPLEX);
362#if MX_HAS_INTERLEAVED_COMPLEX
363 pData = mxGetData(*out);
365 pData_r = mxGetPr(*out);
366 pData_i = mxGetPi(*out);
372 case 1: assert(0);
break;
374 for(i=0; i<dims[0]; i++){
375 for(j=0; j<dims[1]; j++){
376#if MX_HAS_INTERLEAVED_COMPLEX
377 pData[j*dims[0]+i].real = (double)crealf(in[i*dims[1]+j]);
378 pData[j*dims[0]+i].imag = (double)cimagf(in[i*dims[1]+j]);
380 pData_r[j*dims[0]+i] = (double)crealf(in[i*dims[1]+j]);
381 pData_i[j*dims[0]+i] = (double)cimagf(in[i*dims[1]+j]);
387 for(i=0; i<dims[0]; i++){
388 for(j=0; j<dims[1]; j++){
389 for(k=0; k<dims[2]; k++){
390#if MX_HAS_INTERLEAVED_COMPLEX
392 pData[k*dims[1]*dims[0]+j*dims[0]+i].real = (double)crealf(in[i*dims[1]*dims[2]+j*dims[2]+k]);
393 pData[k*dims[1]*dims[0]+j*dims[0]+i].imag = (double)cimagf(in[i*dims[1]*dims[2]+j*dims[2]+k]);
395 pData_r[k*dims[1]*dims[0]+j*dims[0]+i] = (double)crealf(in[i*dims[1]*dims[2]+j*dims[2]+k]);
396 pData_i[k*dims[1]*dims[0]+j*dims[0]+i] = (double)cimagf(in[i*dims[1]*dims[2]+j*dims[2]+k]);
402 default: assert(0);
break;
416 nDims_mx = (mwSize)nDims;
419 pDims_mx =
malloc1d(nDims*
sizeof(mwSize));
420 for(i=0; i<nDims; i++)
421 pDims_mx[i] = (mwSize)dims[i];
424 *out = mxCreateNumericArray(nDims_mx, pDims_mx, mxDOUBLE_CLASS, mxREAL);
425 pData = mxGetData(*out);
430 case 1: assert(0);
break;
432 for(i=0; i<dims[0]; i++)
433 for(j=0; j<dims[1]; j++)
434 pData[j*dims[0]+i] = (
double)in[i*dims[1]+j];
436 default: assert(0);
break;
void * malloc1d(size_t dim1_data_size)
1-D malloc (same as malloc, but with error checking)
void * realloc1d(void *ptr, size_t dim1_data_size)
1-D realloc (same as realloc, but with error checking)
Main include header for the Spatial_Audio_Framework (SAF)
void MEXdouble2SAFsingle(const mxArray *in, float **out, int *nDims, int **pDims)
Convert a mex double-precision array into single precision array for SAF.
#define MSG_STR_LENGTH
Warning/error message character length.
char message[MSG_STR_LENGTH]
Current warning/error message.
void MEXdouble2SAFsingle_int(const mxArray *in, int **out, int *nDims, int **pDims)
Convert a mex double-precision array into single precision array for SAF (integers)
void SAFsingle2MEXdouble_int(int *in, int nDims, int *dims, mxArray **out)
Convert a single precision array used by SAF to mex double-precision array (integers)
MEX_DATA_TYPES
Supported SAF/MEX data conversions.
@ SM_DOUBLE_COMPLEX_2D
Complex 2-D matrix; N x M.
@ SM_DOUBLE_COMPLEX
Scalar, complex valued; 1 x 1.
@ SM_DOUBLE_COMPLEX_1D
Complex 1-D vector; N x 1.
@ SM_DOUBLE_REAL
Scalar, real valued; 1 x 1.
@ SM_DOUBLE_REAL_3D
Real 3-D matrix; N x M x K.
@ SM_INT32
Integer; 1 x 1.
@ SM_DOUBLE_REAL_1D_OR_2D
Real 2-D matrix or 1-D vector; N x M | N x 1.
@ SM_DOUBLE_REAL_2D
Real 2-D matrix; N x M.
@ SM_DOUBLE_COMPLEX_3D
Complex 3-D matrix; N x M x K.
@ SM_DOUBLE_COMPLEX_1D_OR_2D
Complex 2-D matrix or 1-D vector; N x M | N x 1.
@ SM_DOUBLE_REAL_1D
Real 1-D vector; N x 1.
void SAFsingle2MEXdouble(float *in, int nDims, int *dims, mxArray **out)
Convert a single precision array used by SAF to mex double-precision array.
void MEXdouble2SAFsingle_complex(const mxArray *in, float_complex **out, int *nDims, int **pDims)
Convert a mex double-precision array into single precision array for SAF (complex-valued)
void checkNumInOutArgs(int nInputs, int nOutputs, int nInputs_expected, int nOutputs_expected)
Helper function to check number of inputs/outputs arguments are as expected.
void SAFsingle2MEXdouble_complex(float_complex *in, int nDims, int *dims, mxArray **out)
Convert a single precision array used by SAF to mex double-precision array (complex valued)
void checkArgDataTypes(mxArray **hData, MEX_DATA_TYPES *dataTypes, int nArgs)
Helper function to check the format of the input/output arguments are as expected.