With the wide application of model-based design, model in the loop simulation is also gradually more up, we today introduce several embedded common filtering algorithm matlab implementation.
I. Digital filtering algorithm to overcome the error of random interference has the following advantages:
-
Digital filtering does not need other hardware costs, only one calculation process, high reliability, no impedance matching problem. In particular, digital filter can filter very low frequency signals, which is unable to do analog filter.
-
Digital filtering is realized by software algorithm. Multiple input channels can share one filtering program to reduce system cost.
-
As long as the filtering program or operation of the filter is properly changed, its filtering characteristics can be easily changed, which will have great effect on filtering low frequency interference and random signals.
-
In the MCU system commonly used filtering algorithm finite amplitude filtering, median filtering, arithmetic average filtering, weighted average filtering, moving average filtering and so on.
Clear signal= WGN (1,1000,10); signal=10+signal; SignalFited = zeros (1100); for i=1:1000 signalFited(i)=MyFilter(@LowPasFilter,signal(i)); % change the handle name. end plot(signal); hold on; plot(signalFited); Function output= MyFilter(fun,input) %MyFilter. M output=fun(input); end
In the process of this operation, two adjacent samples are subtracted and their increments are calculated. Then, the absolute value of the increments is compared with maxErr, the maximum difference allowed by two samples. The size of maxErr is determined by the specific situation of the object under test. If it is less than or equal to the maximum allowable difference, the sampling is valid. Otherwise, take the last sample value as the sample of this data.
The limiting filtering method is mainly used to deal with the data that changes slowly, such as temperature, position of objects, etc. When using, the key is to select the appropriate gate limit maxErr. This can usually be obtained from empirical data, or experimentally if necessary.
Function out = LimRanFilter(input) % limranfilter.m maxErr=5; % The maximum allowable error if isempty(last) last=input; If end % last time values ((input – last) > maxErr | | (last – input) > maxErr) out = last; else out=input; end end
Median filtering
The process of this operation is to sample a certain parameter consecutively for N times (N is usually an odd number), and then arrange the values of the N samples in ascending order, and then take the intermediate value as the sampling value. The whole process is actually a sequence sorting process.
Median filter is more suitable to remove the fluctuation caused by accidental factors and the pulsation interference caused by the instability of sampler. If the measured value changes slowly, the effect of median filtering method is better, but if the data changes quickly, it is not suitable to use this method.
Function output= MeanFilter(input) %MeanFilter %MeanFilter. M persistent buff; if isempty(buff) buff=[2 0 0 0 0 0]; End % select sequence if(buff(1)<7) buff(buff(1))=input; buff(buff(1))=buff(buff(1))+1; else for i=2:5 if(buff(i)>buff(i+1)) temp=buff(i); buff(i)=buff(i+1); buff(i+1)=temp; end end buff(2:6)=0; buff(1)=2; Output =buff(4); end
4. Moving average filtering
The average filter algorithm has a characteristic, that is, every time to obtain an effective sampling value must be sampled for several times. When the sampling speed is slow, the real-time of the system can not be guaranteed. The moving average filtering algorithm introduced here only samples once, and the average of one sample value and several previous sample values can be used. If N sample values are averaged, N temporary storage areas of data must be opened in the storage area. Every new data collected will be stored in the temporary storage area, and at the same time remove one of the oldest data, save the N data is always the latest updated data. This data storage mode can be realized conveniently by using ring queue structure.
Function output= MovAveFilter(input) %MovAveFilter %MovAveFilter. if isempty(buff) buff=[ 0 0 0 0 0 ]; End % Sliding window persistent numOfBuff; if isempty(numOfBuff) numOfBuff=0; End %´ Persistent k; if isempty(k) k=1; End % counter buff(k)=input; k=mod((k+1),5)+1; numOfBuff=numOfBuff+1; if(numOfBuff>5) numOfBuff=5; end output=sum(buff)/numOfBuff; end
5. Low pass filtering
The differential equation of ordinary hardware RC low-pass filter is expressed as difference equation, and software algorithm can be used to simulate the function of hardware filter. The low-pass filter algorithm is deduced as follows:
Yn=a* Xn+(1-a) *Yn- where Xn — the sampling value Yn-1 — the filtering output value of the last time; A — Filter coefficient, whose value is usually much less than 1; Yn — the output value of this filtering.
Function output= LowPasFilter(input) %LowPasFilter. M persistent yn0; if isempty(yn0) yn0=0; End % Last output value a=0.5; % filter coefficient output=inputa+yn0(1-a); yn0=output; end
These are the use of more filtering algorithms, more convenient for simulation and writing, and the implementation of extended Kalman can be used according to the actual situation, in the general field these filtering algorithms have been able to achieve good results.
- End –
With the rapid development of technology, AMu Lab will keep up with the pace of technology and constantly recommend the latest technology and hardware in the robot industry to everyone. The greatest value of our training is to see our trainees make rapid progress in technology. If you are in the robotics industry, please follow our official account, we will continue to publish the most valuable information and technology in the robotics industry.
Amu Lab is committed to cutting-edge IT technology education and intelligent equipment, so that robot research and development more efficient!