Code/C-Code/dspic.c
/*******************************************************
This function filters the sensor data.
Called from the main() function.
After filtering the output data is sent to FireBird
where the triangulation is done.
*******************************************************/
This function filters the sensor data.
Called from the main() function.
After filtering the output data is sent to FireBird
where the triangulation is done.
This function filters the sensor data . Called from the main () function . After filtering the output data is sent to FireBird where the triangulation is done .
void bpfilter()
{
	int i,j;
	float chout0 = 0;
	float chout1 = 0;
	float chout2 = 0;
	float chout3 = 0;
	for (i = 0; i < BLK_LEN-1; i++)
	{
		ch0out[i] = 0;
		for(j = 0; j < NUMTAPS; j++)
		{
			if( i-j < 0)
				ch0out[i] += ((ch0in[i-j + BLK_LEN-1] * filt_coeff[j])/NUMTAPS);
			else
				ch0out[i] += ((ch0in[i-j] * filt_coeff[j])/NUMTAPS);
		}
	}
	for (i = 0; i < BLK_LEN-1; i++)
	{
		ch1out[i] = 0;
		for(j = 0; j < NUMTAPS; j++)
		{
			if( i-j < 0)
				ch1out[i] += ((ch1in[i-j + BLK_LEN-1] * filt_coeff[j])/NUMTAPS);
			else
				ch1out[i] += ((ch1in[i-j] * filt_coeff[j])/NUMTAPS);
		}
	}
	for (i = 0; i < BLK_LEN-1; i++)
	{
		ch2out[i] = 0;
		for(j = 0; j < NUMTAPS; j++)
		{
			if( i-j < 0)
				ch2out[i] += ((ch2in[i-j + BLK_LEN-1] * filt_coeff[j])/NUMTAPS);
			else
				ch2out[i] += ((ch2in[i-j] * filt_coeff[j])/NUMTAPS);
		}
	}
	for (i = 0; i < BLK_LEN-1; i++)
	{
		ch3out[i] = 0;
		for(j = 0; j < NUMTAPS; j++)
		{
			if( i-j < 0)
				ch3out[i] += ((ch3in[i-j + BLK_LEN-1] * filt_coeff[j])/NUMTAPS);
			else
				ch3out[i] += ((ch3in[i-j] * filt_coeff[j])/NUMTAPS);
		}
	}
	for(i = 0; i < BLK_LEN-1; i++)
	{
		chout0 += ch0out[i] * ch0out[i];
		chout1 += ch1out[i] * ch1out[i];
		chout2 += ch2out[i] * ch2out[i];
		chout3 += ch3out[i] * ch3out[i];		
	}
	chout0 = chout0/(BLK_LEN-1);
	chout1 = chout1/(BLK_LEN-1);
	chout2 = chout2/(BLK_LEN-1);
	chout3 = chout3/(BLK_LEN-1);
	if(chout0 > (max_val * 0.3) || chout1 > (max_val * 0.3) || chout2 > (max_val * 0.3) || chout3 > (max_val * 0.3))
	{
		while(!U1STAbits.TRMT);					
		U1TXREG = (char)chout0;					
		while(!U1STAbits.TRMT);					
		U1TXREG = (char)chout1;					
		while(!U1STAbits.TRMT);					
		U1TXREG = (char)chout2;					
		while(!U1STAbits.TRMT);					
		U1TXREG = (char)chout3;					
	}
}
void bpfilter ( ) { int i , j ; float chout0 = 0 ; float chout1 = 0 ; float chout2 = 0 ; float chout3 = 0 ; for ( i = 0 ; i < BLK_LEN - 1 ; i ++ ) { ch0out [ i ] = 0 ; for ( j = 0 ; j < NUMTAPS ; j ++ ) { if ( i - j < 0 ) ch0out [ i ] += ( ( ch0in [ i - j + BLK_LEN - 1 ] * filt_coeff [ j ] ) / NUMTAPS ) ; else ch0out [ i ] += ( ( ch0in [ i - j ] * filt_coeff [ j ] ) / NUMTAPS ) ; } } for ( i = 0 ; i < BLK_LEN - 1 ; i ++ ) { ch1out [ i ] = 0 ; for ( j = 0 ; j < NUMTAPS ; j ++ ) { if ( i - j < 0 ) ch1out [ i ] += ( ( ch1in [ i - j + BLK_LEN - 1 ] * filt_coeff [ j ] ) / NUMTAPS ) ; else ch1out [ i ] += ( ( ch1in [ i - j ] * filt_coeff [ j ] ) / NUMTAPS ) ; } } for ( i = 0 ; i < BLK_LEN - 1 ; i ++ ) { ch2out [ i ] = 0 ; for ( j = 0 ; j < NUMTAPS ; j ++ ) { if ( i - j < 0 ) ch2out [ i ] += ( ( ch2in [ i - j + BLK_LEN - 1 ] * filt_coeff [ j ] ) / NUMTAPS ) ; else ch2out [ i ] += ( ( ch2in [ i - j ] * filt_coeff [ j ] ) / NUMTAPS ) ; } } for ( i = 0 ; i < BLK_LEN - 1 ; i ++ ) { ch3out [ i ] = 0 ; for ( j = 0 ; j < NUMTAPS ; j ++ ) { if ( i - j < 0 ) ch3out [ i ] += ( ( ch3in [ i - j + BLK_LEN - 1 ] * filt_coeff [ j ] ) / NUMTAPS ) ; else ch3out [ i ] += ( ( ch3in [ i - j ] * filt_coeff [ j ] ) / NUMTAPS ) ; } } for ( i = 0 ; i < BLK_LEN - 1 ; i ++ ) { chout0 += ch0out [ i ] * ch0out [ i ] ; chout1 += ch1out [ i ] * ch1out [ i ] ; chout2 += ch2out [ i ] * ch2out [ i ] ; chout3 += ch3out [ i ] * ch3out [ i ] ; } chout0 = chout0 / ( BLK_LEN - 1 ) ; chout1 = chout1 / ( BLK_LEN - 1 ) ; chout2 = chout2 / ( BLK_LEN - 1 ) ; chout3 = chout3 / ( BLK_LEN - 1 ) ; if ( chout0 > ( max_val * 0.3 ) || chout1 > ( max_val * 0.3 ) || chout2 > ( max_val * 0.3 ) || chout3 > ( max_val * 0.3 ) ) { while ( ! U1STAbits . TRMT ) ; U1TXREG = ( char ) chout0 ; while ( ! U1STAbits . TRMT ) ; U1TXREG = ( char ) chout1 ; while ( ! U1STAbits . TRMT ) ; U1TXREG = ( char ) chout2 ; while ( ! U1STAbits . TRMT ) ; U1TXREG = ( char ) chout3 ; } }
This function filters the sensor data.
This function filters the sensor data .
// Variable definitions // BandPass filter implemented for each channel // Circular convolution is implemented. // Squaring and Calculating the average value of the samples // This commented code was used for calibrating the sensors value. // Find the max value for each sensor individually and calculate a common threshold. // All values can be sent via UART to PC where they are displayed. /*
	if(max_val_ch0 < chout0)
		max_val_ch0 = chout0;
	
	if(max_val_ch1 < chout1)
		max_val_ch1 = chout1;
	
	if(max_val_ch2 < chout2)
		max_val_ch2 = chout2;

	if(max_val_ch3 < chout3)
		max_val_ch3 = chout3;
*/ // If any of the channel data becomes greater than the threshold then send the data // to ATMEGA for traingulation.			 // Wait till transmission completes	for 1 character // transmitting channel 0 data to Firebird // Wait till transmission completes	for 1 character // transmitting channel 1 data to Firebird // Wait till transmission completes	for 1 character // transmitting channel 2 data to Firebird // Wait till transmission completes	for 1 character // transmitting channel 3 data to Firebird	