外部インタフェース/API | ![]() ![]() |
複素データの操作
MATLABの複素数データは、実部と虚部に分かれています。MATLAB APIは、データの実部と虚部の(タイプdouble *
の)ポインタを出力する2つの関数mxGetPr
とmxGetPi
を提供しています。
以下の例は、2つの複素行ベクトルを受け取り、それらを畳込みます。
/* $ Revision: 1.8 $ */ /*========================================================= * convec.c * Example for illustrating how to pass complex data * from MATLAB to C and back again * * Convolves two complex input vectors. * * This is a MEX-file for MATLAB. * Copyright (c) 1984-2000 The MathWorks, Inc. *=======================================================*/ #include "mex.h" /* Computational subroutine */ void convec( double *xr, double *xi, int nx, double *yr, double *yi, int ny, double *zr, double *zi) { int i,j; zr[0]=0.0; zi[0]=0.0; /* Perform the convolution of the complex vectors. */ for(i=0; i<nx; i++) { for(j=0; j<ny; j++) { *(zr+i+j) = *(zr+i+j) + *(xr+i) * *(yr+j) - *(xi+i) * *(yi+j); *(zi+i+j) = *(zi+i+j) + *(xr+i) * *(yi+j) + *(xi+i) * *(yr+j); } } }
/* The gateway routine. */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *xr, *xi, *yr, *yi, *zr, *zi; int rows, cols, nx, ny; /* Check for the proper number of arguments. */ if(nrhs != 2) mexErrMsgTxt("Two inputs required."); if(nlhs > 1) mexErrMsgTxt("Too many output arguments."); /* Check that both inputs are row vectors. */ if( mxGetM(prhs[0]) != 1 || mxGetM(prhs[1]) != 1 ) mexErrMsgTxt("Both inputs must be row vectors."); rows = 1; /* Check that both inputs are complex. */ if( !mxIsComplex(prhs[0]) || !mxIsComplex(prhs[1]) ) mexErrMsgTxt("Inputs must be complex.\n"); /* Get the length of each input vector. */ nx = mxGetN(prhs[0]); ny = mxGetN(prhs[1]); /* Get pointers to real and imaginary parts of the inputs. */ xr = mxGetPr(prhs[0]); xi = mxGetPi(prhs[0]); yr = mxGetPr(prhs[1]); yi = mxGetPi(prhs[1]); /* Create a new array and set the output pointer to it. */ cols = nx + ny - 1; plhs[0] = mxCreateDoubleMatrix(rows, cols, mxCOMPLEX); zr = mxGetPr(plhs[0]); zi = mxGetPi(plhs[0]); /* Call the C subroutine. */ convec(xr, xi, nx, yr, yi, ny, zr, zi); return; }
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
これは、MATLAB組込み関数conv.m
での結果と一致します。
![]() | 構造体とセル配列を渡す | 8ビット、16ビット、32ビットデータの操作 | ![]() |