Ejemplo n.º 1
0
/**
 * Interpolate through the y values of a histogram using a cubic spline,
 * assuming that the calculated "nodes" are stepSize apart.
 * Currently errors are ignored.
 * @param input Input histogram defining x values and containing calculated
 * Y values at stepSize intervals. It is assumed that the first/last points
 * are always calculated points.
 * @param stepSize The space, in indices, between the calculated points
 * @return A new Histogram with the y-values from the result of a linear
 * interpolation. The XMode of the output will match the input histogram.
 */
Histogram interpolateCSpline(const Histogram &input, const size_t stepSize) {
  sanityCheck(input, stepSize, minSizeForCSplineInterpolation(), CSPLINE_NAME);

  HistogramY ynew(input.y().size());
  interpolateYCSplineInplace(input, stepSize, ynew);
  // Cheap copy
  Histogram output(input);
  if (output.yMode() == Histogram::YMode::Counts) {
    output.setCounts(ynew);
  } else {
    output.setFrequencies(ynew);
  }
  return output;
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
    if (argc != 3)
    {
        print_usage();
        exit(EXIT_FAILURE);
    }

    int nw, m;
    nw = atoi(argv[2]);
    if (nw < 2)
    {
        std::cerr << "Error: Savitzky-Golay window half-width must be "
                  << "an integer greater than 1: nw = " << nw << std::endl;
        exit(EXIT_FAILURE);
    }
    m = 2*nw + 1;
    std::cout << "Savitzky-Golay window half-width: " << nw << std::endl;
    std::cout << "Number of Savitzky-Golay points:  " << m << std::endl;
    ArrSection c(-nw, nw, true);
    for (int i = -nw; i <= nw; ++i)
    {
        c[i] = ((3*m*m - 7 - 20*i*i)/4.0) / (m*(m*m - 4)/3.0);
        if (i >= 0) std::cout << i << "  " << c[i] << std::endl;
    }

    std::ifstream ranges("ranges.txt");
    if (!ranges.is_open())
    {
        std::cerr << "\nError: file ranges.txt is not open." << std::endl;
        exit(EXIT_FAILURE);
    }
    std::string lname;
    getline(ranges, lname);
    getline(ranges, lname);
    std::cout << "\nranges.txt" << std::endl;
    double hvmin, hvmax;
    std::vector<DblPair> intervals;
    size_t nranges = 0;
    while (ranges >> lname >> hvmin >> hvmax)
    {
        std::cout << lname << " " << hvmin << " " << hvmax << std::endl;
        intervals.push_back(std::make_pair(hvmin, hvmax));
        ++nranges;
    }

    std::string fname_in(argv[1]);
    std::ifstream infile(fname_in.c_str());
    if (!infile.is_open())
    {
        std::cerr << "Error: file " << fname_in
                  << " is not open." << std::endl;
        exit(EXIT_FAILURE);
    }
    std::vector<double> xold, yold;
    double x, y;
    size_t nold = 0;
    while (infile >> x >> y)
    {
        xold.push_back(x);
        yold.push_back(y);
        ++nold;
    }
    infile.close();
    infile.clear();

    ArrSection ywrk(0, nold-1, false), ynew(0, nold-1, false);
    for (int i = 0; i < nold; ++i) ywrk[i] = yold[i];
    
    std::string fname_out("obj_" + fname_in);
    std::ofstream outfile(fname_out.c_str());
    if (!outfile.is_open())
    {
        std::cerr << "Error: file " << fname_out
                  << " is not open." << std::endl;
        exit(EXIT_FAILURE);
    }
    outfile << "oname ObjectiveName\ndata in arbitrary_units" << std::endl;

    std::string fname_plot("obj_plot_" + fname_in);
    std::ofstream outfile_plot(fname_plot.c_str());
    if (!outfile_plot.is_open())
    {
        std::cerr << "Error: file " << fname_plot
                  << " is not open." << std::endl;
        exit(EXIT_FAILURE);
    }
    outfile_plot << "#  x               y               "
                 << "error(y)" << std::endl;


    // calculate smoothed spectrum ynew from ywrk, residuals ewrk/enew
    ArrSection ewrk(0, nold-1, false), enew(0, nold-1, false);
    for (int ihv = 0; ihv < nold; ++ihv)
    {
        for (int i = -nw; i <= nw; ++i) ynew[ihv] += c(i) * ywrk(ihv+i);
        ewrk[ihv] = fabs( ynew(ihv) - ywrk(ihv) );
    }
    for (int ihv = 0; ihv < nold; ++ihv)
        for (int i = -nw; i <= nw; ++i) enew[ihv] += c(i) * ewrk(ihv+i);

    double w;
    for (int ihv = 0; ihv < nold; ++ihv)
    {
        if (utils::is_contained_in(xold.at(ihv), intervals))
        {
            enew[ihv] *= 2.0;
            w = 1.0 / pow(enew(ihv), 2);
        }
        else
        {
            enew[ihv] = 0.0;
            w = 0.0;
        }
        outfile << utils::double_to_string(xold.at(ihv)) << " "
                << utils::double_to_string(ynew(ihv)) << " "
                << utils::double_to_string(w) << std::endl;
        outfile_plot << utils::double_to_string(xold.at(ihv)) << " "
                     << utils::double_to_string(ynew(ihv)) << " "
                     << utils::double_to_string(enew(ihv)) << std::endl;
    }
    outfile.close();
    outfile.clear();
    outfile_plot.close();
    outfile_plot.clear();

    return EXIT_SUCCESS;
}
Ejemplo n.º 3
0
//---------------------------------------------------------
int main(int argc, char **argv)
{
   TPS<double>         rbf;
   Polinomio<double>   pol;
   Matrix<double>      A,B;
   Vector<double>      x,y,f;
   Vector<double>      lambda,b;
   Vector<double>      xnew,ynew,fnew; 
   double              c=0.01;
   int                 n,ni,m;
 
//make the data in the square [0,1] x [0,1]   
   make_data(0,1,0,1, 21, 21, x, y, ni, n);

//stablish the exponent in: r^beta log(r)
   rbf.set_beta(4);
   
//configure the associate polynomial
// pol.make( data_dimension, degree_pol)
   pol.make(2 , rbf.get_degree_pol());
   
//show the rbf and pol info
   cout<<rbf;
   cout<<pol;
   
//show the number of nodes   
   cout<<endl;
   cout<<"total nodes    N  = "<<n<<endl;
   cout<<"interior nodes ni = "<<ni<<endl;
   cout<<"boundary nodes nf = "<<n-ni<<endl;
   cout<<endl;
   

//get the number of elements in the polynomial base
   m = pol.get_M();
    
//resize the matrices to store the partial derivatives
   A.Resize(n+m,n+m);  A = 0.0;
   B.Resize(n+m,n+m);  B = 0.0;
   
//Recall that this problem has the general form
//   (Uxx+Uyy)   (Pxx+Pyy)   =  f     interior nodes  0..ni 
//     U           P_b       =  g     boundary nodes  ni..n 
//   P^transpose   0         =  0     momentun conditions in P  
//
// P is the polynomial wit size n x m
// P_b is the polynomial working in the boundary nodes, size   nf x m
// Pxx+Pyy  has size  ni x m   
   
//make the matriz derivatives   
   fill_matrix(   "dxx"     , rbf , pol , c , x , y, 0 ,  ni ,  A);
   fill_matrix(   "dyy"     , rbf , pol , c , x , y, 0 ,  ni ,  B);
      
   A = A + B;   //  A <-  Uxx + Uyy
    
//Add the submatriz for the boundary nodes:   U , P_b           boundary nodes  ni..n        
   fill_matrix(   "normal"  , rbf , pol , c , x , y, ni,   n ,  A);
   
//Add the submatriz P^transpose at the end:    P^transpose   
   fill_matrix( "pol_trans" , rbf , pol , c , x , y, n ,  n+m,  A);
     
//resize the vector to store the right_size of the PDE      
   b.Resize(n+m); b = 0.0;
    
//fill with   f  
   for(int i=0;i<ni;i++)
      b(i) = right_side(x(i), y(i));
      
//fill with the boundary condition      
   for(int i=ni;i<n;i++)
      b(i) = boundary_condition(x(i),y(i));
      
//solve the linear system of equations 
   lambda =  gauss(A,b);
    
//make the new data grid    
   int ni2,n2;
   make_data(0,1,0,1, 41, 41, xnew, ynew, ni2, n2);
    
//interpolate on this new data grid (xnew,ynew)     
   fnew = interpolate(rbf,pol,c,lambda,x,y,xnew,ynew);
     
//determine the maximum error
   double e=0.0;
   for(int i=0;i<ni2;i++)
   {
     e = max(e, fabs(fnew(i) - sin(2*M_PI*xnew(i))*cos(2*M_PI*ynew(i)))  );
   }
     
//show the error     
   cout<<endl;
   cout<<"The maximum error: e_max = "<<e<<endl<<endl;
   
//store the interpolated data   
   save_gnu_data("data",xnew,ynew,fnew);
     
  return 0;
}