Esempio n. 1
0
int
test_cspline_sin(void)
{
	cspline * c = cspline_new(9, 1024);
	mfp_block * in = mfp_block_new(1024);
	mfp_block * out = mfp_block_new(1024);
	int x;
	int fail = 0;

	cspline_init(c, 0.0, 2.0*M_PI, sin_coeffs);
	for(x = 0; x < 1024; x++) {
		in->data[x] = (float)x * 2.0*M_PI/1024.0;
	}

	cspline_block_eval(c, in, out);

	for(x = 0; x < 1024; x++) {
		if (fabs(out->data[x] - sin(in->data[x])) > 0.01) {
			fail = 1;
			printf("index=%d x=%f expected=%f got=%f\n", x, in->data[x], sin(in->data[x]), out->data[x]); 
		}
	}
	if (fail)
		return 0;
	return 1;
}
Esempio n. 2
0
int
benchmark_cspline_sin(void) 
{
	struct timeval start, end;
	float naive, fast;
	cspline * c = cspline_new(9, 1024);
	mfp_block * in = mfp_block_new(1024);
	mfp_block * out = mfp_block_new(1024);
	int x;
	int fail = 0;

	cspline_init(c, 0.0, 2.0*M_PI, sin_coeffs);
	for(x = 0; x < 1024; x++) {
		in->data[x] = (float)x * 2.0*M_PI/1024.0;
	}

	gettimeofday(&start, NULL);
	for(x = 0; x < 1024; x++) {
		cspline_block_eval(c, in, out);
	}
	gettimeofday(&end, NULL);
	fast = (end.tv_sec + end.tv_usec/1000000.0) - (start.tv_sec + start.tv_usec / 1000000.0);

	gettimeofday(&start, NULL);
	for(x = 0; x < 1024; x++) {
		naive_block_sin(in, out);
	}
	gettimeofday(&end, NULL);
	naive = (end.tv_sec + end.tv_usec/1000000.0) - (start.tv_sec + start.tv_usec / 1000000.0);

	printf("\n     Naive: %f, fast: %f\n", naive, fast);
	return 1;
}
Esempio n. 3
0
int
test_cspline_create(void)
{
	cspline * c = cspline_new(4, 8);
	mfp_block * in = mfp_block_new(8);
	mfp_block * out = mfp_block_new(8);
	int i;
	int fail = 0;
	float coeff_1[] = { 1.0, 0.0, 0.0, 0.0, 
		                2.0, 0.0, 0.0, 0.0,
		                3.0, 0.0, 0.0, 0.0,
		                4.0, 0.0, 0.0, 0.0 };
	float answers_1[] = { 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0 };

	float coeff_2[] = { 0.0, 1.0, 0.0, 0.0, 
		                0.0, 2.0, 0.0, 0.0,
		                0.0, 3.0, 0.0, 0.0,
		                0.0, 4.0, 0.0, 0.0 };
	float answers_2[] = { 0.0, 0.25, 1.0, 1.50, 3.0, 3.75, 6.0, 7.0 };

	if((c == NULL) || (c->num_segments != 4))
		return 0;

	cspline_init(c, 0.0, 2.0, coeff_1);
	for (i=0; i<8; i++) {
		in->data[i] = (float)i / 4.0;
	}

	cspline_block_eval(c, in, out);

	for (i=0; i<8; i++) {
		if (out->data[i] != answers_1[i]) {
			printf("err %d %f %f\n", i, out->data[i], answers_1[i]);
			fail = 1;
		}
	}

	cspline_init(c, 0.0, 2.0, coeff_2);
	for (i=0; i<8; i++) {
		in->data[i] = (float)i / 4.0;
	}

	cspline_block_eval(c, in, out);

	for (i=0; i<8; i++) {
		if (out->data[i] != answers_2[i]) {
			printf("err (2) %d %f %f\n", i, out->data[i], answers_1[i]);
			fail = 1;
		}
	}
	if (fail)
		return 0;
	return 1;

}
Esempio n. 4
0
int main(int argc, char *argv[]){ 
	// Number of points to be fitted with a given spline 
	int dim = 10; 
	// Number of points for the fitted graph
	int steps = 100; 
	// Allocate memory for a vector x with all the x-coordinates
	double *x1 = malloc(dim*sizeof(double));
	double *x2 = malloc(dim*sizeof(double));
	// Allocate memory for a vector y with all the y-coordinates
	double *y1 = malloc(dim*sizeof(double));
	double *y2 = malloc(dim*sizeof(double));
	// Allocation for the points (z,s) for the interpolant
	double z,s;
	// Allocation of step variables
	double x0, xn, h;
	
	/* 
	 * A. (6 points) Linear and quadratic splines 
	 */
	
	// Input n points for the interpolation 	
	printf("# x y (points)\n");
	for(int i = 0; i < dim; i++){ 
		x1[i] = i + 0.5*sin(i); 
		y1[i] = i + cos(i*i);
		printf("  %2.6f %2.6f \n",x1[i],y1[i]);
	}
	// Calculate m points for the interpolants
	x0 = x1[0];
	xn = x1[dim-1];
	h = (xn - x0)/steps;

	// Linear spline interpolation
	s=0;
	z=0;
	printf("\n\n"); //New block (Gnuplot)
	printf("# x y (liner spline interpolation)\n");
	for(int i = 0; i < steps; i++){
		z = x0 + h*i;
		s = linterp(z,x1,y1,dim);
		printf("  %2.6f %2.6f \n", z, s);
	}
	// Quadratic spline interpolation 
	s=0;
	z=0;
	qspline *qs = qspline_new(dim,x1,y1);
	printf("\n\n"); //New block (Gnuplot)
	printf("# x y (quadratic spline interpolation)\n");
	
	for(int i = 0; i < steps; i++){
		z = x0 + h*i;
		s = qspline_get(qs,z);
		printf("  %2.6f %2.6f \n", z, s);
	}
	/*
	 * B. (3 points) derivatives and integrals with quadratic spline
	*/
	
	//Input for the derivatives and integrals
	fprintf(stderr, "# x y (points)\n");
	for(int i = 0 ;i<dim; i++){ 
	    x2[i] = i*2*PI/(dim-1);
	    y2[i] = sin(x2[i]);
	    fprintf(stderr," %2.6f %2.6f \n", x2[i], y2[i]);
	}
	x0 = x2[0];
	xn = x2[dim-1];
	h = (xn - x0)/steps;
	
	// Quadratic spline interpolation 
	s=0;
	z=0;
	qs = qspline_new(dim,x2,y2);
	fprintf(stderr, "\n\n"); //New block (Gnuplot)
	fprintf(stderr, "# x y (quadratic spline interpolation)\n");
	
	for(int i = 0; i < steps; i++){
		z = x0 + h*i;
		s = qspline_get(qs, z);
		fprintf(stderr, "  %2.6f %2.6f \n", z, s);
	}
	
	// Derivative of the quadratic spline interpolation
	fprintf(stderr, "\n\n"); //New block (Gnuplot)
	fprintf(stderr, "# x y (derivative of the quadratic spline)\n");
	for(int i = 0; i < steps; i++){
	    	z = x0 + h*i;
		s = qspline_deriv(qs, z);
		fprintf(stderr," %2.6f %2.6f \n", z, s);
	}
	
	// Integration the quadratic spline interpolation
	fprintf(stderr, "\n\n"); //New block (Gnuplot)
	fprintf(stderr, "# x y (integration of the quadratic spline)\n");
	for(int i = 0; i < steps; i++){
	    	z = x0 + h*i;
		s = qspline_int(qs, z);
		fprintf(stderr," %2.6f %2.6f \n", z, s);
	}

	/*
	 * C. (1 point) Derivatives and integrals with cubic spline
	 */

	// Cubic spline interpolation with x and y values from A.
	s=0;
	z=0;
	x0 = x1[0];
	xn = x1[dim-1];
	h = (xn - x0)/steps;
	cspline* cs = cspline_new(dim,x1,y1);
	fprintf(stdout, "\n\n"); //New block (Gnuplot)
	fprintf(stdout, "# x y (cubic spline interpolation)\n");
	for(int i = 0; i < steps; i++){
	    z = x0 + h*i;
	    s = cspline_get(cs, z);
	    fprintf(stdout," %2.6f %2.6f \n", z, s);
	}

	// Derivative of the cubic spline interpolation with x,y values from B.
	s=0;
	z=0;
	x0 = x2[0];
	xn = x2[dim-1];
	h = (xn - x0)/steps;
	cs = cspline_new(dim,x2,y2);
	fprintf(stderr, "\n\n"); //New block (Gnuplot)
	fprintf(stderr, "# x y (derivative of the cubic spline)\n");
	for(int i = 0; i < steps; i++){
	    z = x0 + h*i;
	    s = cspline_deriv(cs, z);
	    fprintf(stderr," %2.6f %2.6f \n", z, s);
	}

	// Integration of the cubic spline interpolation with x,y values from B.
	fprintf(stderr, "\n\n"); //New block (Gnuplot)
	fprintf(stderr, "# x y (derivative of the cubic spline)\n");
	for(int i = 0; i < steps; i++){
	    z = x0 + h*i;
	    s = cspline_int(cs, z);
	    fprintf(stderr," %2.6f %2.6f \n", z, s);
	}

	// Free memory
	qspline_free(qs);
	cspline_free(cs);
	free(x1);
	free(x2);
	free(y1);
	free(y2);
	fclose(stdout);
        fclose(stderr);

	return 0;
}