#include "smooth.h" #include "vicinity.h" #include #include #include #include #include #include #include void smooth( const std::vector &x, const std::vector &y, std::vector &smoothed_y, int param ) throw ( TError_Generic_error ) { if( x.size() < size_t( param*2 ) ) throw TError_Generic_error( "Not enough elements in vecotr x" ); if( x.size() != y.size() ) throw TError_Generic_error( "x.size() != y.size() in smooth(...)" ); using std::max; using std::min; const size_t chunk_size = 100, chunk_overlap = 20; smoothed_y.resize( y.size() ); for( size_t k=0; k=chunk_overlap size_t chunk_overlap_start = max( chunk_overlap, chunk_start )-chunk_overlap; size_t chunk_overlap_end = min( chunk_end+chunk_overlap, x.size() ); size_t n = chunk_overlap_end-chunk_overlap_start; gsl_vector_const_view gV_x = gsl_vector_const_view_array( &x[chunk_overlap_start], n ); gsl_vector_const_view gV_y = gsl_vector_const_view_array( &y[chunk_overlap_start], n ); gsl_vector_view gV_sy = gsl_vector_view_array ( &smoothed_y[chunk_overlap_start], n ); size_t ncoeffs = n/param; size_t nbreak = ncoeffs - 2; gsl_bspline_workspace *bw; gsl_vector *B; gsl_vector *c; const gsl_vector *gx, *gy; gsl_matrix *X, *cov; gsl_multifit_linear_workspace *mw; double chisq;//, Rsq, dof, tss; /* allocate a cubic bspline workspace (k = 4) */ bw = gsl_bspline_alloc(4, nbreak); B = gsl_vector_alloc(ncoeffs); gx = &gV_x.vector; gy = &gV_y.vector; X = gsl_matrix_alloc(n, ncoeffs); c = gsl_vector_alloc(ncoeffs); cov = gsl_matrix_alloc(ncoeffs, ncoeffs); mw = gsl_multifit_linear_alloc(n, ncoeffs); /* use uniform breakpoints */ gsl_bspline_knots_uniform(x[chunk_overlap_start], x[chunk_overlap_end-1], bw); /* construct the fit matrix X */ for( size_t i = 0; i < n; ++i ) { double xi = gsl_vector_get(gx, i); /* compute B_j(xi) for all j */ gsl_bspline_eval(xi, B, bw); /* fill in row i of X */ for( size_t j=0; j < ncoeffs; ++j ) { double Bj = gsl_vector_get(B, j); gsl_matrix_set(X, i, j, Bj); } } /* do the fit */ //size_t rank; gsl_multifit_linear(X, gy, c, cov, &chisq, mw); /* dof = n - ncoeffs; tss = gsl_stats_wtss(w->data, 1, gy->data, 1, gy->size); Rsq = 1.0 - chisq / tss; fprintf(stderr, "chisq/dof = %e, Rsq = %f\n", chisq / dof, Rsq); */ /* output the smoothed curve */ { double yi, yerr; //printf("#m=1,S=0\n"); for( size_t i=chunk_start; i