calculate and store intersections and slope

This commit is contained in:
Ahmet Inan 2018-08-18 10:25:19 +02:00
commit 71ba720dc1

View file

@ -12,37 +12,41 @@ namespace DSP {
template <typename TYPE>
class SimpleLinearRegression
{
TYPE avgX, avgY;
TYPE varX, covXY;
TYPE xint_, yint_, slope_;
public:
SimpleLinearRegression(TYPE *x, TYPE *y, int LEN) : avgX(0), avgY(0), varX(0), covXY(0)
SimpleLinearRegression(TYPE *x, TYPE *y, int LEN)
{
TYPE avgX(0), avgY(0);
for (int i = 0; i < LEN; ++i) {
avgX += x[i];
avgY += y[i];
}
avgX /= LEN;
avgY /= LEN;
TYPE varX(0), covXY(0);
for (int i = 0; i < LEN; ++i) {
varX += (x[i] - avgX) * (x[i] - avgX);
covXY += (x[i] - avgX) * (y[i] - avgY);
}
xint_ = avgX - avgY * varX / covXY;
slope_ = covXY / varX;
yint_ = avgY - slope_ * avgX;
}
TYPE xint()
{
return avgX - avgY * varX / covXY;
return xint_;
}
TYPE slope()
{
return covXY / varX;
return slope_;
}
TYPE yint()
{
return avgY - slope() * avgX;
return yint_;
}
TYPE operator () (TYPE x)
{
return yint() + slope() * x;
return yint_ + slope_ * x;
}
};