/* mat_splder.c
 * Usage: [SVIDER] = splder(IDER, M, N, T, X, C, L, Q);
 * 
 * This is a MEX file (C program compiled by MATLAB)
 * which calls the C function SPLDER,  a function which takes coefficient
 * values from a Woltring B-spline function (gcvspl.c) and returns the
 * value of the data (in this case EMG voltages) defined by those
 * coefficients for time T.
 * 
 * The mex file outputted by the CMEX complier will be renamed "splder.mexsg"
 * which is easier to type (and makes the gateway seem seamless to the user--
 * i.e. the user thinks that he/she is calling the actual C subroutine when in
 * fact this program is called which in turn calls the actual C subroutine).
 * 
 * An explanation of the subroutine and arguments can be found in the file called
 * splder.m
 * 
 * Tony Reina                               Created: 4/7/1998
 * The Neurosciences Institute,  San Diego,  CA
 * Motor Control Research Laboratory
 * 
 * Updates:
 * 4/7/1998 by GAR
 * 
 * To compile use the CMEX compiler:
 *      cmex mat_splder.c gcvspl.c -output splder.mexsg
 */
#include <strings.h>
#include "mex.h"

double splder_();

void mexFunction(   int nlhs, 
                    mxArray *plhs[], 
                    int nrhs, 
                    const mxArray *prhs[] )
                    
{
    double *SVIDER, T, *X, *C, *Q,  value;
    double min_time,  max_time;
    int IDER, M, N, L;
    int dims[2] = {1, 1};
    
    /* Verify that there are 8 variables passed into the function */
    /* rhs = right-hand side = input variables */
    if (nrhs != 8) {
        mexErrMsgTxt(strcat("Insufficient number of passed arguments.\n", 
            "Type 'help splder' to obtain more information on this function.\n"));
    }
  
    IDER = (int) (mxGetScalar(prhs[0]));
    M = (int) (mxGetScalar(prhs[1]));
    N = (int) (mxGetScalar(prhs[2]));
    
    T = mxGetScalar(prhs[3]);
    
    X = mxGetPr(prhs[4]);    
    C = mxGetPr(prhs[5]);
    
    L = (int) (mxGetScalar(prhs[6]));
    
    Q = mxGetPr(prhs[7]);
  
    min_time = *X;              /* First time point in data */
    max_time = *(X + N - 1);    /* Last time point in data */
    /* Make sure that requested time point is within data's time range */
    if ((T < min_time) || (T > max_time))
        mexErrMsgTxt("ERROR: Timepoint outside of data range.\n");
    
    plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
    SVIDER = mxGetPr(plhs[0]); 
    
    /* Call the C subroutine SPLDER */
    /* **************************** */
    *SVIDER = splder_(&IDER, &M, &N, &T, X, C, &L, Q);
    
    /* Set the output pointer (plhs[0]) to SVIDER */
    /* ****************************************** */
    mxSetPr(plhs[0], SVIDER);  
}
                    
