/* * Compute global cost matrix - companion * to the dtw R package * (c) Toni Giorgino 2007 * Distributed under GPL-2 with NO WARRANTY. * */ #include #include #include #include #ifdef DMALLOC #include "dmalloc.h" #endif #include "computeCM.h" #ifndef NAN #error "This code requires native IEEE NAN support. Possible solutions: 1) verify you are using gcc with -std=gnu99; 2) use the fallback interpreted DTW version (should happen automatically); 3) ask the author" #endif /* undo R indexing */ #define EP(ii,jj) ((jj)*nsteps+(ii)) #define EM(ii,jj) ((jj)*n+(ii)) #define CLEARCLIST { \ for(int z=0; z=nsteps) { fprintf(stderr,"error on pattern row %d, pattern number %d out of bounds\n", i,pn[i]); exit(1); } } /* assuming pattern ids are in ascending order */ int npats=pn[nsteps-1]; /* prepare a cost list per pattern */ double *clist=(double*) malloc(npats*sizeof(double)); /* lets go */ for(int j=0; j=0 && jj>=0) { /* address ok? C convention */ double cc=sc[s]; if(cc==-1.0) { clist[p]=cm[EM(ii,jj)]; } else { /* we rely on NAN to propagate */ clist[p] += cc*lm[EM(ii,jj)]; } } } int minc=argmin(clist,npats); if(minc>-1) { cm[EM(i,j)]=clist[minc]; sm[EM(i,j)]=minc+1; /* convert to 1-based */ } } } free(clist); free(sc); free(di); free(dj); free(pn); } /* return the arg min, ignoring NANs, -1 if all NANs */ int argmin(double *list, int n) { int ii=-1; double vv=INFINITY; for(int i=0; imyg2 */ #define TS 10 #define TSS (TS*TS) int main(int argc,char **argv) { int ts[]={TS,TS}; int *twm; double *tlm; int tnstepsp[]={6}; double tdir[]={1, 1, 2, 2, 3, 3, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0,-1, 1,-1, 1,-1, 1}; double *tcm; int *tsm; int i,j; twm=malloc(TSS*sizeof(int)); for( i=0;i