Kalman filter c language code

Searching for Kalman filter on google, it is easy to find the following post:

http://blog.csdn.net/lanbing510/article/details/8828109

This is a very simple and vivid explanation of the role of Kalman.

However, in the second half of the post, Kalman returned to a lot of mathematical reasoning and derivation, which is really a headache for students who are not majors in mathematics, or who have a bad foundation in mathematics, especially probability and random processes.
At the end of the post, kalman is implemented using matlab, which is simply named i, j, and k, which may not be too appropriate. When novices understand, there is a deep stack running in their heads.
Of course, the original author undoubtedly has an extremely deep understanding of Kalman, so he can achieve it with ease.

Here, I just briefly introduce my own understanding to the students who are in contact with Kalman for the first time. It's better to help you, please don't spray if you can't help.

2.
First of all, Kalman is a digital filter.

We can build an analog filter with hardware, input the noise-superposed analog signal into the filter, and the filter will give a response.
We take this response as the true value of the input signal after the noise is kicked off.
Of course, the filter parameters can be adjusted to make the response as close to the objective truth as possible, that is, to attenuate the noise as much as possible.

Furthermore, after we digitize the analog signal with AD, because the analog signal itself contains noise, even if there is no error in AD, the digital quantity after digitization also contains noise.
Moreover, it is unavoidable to consider AD errors. We call this error measurement error.

After digitization, the simplest one, we can measure 100 sets of data, sort them, delete the first 20, delete the last 20, and take the average of the remaining 60. This will eliminate some accidental errors.
The working mode of the Kalman filter is similar to the above average method, but his working process is more complicated. After passing the five formulas in the algorithm, the true value will be given well.

A lot of online is about multi-dimensional Kalman implementation. Understanding the multi-dimensional is more laborious.
Refer to some codes on the Internet to realize a one-dimensional filter. For students with C language foundation, it should be easy to understand.

3.
There is this post in Baidu Encyclopedia:

http://wenku.baidu.com/view/8523cb6eaf1ffc4ffe47ac24.html

The one-dimensional Kalman filter is explained, but the final printf is all discrete values, and nothing can be seen.

I refer to that piece of code, rewrite it into the following piece of code, and draw a curve in labwindows, the effect is very intuitive:

/*-------------------------------------------------------------------------------------------------------------*/ 
void KalmanFilter(unsigned int ResrcDataCnt,const double *ResrcData,double *FilterOutput,double ProcessNiose_Q,double MeasureNoise_R,double InitialPrediction)
{
        unsigned int i;
        double R = MeasureNoise_R;
        double Q = ProcessNiose_Q;
        double x_last = *ResrcData;
        double x_mid = x_last;
        double x_now;
        double p_last = InitialPrediction;
        double p_mid ;
        double p_now;
        double kg;

        for(i=0;i<ResrcDataCnt;i++)
        {                 x_mid=x_last; //x_last=x(k-1|k-1),x_mid=x(k|k-1)                 p_mid=p_last+Q; // p_mid=p(k|k-1),p_last=p(k-1|k-1),Q=noise                 kg=p_mid/(p_mid+R); //kg is kalman filter, R is noise                 x_now=x_mid +kg*(*(ResrcData+i)-x_mid);//The estimated optimal value                 p_now=(1-kg)*p_mid;//The covariance corresponding to the optimal value                 *(FilterOutput + i) = x_now;





                

                p_last = p_now; //Update the covariance value
                x_last = x_now; //Update the system status value
        }

}
/*-------------------------------------------------------------------------------------------------------------*/

The above is the filter part. Then, I simulated a piece of AD data, input it to this filter, and then look at the response of this filter, the effect is as follows:


It can be seen from the figure that the true value is 5, 1000 sets of data, and each set is randomly superimposed with 10% noise. You can observe the convergence speed and convergence accuracy of Kalman.
Convergence speed and convergence accuracy are two important metrics of Kalman.
Convergence speed and convergence accuracy are contradictory.

4.
Refer to the above code, after I optimized it a bit, it ran on STM32:

/*------------------------------------------------ -------------------------------------------------- -----------*/
/*        
        Q: process noise, Q increases, dynamic response becomes faster, and convergence stability deteriorates
        R: measurement noise, R increases, dynamic response becomes slower, and convergence is stable Sex gets better        
*/

double KalmanFilter(const double ResrcData,
                                        double ProcessNiose_Q,double MeasureNoise_R,double InitialPrediction)
{
        double R = MeasureNoise_R;
        double Q = ProcessNiose_Q;

        static        double x_last;

        double x_mid = x_last;
        double x_now;

        static        double p_last;

        double p_mid ;
        double p_now;
        double kg;        

        x_mid=x_last; //x_last=x(k-1|k-1),x_mid=x(k|k-1)
        p_mid=p_last+Q; //p_mid=p(k|k-1),p_last= p(k-1|k-1),Q=noise
        kg=p_mid/(p_mid+R); //kg is kalman filter, R is noise
        x_now=x_mid+kg*(ResrcData-x_mid);//estimated The optimal value of
                
        p_now=(1-kg)*p_mid;//The covariance corresponding to the optimal value        

        p_last = p_now; //Update the covariance value
        x_last = x_now; //Update the system status value

        return x_now;                
}

/*-------------------------------------------------------------------------------------------------------------*/

5.
I want to use an accelerometer to measure the acceleration, then integrate the acceleration to get the speed, and then integrate the speed to get the displacement.
Freescale MMA7361 for hardware, analog accelerometer, STM32 on-chip AD, as follows: The upper right corner of the hole board is MMA7361


The upper curve of the LCD is the accelerometer curve, the lower line is the speed curve, and the third is not processed.

Without Kalman, you can see that the acceleration line is very thick:


After using Kalman, the effect is as follows:

6.
Kalman needs to adjust the filter parameters according to specific applications.
Mainly: measuring noise figure, system noise figure, initial estimation. These three.
I asked the teacher, what the teacher meant is that these parameters are often empirical values, and there is no good systematic estimation method.
I personally feel that this is the direction of the development of Kalman theory. I guess many people are studying this.

7. The
attachment is a labwindows simulation C code, an STM32 MDK engineering code, a punctual atom board, and some documents.

 

Guess you like

Origin blog.csdn.net/fanxiaoduo1/article/details/102605779