void ResetBDAC(void)
	{
	int dummy ;
	QRSDet(0,1) ;	// Reset the qrs detector
	RRCount = 0 ;
	Classify(BeatBuffer,0,0,&dummy,&dummy,1) ;
	InitBeatFlag = 1 ;
   BeatQueCount = 0 ;	// Flush the beat que.
	}
void ConvertEcgToIbi::extractRtoR(QVector<int>* input, QVector<double>* output) {

	output->append(QRSDet(input->takeFirst(),1));
	double tmpQrs=0;
	double count=0;
	while (!input->empty()) {
		tmpQrs=QRSDet(input->takeFirst(),0);
		if (tmpQrs==0){
            count=count+1000/sampRate;
		}
		else {
            output->append((count+1000/sampRate)/1000);
			count=0;
		}



	}
}
int BeatDetectAndClassify(int ecgSample, int *beatType, int *beatMatch)
	{
	int detectDelay, rr =0, i, j ;
	int noiseEst = 0, beatBegin =0, beatEnd =0;
	int domType ;
	int fidAdj ;
	int tempBeat[(SAMPLE_RATE/BEAT_SAMPLE_RATE)*BEATLGTH] ;

	// Store new sample in the circular buffer.

	ECGBuffer[ECGBufferIndex] = ecgSample ;
	if(++ECGBufferIndex == ECG_BUFFER_LENGTH)
		ECGBufferIndex = 0 ;

	// Increment RRInterval count.

	++RRCount ;

	// Increment detection delays for any beats in the que.

	for(i = 0; i < BeatQueCount; ++i)
		++BeatQue[i] ;

	// Run the sample through the QRS detector.

	detectDelay = QRSDet(ecgSample,0) ;
	if(detectDelay != 0)
		{
		BeatQue[BeatQueCount] = detectDelay ;
		++BeatQueCount ;
		}

	// Return if no beat is ready for classification.

	if((BeatQue[0] < (BEATLGTH-FIDMARK)*(SAMPLE_RATE/BEAT_SAMPLE_RATE))
		|| (BeatQueCount == 0))
		{
		NoiseCheck(ecgSample,0,rr, beatBegin, beatEnd) ;	// Update noise check buffer
		return 0 ;
		}

	// Otherwise classify the beat at the head of the que.

	rr = RRCount - BeatQue[0] ;	// Calculate the R-to-R interval
	detectDelay = RRCount = BeatQue[0] ;

	// Estimate low frequency noise in the beat.
	// Might want to move this into classify().

	domType = GetDominantType() ;
	if(domType == -1)
		{
		beatBegin = MS250 ;
		beatEnd = MS300 ;
		}
	else
		{
		beatBegin = (SAMPLE_RATE/BEAT_SAMPLE_RATE)*(FIDMARK-GetBeatBegin(domType)) ;
		beatEnd = (SAMPLE_RATE/BEAT_SAMPLE_RATE)*(GetBeatEnd(domType)-FIDMARK) ;
		}
	noiseEst = NoiseCheck(ecgSample,detectDelay,rr,beatBegin,beatEnd) ;

	// Copy the beat from the circular buffer to the beat buffer
	// and reduce the sample rate by averageing pairs of data
	// points.

	j = ECGBufferIndex - detectDelay - (SAMPLE_RATE/BEAT_SAMPLE_RATE)*FIDMARK ;
	if(j < 0) j += ECG_BUFFER_LENGTH ;

	for(i = 0; i < (SAMPLE_RATE/BEAT_SAMPLE_RATE)*BEATLGTH; ++i)
		{
		tempBeat[i] = ECGBuffer[j] ;
		if(++j == ECG_BUFFER_LENGTH)
			j = 0 ;
		}

	DownSampleBeat(BeatBuffer,tempBeat) ;

	// Update the QUE.

	for(i = 0; i < BeatQueCount-1; ++i)
		BeatQue[i] = BeatQue[i+1] ;
	--BeatQueCount ;


	// Skip the first beat.

	if(InitBeatFlag)
		{
		InitBeatFlag = 0 ;
		*beatType = 13 ;
		*beatMatch = 0 ;
		fidAdj = 0 ;
		}

	// Classify all other beats.

	else
		{
		*beatType = Classify(BeatBuffer,rr,noiseEst,beatMatch,&fidAdj,0) ;
		fidAdj *= SAMPLE_RATE/BEAT_SAMPLE_RATE ;
      }

	// Ignore detection if the classifier decides that this
	// was the trailing edge of a PVC.

	if(*beatType == 100)
		{
		RRCount += rr ;
		return(0) ;
		}

	// Limit the fiducial mark adjustment in case of problems with
	// beat onset and offset estimation.

	if(fidAdj > MS80)
		fidAdj = MS80 ;
	else if(fidAdj < -MS80)
		fidAdj = -MS80 ;

	return(detectDelay-fidAdj) ;
	}