diff --git a/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.cpp b/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.cpp index c1d8984a..e4d005cc 100644 --- a/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.cpp +++ b/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.cpp @@ -322,6 +322,9 @@ void QECorrelation::setup () QObject::connect (this->uiForm->Time_Select_Button, SIGNAL (clicked (bool)), this, SLOT (timeSelectButtonClick (bool))); + QObject::connect (this->uiForm->Show_Regression_CheckBox, SIGNAL (stateChanged (int)), + this, SLOT (plotRegressionStateChanged (int))); + QObject::connect (this->uiForm->Number_Samples_Edit, SIGNAL (valueChanged (const double)), this, SLOT (numberSamplesEditChange (const double))); @@ -688,10 +691,10 @@ void QECorrelation::setReadOut (const QString status) //------------------------------------------------------------------------------ // Refer to http://en.wikipedia.org/wiki/Correlation_and_dependence +// and to https://en.wikipedia.org/wiki/Simple_linear_regression // -double QECorrelation::calculateCorrelationCoefficient () +void QECorrelation::calculateCorrelationAndRegression () { - double result; int number; double sumX; double sumY ; @@ -709,7 +712,12 @@ double QECorrelation::calculateCorrelationCoefficient () double sdY; number = this->xData.count (); - if (number <= 0) return 0.0; + if (number <= 0){ + this->correlationCoef = 0.0; + this->regressionSlope = std::nan("0"); + this->regressionIntersept = std::nan("0"); + return ; + } // Sum x, x^2, y, y^2 and xy. // @@ -753,12 +761,19 @@ double QECorrelation::calculateCorrelationCoefficient () sdX = sqrt (varX); sdY = sqrt (varY); - result = (meanXY - meanX * meanY) / (sdX * sdY); + this->correlationCoef = (meanXY - meanX * meanY) / (sdX * sdY); } else { - result = 0.0; + this->correlationCoef = 0.0; } - return result; + if (varX != 0.0){ + this->regressionSlope = (meanXY - meanX * meanY) / varX; + this->regressionIntersept = meanY - regressionSlope*meanX; + }else{ + this->regressionSlope = std::nan("0"); + this->regressionIntersept = std::nan("0"); + } + return; } //------------------------------------------------------------------------------ @@ -825,6 +840,16 @@ void QECorrelation::reDrawPlane () pen.setStyle (Qt::DashLine); this->plotArea->setGridPen (pen); + if (this->uiForm->Show_Regression_CheckBox->checkState() == Qt::Checked){ + if (this->regressionSlope!=this->regressionSlope){ // is nan + }else{ + pen.setColor (QColor ("red")); + this->plotArea->setCurveStyle (QwtPlotCurve::Lines); + this->plotArea->setCurvePen (pen); + this->plotArea->plotCurveData ({TX_Min, TX_Max}, {this->regressionSlope*TX_Min+this->regressionIntersept, this->regressionSlope*TX_Max+this->regressionIntersept}); + } + } + pen.setColor (QColor ("blue")); pen.setStyle (Qt::SolidLine); @@ -882,13 +907,19 @@ void QECorrelation::updateDataArrays () maximumPeriod = samplePeriod * maximumPoints; currentPeriod = samplePeriod * number; - correlation = this->calculateCorrelationCoefficient (); + this->calculateCorrelationAndRegression (); this->uiForm->Number_Points_Label->setText (QString ("%1").arg (number)); this->uiForm->Maximum_Sample_Label->setText (QEUtilities::intervalToString (maximumPeriod, 0, false)); this->uiForm->Ongoing_Sample_Label->setText (QEUtilities::intervalToString (currentPeriod, 0, false)); - this->uiForm->Correlation_Value_Label->setText (QString ("%1").arg (correlation, 0, 'f', 4)); - + this->uiForm->Correlation_Value_Label->setText (QString ("%1").arg (this->correlationCoef, 0, 'f', 4)); + if (this->regressionSlope!=this->regressionSlope){ // is nan + this->uiForm->Regression_Slope_Value_Label->setText (QString ("---")); + this->uiForm->Regression_Intercept_Value_Label->setText (QString ("---")); + }else{ + this->uiForm->Regression_Slope_Value_Label->setText (QString ("%1").arg (this->regressionSlope, 0, 'f', 4)); + this->uiForm->Regression_Intercept_Value_Label->setText (QString ("%1").arg (this->regressionIntersept, 0, 'f', 4)); + } this->replotIsRequired = true; } @@ -1360,6 +1391,12 @@ void QECorrelation::timeSelectButtonClick (bool) } } +//------------------------------------------------------------------------------ +// +void QECorrelation::plotRegressionStateChanged (bool){ + this->replotIsRequired = true; +} + //------------------------------------------------------------------------------ // void QECorrelation::setArchiveData (const QObject* userData, const bool isOkay, diff --git a/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.h b/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.h index 58d3c231..5b0a3332 100644 --- a/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.h +++ b/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.h @@ -180,6 +180,10 @@ class QE_FRAMEWORK_LIBRARY_SHARED_EXPORT QECorrelation : public QEAbstractDynami double yMin; double yMax; + double regressionSlope; + double regressionIntersept; + double correlationCoef; + enum PlotModes { pmDots = 1, pmLines }; enum ScaleModes { smDynamic, smManual, smData }; @@ -194,7 +198,7 @@ class QE_FRAMEWORK_LIBRARY_SHARED_EXPORT QECorrelation : public QEAbstractDynami void setup (); void setReadOut (const QString status); - double calculateCorrelationCoefficient (); + void calculateCorrelationAndRegression (); void updateDataArrays (); void reDrawPlane (); void runSelectNameDialog (const int instance); @@ -234,6 +238,7 @@ private slots: void manualScaleClick (bool); void plotModeSelect (bool); void timeSelectButtonClick (bool); + void plotRegressionStateChanged (bool); void numberSamplesEditChange (const double); void sampleIntervalEditChange (const double); }; diff --git a/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.ui b/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.ui index ac02df25..365293f8 100644 --- a/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.ui +++ b/qeframeworkSup/project/widgets/QECorrelation/QECorrelation.ui @@ -7,7 +7,7 @@ 0 0 672 - 536 + 562 @@ -65,13 +65,13 @@ 0 - 136 + 162 16777215 - 136 + 162 @@ -563,7 +563,7 @@ 0 - 92 + 118 660 20 @@ -659,7 +659,7 @@ 0 - 112 + 138 660 20 @@ -815,6 +815,111 @@ :/qe/stripchart/save_file.png:/qe/stripchart/save_file.png + + + + 120 + 95 + 84 + 16 + + + + QLabel { background-color: rgb(224,224,224); } QLabel { color: rgb(0,0,0); } + + + +99.7852 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 6 + + + + + + 4 + 95 + 108 + 16 + + + + + 8 + + + + QLabel { color: rgb(0,0,0); } + + + Regression slope: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + 216 + 95 + 108 + 16 + + + + + 8 + + + + QLabel { color: rgb(0,0,0); } + + + Intercept: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + 292 + 95 + 84 + 16 + + + + QLabel { background-color: rgb(224,224,224); } QLabel { color: rgb(0,0,0); } + + + -1.2345 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 6 + + + + + + 480 + 7 + 100 + 24 + + + + Plot regression + +