00001
00002
00003
00004 char* series_linear_cpp = "$Id: sxy_series_linear.cpp,v 1.3 2002/11/28 21:01:01 clinio Exp $";
00005
00006
00007
00008 #include <float.h>
00009 #include "sxy_series_linear.h"
00010
00011
00012
00013
00014
00015 SXYLinearSeries::SXYLinearSeries(unsigned int buffer_size,
00016 unsigned int resolution) :
00017 SXYCoeficientsAproximationSeries(2, buffer_size, resolution) {
00018 }
00019
00020
00021
00022 SXYLinearSeries::~SXYLinearSeries(void) {
00023 }
00024
00025
00026
00027 unsigned int SXYLinearSeries::aproxFunctionCallback( double x, double& y ) {
00028 double A, B;
00029 if (!getCoeficient(0, A) || !getCoeficient(1, B) ) return 0;
00030 y = A*x + B;
00031 return 1;
00032 }
00033
00034
00041 unsigned int SXYLinearSeries::calculateCoeficients(unsigned int n,
00042 double* buffer_x, double* buffer_y) {
00043 double sxx = 0, sx = 0, sy = 0, sxy = 0, syy = 0;
00044
00045
00046 if ( n <= 1 ) return 0;
00047
00048 for ( unsigned int i = 0; i < n; i++ ) {
00049 double x = buffer_x[i];
00050 double y = buffer_y[i];
00051
00052 sx = sx + x;
00053 sy = sy + y;
00054 sxx = sxx + (x * x);
00055 sxy = sxy + (x * y);
00056 syy = syy + (y * y);
00057 }
00058
00059
00060 if (fabs( ((double)n)*sxx -sx*sx) < 1e-10) return 0;
00061
00062 double A = ( ((double)n)*sxy-sx*sy ) / ( ((double)n)*sxx-sx*sx );
00063 double B = (sy-A*sx)/((double)n);
00064
00065 setCoeficient(0,A);
00066 setCoeficient(1,B);
00067
00068 return 1;
00069 }
00070