#include "hellodialog.h"
#include "ui_hellodialog.h"
#include <QDebug>
float pY[25] = { 10.98, 11.13, 12.51, 8.40, 9.27,
8.73, 6.36, 8.50, 7.82, 9.14,
8.24, 12.19, 11.88, 9.57, 10.94,
9.58, 10.09, 8.11, 6.83, 8.88,
7.68, 8.47, 8.86, 10.38, 11.08 };
float pX[25] = { 35.3, 29.7, 30.8, 58.8, 61.4,
71.3, 74.4, 76.6, 70.7, 57.5,
46.4, 28.9, 28.1, 39.1, 46.8,
48.5, 59.3, 70.0, 70.0, 74.5,
72.1, 58.1, 44.6, 33.4, 28.6 };
float spY[25] = { 1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25 };
float spX[25] = { 1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25 };
float a;
float b;
void LineFitLeastSquares(float *data_x, float *data_y, int data_n, float &a, float &b)
{
float A = 0.0;
float B = 0.0;
float C = 0.0;
float D = 0.0;
float E = 0.0;
float F = 0.0;
for (int i=0; i<data_n; i++)
{
A += data_x[i] * data_x[i];
B += data_x[i];
C += data_x[i] * data_y[i];
D += data_y[i];
}
// 计算斜率a和截距b
float temp = 0;
if( temp = (data_n*A - B*B) )// 判断分母不为0
{
a = (data_n*C - B*D) / temp;
b = (A*D - B*C) / temp;
}
else
{
a = 1;
b = 0;
}
// 计算相关系数r
float Xmean, Ymean;
Xmean = B / data_n;
Ymean = D / data_n;
float tempSumXX = 0.0, tempSumYY = 0.0;
for (int i=0; i<data_n; i++)
{
tempSumXX += (data_x[i] - Xmean) * (data_x[i] - Xmean);
tempSumYY += (data_y[i] - Ymean) * (data_y[i] - Ymean);
E += (data_x[i] - Xmean) * (data_y[i] - Ymean);
}
F = sqrt(tempSumXX) * sqrt(tempSumYY);
float r;
r = E / F;
qDebug()<<"Send filepage len a= "<<a<<".";
qDebug()<<"Send filepage len b= "<<b<<".";
qDebug()<<"Send filepage len r*r= "<<r*r<<".";
// vResult.push_back(a);
// vResult.push_back(b);
// vResult.push_back(r*r);
}
typedef struct
{
float x;
float y;
}point;
point points[25];
float c;
float fabs(float data)
{
if(data < 0)
return (-data);
return data;
}
bool lineFit( float &a, float &b, float &c)
{
int size = 25;
int i;
float x_mean = 0;
float y_mean = 0;
for(int i = 0; i < size; i++)
{
points[i].x = pX[i];
points[i].y = pY[i];
}
for(int i = 0; i < size; i++)
{
x_mean += points[i].x;
y_mean += points[i].y;
}
x_mean /= size;
y_mean /= size; //至此,计算出了 x y 的均值
float Dxx = 0, Dxy = 0, Dyy = 0;
for(int i = 0; i < size; i++)
{
Dxx += (points[i].x - x_mean) * (points[i].x - x_mean);
Dxy += (points[i].x - x_mean) * (points[i].y - y_mean);
Dyy += (points[i].y - y_mean) * (points[i].y - y_mean);
}
float lambda = ( (Dxx + Dyy) - sqrt( (Dxx - Dyy) * (Dxx - Dyy) + 4 * Dxy * Dxy) ) / 2.0;
float den = sqrt( Dxy * Dxy + (lambda - Dxx) * (lambda - Dxx) );
if(fabs(den) < 1e-5) //针对 den=0做特殊处理
{
if( fabs(Dxx / Dyy - 1) < 1e-5) //这时没有一个特殊的直线方向,无法拟合 1e-5 == 0.00001
{
return false;
}
else
{
a = 1;
b = 0;
c = - x_mean;
}
}
else
{
a = Dxy / den;
b = (lambda - Dxx) / den;
c = - a * x_mean - b * y_mean;
}
qDebug()<<"Send filepage len a= "<<a<<".";
qDebug()<<"Send filepage len b= "<<b<<".";
qDebug()<<"Send filepage lenc= "<<c<<".";
qDebug()<<"Send filepage fabs(Dxx / Dyy - 1)= "<<fabs(Dxx / Dyy - 1)<<".";
return true;
}
HelloDialog::HelloDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::HelloDialog)
{
ui->setupUi(this);
LineFitLeastSquares(pX, pY, 25, a, b);
// LineFitLeastSquares(spX, spY, 25, a, b);
// lineFit( a, b, c);
}
HelloDialog::~HelloDialog()
{
delete ui;
}