core/matlab/examples/refbook/convec.c

00001 /* $Revision: 1.1 $ */
00002 /*=========================================================
00003  * convec.c
00004  * example for illustrating how to use pass complex data 
00005  * from MATLAB to C and back again
00006  *
00007  * convolves  two complex input vectors
00008  *
00009  * This is a MEX-file for MATLAB.
00010  * Copyright 1984-2006 The MathWorks, Inc.
00011  *=======================================================*/
00012 #include "mex.h"
00013 
00014 /* computational subroutine */
00015 void convec( double *xr, double *xi, mwSize nx,
00016              double *yr, double *yi, mwSize ny,
00017              double *zr, double *zi)
00018 {
00019     mwSize i,j;
00020   
00021     zr[0]=0.0;
00022     zi[0]=0.0;
00023     /* perform the convolution of the complex vectors */
00024     for(i=0; i<nx; i++) {
00025     for(j=0; j<ny; j++) {
00026         *(zr+i+j) = *(zr+i+j) + *(xr+i) * *(yr+j) - *(xi+i) * *(yi+j);
00027         *(zi+i+j) = *(zi+i+j) + *(xr+i) * *(yi+j) + *(xi+i) * *(yr+j);
00028     }
00029     }
00030 }
00031 
00032 /* The gateway routine. */
00033 void mexFunction( int nlhs, mxArray *plhs[],
00034                   int nrhs, const mxArray *prhs[] )
00035 {
00036     double  *xr, *xi, *yr, *yi, *zr, *zi;
00037     mwSize  rows, cols, nx, ny;
00038     
00039     /* check for the proper number of arguments */
00040     if(nrhs != 2)
00041       mexErrMsgTxt("Two inputs required.");
00042     if(nlhs > 1)
00043       mexErrMsgTxt("Too many output arguments.");
00044     /*Check that both inputs are row vectors*/
00045     if( mxGetM(prhs[0]) != 1 || mxGetM(prhs[1]) != 1 )
00046       mexErrMsgTxt("Both inputs must be row vectors.");
00047     rows = 1; 
00048     /* Check that both inputs are complex*/
00049     if( !mxIsComplex(prhs[0]) || !mxIsComplex(prhs[1]) )
00050       mexErrMsgTxt("Inputs must be complex.\n");
00051   
00052     /* get the length of each input vector */
00053     nx = mxGetN(prhs[0]);
00054     ny = mxGetN(prhs[1]);
00055 
00056 
00057     /* get pointers to the real and imaginary parts of the inputs */
00058     xr = mxGetPr(prhs[0]);
00059     xi = mxGetPi(prhs[0]);
00060     yr = mxGetPr(prhs[1]);
00061     yi = mxGetPi(prhs[1]);
00062   
00063     /* create a new array and set the output pointer to it */
00064     cols = nx + ny - 1;
00065     plhs[0] = mxCreateDoubleMatrix(rows, cols, mxCOMPLEX);
00066     zr = mxGetPr(plhs[0]);
00067     zi = mxGetPi(plhs[0]);
00068 
00069     /* call the C subroutine */
00070     convec(xr, xi, nx, yr, yi, ny, zr, zi);
00071 
00072     return;
00073 }
00074 
00075 
00076 
00077 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy