osipyonok

Untitled

Feb 19th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.14 KB | None | 0 0
  1. #include "stdafx.h"
  2. // Example for C++ Interface to Gnuplot
  3.  
  4. // requirements:
  5. // * gnuplot has to be installed (http://www.gnuplot.info/download.html)
  6. // * for Windows: set Path-Variable for Gnuplot path (e.g. C:/program files/gnuplot/bin)
  7. //             or set Gnuplot path with: Gnuplot::set_GNUPlotPath(const std::string &path);
  8.  
  9. #include "decimal.h"
  10. #include <iostream>
  11. #include <complex>
  12. #include <cassert>
  13. #include<vector>
  14. #include<bits/stdc++.h>
  15. #include "gnuplot_i.hpp" //Gnuplot class handles POSIX-Pipe-communikation with Gnuplot
  16. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
  17.  #include <conio.h>   //for getch(), needed in wait_for_key()
  18.  #include <windows.h> //for Sleep()
  19.  void sleep(int i) { Sleep(i*1000); }
  20. #endif
  21.  
  22.  
  23. #define SLEEP_LGTH 2  // sleep time in seconds
  24. #define NPOINTS    50 // length of array
  25.  
  26. void wait_for_key(); // Programm halts until keypress
  27.  
  28. using std::cout;
  29. using std::endl;
  30. using namespace std;
  31. using namespace dec;
  32.  
  33.  
  34. #define M_PI 3.14159265358979323846
  35.  
  36. inline string dtos(double d){ostringstream strs; strs << d; return strs.str();}
  37.  
  38.  
  39. int main(int argc, char* argv[])
  40. {
  41.     // if path-variable for gnuplot is not set, do it with:
  42.     // Gnuplot::set_GNUPlotPath("C:/program files/gnuplot/bin/");
  43.  
  44.     // set a special standard terminal for showonscreen (normally not needed),
  45.     //   e.g. Mac users who want to use x11 instead of aqua terminal:
  46.     // Gnuplot::set_terminal_std("x11");
  47.  
  48.     cout << "*** example of gnuplot control through C++ ***" << endl << endl;
  49.  
  50.     //
  51.     // Using the GnuplotException class
  52.     //
  53.     freopen("input.txt","r",stdin);
  54.     freopen("output.txt","w",stdout);
  55.  
  56.     int N = 0;
  57.     vector<double> x_1;
  58.     vector<decimal<10>> x;
  59.     double tmp;
  60.     while(cin >> tmp){
  61.         ++N;
  62.         //x_1.push_back(tmp);
  63.         x.push_back(decimal<10>(tmp));
  64.     }
  65.     int n = N;
  66.     vector<double> dft(N + 2);
  67. //  vector<complex<double>> ff(N + 2);
  68.  
  69.  
  70.  
  71.     for (int k = 0; k < n; k++) {  // For each output element
  72.     //    double sumreal = 0;
  73.     //    double sumimag = 0;
  74.         decimal<10> sumreal = decimal<10>(0);
  75.         decimal<10> sumimag = decimal<10>(0);
  76.         for (int t = 0; t < n; t++) {  // For each input element
  77.             //double angle = 2 * M_PI * t * k / n;
  78.             decimal<10> angle = decimal<10>(2 * M_PI * t * k / n);
  79.             //angle *= decimal<10>(M_PI);
  80.             sumreal +=  x/*_1*/[t] * decimal<10>(cos(angle.getAsXDouble()));
  81.             sumimag += -x/*_1*/[t] * decimal<10>(sin(angle.getAsDouble()));
  82.         }
  83.         dft[k] = abs(sumreal.getAsDouble());
  84.  //       cout << sumreal << endl;
  85.     }
  86.     vector<double> mxx;
  87.     n /= 2;
  88.     for(int k = 0 ; k < n ; ++k){
  89.         if(k && (k - n + 1)){
  90.             if(dft[k] > dft[k - 1] && dft[k] > dft[k + 1]){
  91.                 mxx.push_back(k * 0.01);
  92.             }
  93.         }
  94.         if(!k){
  95.             if(dft[k] > dft[k + 1]){
  96.                 mxx.push_back(k * 0.01);
  97.             }
  98.         }
  99.         if(!(k - n + 1)){
  100.             if(dft[k] > dft[k - 1]){
  101.                 mxx.push_back(k * 0.01);
  102.             }
  103.         }
  104.     }
  105.     for(int i = 0 ; i < mxx.size() ; ++i){
  106.         cout << mxx[i] << " ";
  107.     }
  108.     cout << endl;
  109.  /*   for (int k = 0; k < n; k++) {
  110.         double sumreal = 0;
  111.         double sumimag = 0;
  112.         for (int t = 0; t < n; t++) {  // For each input element
  113.             double angle = 2 * M_PI * t * k / n;
  114.             sumreal +=  x_1[t] * cos(angle);
  115.             sumimag += -x_1[t] * sin(angle);
  116.         }
  117.         //cout << sumreal << endl;
  118.         //mx = max(mx , fabs(sumreal));
  119.         dft[k] = sumreal;
  120.     }*/
  121.     //for(int k = 1 ; k <= n ; ++k){
  122.     //  ff[k] = 0;
  123.     //  for(int i = 1 ; i <= n ; ++i){
  124.     //      ff[k] = ff[k] + (x_1[i] * exp(complex<double>(0 , -1 * 2 * M_PI * (i - 1) * (k - 1) / n)));
  125.         //  dft[k] = dft[k] + (x_1[i] * exp((complex<double>(0 , -1)));
  126.     //  }
  127.     //}
  128.     //for(int k = 0 ; k < n ; ++k)dft[k] = ff[k+1].real();
  129.     reverse(dft.begin() , dft.end());
  130.     dft.pop_back();
  131.     reverse(dft.begin() , dft.end());
  132.     string to_plot = "[0:5] ";
  133.     int cur = 0;
  134.     for(double i = 0. ; fabs(i - 5.00) > 1e-3 ; i += 0.01){
  135.         if(fabs(i - 4.99) <= 1e-3){
  136.             to_plot += dtos(fabs(dft[cur]));
  137.             break;
  138.         }
  139.         to_plot += "x>=";
  140.         to_plot += dtos(i);
  141.         to_plot += "&x<";
  142.         to_plot += dtos(i + 0.01);
  143.         to_plot += "?";
  144.         to_plot += dtos(fabs(dft[cur++]));
  145.         to_plot += ":";
  146.     }
  147. //  cout << to_plot << endl;
  148.     try
  149.     {
  150.         Gnuplot g1("lines");
  151.        
  152.         //
  153.         // Slopes
  154.         //
  155.         cout << "*** plotting slopes" << endl;
  156.         g1.set_title("Slopes\\nNew Line");
  157.  
  158.         cout << "y = x" << endl;
  159.       //  g1.plot_slope(1.0,0.0,"y=x");
  160.         g1.plot_equation(to_plot,"dft");
  161.         //g1.plot_equation("[0:10] x>=1&x<2?1.0002+0*(x-1)+0.3134*(x-1)**2+-0.2795*(x-1)**3:x>=2&&x<3?1.0341+-0.21178*(x-2)+-0.5252*(x-2)**2+0.3029*(x-2)**3:0","sine");
  162. /*
  163.         cout << "y = 2*x" << endl;
  164.         g1.plot_slope(2.0,0.0,"y=2x");
  165.  
  166.         cout << "y = -x" << endl;
  167.         g1.plot_slope(-1.0,0.0,"y=-x");
  168.         g1.unset_title();
  169.  
  170.         //
  171.         // Equations
  172.         //
  173.         g1.reset_plot();
  174.         cout << endl << endl << "*** various equations" << endl;
  175.  
  176.         cout << "y = sin(x)" << endl;
  177.         g1.plot_equation("sin(x)","sine");
  178.  
  179.         cout << "y = log(x)" << endl;
  180.         g1.plot_equation("log(x)","logarithm");
  181.  
  182.         cout << "y = sin(x) * cos(2*x)" << endl;
  183.         g1.plot_equation("sin(x)*cos(2*x)","sine product");
  184.  
  185.         //
  186.         // Styles
  187.         //
  188.         g1.reset_plot();
  189.         cout << endl << endl << "*** showing styles" << endl;
  190.  
  191.         cout << "sine in points" << endl;
  192.         g1.set_pointsize(0.8).set_style("points");
  193.         g1.plot_equation("sin(x)","points");
  194.  
  195.         cout << "sine in impulses" << endl;
  196.         g1.set_style("impulses");
  197.         g1.plot_equation("sin(x)","impulses");
  198.  
  199.         cout << "sine in steps" << endl;
  200.         g1.set_style("steps");
  201.         g1.plot_equation("sin(x)","steps");
  202.  
  203.         //
  204.         // Save to ps
  205.         //
  206.         g1.reset_all();
  207.         cout << endl << endl << "*** save to ps " << endl;
  208.  
  209.         cout << "y = sin(x) saved to test_output.ps in working directory" << endl;
  210.         g1.savetops("test_output");
  211.         g1.set_style("lines").set_samples(300).set_xrange(0,5);
  212.         g1.plot_equation("sin(12*x)*exp(-x)").plot_equation("exp(-x)");
  213.  
  214.         g1.showonscreen(); // window output
  215.  
  216.  
  217.         //
  218.         // User defined 1d, 2d and 3d point sets
  219.         //
  220.         std::vector<double> x, y, y2, dy, z;
  221.  
  222.         for (int i = 0; i < NPOINTS; i++)  // fill double arrays x, y, z
  223.         {
  224.             x.push_back((double)i);             // x[i] = i
  225.             y.push_back((double)i * (double)i); // y[i] = i^2
  226.             z.push_back( x[i]*y[i] );           // z[i] = x[i]*y[i] = i^3
  227.             dy.push_back((double)i * (double)i / (double) 10); // dy[i] = i^2 / 10
  228.         }
  229.         y2.push_back(0.00); y2.push_back(0.78); y2.push_back(0.97); y2.push_back(0.43);
  230.         y2.push_back(-0.44); y2.push_back(-0.98); y2.push_back(-0.77); y2.push_back(0.02);
  231.  
  232.  
  233.         g1.reset_all();
  234.         cout << endl << endl << "*** user-defined lists of doubles" << endl;
  235.         g1.set_style("impulses").plot_x(y,"user-defined doubles");
  236.  
  237.         g1.reset_plot();
  238.         cout << endl << endl << "*** user-defined lists of points (x,y)" << endl;
  239.         g1.set_grid();
  240.         g1.set_style("points").plot_xy(x,y,"user-defined points 2d");
  241.  
  242.         g1.reset_plot();
  243.         cout << endl << endl << "*** user-defined lists of points (x,y,z)" << endl;
  244.         g1.unset_grid();
  245.         g1.plot_xyz(x,y,z,"user-defined points 3d");
  246.  
  247.         g1.reset_plot();
  248.         cout << endl << endl << "*** user-defined lists of points (x,y,dy)" << endl;
  249.         g1.plot_xy_err(x,y,dy,"user-defined points 2d with errorbars");
  250.  
  251.  
  252.         //
  253.         // Multiple output screens
  254.         //
  255.         cout << endl << endl;
  256.         cout << "*** multiple output windows" << endl;
  257.  
  258.         g1.reset_plot();
  259.         g1.set_style("lines");
  260.         cout << "window 1: sin(x)" << endl;
  261.         g1.set_grid().set_samples(600).set_xrange(0,300);
  262.         g1.plot_equation("sin(x)+sin(x*1.1)");
  263.  
  264.         g1.set_xautoscale().replot();
  265.  
  266.         Gnuplot g2;
  267.         cout << "window 2: user defined points" << endl;
  268.         g2.plot_x(y2,"points");
  269.         g2.set_smooth().plot_x(y2,"cspline");
  270.         g2.set_smooth("bezier").plot_x(y2,"bezier");
  271.         g2.unset_smooth();
  272.  
  273.         Gnuplot g3("lines");
  274.         cout << "window 3: log(x)/x" << endl;
  275.         g3.set_grid();
  276.         g3.plot_equation("log(x)/x","log(x)/x");
  277.  
  278.         Gnuplot g4("lines");
  279.         cout << "window 4: splot x*x+y*y" << endl;
  280.         g4.set_zrange(0,100);
  281.         g4.set_xlabel("x-axis").set_ylabel("y-axis").set_zlabel("z-axis");
  282.         g4.plot_equation3d("x*x+y*y");
  283.  
  284.         Gnuplot g5("lines");
  285.         cout << "window 5: splot with hidden3d" << endl;
  286.         g5.set_isosamples(25).set_hidden3d();
  287.         g5.plot_equation3d("x*y*y");
  288.  
  289.         Gnuplot g6("lines");
  290.         cout << "window 6: splot with contour" << endl;
  291.         g6.set_isosamples(60).set_contour();
  292.         g6.unset_surface().plot_equation3d("sin(x)*sin(y)+4");
  293.  
  294.         g6.set_surface().replot();
  295.  
  296.         Gnuplot g7("lines");
  297.         cout << "window 7: set_samples" << endl;
  298.         g7.set_xrange(-30,20).set_samples(40);
  299.         g7.plot_equation("besj0(x)*0.12e1").plot_equation("(x**besj0(x))-2.5");
  300.  
  301.         g7.set_samples(400).replot();
  302.  
  303.         Gnuplot g8("filledcurves");
  304.         cout << "window 8: filledcurves" << endl;
  305.         g8.set_legend("outside right top").set_xrange(-5,5);
  306.         g8.plot_equation("x*x").plot_equation("-x*x+4");
  307.  
  308.         //
  309.         // Plot an image
  310.         //
  311.         Gnuplot g9;
  312.         cout << "window 9: plot_image" << endl;
  313.         const int iWidth  = 255;
  314.         const int iHeight = 255;
  315.         g9.set_xrange(0,iWidth).set_yrange(0,iHeight).set_cbrange(0,255);
  316.         g9.cmd("set palette gray");
  317.         unsigned char ucPicBuf[iWidth*iHeight];
  318.         // generate a greyscale image
  319.         for(int iIndex = 0; iIndex < iHeight*iWidth; iIndex++)
  320.         {
  321.             ucPicBuf[iIndex] = iIndex%255;
  322.         }
  323.         g9.plot_image(ucPicBuf,iWidth,iHeight,"greyscale");
  324.  
  325.         g9.set_pointsize(0.6).unset_legend().plot_slope(0.8,20);
  326.  
  327.         //
  328.         // manual control
  329.         //
  330.         Gnuplot g10;
  331.         cout << "window 10: manual control" << endl;
  332.         g10.cmd("set samples 400").cmd("plot abs(x)/2"); // either with cmd()
  333.         g10 << "replot sqrt(x)" << "replot sqrt(-x)";    // or with <<
  334.         */
  335.         wait_for_key();
  336.  
  337.     }
  338.     catch (GnuplotException ge)
  339.     {
  340.         cout << ge.what() << endl;
  341.     }
  342.  
  343.  
  344.     cout << endl << "*** end of gnuplot example" << endl;
  345.     system("pause");
  346.     return 0;
  347. }
  348.  
  349.  
  350.  
  351. void wait_for_key ()
  352. {
  353. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)  // every keypress registered, also arrow keys
  354.     cout << endl << "Press any key to continue..." << endl;
  355.  
  356.     FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  357.     _getch();
  358. #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
  359.     cout << endl << "Press ENTER to continue..." << endl;
  360.  
  361.     std::cin.clear();
  362.     std::cin.ignore(std::cin.rdbuf()->in_avail());
  363.     std::cin.get();
  364. #endif
  365.     return;
  366. }
Add Comment
Please, Sign In to add comment