/* 
 * Thomas Pantzer: t_sensor.cc, v0.1b  8.6.99 16:49 
 * 
 * 
 * Copyright 1999 by Thomas Pantzer
 * 
 * Permission to use, copy, modify, and distribute this software for noncommercial 
 * use and without fee is hereby granted, provided that the above copyright notice 
 * appear in all copies and that both that copyright notice and this permission 
 * notice appear in supporting documentation, and that the name of the Author not be 
 * used in advertising or publicity pertaining to distribution of the software 
 * without specific, written prior permission. The Author makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is" 
 * without expressed or implied warranty. 
 * 
 * 
 * pantec@isys2.informatik.uni-leipzig.de   (Thomas Pantzer) 
 * 
 */



#include "copyright.h"


#include "t_sensor.h"

#include <math.h>
#include <stdio.h>

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
//		t_Sensor
//
//_______________________________________________
void t_Sensor::Init(double loc, double avg, int len)
{
//	fprintf(stderr,"t_Sensor::Init()\n");
	alen = (double)len;
	Reset();

	Loc = loc;
	AvgV = avg;
	Value = avg;
}


void t_Sensor::Set(double d)
{
#ifdef DEBUG
	fprintf(stderr,"t_Sensor::Set(%g)\t",d); 
#endif
	Update(d);
	Value = (d - min) / size;

	Add(Value);

//	Extra += ((Value - Extra) / (double)(alen * 3));
}



//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
//		t_SensorGroup
//
//_______________________________________________


void t_SensorGroup::Init(int n, double avg, int alen, double loc[])
{
#ifdef DEBUG
	fprintf(stderr,"t_SensorGroup::Init()\n");
#endif
	nsensors = n;
	Sensor = new t_Sensor* [n];

	for (int i=0; i<nsensors; i++)
	  Sensor[i] = new t_Sensor;
	
	Reset(avg,alen,loc);
}

void t_SensorGroup::Done(void)
{
	for (int i=0; i<nsensors; i++)
	  delete Sensor[i];

	delete [] Sensor;
	nsensors = 0;
}

void t_SensorGroup::Reset(double avg, int alen, double loc[])
{
//	fprintf(stderr,"t_SensorGroup::Reset()\n");

	for (int i=0; i<nsensors; i++)
	   Sensor[i]->Init(loc[i],avg,alen);

	Range.Reset();
	total_Range.Reset();		
	min_idx = 0;
	max_idx = nsensors;
}


double t_SensorGroup::Information(void)
{
	double S = 0.0;

	for (int i=0; i<nsensors; i++)
	   S +=  (Sensor[i]->Value * log(Sensor[i]->Value));

	double I = -S / (double)nsensors;

	return I;
}


double t_SensorGroup::GetRange(void)
{
	return (Range.max - Range.min);
}


void t_SensorGroup::Update(double val[])
{
#ifdef DEBUG
	fprintf(stderr,"t_SensorGroup::Update\t");
#endif
	Range.Reset();

	for (int i=0; i<nsensors; i++)
	  {
#ifdef DEBUG
	   fprintf(stderr,"[%d]:%g\t",i,val[i]);
#endif
	   Sensor[i]->Set(val[i]);

//	   Range.Update(val[i]);
	   total_Range.Update(val[i]);	

	   switch (Range.Update(val[i]))
             {
	      case -1: min_idx = i; break;
	      case  1: max_idx = i; break;
	      default: break;	 
	     }
          }

	double size = GetRange();

	for (int i=0; i<nsensors; i++)
	  Sensor[i]->ClipV = (val[i] - Range.min) / size;
	
//	fprintf(stderr,"\n");
}











/**/
/**/
/**/
/**/
/**/
/**/
/**/
/**/
/*****  End Of File  ****************************/
