当前位置:网站首页>Implementation notes of least square fitting conic in stm32
Implementation notes of least square fitting conic in stm32
2022-06-26 00:51:00 【To, violet】
The sensor shall be calibrated before use , Curve fitting must be carried out during calibration . It's easy to deal with it by computer , But in practice, the microcontroller is used to bid timing , Only general algebraic operations can be performed , No matrix operation , It is very inconvenient to handle . The algebraic formula for calculating the undetermined coefficients of quadratic polynomial curve fitting is derived by the least square method , It is very convenient to use these formulas to process data .
There is a set of measured data (x i , yi)i =1 , 2 , … , n , The fitting function is

One 、MATLAB Verification in China
Assume that the standard input and output curve of a sensor is as shown in Figure 1( Red curve ) Shown , The input and output curves of the sensor deviate due to the environment and external interference , Pictured 1( Blue curve ). If you want to get the correct data from the sensor, you need a correction curve , Correct the input curve with error to a red standard curve . First we have to confirm the error curve , Pictured 2 Shown . Red is the smoothed curve .


MATLAB The correction process is briefly described as follows :
- Import standard curves into MATLAB
- Import the error curve collected on site into MATLAB
- Take the standard curve and the curve with error 7 Data
- Calculation error ( Output of fitting curve )
- Set error curve by least square method
- F( standard output ) = F( Field input )-F( error )
The fitted curve is shown in the figure 3 Shown :

MATLAB The code is as follows ( Direct operation ):
%============== Define the standard ====================%
a = [310.0 305.9 302.6 300.2 298.1 296.0 294.1 292.4 290.8 288.8 287.4 286.1 284.8 283.7 282.7 281.9 281.0 280.2 279.4 278.8 278.2 277.6 277.0 276.4 275.9 275.5 275.0 274.3 273.8 273.4];
a = a/10.0;
%============== Define the actual input ====================%
b = [308.7 304.3 301.0 298.4 296.2 294.1 291.9 290.0 288.2 286.6 285.2 283.7 282.5 281.5 280.5 279.5 278.7 277.9 277.2 276.5 275.9 275.2 274.7 274.0 273.5 273.0 272.5 272.0 271.5 271.1];
b = b/10.0;
bb = [b(1),b(5),b(10),b(15),b(20),b(25),b(30)];% Data to be fitted , Take one of the input 7 Data
%============= Define standard output ( Is the error )=================%
out = [a(1)-b(1),a(5)-b(5),a(10)-b(10),a(15)-b(15),a(20)-b(20),a(25)-b(25),a(30)-b(30)];
%================= The following is the calculation parameter ===================%
b1 = sum(bb);
b2 = sum(bb.^2);
b3 = sum(bb.^3);
b4 = sum(bb.^4);
c1 = sum(out);
c2 = sum(out.*bb);
c3 = sum(out.*bb.^2);
n=7;% The length of fitting data is 7
k = n*b2*b4 + 2*b1*b2*b3 - n*b3^2 - b1^2*b4 - b2^3;%% Calculation K
a0 = ((b2*b4-b3^2)*c1 + (b2*b3-b1*b4)*c2 + (b1*b3-b2^2)*c3 )/k;
a1 = ((b2*b3-b1*b4)*c1 + (n*b4-b2^2)*c2 + (b1*b2-n*b3)*c3 )/k;
a2 = ((b1*b3-b2^2)*c1 + (b1*b2-n*b3)*c2 + (n*b2-b1^2)*c3 )/k;
%% Fit function %%%%%
syms funb x0
funb = a0 + a1*x0 + a2*x0*x0;%% function
%================ Error curve and smoothed curve ============%
error = a-b;% Get the data error
t = 1:1:30;% Define abscissa
error_p = zeros(1,30);% Define an empty vector
for i=2:30
error_p(i) = error_p(i-1)*0.8 + error(i)*(1-0.8);% First order low-pass filter
end
plot(t,error);% Draw an error curve
hold on;
plot(t,error_p);% Draw a smoothing error curve
figure;
ezplot(funb,[27,38]);% Draw a fit curve
Two 、 stay STM32 To realize
since STM32F4 After the launch of the series ,STM32 Already with DSP Arithmetic unit , Even if you need real-time curve fitting ,STM32 Can also be fully competent .
The calculation code is as follows :
/*
Show function
Error curve fitting
among n To fit the data length
among g_dData Is the curve data to be fitted (x)
among g_dOut Is the output of the fitting curve (Y)
*/
void disposeHMI()
{
double b1=0;
double b2=0;
double b3=0;
double b4=0;
double c1=0;
double c2=0;
double c3=0;
double n=0;
double k=0;
unsigned char i;
unsigned int j;
if(g_cUartData == 0xfe)// If a measurement instruction is issued
{
g_cUartData = 0;// Empty
Bi = 0;
delay_ms(200);
Bi = 1;
//============================= The following is the fitting ==========================//
//========================= The fitting data is 1 5 10 15 20 25 30 ====7 Number ======//
{
n=7;
//================ Let's calculate BCk coefficient
for(i=0;i<7;i++)
{
b1 += g_dData[i];
}
for(i=0;i<7;i++)
{
b2 += pow(g_dData[i],2);
}
for(i=0;i<7;i++)
{
b3 += pow(g_dData[i],3);
}
for(i=0;i<7;i++)
{
b4 += pow(g_dData[i],4);
}
for(i=0;i<7;i++)
{
c1 += g_dOut[i];
}
for(i=0;i<7;i++)
{
c2 += g_dOut[i]*g_dData[i];
}
for(i=0;i<7;i++)
{
c3 += g_dOut[i]*pow(g_dData,2);
}
k = n*b2*b4 + 2*b1*b2*b3 - n*b3*b3 - b1*b1*b4 - b2*b2*b2;
//============== Let's calculate the coefficient ============================//
a0 = ((b2*b4-b3*b3)*c1 + (b2*b3-b1*b4)*c2 + (b1*b3-b2*b2)*c3 )/k;
a1 = ((b2*b3-b1*b4)*c1 + (n*b4-b2*b2)*c2 + (b1*b2-n*b3)*c3 )/k;
a2 = ((b1*b3-b2*b2)*c1 + (b1*b2-n*b3)*c2 + (n*b2-b1*b1)*c3 )/k;
}
}
}
3、 ... and 、 summary
It is convenient to calculate the undetermined coefficient by using the algebraic calculation formula of fitting the undetermined coefficient with the quadratic term curve derived in this paper , then , Then curve fitting , Its fitting accuracy is also very high , In particular, the advantages of calibration with single-chip microcomputer are more significant . The author's ability is limited , If there is any error, please prompt for correction .
边栏推荐
- Leetcode 513. Find the value in the lower left corner of the tree
- [OEM special event] in the summer of "core cleaning", there are prize papers
- mysql cluster
- Mining pit record of modified field information in Dameng database
- Solution to SMT grape ball phenomenon
- Preorder and middle order traversal of forest
- 86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)
- Cloud rendering and Intel jointly create the "core" era of cloud rendering
- Electronic training.
- 鼠标拖拽围绕某个物体旋转展示
猜你喜欢

随便画画的

86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)

Redisson 3.17.4 release

Analyze the five root causes of product development failure

Installation and configuration of gradle environment

下载安装Flume

Middle order clue binary tree

Idea set the template of mapper mapping file

Regular expression introduction and some syntax

Compiler Telegram Desktop end (tdesktop) en utilisant vs2022
随机推荐
【系统架构】-什么是MDA架构、ADL、DSSA
Final review [machine learning]
Precautions for cleaning PCBA board in SMT chip processing
Reentrant functions must be used within signal processing functions
使用VS2022編譯Telegram桌面端(tdesktop)
Why is it best to use equals for integer comparisons
1-10Vmware构建自定义的网络架构
继承--圣杯模式
jarvisoj_ level2_ x64
C IO stream (I) basic concept_ Basic definition
What are the red lines of open source that should not be trodden on?
从进程的角度来解释 输入URL后浏览器会发生什么?
Stream data
flink报错:No ExecutorFactory found to execute the application
机器视觉:照亮“智”造新“视”界
Analysis and comparison of common test methods for PCBA in SMT chip processing industry
mysql cluster
SVN
Establish a j-link GDB cross debugging environment for Px4
. user. PHP website installation problems caused by INI files