cctw  0.2.1
cctwlinearfitter.cpp
Go to the documentation of this file.
1 #include "cctwlinearfitter.h"
2 
3 CctwLinearFitter::CctwLinearFitter(QString name, QObject *parent) :
4  CctwObject(name, parent)
5 {
6  startNewFit();
7 }
8 
10 {
11  m_XData.resize(0);
12  m_YData.resize(0);
13  m_Slope = 0;
14  m_Intercept = 0;
15  m_RSquared = 0;
16 }
17 
18 void CctwLinearFitter::appendPoint(double x, double y)
19 {
20  m_XData.append(x);
21  m_YData.append(y);
22 }
23 
24 void CctwLinearFitter::performFit(int skipStart, int skipEnd)
25 {
26  int start = skipStart, end = m_XData.count()-skipEnd;
27 
28  double n = 0, meanx = 0, meany = 0;
29 
30  for (int i=start; i<end; i++) {
31  n += 1;
32  meanx += m_XData[i];
33  meany += m_YData[i];
34  }
35 
36  meanx /= n;
37  meany /= n;
38 
39  double ssxx = 0, ssyy = 0, ssxy = 0;
40 
41  for (int i=start; i<end; i++) {
42  double x = m_XData[i] - meanx;
43  double y = m_YData[i] - meany;
44 
45  ssxx += x*x;
46  ssyy += y*y;
47  ssxy += x*y;
48  }
49 
50  ssxx /= n;
51  ssyy /= n;
52  ssxy /= n;
53 
54  m_Slope = ssxy / ssxx;
55  m_Intercept = meany - m_Slope*meanx;
56  m_RSquared = (ssxy*ssxy)/(ssxx*ssyy);
57 }
58 
60 {
61  return m_Slope;
62 }
63 
65 {
66  return m_Intercept;
67 }
68 
70 {
71  return m_RSquared;
72 }
QVector< double > m_YData
void performFit(int skipStart, int skipEnd)
QVector< double > m_XData
void appendPoint(double x, double y)
CctwLinearFitter(QString name, QObject *parent=0)