issues in creating a mex file that uses as c++ object that works on complex<int32_t> data type...

2 visualizaciones (últimos 30 días)
Hi,
I am trying to create a mex file that uses a c++ object called my_chain. The c++ function process() uses inputs that are of complex<int32_t> data type.
So, I used mxComplexInt32 to declare my arrays. Though the mex file gets compiled, Matlab crashes when I try to run the mex routine.
Any help in this regard is appreciated.
Regards
surendra
PS: Please find attached my mex file..
#include <iostream>
#include <complex>
#include "stdlib.h"
#include "mex.h"
#include "my_chain.h"
using namespace std;
/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray* prhs[])
{
complex<int> *input_data, *output_data;
int num_input_samples=0, num_output_samples=0, num_rows_out;
int bw_MHz; // Operating bandwidth
double f_rf_MHz; // RF frequency
double rs_ratio;
// Check for proper number of arguments
if (nrhs != 4) {
mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin", "mexfn_rx_ds_chain requires four input arguments.");
}
if (nlhs != 1) {
mexErrMsgIdAndTxt("MATLAB:mexcpp:nargout", "mexfn_rx_ds_chain requires two outputs argument.");
}
f_rf_MHz = mxGetScalar(prhs[0]);
bw_MHz = mxGetScalar(prhs[1]);
input_data = (complex<int> *)mxGetComplexInt32s(prhs[2]);
num_input_samples = mxGetScalar(prhs[3]);
rs_ratio = f_rf_MHz/bw_MHz;
num_rows_out = (int)((double)num_input_samples/(32.0 * rs_ratio)) + 2;
plhs[0] = mxCreateNumericMatrix(num_rows_out,1,mxINT32_CLASS, mxCOMPLEX);
output_data = (complex<int> *)mxGetComplexInt32s(plhs[0]);
// instantiate object
my_chain object1(f_rf_MHz,bw_MHz);
mexPrintf("f_rf_MHz is %f\n",f_rf_MHz);
mexPrintf("num_input_samples is %d\n",num_input_samples);
//Matlab crashes if I the below lines are executed.
for(int ii=0; ii<20; ii++)
{
mexPrintf("%d %d\n",input_data[ii].real(), input_data[ii].imag());
}
num_output_samples = object1.process(input_data, num_input_samples, output_data);
return;
}

Respuestas (1)

Suman
Suman el 8 de Mayo de 2024
Hi Surendra,
From what I understand from the provided code snippet, the issue is that you are casting an mxArray data type to a C++ type, i.e., mxComplexInt32 to complex<int>. You should not do this casting and instead work with the mx data type.
Assuming you have an array of mxComplexInt32 type in prhs[2], you can do the following:
mxComplexInt32* input_data, *output_data;
input_data = mxGetComplexInt32s(prhs[2]); // this returns a pointer to the first element in the mxArray prhs[2]
for(int ii=0; ii<20; ii++) {
mexPrintf("%d %d\n",input_data[ii].real, input_data[ii].imag);
}
Please refer to these documentations to learn more about working with complex types in mxArray:
I hope that is helpful!

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by