
#define _CRT_SECURE_NO_WARNINGS	  /* disable compiler warnings for standard C functions like strcpy() */

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winuser.h>
#include <math.h>

#include "globals.h"


		/* Functions for machine detection of bite patterns, and for evaluating them against GT */

void MachineDetectBites()

{
int		i,pattern_event,start,end,dt;
int		t,t_left,t_mid;
double	HysteresisRatio=2.0,MinThreshold=10.0;

		/* if GT bites exist use them for start-10sec and end+10sec of analysis; otherwise use whole file */
start=14;		/* default is all data (14 is after cal time) */
end=TotalData-1;	/* default is all data */
if (TotalGTBites > 0)
  {
  start=GTBiteIndex[0]-150;	/* 10 second prior to first actual bite */
  end=GTBiteIndex[TotalGTBites-1]+150;		/* 10 seconds after last actual bite */
  if (start < 0) start=0;
  if (end > TotalData-1) end=TotalData-1;
  }


		/***********************************/
		/* BITE COUNTER BASELINE ALGORITHM */
		/***********************************/

TotalMDBites=0;	/* total bites detected */
pattern_event=0;	/* the current event in a bite cycle:
		/*	0=waiting for roll>T; 1=waiting for 2sec;
		/*	2=waiting for roll<-T; 3=waiting for 6sec */
i=start;
while (i <= end)
  {
  if (pattern_event == 0  &&  SmoothedData[5][i] > RollThresh)  /* in this program units are deg/sec */
    {
    pattern_event=1;
    dt=0;
    }
  if (pattern_event == 1)
    {
    dt++;
    if (dt > Time1Thresh)	/* count up to 2 seconds */
      pattern_event=2;
    }
  if (pattern_event == 2  &&  SmoothedData[5][i] < (0.0-RollThresh))
    {
    pattern_event=3;
    dt=0;
	MDBiteIndex[TotalMDBites]=i;
	TotalMDBites++;
    }
  if (pattern_event == 3)
    {
    dt++;
    if (dt > Time2Thresh)	/* count up to 8 seconds */
      {
      pattern_event=0;
      }
    }
  i++;
  }


		/* Peak detector -- was using this for researching patterns, visualizing negative and positive peaks */
TotalPeaks=0;
if (0)
  {
for (t=start; t<=end; t++)
  {
  if (fabs(SmoothedData[5][t]) < MinThreshold)
	continue;	/* at rest */
  t_left=t_mid=t;
  if (SmoothedData[5][t] >= MinThreshold)
	{
	while (SmoothedData[5][t] >= MinThreshold)
	  {
	  if (SmoothedData[5][t] > SmoothedData[5][t_mid])
		t_mid=t;
	  t++;
	  }
	PeakType[TotalPeaks]=1;
	}
  else
	{
	while (SmoothedData[5][t] <= 0.0-MinThreshold)
	  {
	  if (SmoothedData[5][t] < SmoothedData[5][t_mid])
		t_mid=t;
	  t++;
	  }
	PeakType[TotalPeaks]=-1;
	}
  PeakIndex[TotalPeaks]=t_mid;
  PeakLeft[TotalPeaks]=t_left;
  PeakRight[TotalPeaks]=t;
  TotalPeaks++;
  if (TotalPeaks >= MAX_PEAKS)
	{
	MessageBox(NULL,"MAX_PEAKS exceeded","Error",MB_OK);
	exit(0);
	}
  }
  }


}



void EvaluateBiteDetections()

{
int	  i,j;

	/* find matches from comp<->gt */
for (i=0; i<TotalGTBites; i++)
  GTMatched[i]=NOT_MATCHED;
for (i=0; i<TotalMDBites; i++)
  MDMatched[i]=NOT_MATCHED;
for (i=0; i<TotalMDBites; i++)
  {
		/* find first GT bite that is not yet matched but occurs
		** after the previous computer detected bite */
  for (j=0; j<TotalGTBites; j++)
    if (GTMatched[j] == NOT_MATCHED  &&
	(i == 0  ||  GTBiteIndex[j] > MDBiteIndex[i-1]) )
	//  && abs(gt_bite_indices[j]-comp_bite_indices[i]) <= 15)
      break;
  if (j == TotalGTBites)
    continue;	/* no GT bite can possibly match */
		/* check if this GT bite occurs before next comp detection */
  if (i == TotalMDBites-1  ||  GTBiteIndex[j] < MDBiteIndex[i+1])
    {		/* ok, match them */
    MDMatched[i]=GTBiteIndex[j];
    GTMatched[j]=MDBiteIndex[i];
    }
  }

	/* count up TP, FP and U */
TP=FP=U=0;
for (i=0; i<TotalGTBites; i++)
  if (GTMatched[i] != NOT_MATCHED)
    {
    TP++;
    }
  else
    U++;
for (i=0; i<TotalMDBites; i++)
  if (MDMatched[i] == NOT_MATCHED)
    FP++;
}
