/* The gateway function */ void mexFunction(int nlhs, /* number of expected outputs */ mxArray *plhs[], /* array of pointers to output arguments */ int nrhs, /* number of inputs */ const mxArray *prhs[]) /* array of pointers to input arguments */ { double *x_in; /* Input time-series vector */ int numSamples, i; /* number of samples in the time series */ double m; /* embedding dimension */ double r; /* threshold value */ double *sampEnt; /* The sample entropies computed from the algorithm */ double *sampEnt_out; /* outputs */ /* Check for proper number of arguments */ if(nrhs!=3) { mexErrMsgIdAndTxt("hctsa:sampen:nrhs","Three inputs required."); } if(nlhs!=1) { mexErrMsgIdAndTxt("hctsa:sampen:nlhs","One output required."); } /* check that the first input argument is double */ if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0])) { mexErrMsgIdAndTxt("hctsa:sampen:notScalar","Input must be a vector of doubles."); } /* check that the second input argument is double */ if( !mxIsDouble(prhs[1]) || mxIsComplex(prhs[1])) { mexErrMsgIdAndTxt("hctsa:sampen:notScalar","Second input must be a double."); } /* check that the third input argument is double */ if( !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2])) { mexErrMsgIdAndTxt("hctsa:sampen:notScalar","Third input must be a double."); } /* check that number of rows in second input argument is 1 */ if(mxGetM(prhs[0])!=1) { mexErrMsgIdAndTxt("hctsa:sampen:notRowVector","Input must be a row vector."); } /* create a pointer to the real data in the input vector, x_in */ x_in = mxGetPr(prhs[0]); /* Value of embedding dimension, m */ m = mxGetScalar(prhs[1]); /* Pointer to threshold value, r */ r = mxGetScalar(prhs[2]); /* Number of samples in the time series */ numSamples = mxGetN(prhs[0]); /* Calculate number of scales needed */ sampEnt = (double *)mxCalloc(m+1, sizeof(double)); /* mexPrintf("\nRunning SampEn(%f,%f) on a time series with %d samples\n",m,r,numSamples); */ /* call the computational routine */ sampen(x_in, m, r, numSamples, sampEnt); /* assign output: */ /* mexPrintf("\nSampEn(%f,%f) completed! Assigning output....\n",m,r); */ plhs[0] = mxCreateDoubleMatrix(m+1, 1, mxREAL); sampEnt_out = mxGetPr(plhs[0]); for (i = 0; i < m; i ++) { sampEnt_out[i] = sampEnt[i]; /* mexPrintf("\n %u SampEn(%u) = %f.\n",i,i,sampEnt[i]); */ } /* Release allocated memory. */ mxFree(sampEnt); return; }
int main(int argc, char *argv[]) { double *data = NULL; double r = .2; int n, mm = 2; char *filenm = NULL; int nflag = 0; int vflag = 0; int i; /* Read input and optional arguments. */ for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'h': help(); exit(0); break; case 'r': if (argv[i][2] != '\0') { fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } if (i + 1 < argc) { i++; r = atof(argv[i]); break; } fprintf(stderr, "sampen: No value specified for option '-r'\n"); help(); exit(1); break; case 'm': if (argv[i][2] != '\0') { fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } if (i + 1 < argc) { i++; mm = atoi(argv[i]); break; } fprintf(stderr, "sampen: No value specified for option '-m'\n"); help(); exit(1); break; case 'n': if (argv[i][2] != '\0') { fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } nflag = 1; break; case 'v': if (argv[i][2] != '\0') { fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } vflag = 1; break; default: fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } } else { if (!filenm) filenm = argv[i]; else { fprintf(stderr, "sampen: Too many inputs\n"); exit(1); } } } if (!filenm) filenm = "-"; /* use the standard input if no input file specified */ data = readdata(filenm, &n); if (mm > n / 2) { fprintf(stderr, "sampen: m too large for time series of length %d\n", n); exit(1); } if (nflag) normalize(data, n); if (vflag) sampen2(data, mm, r, n); else sampen(data, mm, r, n); free(data); return 0; }