#include "mex.h"

/*
 * getwfs.c 
 *
 * multiplies an input scalar times an input matrix and outputs a
 * matrix 
 *
 * This is a MEX-file for MATLAB.
 * Copyright 1984-2000 The MathWorks, Inc.
 */

/* $Revision: 1.10 $ */

void getwfs(int *x, double *y, double *z, int pre, int post, int mrows)
{
  int i,j,count=0;
  
  for (i=0; i<mrows; i++) {
    for (j=0; j<pre+post+1; j++) {
      *(z+count) = *(y+*(x+i)-1-pre+j);
      count++;
    }
  }
}

/* the gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
  double *y,*z;
  int  *x;
  int  pre,post,mrows;   
/*status,mrows,ncols;*/
  
  /*  check for proper number of arguments */
  /* NOTE: You do not need an else statement when using mexErrMsgTxt
     within an if statement, because it will never get to the else
     statement if mexErrMsgTxt is executed. (mexErrMsgTxt breaks you out of
     the MEX-file) */
  if(nrhs!=4) 
    mexErrMsgTxt("Four inputs required.");
  if(nlhs!=1) 
    mexErrMsgTxt("One output required.");
  
  /* check to make sure the first input argument is an integer column */
  if( !mxIsClass(prhs[0],"int32") || 
      mxGetN(prhs[0])!=1 ) {
    mexErrMsgTxt("Input ind must be an integer column.");
  }
  
    /* check to make sure the second input argument is a double column */
  if( !mxIsDouble(prhs[1]) || 
      mxGetN(prhs[1])!=1 ) {
    mexErrMsgTxt("Input x must be a double column.");
  }
  
   /* check to make sure we are not out of range */
/*  if( mxGetScalar(prhs[0])-mxGetScalar(prhs[2])<1 ||
   mxGetScalar(prhs[0])+mxGetScalar(prhs[3])>mxGetM(prhs[1])) {
    mexErrMsgTxt("Input x out of range.");
  }*/
  
  /*  create a pointer to the input array ind */
  x = mxGetData(prhs[0]);
  
  /*  create a pointer to the input matrix x */
  y = mxGetPr(prhs[1]);
  
  /*  get the scalar inputs pre and post */
  pre = mxGetScalar(prhs[2]);
  post = mxGetScalar(prhs[3]);
  
  /*  get the dimensions of the integer input ind */
/*  mrows = mxGetM(prhs[1]);*/
    mrows = mxGetM(prhs[0]);
  
  /*  set the output pointer to the output matrix */
  plhs[0] = mxCreateDoubleMatrix(pre+post+1, mrows, mxREAL); 
/*mxCreateDoubleMatrix(mrows,ncols, mxREAL);*/
  
  /*  create a C pointer to a copy of the output matrix */
  z = mxGetPr(plhs[0]);
  
  /*  call the C subroutine */
  getwfs(x,y,z,pre,post,mrows);
/*getwfs(x,y,z,mrows,ncols);*/
  
}

