#include <math.h>
//#include <alloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "dtw.h"
float test[T_FRAME][NUM_CEPS], ref[R_FRAME][NUM_CEPS];
float D[T_FRAME][R_FRAME];
void main()
{
int i, num, t_frame, r_frame,j,k;
float dis, average, diva;
FILE *fpr, *fpt, *fp, *ftime;
time_t tm_begin, tm_end;
long FrameLength;
fpr=fopen("one.mfc","rb");
rewind(fpr);
fread(&r_frame,sizeof(long),1,fpr);
printf("frame is %d\n",r_frame);
if(r_frame > R_FRAME) printf("\nr_frame>R_FRAME!"), exit(0);
if(fseek(fpr,3*sizeof(long),0)==-1) /*跳过文件头12字节*/
{
printf("error!\n");
exit(-1);
}
fread(ref, (NUM_CEPS)*sizeof(float),r_frame,fpr);
for(k=0;k<36;k++)printf(" \n %f",ref[0][k]);
// getchar();
fclose(fpr);
//tm_begin = time(NULL); //kuang
fpt=fopen("zero.mfc","rb"); //test//
rewind(fpt);
fread(&t_frame,sizeof(long),1,fpt);
printf("\nframe is %d\n",t_frame);
if(t_frame>T_FRAME) printf("t_frame>T_FRAME"), exit(0);
if(fseek(fpr,3*sizeof(long),0)==-1) /*跳过文件头12字节*/
{
printf("error!\n");
exit(-1);
}
fread(test, (NUM_CEPS)*sizeof(float),t_frame,fpt);
for(k=0;k<36;k++)printf(" \n %f",test[0][k]);
dis = dtw(t_frame, test, r_frame, ref);
// getchar();
printf("\n dis is %f",dis);
fclose all();
return;
}
float cal_loc_dis(float *test, float *ref)
{
float temp;
int i;
temp=0.0;
for(i=0; i<NUM_CEPS; i++) temp += (test[i]-ref[i])*(test[i]-ref[i]);
return(temp);
}
void global_constraint(int x,int frame_x,int frame_y,int *high,int *low)
{
float slop1, slop2;
float y1, y2;
slop1 = (float)frame_y/(float)(2*frame_x);
slop2 = (float)(2*frame_y)/(float)(frame_x);
//Fine the lower point
if(x <= DX) y1 = 1;
else y1 = slop1*(x-DX);
y2 = slop2*(x-frame_x) + frame_y;
if (y1 > y2) *low = y1;
else *low = y2;
//Fine the upper point
y1 = DY + slop2*x;
y2 = slop1*(x-frame_x) + frame_y;
if (y1 < y2 ) *high = y1;
else *high = y2;
return;
}
/* This is the standard DTW algorithm
Local constraints : (0,1)(1,2)(1,1)(2,1)(1,0)
global variables : float D[MAX_FRAME][MAX_FRAME]
struct unit is define in dtw.h
Macro : NUM_CEPS, MAX_FRAME,LARGE
called functions : cal_loc_dis(float *test,float *ref)
global_constraint() */
float dtw( int t_frame, float (*test)[NUM_CEPS],
int r_frame, float (*ref)[NUM_CEPS] ) //kuang
{
int t, r, top, low;
float minD, temp, wt,dist;
float slope_w[] ={1.4,1.2,1.0,1.2,1.4};
if( t_frame > T_FRAME) t_frame=T_FRAME;
t_frame--; r_frame--; //kuang
//Adjust slope_w[5] according to t_frame and r_frame
temp = (float)r_frame/(float)t_frame;
if(temp<1.0) {
slope_w[0] *= (2-temp); slope_w[1] *= (2-temp);
slope_w[3] *= temp; slope_w[4] *= temp;
}
else {
temp = 1.0/temp;
slope_w[0] *= temp; slope_w[1] *= temp;
slope_w[3] *= (2-temp); slope_w[4] *= (2-temp);
}
// Initialization
for(t=0; t < T_FRAME; t++) for(r=0; r < R_FRAME; r++) D[t][r] = LARGE;
// Special treatment for the allowable starting points
for(t=0; t<=DX; t++) D[t][0] = cal_loc_dis(test[t], ref[0]);
// printf("dis is %d \n",D[1][0]);
for(r=1; r<=DY; r++) D[0][r] = cal_loc_dis(test[0], ref[r]); //kuang
for (t=1; t<=t_frame; t++)
{
global_constraint(t, t_frame, r_frame, &top, &low);
for(r=low; r<=top; r++)
{
/* choose minD from 5 paths*/
dist = cal_loc_dis(test[t],ref[r]);
minD = D[t-1][r] + slope_w[4]*dist; //(1,0)
if(t>=2)
{ temp = D[t-2][r-1] + slope_w[3]*dist; //(2,1)
if(temp < minD) minD = temp;
}
temp = D[t-1][r-1] + slope_w[2]*dist; //(1,1)
if(temp < minD) minD=temp;
if(r>=2) { //(1,2)
temp = D[t-1][r-2] + slope_w[1]*dist;
if(temp < minD) minD = temp;
}
temp = D[t][r-1] + slope_w[0]*dist; //(0,1)
if(temp < minD) minD=temp;
D[t][r] = minD;
}
}
temp = D[t_frame][r_frame]/(t_frame+1);
//printf("%f",temp);
return (temp);
}