The real temperature test data is measured by heating a basin of water with a heating rod. The X-axis is time in seconds and the Y-axis is temperature:
1) Before filtering
2) After filtering (p=10, q=0.0001, r=0.05, kGain=0;)
2) After filtering (p=10, q=0.00001, r=1, kGain=0;), the Y axis is enlarged 10 times and rounded
.
Related C language code:
#define LINE 1024
static float prevData=0;
static float p=10, q=0.0001, r=0.05, kGain=0;
float kalmanFilter(float inData)
{
p = p + q;
kGain = p/(p+r);
inData = prevData+(kGain*(inData-prevData));
p = (1-kGain)*p;
prevData = inData;
return inData;
}
char *ReadData(FILE *fp, char *buf)
{
return fgets(buf, LINE, fp);
}
int main()
{
FILE *fp, *fp2;
char *p, *buf;
size_t len = 0;
ssize_t read;
float inData[1000];
float outData[1000];
uint32_t i,cnt=0;
fp = fopen("d2.txt", "r");
if (fp==NULL)
exit(1);
buf = (char*)malloc(LINE*sizeof(char));
p=ReadData(fp, buf);
while(p) {
inData[cnt]=atof(p);
cnt++;
p=ReadData(fp,buf);
}
fclose(fp);
for(i=0;i<cnt;i++)
outData=kalmanFilter(inData);
}
fp2 = fopen("d3.txt", "w");
for(i=0;i<cnt;i++)
fprintf(fp2, "%f\n",outData);
}
fclose(fp2);
}
Matlab code:
d2 = load('d2.txt');
plot(d2);
prevData=0.0;
p=10;
q=0.0001;
r=0.05;
kGain=0;
outData=[];
for i=1:length(d2)
p=p+q;
kGain=p/(p+r);
temp=d2(i);
temp=prevData+(kGain*(temp-prevData));
p=(1-kGain)*p;
prevData=temp;
outData(i)=temp;
end
plot(outData);
Note: d2.txt stores the input data, one per line. d3 is the output data.
The r parameter adjusts the closeness between the filtered curve and the measured curve. The smaller r is, the closer it is.
The q parameter adjusts the smoothness of the filtered curve. The smaller q is, the smoother it is.
|