//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//This script performs a low pass / high pass filtering with the possibility of making the derivative of the obtained signal and can be used as a starting point for other processing.
//The parameters that can simply be changed are:
// - channel: number from 1 to 8 indicates the analog input on which to perform the filter
// - highPass: if set high it indicates that a high pass filter must be performed, if false a low pass
// - derivative: indicates whether the signal must also be derived
// - filterDuration: indicates the duration in seconds of the moving average filter


filterDuration =0.5; //in seconds
highPass =false;
channel =2; //From 1 to 8 indicates the analog channel
derivative =true;


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

data =[];
channel =Math.floor(channel);
if (channel<1) channel=1;
if (channel>8) channel=8;

for (i=0;i<size;i++){ //We create an array with the data of the selected channel
	switch(channel){
		case 1: data[i]=An1[i]; break;
		case 2: data[i]=An2[i]; break;
		case 3: data[i]=An3[i]; break;
		case 4: data[i]=An4[i]; break;
		case 5: data[i]=An5[i]; break;
		case 6: data[i]=An6[i]; break;
		case 7: data[i]=An7[i]; break;
		case 8: data[i]=An8[i]; break;
		default: data[i]=dAn1[i]; break;
	}	
}
samplingTime=time[1]-time[0];
filterElements= filterDuration/samplingTime; //Number of points that compose the averaging filter
filterElements =Math.floor(filterElements);  //We force an integer number
if (filterElements ==0) filterElements =1;
if (filterElements%2==0) filterElements = filterElements +1; //the number of points must be odd in order not to create delays

value=0;
initialLenght=1;
index=0;

//If the size of the data array is smaller than the filter length the filter is cancelled
if (filterElements>=size) filterElements =1;

//Filtering is done in three parts. In the first and last phase, the data is filtered with a filter length equal to the data actually available to make the filter symmetrical. 
//In the central part a classic moving average filter is applied.

for (h=0;h<(filterElements-1)/2;h++){
	initialLenght =h*2+1;
	value =0;
	for (j=0;j<initialLenght;j++){
		index =h-(initialLenght-1)/2+j;
		value = value + data[index];
	}
	value = value/initialLenght;
	output[h]= value;
}

for (h=((filterElements-1)/2);h<size-((filterElements-1)/2);h++){
	value =0;
	for ( k=0;k<filterElements;k++){
		index =h-(filterElements-1)/2+k;
		value = value + data[index];
	}
	value = value/filterElements;
	output[h]= value;
}
for (h=size-((filterElements-1)/2);h<size;h++){
	initialLenght =(size-1-h)*2+1;
	value =0;
	for (k=0;k<initialLenght;k++){
		index =h-(initialLenght-1)/2+k;
		value = value + data[index];
	}
	value = value/initialLenght;
	output[h]= value;
}
if (highPass){ //if the hiPass is set the high pass filter is obtained with a difference with the original data
	for (i=0;i<size;i++){
		output[i]= data[i]-output[i];
	}
}
if (derivative){
	exit= [];
 	exit[0]=0;
 	for (i=1;i<size;i++){
		exit[i]=(output[i]-output[i-1])/samplingTime;
 	}
 	for (i=0;i<size;i++){
		output[i]= exit[i];
 	}
}

