Main Content

Handle Complex Data in C MEX File

The example convec.c takes two complex row vectors and convolves them. The MEX file uses functions in the C Matrix API.

Create Source File

The following statements document the MEX function.

/*=========================================================
 * convec.c
 * example for passing complex data from MATLAB to C and back again
 *
 * convolves  two complex input vectors
 *
 * This is a MEX-file for MATLAB.
 * Copyright 1984-2017 The MathWorks, Inc.
 *=======================================================*/

Create Gateway Routine and Verify Input and Output Parameters

The following statements add the C/C++ header file mex.h and creates the mexFunction entry point.

#include "mex.h"

/* The gateway routine. */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
    return;
}

The following statements validate the number of parameters.

    /* check for the proper number of arguments */
    if(nrhs != 2)
      mexErrMsgIdAndTxt( "MATLAB:convec:invalidNumInputs",
              "Two inputs required.");
    if(nlhs > 1)
      mexErrMsgIdAndTxt( "MATLAB:convec:maxlhs",
              "Too many output arguments.");

The following statement verifies that the input arguments are row vectors.

    /*Check that both inputs are row vectors*/
    if( mxGetM(prhs[0]) != 1 || mxGetM(prhs[1]) != 1 )
      mexErrMsgIdAndTxt( "MATLAB:convec:inputsNotVectors",
              "Both inputs must be row vectors.");

The following statement verifies that the input arguments are complex.

    /* Check that both inputs are complex*/
    if( !mxIsComplex(prhs[0]) || !mxIsComplex(prhs[1]) )
      mexErrMsgIdAndTxt( "MATLAB:convec:inputsNotComplex",
              "Inputs must be complex.\n");

Create Output mxArray

The following statements define the parameters for creating the output mxArray.

    size_t rows, cols;
    size_t nx, ny;

    /* get the length of each input vector */
    nx = mxGetN(prhs[0]);
    ny = mxGetN(prhs[1]);

    rows = 1; 
    cols = nx + ny - 1;

The following statement creates an array and sets the output pointer plhs[0] to it.

    plhs[0] = mxCreateDoubleMatrix( (mwSize)rows, (mwSize)cols, mxCOMPLEX);

Create Computational Routine

The following statements define convec, the computational routine.

void convec(mxArray * x, mxArray * y, mxArray * z,
            size_t nx, size_t ny)
{
    mwSize i,j;

    /* get pointers to the complex arrays */
    mxComplexDouble * xc = mxGetComplexDoubles(x);
    mxComplexDouble * yc = mxGetComplexDoubles(y);
    mxComplexDouble * zc = mxGetComplexDoubles(z);
    zc[0].real = 0;
    zc[0].imag = 0;
    /* perform the convolution of the complex vectors */
    for(i=0; i<nx; i++) {
        for(j=0; j<ny; j++) {
            zc[i+j].real =
            zc[i+j].real + xc[i].real * yc[j].real - xc[i].imag * yc[j].imag;

            zc[i+j].imag =
            zc[i+j].imag + xc[i].real * yc[j].imag + xc[i].imag * yc[j].real;
        }
    }
}

Call convec

The following statement calls the computational routine.

    convec(prhs[0], prhs[1], plhs[0], nx, ny);

Build and Test

At the MATLAB® command prompt type:

mex -R2018a convec.c

Test the MEX file.

x = [3.000 - 1.000i, 4.000 + 2.000i, 7.000 - 3.000i];
y = [8.000 - 6.000i, 12.000 + 16.000i, 40.000 - 42.000i];
z = convec(x,y)
z =
   1.0e+02 *

Columns 1 through 4 

0.1800 - 0.2600i 0.9600 + 0.2800i 1.3200 - 1.4400i 3.7600 - 0.1200i

Column 5 

1.5400 - 4.1400i

Compare the results with the built-in MATLAB function conv.

conv(x,y)

See Also

Related Topics