#include "mex.h" #include void copyVector(double *, double *, unsigned int); void addVectors(double *, double *, double *, unsigned int); void setVectorToValue(double *, double, unsigned int); void setVectorToValue_int(int *, int, unsigned int); void maxVectorInPlace(double *, int *, double *, unsigned int); void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double * prior, * transmat, * obslik; int K, T, tmp; double * delta, *changes; int changesCounter; int * psi, * path; int t,k,j; double loglik; double * d; /* buffer */ double *outputToolPtr; if ( nrhs!=3 || nlhs!=3) mexErrMsgTxt("hmmViterbiC requires 3 inputs and 3 outputs"); prior=mxGetPr(prhs[0]); transmat=mxGetPr(prhs[1]); obslik=mxGetPr(prhs[2]); /* Assuming this will allow me to take row or colums as arguments. */ K = mxGetN(prhs[0]) * mxGetM(prhs[0]); /* mexPrintf("K = %d\n", K); */ tmp = mxGetN(prhs[1]); if (tmp != K) mexErrMsgTxt("The transition matrix must be of size KxK."); tmp = mxGetM(prhs[1]); if (tmp != K) mexErrMsgTxt("The transition matrix must be of size KxK."); /* meaning that obslik is K-by-T, with the chain running along with horizontal */ T = mxGetN(prhs[2]); tmp = mxGetM(prhs[1]); if (tmp != K) mexErrMsgTxt("The obslik must have K rows."); delta = mxMalloc(K*T*sizeof(double)); psi = mxMalloc(K*T*sizeof(int)); path = mxMalloc(T*sizeof(int)); t = 0; addVectors(delta + t*K, prior, obslik + t*K, K); setVectorToValue_int(psi + t*K, 0, K); d = mxMalloc(K*sizeof(double)); /* forward */ for(t=1;t=0;--t) { path[t] = psi[path[t+1] + (t+1)*K]; /*mexPrintf("Setting path[%d]=%d\n", t, path[t]); */ } changes = mxMalloc(4*T*sizeof(double)); changesCounter = 0; changes[changesCounter + 0*T] = 0; changes[changesCounter + 1*T] = 0; /* overwritten */ changes[changesCounter + 2*T] = path[0]; changes[changesCounter + 3*T] = 0; changesCounter = 1; for(t=1;t