223 lines
9.2 KiB
C++
223 lines
9.2 KiB
C++
#include "Fit.h"
|
|
#include <QtMath>
|
|
bool CFit:: LinearFit(QVector<double>& _watch_x,QVector<double>& _watch_y,QVector<double>& _outputData,double &_output_vari)
|
|
{
|
|
Q_UNUSED(_output_vari);
|
|
//客户默认使用使用的线性
|
|
if(_watch_x.size()!=_watch_y.size())
|
|
{
|
|
return false;
|
|
}
|
|
QVector<double>::size_type size = _watch_x.size();
|
|
double AA=0.0,BB=0.0,CC=0.0,DD=0.0;
|
|
for(QVector<double>::size_type pos=0;pos<size;pos++)
|
|
{
|
|
AA += _watch_y[pos];
|
|
BB += _watch_x[pos];
|
|
CC += _watch_y[pos]*_watch_x[pos];
|
|
DD += _watch_x[pos]*_watch_x[pos];
|
|
}
|
|
double divison = (BB*BB-(size+1)*DD);
|
|
if(0.0 == divison)
|
|
{
|
|
return false;
|
|
}
|
|
double factor_one = (AA*BB-(size+1)*CC)/divison;
|
|
double factor_two = (BB*CC-AA*DD)/divison;
|
|
_outputData.clear();
|
|
_outputData.append(factor_one);
|
|
_outputData.append(factor_two);
|
|
return true;
|
|
}
|
|
bool CFit::LinearFitEquation(QVector<double>& _watch_x,QVector<double>& _fit_para,QVector<int>& _output_y)
|
|
{
|
|
if(_fit_para.size()<2)
|
|
{
|
|
return false;
|
|
}
|
|
_output_y.clear();
|
|
double dTemp = 0.0;
|
|
QVector<double>::size_type size = _watch_x.size();
|
|
for(QVector<double>::size_type pos=0;pos<size;pos++)
|
|
{
|
|
dTemp = _fit_para.at(0)*_watch_x.at(pos)+_fit_para.at(1);
|
|
_output_y.append(dTemp);
|
|
}
|
|
return true;
|
|
}
|
|
bool CFit::LinearFitEquation(QVector<double>& _watch_x,QVector<double>& _fit_para,QVector<double>& _output_y)
|
|
{
|
|
if(_fit_para.size()<2)
|
|
{
|
|
return false;
|
|
}
|
|
_output_y.clear();
|
|
double dTemp = 0.0;
|
|
QVector<double>::size_type size = _watch_x.size();
|
|
for(QVector<double>::size_type pos=0;pos<size;pos++)
|
|
{
|
|
dTemp = _fit_para.at(0)*_watch_x.at(pos)+_fit_para.at(1);
|
|
_output_y.append(dTemp);
|
|
}
|
|
return true;
|
|
}
|
|
bool CFit::_2PloynimialFit(QVector<double>& _watch_x,QVector<double>& _watch_y,QVector<double>& _outputData,double &_output_vari)
|
|
{
|
|
Q_UNUSED(_output_vari);
|
|
QVector<double> rData(3,0);
|
|
//取整运算判断:
|
|
double divison = 0.0;
|
|
divison = (1*ArrayPowerSumAverage(_watch_y,4)-qPow(ArrayPowerSumAverage(_watch_y,2),2));
|
|
if(0.0 == divison)
|
|
{
|
|
return false;
|
|
}
|
|
divison = (1*ArrayPowerSumAverage(_watch_y,2)-qPow(ArrayPowerSumAverage(_watch_y),2));
|
|
if(0.0 == divison)
|
|
{
|
|
return false;
|
|
}
|
|
divison = (1-
|
|
((ArrayPowerSumAverage(_watch_y)*ArrayPowerSumAverage(_watch_y,2)-1*ArrayPowerSumAverage(_watch_y,3)) / \
|
|
(1*ArrayPowerSumAverage(_watch_y,2)-qPow(ArrayPowerSumAverage(_watch_y),2))) * \
|
|
((ArrayPowerSumAverage(_watch_y)*ArrayPowerSumAverage(_watch_y,2)-1*ArrayPowerSumAverage(_watch_y,3)) / \
|
|
(1*ArrayPowerSumAverage(_watch_y,4)-qPow(ArrayPowerSumAverage(_watch_y,2),2))) \
|
|
);
|
|
if(0.0 == divison)
|
|
{
|
|
return false;
|
|
}
|
|
//开始计算方程系数
|
|
rData[2] = ( \
|
|
((1*TwoArrayPowerSumAverage(_watch_x,_watch_y,2)-ArrayPowerSumAverage(_watch_x)*ArrayPowerSumAverage(_watch_y,2))/
|
|
(1*ArrayPowerSumAverage(_watch_y,4)-qPow(ArrayPowerSumAverage(_watch_y,2),2))) + \
|
|
((1*TwoArrayPowerSumAverage(_watch_y,_watch_x)-ArrayPowerSumAverage(_watch_x)*ArrayPowerSumAverage(_watch_y)) / \
|
|
(1*ArrayPowerSumAverage(_watch_y,2)-qPow(ArrayPowerSumAverage(_watch_y),2))) * \
|
|
((ArrayPowerSumAverage(_watch_y)*ArrayPowerSumAverage(_watch_y,2)-1*ArrayPowerSumAverage(_watch_y,3)) / \
|
|
(1*ArrayPowerSumAverage(_watch_y,4)-qPow(ArrayPowerSumAverage(_watch_y,2),2))) \
|
|
) / \
|
|
(1-
|
|
((ArrayPowerSumAverage(_watch_y)*ArrayPowerSumAverage(_watch_y,2)-1*ArrayPowerSumAverage(_watch_y,3)) / \
|
|
(1*ArrayPowerSumAverage(_watch_y,2)-qPow(ArrayPowerSumAverage(_watch_y),2))) * \
|
|
((ArrayPowerSumAverage(_watch_y)*ArrayPowerSumAverage(_watch_y,2)-1*ArrayPowerSumAverage(_watch_y,3)) / \
|
|
(1*ArrayPowerSumAverage(_watch_y,4)-qPow(ArrayPowerSumAverage(_watch_y,2),2))) \
|
|
);
|
|
rData[1] = ( \
|
|
(1*TwoArrayPowerSumAverage(_watch_y,_watch_x)-ArrayPowerSumAverage(_watch_x)*ArrayPowerSumAverage(_watch_y)) / \
|
|
(1*ArrayPowerSumAverage(_watch_y,2)-qPow(ArrayPowerSumAverage(_watch_y),2)) \
|
|
)+ \
|
|
rData[2]*( \
|
|
(ArrayPowerSumAverage(_watch_y)*ArrayPowerSumAverage(_watch_y,2)-1*ArrayPowerSumAverage(_watch_y,3))/ \
|
|
(1*ArrayPowerSumAverage(_watch_y,2)-qPow(ArrayPowerSumAverage(_watch_y),2)) \
|
|
);
|
|
rData[0] = ( \
|
|
ArrayPowerSumAverage(_watch_x)-rData[1]*ArrayPowerSumAverage(_watch_y)-rData[2]*ArrayPowerSumAverage(_watch_y,2) \
|
|
)/ \
|
|
1;
|
|
_outputData = rData;
|
|
return true;
|
|
}
|
|
bool CFit::_2PloynimialFitEquation(QVector<double>& _watch_x,QVector<double>& _fit_para,QVector<int>& _output_y)
|
|
{
|
|
_output_y.clear();
|
|
if(_fit_para.size()<3||_watch_x.size()<0) //刻度系数大小错误 或 限制数据不存在
|
|
{
|
|
return false;
|
|
}
|
|
int dataTemp = 0;
|
|
for(QVector<double>::size_type pos=0;pos<_watch_x.size();pos++)
|
|
{
|
|
dataTemp = int(_fit_para[0]+_fit_para[1]*_watch_x[pos]+_fit_para[2]*_watch_x[pos]*_watch_x[pos]+0.5);
|
|
_output_y.append(dataTemp);
|
|
}
|
|
return true;
|
|
}
|
|
bool CFit::_2PloynimialFitEquation(QVector<double>& _watch_x,QVector<double>& _fit_para,QVector<double>& _output_y)
|
|
{
|
|
_output_y.clear();
|
|
if(_fit_para.size()<3||_watch_x.size()<0) //刻度系数大小错误 或 限制数据不存在
|
|
{
|
|
return false;
|
|
}
|
|
double dataTemp = 0;
|
|
for(QVector<double>::size_type pos=0;pos<_watch_x.size();pos++)
|
|
{
|
|
dataTemp = _fit_para[0]+_fit_para[1]*_watch_x[pos]+_fit_para[2]*_watch_x[pos]*_watch_x[pos];
|
|
_output_y.append(dataTemp);
|
|
}
|
|
return true;
|
|
}
|
|
bool CFit::_3PloynimialFit(QVector<double>& _watch_x,QVector<double>& _watch_y,QVector<double>& _outputData,double &_output_vari)
|
|
{
|
|
Q_UNUSED(_watch_x);
|
|
Q_UNUSED(_watch_y);
|
|
Q_UNUSED(_outputData);
|
|
Q_UNUSED(_output_vari);
|
|
return false;
|
|
}
|
|
bool CFit::_3PloynimialFitEquation(QVector<double>& _watch_x,QVector<double>& _fit_para,QVector<int>& _output_y)
|
|
{
|
|
Q_UNUSED(_watch_x);
|
|
Q_UNUSED(_fit_para);
|
|
Q_UNUSED(_output_y);
|
|
return false;
|
|
}
|
|
bool CFit::_3PloynimialFitEquation(QVector<double>& _watch_x,QVector<double>& _fit_para,QVector<double>& _output_y)
|
|
{
|
|
Q_UNUSED(_watch_x);
|
|
Q_UNUSED(_fit_para);
|
|
Q_UNUSED(_output_y);
|
|
return false;
|
|
}
|
|
bool CFit::GaussFit(QVector<double>& _watch_x,QVector<double>& _watch_y,QVector<double>& _outputData,double &_output_vari)
|
|
{
|
|
Q_UNUSED(_watch_x);
|
|
Q_UNUSED(_watch_y);
|
|
Q_UNUSED(_outputData);
|
|
Q_UNUSED(_output_vari);
|
|
return false;
|
|
}
|
|
|
|
bool CFit::GaussFitEquation(QVector<double>& _watch_x,QVector<double>& _fit_para,QVector<int>& _output_y)
|
|
{
|
|
Q_UNUSED(_watch_x);
|
|
Q_UNUSED(_fit_para);
|
|
Q_UNUSED(_output_y);
|
|
return false;
|
|
}
|
|
bool CFit::GaussFitEquation(QVector<double>& _watch_x,QVector<double>& _fit_para,QVector<double>& _output_y)
|
|
{
|
|
Q_UNUSED(_watch_x);
|
|
Q_UNUSED(_fit_para);
|
|
Q_UNUSED(_output_y);
|
|
return false;
|
|
}
|
|
double CFit::ArrayPowerSumAverage(QVector<double>& _data,int _pow)
|
|
{
|
|
double rData=0;
|
|
size_t size = _data.size();
|
|
for(size_t pos=0;pos<size;pos++)
|
|
{
|
|
rData += qPow(_data.at(pos),_pow);
|
|
}
|
|
if(size)
|
|
{
|
|
rData /=size;
|
|
}
|
|
return rData;
|
|
}
|
|
double CFit::TwoArrayPowerSumAverage(QVector<double>& _first,QVector<double>& _second,int _pow)
|
|
{
|
|
double rData=0;
|
|
size_t minSize = (_first.size()<=_second.size()?_first.size():_second.size());
|
|
for(size_t pos=0;pos<minSize;pos++)
|
|
{
|
|
rData+= _first.at(pos)*qPow(_second.at(pos),_pow);
|
|
}
|
|
if(minSize)
|
|
{
|
|
rData /= minSize;
|
|
}
|
|
return rData;
|
|
}
|