필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
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;
}

답변 (1개)

Suman
Suman 2024년 5월 8일
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!

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by