msg_pt100/users/linear.c

61 lines
1.1 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "linear.h"
#include <stdlib.h>
long point_v[1024] = {0};
long point_l[1024] = {0};
int point_cnt = 0;
double k = 0;
double b = 0;
double theory_point[1024] = {0};
double linearity = 0;
extern double fabs(double __x);
/// <LinearFit>
/// 利用两点法进行线性拟合 y = kx + b,
/// <x,y—存放长度与电压的数组>
void linear_fit()
{
// Ax+By+C=0
double A = 0;
double B = 0;
double C = 0;
int m = point_cnt;
A = point_v[m - 1] - point_v[0];
B = point_l[0] - point_l[m-1];
C = point_l[m - 1] * point_v[0] - point_l[0] * point_v[m - 1];
k = -(A / B);
b = -(C / B);
}
/// <summary>
/// 更新数组y替换为电压理论值
void linear_val()
{
for (int i = 0; i < point_cnt; i++)
{
theory_point[i] = k * point_l[i] + b;
}
}
void get_linearity()
{
double max_bias = 0;
linear_fit();
linear_val();
for (int i = 0; i < point_cnt; i++)
{
if(fabs(max_bias) < fabs(point_v[i] - theory_point[i]))
{
max_bias = point_v[i] - theory_point[i];
}
}
linearity = fabs(max_bias / (point_v[point_cnt-1] - point_v[0]));
}