Use Linear Regression to Find Slope of Many Datapoints

438px-Linear_regression.svgHere is the implementation of a program that analyses several days worth of data coming from a sensor that might change a tiny amount over that time.  We were interested in how much of a change over time occurred, so I ‘fitted’ a line through the data points that would fit the best and calculated the slope of that line using linear regression using the recommended formula for the sensor and process used:

Screenshot from 2015-05-07 22:11:22

Here is the code used to implement the formula above:  (Redlion Crimson 3)

float mu=0, s_t=0.0, s_b=0.0;
int i;

mu = (Sum(Regression1.x[0],Regression1.validPoints))/Regression1.validPoints;

for( i=0; i<Regression1.validPoints; i++ ) {
 s_t += Regression1.y[i]*(Regression1.x[i]-mu); 
 s_b += Power((Regression1.x[i]-mu),2); 
 }
 
Regression1.slope=s_t/s_b;

if (Regression1.validPoints < 10)
 Regression1.slopeValid = false;
 else
 Regression1.slopeValid = true;

This code below is executed each time a sample was to be taken.  It shifts the data down in the array and places the newest data in the first location.

//call this program each time a sample is taken
//recorded x is in incrementing number
//recorded y is in mills (0.0 to 40.0) 
//adds most recent data point to array place [0], and shifts all other points down
//assume array size is [1500]

int i; 
 
//shift x and y arrays down 1 spot
for( i=0; i<1500; i++ ) {
 Regression1.x[1499-i+1] = Regression1.x[1499-i]; 
 Regression1.y[1499-i+1] = Regression1.y[1499-i]; 
 }

//record the x
Regression1.x[0]++;

//record the y
Regression1.y[0] = Regression1.Xin;

And this bit of code checks to see if the current data collected is valid by comparing the current time to the last time a sample was taken.  The sample size will grow to the size of the period.

//compare time stamp between current data and 
// last data point to determine of valid points should be reset

int points_max; 

points_max = int(Regression1.period * 24 * 60 / Regression1.sampleRate); 

//if difference in current data point and last data point is more than sample period,
// reset valid points number

if (Abs(Regression1.lastTimeStamp - TimeNow) > (Regression1.period * 24 * 60 * 60)) 
 Regression1.validPoints = 1;
 else
 {
 Regression1.validPoints ++;
 if (Regression1.validPoints > points_max)
 Regression1.validPoints = points_max;
 }
Regression1.lastTimeStamp = TimeNow;

 

This entry was posted in Industrial Automation and tagged , , , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.