Пример #1
0
int main()
{
	const int NP = 8; // Number of given data points
	const int M = 50; // number of computed data points
	/* const int NC = 2 ; Number of coefficients */
	double x[] = {1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002};
	double y[] = {5500, 8500, 13500, 20500, 29500, 40500, 53500, 68500};
	double coeff[NC]; // vector of coefficients
	int i;
	double xc[M]; /* computed values for x using polynomial */
	double yc[M];
	printf("Program finds best fit polynomial of degree %d \n",
		NC-1);
	printf("Data points (x,y): \n");
	for (i = 0; i < NP; i++)
		printf(" %f %f \n", x[i], y[i]);
	polynomialfit(NP, NC, x, y, coeff); /* find coefficients */
	printf("\n\nCoefficients of polynomial found\n");
	for(i=0; i < NC; i++) {
		printf("%lf\n", coeff[i]);
	}
	/* Evaluate the fitted polynomial */
	linspace(xc, 0.0, 12.0, M);
	polyval(coeff, xc, yc, NC, M);
	printf("\nData points calculated with the polynomial \n");
	for(i=0; i < M; i++)
		printf("%d %+.18f %+.18f \n", i, xc[i], yc[i]);
	return 0;
} /* end main */
Пример #2
0
int main(int argc, char *argv[]) {
    if(argc < 3)
        print_usage(argv[0]);
    int data_n = strtol(argv[1], NULL, 10);
    int poly_n = strtol(argv[2], NULL, 10);

    int gnuplot_format = 0;

    if(argc > 3 && strcmp(argv[3], "--gnuplot") == 0) {
        gnuplot_format = 1;
    }

    poly_n += 1;


    double* dx = malloc(data_n * sizeof(double));
    double* dy = malloc(data_n * sizeof(double));
    double* res = malloc(poly_n * sizeof(double));


    for(int i = 0; data_n > i; i++) {
        scanf("%lf %lf", &dx[i], &dy[i]);
    }

    //** SOLVE
    double err = polynomialfit(data_n, poly_n, dx, dy, res);


    
    if(!gnuplot_format) {
        printf("E: %.10lf\n", err);
        for(int i = 0; poly_n > i; i++) {
            printf("%.30lf ", res[i]);
        }
        printf("\n");
    } else {
        printf("f(x) = ");
        int i = 0;
        for(i = 0; poly_n - 1 > i; i++) {
            printf("%.30lf ", res[i]);
            for(int j = 0; j < i; j++) {
                printf("*x");
            }
            printf(" + ");
        }
        if(i < poly_n) {
            printf("%.30lf ", res[i]);
            for(int j = 0; j < i; j++) {
                printf("*x");
            }
        }
        printf("\n");
    }

    free(dx);
    free(dy);
    free(res);

    return 0;
}
Пример #3
0
int main()
{
  int i;
  double chisq=0;
 
  polynomialfit(NP, DEGREE, x, y, e, chisq, coeff);
  for(i=0; i<DEGREE; i++)   printf("%g\t", coeff[i]);
  printf("%g\n", chisq);
  return 0;
}
Пример #4
0
int main_fitting() {
	// 3 x^2 + 2 x + 1
	std::vector<double> x = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	std::vector<double> y = { 1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321 };

	int degree = 3;

	// return m_coeffs[0] + m_coeffs[1]*x + m_coeffs[2]*x^2 + ...
	std::vector<double> coeffs = polynomialfit(degree, x, y);
	for (auto i : coeffs) {
		std::cout << i << std::endl;
	}
	return 0;
}
Пример #5
0
double DescenderPath::computeHalfScore(bool upper, bool print) const
{
    double meanCurve;
    double stdDevCurve;
                      
    double meanSlope;
    double stdDevSlope;
    int startIndex;
    int endIndex;
//    const QVector<unsigned int>* path;
    if (upper)
    {
        meanCurve = NEW_UPPER_MEAN_CURVE;
        stdDevCurve = NEW_UPPER_STD_DEV_CURVE;
        
        meanSlope = NEW_UPPER_MEAN_SLOPE;
        stdDevSlope = NEW_UPPER_STD_DEV_SLOPE;
//        path = &upperPath;
        startIndex=divideIndex;
        endIndex=path.size()-1;
        if (print)
            printf("Upper:\t");
    }
    else
    {
        meanCurve = NEW_LOWER_MEAN_CURVE;
        stdDevCurve = NEW_LOWER_STD_DEV_CURVE;
        
        meanSlope = NEW_LOWER_MEAN_SLOPE;
        stdDevSlope = NEW_LOWER_STD_DEV_SLOPE;
//        path = &lowerPath;
        startIndex=0;
        endIndex=divideIndex>0?divideIndex:path.size()-1;
        if (print)
            printf("Lower:\t");
    }
    
    //        double relativeAngle = getRelAngle(skeleton, currentPath->at(currentPath->size()-2), currentPath->last(), nextIndex);
    //        double distance = getDist(skeleton, currentPath->last(), nextIndex);
    //        double newClockwiseScore = (clockwiseScore/1.5)+std::min(PI-relativeAngle,PI/3)*distance;
    double avgAngle;
    
    int sampleSize = 1+endIndex-startIndex;
    
    double x[sampleSize];
    double y[sampleSize];
//    QVector<double> x;
//    QVector<double> y;
    for (int i=startIndex; i<=endIndex; i++)
    {
        x[i-startIndex]=(*skeleton)[path[i]].x;
        y[i-startIndex]=(*skeleton)[path[i]].y;
        
//        foreach (QPoint p, (*skeleton).getRegion(path[i]))
//        {
//            if ((*skeleton).pixel(p.x(),p.y()))
//            {
//                x.append((double)p.x());
//                y.append((double)p.y());
//            }
//        }
//        if (i>startIndex)
//        {
//            int curX=(*skeleton)[path[i-1]].x;
//            int curY=(*skeleton)[path[i-1]].y;
//            int nextX=(*skeleton)[path[i]].x;
//            int nextY=(*skeleton)[path[i]].y;
//            QVector<QPoint> line;
//            QPoint start(curX,curY);
//            line.append(start);
//            if (curX==nextX || fabs((curY-nextY)/((double)curX-nextX)) > 1)
//            {
//                double slope = ((double)curX-nextX)/(curY-nextY);
//                double intersect = curX-curY*slope;
//                int inc = copysign(1.0, nextY-curY);
//                for (int y=curY+inc; inc*y<inc*nextY; y+=inc)
//                {
//                    QPoint toAdd(y*slope+intersect,y);
//                    if (((BPixelCollection*)skeleton)->pixel(toAdd))
//                        line.append(toAdd);
//                    else
//                    {
//        //                return;
//                    }
//                }
//            }
//            else
//            {
//                double slope = (curY-nextY)/((double)curX-nextX);
//                double intersect = curY-curX*slope;
//                int inc = copysign(1.0, nextX-curX);
//                for (int x=curX+inc; inc*x<inc*nextX; x+=inc)
//                {
//                    QPoint toAdd(x,slope*x+intersect);
//                    if (((BPixelCollection*)skeleton)->pixel(toAdd))
//                        line.append(toAdd);
//                    else
//                    {
//        //                return;
//                    }
//                }
//            }
//            QPoint end(nextX,nextY);
//            line.append(end);
            
//            foreach (QPoint p, line)
//            {

//                x.append((double)p.x());
//                y.append((double)p.y());
//            }
//        }
        
        if (i-startIndex>1)
        {
//            std::min(PI-relativeAngle,PI/3)
            avgAngle += std::max(getRelAngle(path[i-2], path[i-1], path[i]),PI*0.6666);
        }
    }
    
//    int sampleSize = x.size();
    
    if (sampleSize>2)
    {
        avgAngle /= sampleSize-2;
    }
    else
    {
        avgAngle = 0;
    }
    
    double halfScore=0;

    if (sampleSize>2)
    {
        double tss = gsl_stats_tss(x,1,sampleSize);
        double cov[9];
        double linOut[2];
        double chisqSlope= polynomialfit(sampleSize,2,y,x,linOut,cov);
        double slope=linOut[1];
        double rsqSlope=1-chisqSlope/tss;
        
        double quadOut[3];
        double chisqCurve = polynomialfit(sampleSize,3,y,x,quadOut,cov);
        double curvature=quadOut[2];
        double rsqCurve=1-chisqCurve/tss;
        
        double yOfVertex = quadOut[1]/(2*quadOut[2]);
        
        halfScore = (copysign(1.0, curvature) == copysign(1.0, meanCurve) ||
                             fabs(curvature)<0.001) ? 
                     10*(1/std::max(rsqCurve,0.1))*std::max(fabs(curvature-meanCurve),2*stdDevCurve)/(2*stdDevCurve) :
                     15*(1/std::max(rsqCurve,0.1))*std::max(fabs(curvature-meanCurve),2*stdDevCurve)/(2*stdDevCurve);
//        halfScore = 10*(1/std::max(rsqCurve,0.1))*std::max(fabs(curvature-meanCurve),2*stdDevCurve)/(2*stdDevCurve) + .1*std::max(fabs(slope-meanSlope),2*stdDevSlope)/(2*stdDevSlope);
        
        
    }
    
    if(print)
        printf("fit=%f\tangle=%f\t",halfScore,.1*avgAngle);
    
    halfScore += .1*avgAngle;
    
    if(print)
        printf("total=%f\n",halfScore);
    
    return halfScore;
}
Пример #6
0
//--------------------------------------------------------------
int cam_cap_subpixel(Cam *cam, Mat ima1, Mat thr1){

    Mat sub1;
    int cont, jmax, jmin;
    float x1;

    double x[200], y[200];
    int k, imax1;

    double coeff[DEGREE];

    k = 0;
    x1 = 0;
    jmax = -1;
    jmin = -1;

    cvtColor(ima1, sub1, CV_BGR2GRAY);

    x1 = 0;
    cont = 0;
    imax1 = 0;

    for(int i=0; i<cam->resy; ++i){
        cam->p[i].x = 1024;
        cam->p[i].y = 0;
    }
    for(int i=0; i<thr1.rows; ++i){
        uchar * pixel1 = thr1.ptr<uchar>(i);

        for(int j=0; j<thr1.cols; ++j){
            if( ((int)sub1.at<uchar>(i,j)>cam->GPLL)&&((int)sub1.at<uchar>(i,j)>imax1)&&(pixel1[j] != 0) ){
                imax1 = (int)sub1.at<uchar>(i,j);
                jmin = jmax;
                jmax = j;
            }
        }
        if(jmin != -1){
            jmax = ( (jmax - jmin) / 2 ) + jmin; //trobo dos max
        }
        for(int j=jmax-cam->PAP; j<jmax+cam->PAP; ++j){
            if(j>0 && j<thr1.cols &&(int)sub1.at<uchar>(i,j)>cam->GPLL && pixel1[j]!=0){
                x[k] = (double)j;
                y[k] = (double)sub1.at<uchar>(i,j);
                cont = 1;
                k = k + 1;
            }
        } // end for
        if(k >= cam->PMP){
            polynomialfit(k, DEGREE, x, y, coeff);
//                    if (coeff[3]!=0)
//                        {x1=fabs((-2*coeff[2]+sqrt(4*coeff[2]*coeff[2]-4*(3*coeff[3]*coeff[1])))/(2*3*coeff[3]));
//                        if(abs(x1-jmax)>10)x1=jmax;
//                    }}
//                    else{x1=1024;}

            if(coeff[2] != 0){
                x1 = fabs( (-1 * coeff[1]) / (2 * coeff[2]) );
                if(fabs(x1-jmax )> 10){
                    x1 = 1024;
                }
            }
        } // end if(k >= cam->PMP)
        else{
            x1 = 1024;
        }

        if(cont == 1){
//            for (jj=0; jj<k; jj++){ x1 = x1 + xcan[jj] };
            cont = 0;
            if(x1>0 && x1<1024 && k>=cam->PMP){
                cam->p[i].x = x1;
                cam->p[i].y = i;
                cam->p[i].a = k;
                cam->p[i].q = k;
            }
        }

        x1 = 0;
        imax1 = 0;
        jmax = -1;
        jmin = -1;
        k = 0;

    } // end for(int i=0; i<thr1.rows; ++i)
}