#include "mex.h" #include #include #include /* Utility function that returns index into */ /* 1D array with range checking. */ int getindex(mwSignedIndex i, mwSignedIndex j, mwSignedIndex k, mwSize dim[3]) { if ((i<0) | (i>(dim[0]-1)) | (j<0) | (j>(dim[1]-1)) | (k<0) | (k>(dim[2]-1))) return(-1); else return(k*dim[0]*dim[1]+j*dim[0]+i); } /* smooth */ void smooth(double *pm, double *wmap, mwSize dim[3], double *krnl, mwSize kdim[3], double *opm) { mwIndex i=0, j=0, k=0; mwIndex ki=0, kj=0, kk=0; mwSignedIndex ndx=0, kndx=0; double ii=0.0, wgt=0.0, twgt=0.0; for (i=0; i -1) { wgt = krnl[getindex(ki,kj,kk,kdim)] * wmap[kndx]; ii += pm[kndx] * wgt; twgt += wgt; } } } } if (twgt) { opm[ndx] = ii/twgt; } else { opm[ndx]=pm[ndx]; } } } } return; } /* Gateway function with error check. */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwSize ndim, wmap_ndim, krn_ndim; mwIndex n, i; const mwSize *cdim = NULL, *wmap_cdim = NULL, *krn_cdim = NULL; mwSize dim[3], kdim[3]; double *pm = NULL; double *wmap = NULL; double *opm = NULL; double *krnl = NULL; if (nrhs == 0) mexErrMsgTxt("usage: pm = pm_pad(pm,wmap,kernel)"); if (nrhs != 3) mexErrMsgTxt("pm_smooth_phasemap_dtj: 3 input arguments required"); if (nlhs != 1) mexErrMsgTxt("pm_smooth_phasemap_dtj: 1 output argument required"); /* Get phase map. */ if (!mxIsNumeric(prhs[0]) || mxIsComplex(prhs[0]) || mxIsSparse(prhs[0]) || !mxIsDouble(prhs[0])) { mexErrMsgTxt("pm_smooth_phasemap_dtj: pm must be numeric, real, full and double"); } ndim = mxGetNumberOfDimensions(prhs[0]); if ((ndim < 2) | (ndim > 3)) { mexErrMsgTxt("pm_smooth_phasemap_dtj: pm must be 2 or 3-dimensional"); } cdim = mxGetDimensions(prhs[0]); pm = mxGetPr(prhs[0]); /* Get weight-map (reciprocal of variance). */ if (!mxIsNumeric(prhs[1]) || mxIsComplex(prhs[1]) || mxIsSparse(prhs[1]) || !mxIsDouble(prhs[1])) { mexErrMsgTxt("pm_smooth_phasemap_dtj: wmap must be numeric, real, full and double"); } wmap_ndim = mxGetNumberOfDimensions(prhs[1]); if (wmap_ndim != ndim) { mexErrMsgTxt("pm_smooth_phasemap_dtj: pm and wmap must have same dimensionality"); } wmap_cdim = mxGetDimensions(prhs[1]); for (i=0; i