Ejemplo n.º 1
0
int main()
{
	int n;
	printf("Enter the Number of points to be input\n");
	scanf("%d",&n);
	linreg(n);
	return 0;
}
Ejemplo n.º 2
0
// Compute the difference between the samples in d1 and d2, returning
// the slope of a line through the points, (i, d1[i] - d2[i])
// for i in [0,n)
// Returns the y intercept in aa.
// Return value is the slope of the fitted line.  If it decides we're
// done, returns 0.0
double
find_slope(double *d1, double *d2, int n, double *aa,
	   double period) {
  int i;
  double a,b,r;
  double yi;
  double *err = calloc(n, sizeof(*err));
  double *x = calloc(n, sizeof(*x));
  double fit_err, tot_err;
  double var;
  double thresh = period / 2.0;

  int addct = 0;
  int n2 = 0;
  
  if(n == 1) {
    *aa = d1[0] - d2[0];
    return 0.0;
  }

  if(n == 2) {
    *aa = 0.0;
    return d1[1] - d2[1];
  }

  for(i = 1; i < n; i++) {
    yi = d1[i] - d2[i];

    if(-thresh <= yi && yi <= thresh) {
      //      printf("%3d\t%f\n", i, yi);
      err[n2] = yi;
      x[n2] = i;
      n2++;
    }
  }

  r = linreg(x, err, n2, &a, &b);

  for(i = 0; i < n2; i++) {
    printf("%3d\t%f\n", (int)x[i], err[i]);
  }

  var = vari(err,n);

  printf("%s() a = %f, b = %f, r = %f, var = %f\n", __func__, a, b, r, var);

  *aa = a;

  free(err);
  free(x);

  return b;
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
	int n = 10;
	float x[10]= {1, 2, 3,  4,  5,  6,  7,  8,  9, 10};
	float y[10]= {2, 5, 9, 10, 14, 18, 20, 22, 25, 29};

	float m, b, r;

	linreg(n, x, y, &m, &b, &r);

	printf("m=%g b=%g r=%g\n",m, b, r);

	return 0;
}
Ejemplo n.º 4
0
void main() {

  //* Initialize data to be fit. Data is quadratic plus random number.
  Matrix c(3);
  cout << "Curve fit data is created using the quadratic" << endl;
  cout << "  y(x) = c(1) + c(2)*x + c(3)*x^2" << endl;
  cout << "Enter the coefficients:" << endl;
  cout << "c(1) = "; cin >> c(1);
  cout << "c(2) = "; cin >> c(2);
  cout << "c(3) = "; cin >> c(3);  
  double alpha;
  cout << "Enter estimated error bar: "; cin >> alpha;
  int i, N = 50;        // Number of data points
  long seed = 1234;     // Seed for random number generator
  Matrix x(N), y(N), sigma(N);
  for( i=1; i<=N; i++ ) {
    x(i) = i;                  // x = [1, 2, ..., N]
    y(i) = c(1) + c(2)*x(i) + c(3)*x(i)*x(i) + alpha*randn(seed);
    sigma(i) = alpha;          // Constant error bar
  }
  
  //* Fit the data to a straight line or a more general polynomial
  cout << "Enter number of fit parameters (=2 for line): ";
  int M;  cin >> M;
  Matrix a_fit(M), sig_a(M), yy(N);	 double chisqr;
  if( M == 2 )  //* Linear regression (Straight line) fit   
    linreg( x, y, sigma, a_fit, sig_a, yy, chisqr);
  else          //* Polynomial fit
    pollsf( x, y, sigma, M, a_fit, sig_a, yy, chisqr);

  //* Print out the fit parameters, including their error bars.
  cout << "Fit parameters:" << endl;
  for( i=1; i<=M; i++ )
    cout << " a(" << i << ") = " << a_fit(i) << 
            " +/- " << sig_a(i) << endl;

  cout << "Chi square = " << chisqr << "; N-M = " << N-M << endl;
  
  //* Print out the plotting variables: x, y, sigma, yy
  ofstream xOut("x.txt"), yOut("y.txt"), 
	       sigmaOut("sigma.txt"), yyOut("yy.txt");
  for( i=1; i<=N; i++ ) {
    xOut << x(i) << endl;
    yOut << y(i) << endl;
    sigmaOut << sigma(i) << endl;
    yyOut << yy(i) << endl;
  }
}
Ejemplo n.º 5
0
void cvLineFitter::fitLine(ofxPoint2f * pts, int nPts, float &slope, float &intercept, float &chiSqr){

	Matrix x(nPts), y(nPts), sigma(nPts);
	for(int i=1; i<=nPts; i++ ) {
		x(i) = pts[i-1].x;                  
		y(i) = pts[i-1].y;
		sigma(i) = 1.0f;         //error bar? what is this? 
	}
  	
  	Matrix a_fit(2), sig_a(2), yy(nPts);	 
  	double chiSqrd;
	linreg( x, y, sigma, a_fit, sig_a, yy, chiSqrd);
	
	chiSqr = (double)chiSqrd;
	intercept =  a_fit(1);
	slope=  a_fit(2);
	
	chiSqr = sig_a(1);
	
	//printf("fit: %f %f \n",  a_fit(1), sig_a(1));
   //printf("sig 1: %f %f\n", sig_a(1), sig_a(2));
  
}
Ejemplo n.º 6
0
int Line(Global::Image *mImageParser, float* pParser_CSV, Sample &mSample, const bool bSw = false)
{
	//float fRandY0 = GlobalTool::Max(pParser_CSV[nRandX0], pParser_CSV[nRandX1]);
	//float fRandY1 = GlobalTool::Min(pParser_CSV[nRandX0], pParser_CSV[nRandX1]);
	

	float fLSX[2000] = { 0, };
	float fLSY[2000] = { 0, };
	int nCount = 0;
	mSample.nSn = 0;

	float fRandY0 = pParser_CSV[mSample.nX0];
	float fRandY1 = pParser_CSV[mSample.nX1];


	//int nMaxArray = GlobalTool::Max(mSample.nX0, mSample.nX1);
	//int nMinArray = GlobalTool::Min(mSample.nX0, mSample.nX1);

	float fU = mSample.nX1 - mSample.nX0;
	float fV = fRandY1 - fRandY0;
	float fM = (fV / fU);
	float nSurchDiff0;
	
	
	if (fM < 0)
	{
		nSurchDiff0 = fSurchDiff;
		//printf("slope : %f  %f \n",90 + degree, (atan(fM) * 180) / M_PI);
	}
	else
	{
		nSurchDiff0 = -fSurchDiff;
		//printf("slope : %f  %f \n", 90 - degree, (atan(fM) * 180) / M_PI);
	}

	float fDegree = (atan(fM) * 180) / M_PI;

	//double rad = atan2(fU, fV);
	//double degree = ((rad * 180) / M_PI);
	if (abs(fDegree) >= 30)
	{
		//printf("height : %f  %f  \n", fRandY0, fRandY1);
		//printf("slope : %f \n", (atan(fM) * 180) / M_PI);
		return 0;
	}

	
	if (mImageParser && bSw)
	{
		Global::Circle(mImageParser, mSample.nX0, fRandY0, 15, RGB8(0, 255, 255));
		Global::Circle(mImageParser, mSample.nX1, fRandY1, 15, RGB8(0, 255, 255));
		//printf("slope : %f \n", atan(fM));
	}


	for (int i = 1; i < nWidthParser - 1; i++)
	{

		float nY0 = fM * ((i - 1) - mSample.nX0) + fRandY0;
		float nY1 = fM * ((i + 1) - mSample.nX0) + fRandY0;
		//printf("x: %d  y : %d  y : %d \n", i, nY0, nY1);
		if (nY0 < nheightParser || nY0 > 0)
		{
			if (mImageParser)
			{
				//Global::Line(mImageParser, (i - 1), nY0, (i + 1), nY1, RGB8(0, 255, 0));
				//Global::Line(mImageParser, (i - 1) - nSurchDiff0, nY0 - fSurchDiff, (i + 1) - nSurchDiff0, nY1 - fSurchDiff, RGB8(0, 255, 0));
				//Global::Line(mImageParser, (i - 1) + nSurchDiff0, nY0 + fSurchDiff, (i + 1) + nSurchDiff0, nY1 + fSurchDiff, RGB8(0, 255, 0));
			}


			if (pParser_CSV[i] != 0)
			{
				float fSurchPointMin = fM * ((i + 1) - mSample.nX0 + nSurchDiff0) + fRandY0;
				float fSurchPointMax = fM * ((i + 1) - mSample.nX0 - nSurchDiff0) + fRandY0;
				if (pParser_CSV[i + 1] > fSurchPointMin - nSurchDiff0 && pParser_CSV[i + 1] < fSurchPointMax + nSurchDiff0)
				{
					fLSX[nCount] = i + 1;
					fLSY[nCount++] = pParser_CSV[i + 1];
					mSample.nSn++;
					if (mImageParser && bSw)
					{
						Global::Circle(mImageParser, i + 1, pParser_CSV[i + 1], 2, RGB8(255, 0, 0));
					}
				}
				//Sleep(10);
				//Global::Show("Parser ", mImageParser);			
			}
		}
	}
	
	
	if (bSw)
	{
		linreg(nCount, fLSX, fLSY, &mSample.m, &mSample.b, &mSample.r);
		//Global::Line(mImageParser, 1, m + b, (mImageParser->width - nSurchDiff0), m*(mImageParser->width - nSurchDiff0) + b, RGB8(255, 0, 0));
		printf("m=%g b=%g r=%g  Ag=%f MeanAg=%f \n", mSample.m, mSample.b, mSample.r, fDegree, (atan(mSample.m) * 180) / M_PI);
	}
	if (mImageParser && bSw)
	{
		for (int i = 1; i < nWidthParser - 1; i++)
		{
			float nY0 = mSample.m * (i - 1) + mSample.b;
			float nY1 = mSample.m * (i + 1) + mSample.b;
			if (nY0 < mImageParser->height || nY0 > 0)
			{
				Global::Line(mImageParser, (i - 1), nY0, (i + 1), nY1, RGB8(0, 255, 0));
			}
		}
	}
	return 1;
}
Ejemplo n.º 7
0
Archivo: nterm.c Proyecto: rforge/muste
static int linear_regression(int m, int n) {
  double *Y, *X, *S, *U, *T, *B, *R;
  double *oma;
  int real_m; // ,real_n;
  int i,j,k,a;
  int tulos;
  double tulosterm;
  int tulosl;
// RS REM  struct murtoluku tulosml;

  oma=regressor[0];
  if (n>=m) n=m-1;
  nterm_type=UNIDENTIFIED;
  strcpy(nterm_output_buffer,comment_1);


  Y=matrixmalloc(m,1);
  if (Y==NULL) return(-1);
  for (i=0;i<m;i++) {
    *(Y+i)=sequence[i];
  }

  X=matrixmalloc(m,n);
  if (X==NULL) return(-1);
  S=matrixmalloc(m,n);
  if (S==NULL) return(-1);
  U=matrixmalloc(n,n);
  if (U==NULL) return(-1);  
  T=matrixmalloc(m,n);
  if (T==NULL) return(-1);  
  B=matrixmalloc(m,1);
  if (B==NULL) return(-1);
  R=matrixmalloc(m,1);
  if (R==NULL) return(-1);

  m-=1;n-=1;
  i=0;
  tulos=FALSE;




  k=0;
  polyfill(X,m,n,k);
  tulos=linreg(X,Y,S,U,T,B,R,m,n);
  if (tulos<0) return(-1);

  if (!tulos) {

    j=k;
    k++;

    do {
      j++;

      oma=regressor[j-1];
      for (i=0;i<m;i++) {
        *(X+i)=*(oma+i);
      }
      muutnim=data.v[j-1];

      polyfill(X,m,n,k);

/*
    showmatrix(X,m,n);
*/

      tulos=linreg(X,Y,S,U,T,B,R,m,n);
      if (tulos<0) return(-1);

    } while (tulos!=TRUE && j<data.m_act);
  }

/*
 showmatrix(B,m,1);
*/

  if (tulos) {

    if (k>0) *(X+0)=*(oma+m);

    polyrow(X,1,n,0,k,m+1);

/*
 showmatrix(X,1,n);
*/
    if ( pow( *(Y+m)-solve_term(X,B,1,n,0) ,2) < MINTOLERANCE ) {

      if (k>0) *(X+0)=*(oma+m+1);
      polyrow(X,1,n,0,k,m+2);
      tulosterm=solve_term(X,B,1,n,0);

      tulosl=(int)floor(tulosterm);
      if (fabs(tulosterm-tulosl)>.5) tulosl=(int)ceil(tulosterm);

      muste_ltoa(tulosl,nterm_output_buffer,10);
      nterm_type=POLYNOMIAL;



      construct_formula(B,k,n);


    }
  }
  else {

    real_m=m; // real_n=n;

  for (n=1;n<(real_m-n-1);n++) {

    a=n+1;
    m=real_m-n;

    for (i=0;i<m+1;i++) {
      *(Y+i)=sequence[i+n];
    }

/*
    showmatrix(Y,m,1);
*/


    for (i=0;i<m;i++) *(X+i)=1;

    for (j=1;j<=n;j++) {
      for (i=0;i<m;i++) {
        *(X+i+m*j)=sequence[n-j+i];
      }
    }

/*
    showmatrix(X,m,a);
*/

    tulos=linreg(X,Y,S,U,T,B,R,m,a);
    if (tulos<0) return(-1);

/*
    showmatrix(B,a,1);
*/

    if (tulos) {
      tulosterm=*(B+0);
      for (k=0;k<n;k++) {
        tulosterm+=(*(Y+m-1-k))*(*(B+1+k));
      }
/*
printf("\ntark:%f\n",pow(tulosterm-*(Y+m+1),2));WAIT;
*/

      if (pow(tulosterm-*(Y+m+1),2) < MINTOLERANCE) {
        tulosterm=*(B+0);
        for (k=0;k<n;k++) {
         tulosterm+=(*(Y+m-k))*(*(B+1+k));
        }
        tulosl=(int)floor(tulosterm);
        if (fabs(tulosterm-tulosl)>.5) tulosl=(int)ceil(tulosterm);

        muste_ltoa(tulosl,nterm_output_buffer,10);

        nterm_type=FIBONACCI;

        construct_formula(B,0,n);
        break;
      }
    }
  }

}
return(1);
}