Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- // Example for C++ Interface to Gnuplot
- // requirements:
- // * gnuplot has to be installed (http://www.gnuplot.info/download.html)
- // * for Windows: set Path-Variable for Gnuplot path (e.g. C:/program files/gnuplot/bin)
- // or set Gnuplot path with: Gnuplot::set_GNUPlotPath(const std::string &path);
- #include "decimal.h"
- #include <iostream>
- #include <complex>
- #include <cassert>
- #include<vector>
- #include<bits/stdc++.h>
- #include "gnuplot_i.hpp" //Gnuplot class handles POSIX-Pipe-communikation with Gnuplot
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
- #include <conio.h> //for getch(), needed in wait_for_key()
- #include <windows.h> //for Sleep()
- void sleep(int i) { Sleep(i*1000); }
- #endif
- #define SLEEP_LGTH 2 // sleep time in seconds
- #define NPOINTS 50 // length of array
- void wait_for_key(); // Programm halts until keypress
- using std::cout;
- using std::endl;
- using namespace std;
- using namespace dec;
- #define M_PI 3.14159265358979323846
- inline string dtos(double d){ostringstream strs; strs << d; return strs.str();}
- int main(int argc, char* argv[])
- {
- // if path-variable for gnuplot is not set, do it with:
- // Gnuplot::set_GNUPlotPath("C:/program files/gnuplot/bin/");
- // set a special standard terminal for showonscreen (normally not needed),
- // e.g. Mac users who want to use x11 instead of aqua terminal:
- // Gnuplot::set_terminal_std("x11");
- cout << "*** example of gnuplot control through C++ ***" << endl << endl;
- //
- // Using the GnuplotException class
- //
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- int N = 0;
- vector<double> x_1;
- vector<decimal<10>> x;
- double tmp;
- while(cin >> tmp){
- ++N;
- //x_1.push_back(tmp);
- x.push_back(decimal<10>(tmp));
- }
- int n = N;
- vector<double> dft(N + 2);
- // vector<complex<double>> ff(N + 2);
- for (int k = 0; k < n; k++) { // For each output element
- // double sumreal = 0;
- // double sumimag = 0;
- decimal<10> sumreal = decimal<10>(0);
- decimal<10> sumimag = decimal<10>(0);
- for (int t = 0; t < n; t++) { // For each input element
- //double angle = 2 * M_PI * t * k / n;
- decimal<10> angle = decimal<10>(2 * M_PI * t * k / n);
- //angle *= decimal<10>(M_PI);
- sumreal += x/*_1*/[t] * decimal<10>(cos(angle.getAsXDouble()));
- sumimag += -x/*_1*/[t] * decimal<10>(sin(angle.getAsDouble()));
- }
- dft[k] = abs(sumreal.getAsDouble());
- // cout << sumreal << endl;
- }
- vector<double> mxx;
- n /= 2;
- for(int k = 0 ; k < n ; ++k){
- if(k && (k - n + 1)){
- if(dft[k] > dft[k - 1] && dft[k] > dft[k + 1]){
- mxx.push_back(k * 0.01);
- }
- }
- if(!k){
- if(dft[k] > dft[k + 1]){
- mxx.push_back(k * 0.01);
- }
- }
- if(!(k - n + 1)){
- if(dft[k] > dft[k - 1]){
- mxx.push_back(k * 0.01);
- }
- }
- }
- for(int i = 0 ; i < mxx.size() ; ++i){
- cout << mxx[i] << " ";
- }
- cout << endl;
- /* for (int k = 0; k < n; k++) {
- double sumreal = 0;
- double sumimag = 0;
- for (int t = 0; t < n; t++) { // For each input element
- double angle = 2 * M_PI * t * k / n;
- sumreal += x_1[t] * cos(angle);
- sumimag += -x_1[t] * sin(angle);
- }
- //cout << sumreal << endl;
- //mx = max(mx , fabs(sumreal));
- dft[k] = sumreal;
- }*/
- //for(int k = 1 ; k <= n ; ++k){
- // ff[k] = 0;
- // for(int i = 1 ; i <= n ; ++i){
- // ff[k] = ff[k] + (x_1[i] * exp(complex<double>(0 , -1 * 2 * M_PI * (i - 1) * (k - 1) / n)));
- // dft[k] = dft[k] + (x_1[i] * exp((complex<double>(0 , -1)));
- // }
- //}
- //for(int k = 0 ; k < n ; ++k)dft[k] = ff[k+1].real();
- reverse(dft.begin() , dft.end());
- dft.pop_back();
- reverse(dft.begin() , dft.end());
- string to_plot = "[0:5] ";
- int cur = 0;
- for(double i = 0. ; fabs(i - 5.00) > 1e-3 ; i += 0.01){
- if(fabs(i - 4.99) <= 1e-3){
- to_plot += dtos(fabs(dft[cur]));
- break;
- }
- to_plot += "x>=";
- to_plot += dtos(i);
- to_plot += "&x<";
- to_plot += dtos(i + 0.01);
- to_plot += "?";
- to_plot += dtos(fabs(dft[cur++]));
- to_plot += ":";
- }
- // cout << to_plot << endl;
- try
- {
- Gnuplot g1("lines");
- //
- // Slopes
- //
- cout << "*** plotting slopes" << endl;
- g1.set_title("Slopes\\nNew Line");
- cout << "y = x" << endl;
- // g1.plot_slope(1.0,0.0,"y=x");
- g1.plot_equation(to_plot,"dft");
- //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");
- /*
- cout << "y = 2*x" << endl;
- g1.plot_slope(2.0,0.0,"y=2x");
- cout << "y = -x" << endl;
- g1.plot_slope(-1.0,0.0,"y=-x");
- g1.unset_title();
- //
- // Equations
- //
- g1.reset_plot();
- cout << endl << endl << "*** various equations" << endl;
- cout << "y = sin(x)" << endl;
- g1.plot_equation("sin(x)","sine");
- cout << "y = log(x)" << endl;
- g1.plot_equation("log(x)","logarithm");
- cout << "y = sin(x) * cos(2*x)" << endl;
- g1.plot_equation("sin(x)*cos(2*x)","sine product");
- //
- // Styles
- //
- g1.reset_plot();
- cout << endl << endl << "*** showing styles" << endl;
- cout << "sine in points" << endl;
- g1.set_pointsize(0.8).set_style("points");
- g1.plot_equation("sin(x)","points");
- cout << "sine in impulses" << endl;
- g1.set_style("impulses");
- g1.plot_equation("sin(x)","impulses");
- cout << "sine in steps" << endl;
- g1.set_style("steps");
- g1.plot_equation("sin(x)","steps");
- //
- // Save to ps
- //
- g1.reset_all();
- cout << endl << endl << "*** save to ps " << endl;
- cout << "y = sin(x) saved to test_output.ps in working directory" << endl;
- g1.savetops("test_output");
- g1.set_style("lines").set_samples(300).set_xrange(0,5);
- g1.plot_equation("sin(12*x)*exp(-x)").plot_equation("exp(-x)");
- g1.showonscreen(); // window output
- //
- // User defined 1d, 2d and 3d point sets
- //
- std::vector<double> x, y, y2, dy, z;
- for (int i = 0; i < NPOINTS; i++) // fill double arrays x, y, z
- {
- x.push_back((double)i); // x[i] = i
- y.push_back((double)i * (double)i); // y[i] = i^2
- z.push_back( x[i]*y[i] ); // z[i] = x[i]*y[i] = i^3
- dy.push_back((double)i * (double)i / (double) 10); // dy[i] = i^2 / 10
- }
- y2.push_back(0.00); y2.push_back(0.78); y2.push_back(0.97); y2.push_back(0.43);
- y2.push_back(-0.44); y2.push_back(-0.98); y2.push_back(-0.77); y2.push_back(0.02);
- g1.reset_all();
- cout << endl << endl << "*** user-defined lists of doubles" << endl;
- g1.set_style("impulses").plot_x(y,"user-defined doubles");
- g1.reset_plot();
- cout << endl << endl << "*** user-defined lists of points (x,y)" << endl;
- g1.set_grid();
- g1.set_style("points").plot_xy(x,y,"user-defined points 2d");
- g1.reset_plot();
- cout << endl << endl << "*** user-defined lists of points (x,y,z)" << endl;
- g1.unset_grid();
- g1.plot_xyz(x,y,z,"user-defined points 3d");
- g1.reset_plot();
- cout << endl << endl << "*** user-defined lists of points (x,y,dy)" << endl;
- g1.plot_xy_err(x,y,dy,"user-defined points 2d with errorbars");
- //
- // Multiple output screens
- //
- cout << endl << endl;
- cout << "*** multiple output windows" << endl;
- g1.reset_plot();
- g1.set_style("lines");
- cout << "window 1: sin(x)" << endl;
- g1.set_grid().set_samples(600).set_xrange(0,300);
- g1.plot_equation("sin(x)+sin(x*1.1)");
- g1.set_xautoscale().replot();
- Gnuplot g2;
- cout << "window 2: user defined points" << endl;
- g2.plot_x(y2,"points");
- g2.set_smooth().plot_x(y2,"cspline");
- g2.set_smooth("bezier").plot_x(y2,"bezier");
- g2.unset_smooth();
- Gnuplot g3("lines");
- cout << "window 3: log(x)/x" << endl;
- g3.set_grid();
- g3.plot_equation("log(x)/x","log(x)/x");
- Gnuplot g4("lines");
- cout << "window 4: splot x*x+y*y" << endl;
- g4.set_zrange(0,100);
- g4.set_xlabel("x-axis").set_ylabel("y-axis").set_zlabel("z-axis");
- g4.plot_equation3d("x*x+y*y");
- Gnuplot g5("lines");
- cout << "window 5: splot with hidden3d" << endl;
- g5.set_isosamples(25).set_hidden3d();
- g5.plot_equation3d("x*y*y");
- Gnuplot g6("lines");
- cout << "window 6: splot with contour" << endl;
- g6.set_isosamples(60).set_contour();
- g6.unset_surface().plot_equation3d("sin(x)*sin(y)+4");
- g6.set_surface().replot();
- Gnuplot g7("lines");
- cout << "window 7: set_samples" << endl;
- g7.set_xrange(-30,20).set_samples(40);
- g7.plot_equation("besj0(x)*0.12e1").plot_equation("(x**besj0(x))-2.5");
- g7.set_samples(400).replot();
- Gnuplot g8("filledcurves");
- cout << "window 8: filledcurves" << endl;
- g8.set_legend("outside right top").set_xrange(-5,5);
- g8.plot_equation("x*x").plot_equation("-x*x+4");
- //
- // Plot an image
- //
- Gnuplot g9;
- cout << "window 9: plot_image" << endl;
- const int iWidth = 255;
- const int iHeight = 255;
- g9.set_xrange(0,iWidth).set_yrange(0,iHeight).set_cbrange(0,255);
- g9.cmd("set palette gray");
- unsigned char ucPicBuf[iWidth*iHeight];
- // generate a greyscale image
- for(int iIndex = 0; iIndex < iHeight*iWidth; iIndex++)
- {
- ucPicBuf[iIndex] = iIndex%255;
- }
- g9.plot_image(ucPicBuf,iWidth,iHeight,"greyscale");
- g9.set_pointsize(0.6).unset_legend().plot_slope(0.8,20);
- //
- // manual control
- //
- Gnuplot g10;
- cout << "window 10: manual control" << endl;
- g10.cmd("set samples 400").cmd("plot abs(x)/2"); // either with cmd()
- g10 << "replot sqrt(x)" << "replot sqrt(-x)"; // or with <<
- */
- wait_for_key();
- }
- catch (GnuplotException ge)
- {
- cout << ge.what() << endl;
- }
- cout << endl << "*** end of gnuplot example" << endl;
- system("pause");
- return 0;
- }
- void wait_for_key ()
- {
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) // every keypress registered, also arrow keys
- cout << endl << "Press any key to continue..." << endl;
- FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
- _getch();
- #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
- cout << endl << "Press ENTER to continue..." << endl;
- std::cin.clear();
- std::cin.ignore(std::cin.rdbuf()->in_avail());
- std::cin.get();
- #endif
- return;
- }
Add Comment
Please, Sign In to add comment