求平均值的方法很多,将所有的和相加除以个数就是平均值了。还有去最大和最小值在求平均值的方法也是不少,下面就是我觉得不错的一种,将其记录起来:
这种办法是将说有的数据全部排列起来,去掉头和尾巴,再求平均值。
#define buf_size 8 //设置缓冲区的大小
unsigned char data_buf[buf_size];//定义缓冲区
void array_up(unsigned char *pos,unsigned char num)
{
unsigned char i,j;
unsigned int pos_temp;
for(i=0; i<(num-1); i++)
{
for(j=i+1; j<num; j++)
{
if(pos[j]<pos[i])
{
pos_temp = pos[j];
pos[j] = pos[i];
pos[i] = pos_temp;
}
}
}
}
unsigned char average_value(unsigned char data)
{
unsigned char i;
unsigned int point;
data_ buf[7]= data; //将最大值和最小值改为输入值
data_uf[0] = data;
array_up(data_buf,buf_size); //数组排序
//*****除去最大两个数和最小两个数取平均值**************
point = data_buf[2];
for(i=3; i< (buf_size - 2) ; i++)
point = point + data_buf[i];
point=point /(buf_size-4);
return (unsigned char)point;
//******************************************************
}