本程序中电压和电流采样使用的是中位值平均滤波法,具体介绍如下:
1、中位值平均滤波法又称脉冲干扰平均滤波法,相当于“中位值滤波法”+“算术平均滤波法”。
连续采集N个数据,去掉一个最大和最小值,然后计算N-2个数的平均值。
程序实现:
#define N 100
void Middle(void)
{
char i,j,k,l;
int32_t temp;
char display1[20]={0};
char display2[20]={0};
char temp1[5],temp2[5];
int32_t voltage_ArrDataBuffer[N];
int32_t current_ArrDataBuffer[N];
int32_t sum=0,voltage_Value,current_Value,power_sump;
int32_t current_avg[N];
char hour[2],miniute[2],second[2];
for(i=0;i
{
contus_ad();
voltage_ArrDataBuffer[i]=data[0];
current_ArrDataBuffer[i]=data[1];
delay(10);
}
//voltage
for(j=0;j
{
for(k=0;k
{
if(voltage_ArrDataBuffer[k]>voltage_ArrDataBuffer[k+1])
{
temp=voltage_ArrDataBuffer[k];
voltage_ArrDataBuffer[k]=voltage_ArrDataBuffer[k+1];
voltage_ArrDataBuffer[k+1]=temp;
}
}
}
for(l=1;l
{
sum+=voltage_ArrDataBuffer[l];
// printf("voltage_ArrDataBuffer[%d] is %drn",l,voltage_ArrDataBuffer[l]);
}
voltage_Value=sum/(N-2);
//current
sum=0;
for(j=0;j
{
for(k=0;k
{
if(current_ArrDataBuffer[k]>current_ArrDataBuffer[k+1])
{
temp=current_ArrDataBuffer[k];
current_ArrDataBuffer[k]=current_ArrDataBuffer[k+1];
current_ArrDataBuffer[k+1]=temp;
}
}
}
for(l=1;l
{
sum+=current_ArrDataBuffer[l];
// printf("current_ArrDataBuffer[%d] is %drn",l,current_ArrDataBuffer[l]);
}
current_Value=sum/(N-2);
power_sump=(voltage_Value/1000.0)*current_Value;
}
结论:算法实现的测量结果一致性较好。
对于电量计测试来说,我使用的是平均值滤波法,但是采样样本大于1200,所以在测量时间内比较准确。