Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.42 KB | None | 0 0
  1. //window.cpp
  2. #include "window.h"
  3.  
  4. window::window()
  5. {
  6.     setupUi(this);
  7.  
  8.     //alle möglichen slider
  9.     connect(kopplung_x_slider, SIGNAL(valueChanged(int)),
  10.         this, SLOT(scale_slider_kopplung_x(int)));
  11.     connect(kopplung_y_slider, SIGNAL(valueChanged(int)),
  12.         this, SLOT(scale_slider_kopplung_y(int)));
  13.     connect(noise_x_slider, SIGNAL(valueChanged(int)),
  14.         this, SLOT(scale_slider_noise_x(int)));
  15.     connect(noise_y_slider, SIGNAL(valueChanged(int)),
  16.         this, SLOT(scale_slider_noise_y(int)));
  17.     connect(slider_length_timeseries, SIGNAL(valueChanged(int)),
  18.         this, SLOT(scale_slider_length_timeseries(int)));
  19.     connect(timelag_x_slider, SIGNAL(valueChanged(int)),
  20.         this, SLOT(scale_slider_timelag_x(int)));
  21.     connect(timelag_y_slider, SIGNAL(valueChanged(int)),
  22.         this, SLOT(scale_slider_timelag_y(int)));
  23.     connect(varianz_slider, SIGNAL(valueChanged(int)),
  24.         this, SLOT(scale_slider_varianz(int)));
  25.     connect(int_size_slider, SIGNAL(valueChanged(int)),
  26.         this, SLOT(scale_slider_int_size(int)));
  27.  
  28.     //Auswahl Kernschätzer
  29.     connect(actionGauss, SIGNAL(triggered()),
  30.         this, SLOT(check_kernel_gauss()));
  31.     connect(actionEpanechnikov, SIGNAL(triggered()),
  32.         this, SLOT(check_kernel_epanechnikov()));
  33.     this, SLOT(check_auto());
  34.  
  35.  
  36.     //Beenden
  37.     connect(actionBeenden, SIGNAL (triggered()),
  38.         qApp, SLOT(quit()));  
  39.     //Berechnen
  40.     connect(actionBerechnen, SIGNAL (triggered()),
  41.         this, SLOT(read_var()));
  42.     //Exportieren
  43.     connect(actionExport, SIGNAL(triggered()),
  44.         this, SLOT(exportDocument()));
  45.  
  46.  
  47.     // Set axes
  48.     plot_x -> setAxisTitle(xBottom, "t");
  49.     plot_x -> setAxisScale(xBottom, 1.0, (length_timeseries_text -> text()).toDouble());
  50.     plot_x -> setAxisTitle(yLeft, "x");
  51.     plot_x -> setAxisScale(yLeft, 0, 1.0);
  52.  
  53.     plot_y -> setAxisTitle(xBottom, "t");
  54.     plot_y -> setAxisScale(xBottom, 1.0, (length_timeseries_text -> text()).toDouble());
  55.     plot_y -> setAxisTitle(yLeft, "y");
  56.     plot_y -> setAxisScale(yLeft, 0, 1.0);
  57.  
  58.     QString tau = 0x03C4;   //Falls es individueller sein soll
  59.     QwtText title_ordinate(tau);
  60.     title_ordinate.setFont(QFont("MS Shell Dlg", 14));
  61.     plot_mutualinf -> setAxisTitle(xBottom, title_ordinate);
  62.     plot_mutualinf -> setAxisScale(xBottom, -20, 20);
  63. //  plot_mutualinf -> setAxisScale(xBottom, -(length_timeseries_text -> text()).toDouble(), (length_timeseries_text -> text()).toDouble());
  64.     plot_mutualinf -> setAxisTitle(yLeft, "I / bit");
  65.     plot_mutualinf -> setAxisScale(yLeft, 0, 1.0);
  66.  
  67.     //Statusbar
  68.     statusBar()->showMessage("Bereit");
  69.  
  70. }
  71.  
  72.  
  73. //Die slider mappen für die gewünschten Bereiche
  74. void window::scale_slider_kopplung_x(int _scale)
  75. {
  76.     double _scale_label=0;
  77.     _scale_label = (double)_scale/1000;
  78.     kopplung_x_text -> setNum(_scale_label);
  79.     label_11 -> setNum(_scale_label);
  80.     label_15 -> setNum(1-_scale_label);
  81. }
  82. void window::scale_slider_kopplung_y(int _scale)
  83. {
  84.     double _scale_label=0;
  85.     _scale_label = (double)_scale/1000;
  86.     kopplung_y_text -> setNum(_scale_label);
  87.     label_20 -> setNum(_scale_label);
  88.     label_24 -> setNum(1-_scale_label);
  89. }
  90. void window::scale_slider_noise_x(int _scale)
  91. {
  92.     double _scale_label=0;
  93.     _scale_label = (double)_scale/1000;
  94.     noise_x_text -> setNum(_scale_label);
  95.     label_17 -> setNum(_scale_label);
  96. }
  97. void window::scale_slider_noise_y(int _scale)
  98. {
  99.     double _scale_label=0;
  100.     _scale_label = (double)_scale/1000;
  101.     noise_y_text -> setNum(_scale_label);
  102.     label_26 -> setNum(_scale_label);
  103. }
  104. void window::scale_slider_length_timeseries(int _scale)
  105. {
  106.     int _scale_label=10;
  107.     if (_scale <= 10) _scale_label = 10*_scale;
  108.     else if (_scale > 10 && _scale <= 19) _scale_label=100*((_scale - 10)+1);
  109.     else _scale_label=1000*((_scale - 19)+1);
  110.     length_timeseries_text -> setNum(_scale_label);
  111.     timelag_x_slider -> setMaximum(_scale_label-1);
  112.     timelag_y_slider -> setMaximum(_scale_label-1);
  113.     plot_x -> setAxisScale(xBottom, 1.0, (length_timeseries_text -> text()).toDouble());
  114.     plot_y -> setAxisScale(xBottom, 1.0, (length_timeseries_text -> text()).toDouble());
  115. //  plot_mutualinf -> setAxisScale(xBottom, -(length_timeseries_text -> text()).toDouble(), (length_timeseries_text -> text()).toDouble());
  116.  
  117.     plot_x -> replot();
  118.     plot_y -> replot();
  119. //  plot_mutualinf -> replot();
  120. }
  121. void window::scale_slider_timelag_x(int _scale)
  122. {
  123.     label_13 -> setNum(_scale);
  124. }
  125. void window::scale_slider_timelag_y(int _scale)
  126. {
  127.     label_22 -> setNum(_scale);
  128. }
  129. void window::scale_slider_varianz(int _scale)
  130. {
  131.     //varianz_text -> setNum((double)(_scale)/(double)(length_timeseries_text -> text()).toDouble());
  132.     varianz_text -> setNum(_scale);
  133. }
  134. void window::scale_slider_int_size(int _scale)
  135. {
  136.     int _scale_label=pow(2., (int)_scale);
  137.     int_size_text -> setNum(_scale_label);
  138. }
  139.  
  140. //Auswahl Kernschätzer (Funktion)
  141. void window::check_kernel_gauss()
  142. {
  143.     actionGauss -> setChecked(true);
  144.     actionEpanechnikov -> setChecked(false);
  145.     QString dot = 0x22C5;
  146.     QString sigma = 0x03C3;
  147.     label_9 -> setText(QString("Varianz T%1%2%3").arg(dot).arg(sigma).arg("²"));
  148. //  label_9 -> setText(QString("Varianz %1").arg(sigma));
  149. }
  150. void window::check_kernel_epanechnikov()
  151. {
  152.     actionGauss -> setChecked(false);
  153.     actionEpanechnikov -> setChecked(true);
  154.     QString dot = 0x22C5;
  155.     QString alpha = 0x03B1;
  156.     label_9 -> setText(QString("Steuerparameter 5%1%2%3%4").arg(dot).arg("T").arg(dot).arg(alpha));
  157. //  label_9 -> setText(QString("Steuerparameter %1").arg(alpha));
  158. }
  159.  
  160.  
  161. //Variablen auslesen und übergeben und anschließend Graphen plotten
  162. QVector<QPointF> window::read_var()
  163. //void window::read_var()
  164. {
  165.     statusBar()->showMessage("Berechnen");
  166.     kopplung_x = (kopplung_x_text -> text()).toDouble();
  167.     kopplung_y = (kopplung_y_text -> text()).toDouble();
  168.     timelag_x = (timelag_x_slider -> text()).toDouble();
  169.     timelag_y = (timelag_y_slider -> text()).toDouble();
  170.     noise_x = (noise_x_text -> text()).toDouble();
  171.     noise_y = (noise_y_text -> text()).toDouble();
  172.     length = (length_timeseries_text -> text()).toDouble();
  173.     varianz = (varianz_text -> text()).toDouble();
  174.     field_size = (int_size_text -> text()).toDouble();
  175.    
  176.     if (actionGauss->isChecked() == true) kernel = gauss;
  177.     else kernel = epa;
  178.     if (action_auto->isChecked() == true) auto_mi = true;
  179.     else auto_mi = false;
  180.  
  181.  
  182.     mut_inf.set_timeseries(kopplung_x, kopplung_y, timelag_x, timelag_y, noise_x, noise_y, length, varianz, kernel, auto_mi, field_size);
  183.     TimeSeries timeseries;
  184.     timeseries = mut_inf.generate_timeseries();
  185.  
  186.     //Zeitreihen unverändert ausgeben
  187.     QFile writeFile2("Zeitreihen.txt");
  188.     writeFile2.open(QIODevice::WriteOnly | QIODevice::Text);
  189.     QTextStream out2(&writeFile2);
  190.     out2 << "Index\tx\ty" << endl;
  191.     for (int i=0; i<timeseries.x.size(); ++i) out2 << i << "\t" << timeseries.x.at(i) << "\t" << timeseries.y.at(i) <<endl;
  192.     writeFile2.close();
  193.  
  194.  
  195.     //Zeitreihen zeichnen
  196.     QVector<QPointF> data_x, data_y;
  197.     for (int i=1; i<=timeseries.x.size(); ++i)
  198.     {
  199.         data_x.append(QPointF(i , timeseries.x.at(i)));
  200.         data_y.append(QPointF(i , timeseries.y.at(i)));
  201.     }
  202.  
  203.     QwtPlotCurve curve_x, curve_y;
  204.     curve_x.setSamples(data_x);
  205.     curve_x.attach(plot_x);
  206.     curve_y.setSamples(data_y);
  207.     curve_y.attach(plot_y);
  208.  
  209.     plot_x -> replot();
  210.     plot_y -> replot();
  211.  
  212.  
  213.     //relative Rangzahlen berechnen und daraus die Transinformation
  214.     QVector<double> final(40+1);
  215.     clock_t start=0, stop=0;
  216.     start = clock();
  217.     final = mut_inf.calculate_rang(timeseries);
  218.     stop = clock();
  219.  
  220.  
  221.     //Transinformation zeichnen
  222.     double maximum=0;
  223.     for (int i=0; i<(2*20); ++i) if (maximum < std::max(final.at(i), final.at(i+1))) maximum = std::max(final.at(i), final.at(i+1));
  224.     plot_mutualinf -> setAxisScale(yLeft, 0, (ceil(maximum*10))/10.  );
  225.  
  226.  
  227.     QFile writeFile1("mutual_inf.tex");
  228.     writeFile1.open(QIODevice::WriteOnly | QIODevice::Text);
  229.     QTextStream out1(&writeFile1);
  230.     out1 << "\\beginpicture" << endl;
  231.     out1 << "\\setcoordinatesystem units <.02\\textwidth," << 3.5/((double)(ceil(maximum*10))/10.+.1)-1.25  << "cm> point at 0 0" << endl;
  232.     out1 << "\\setplotarea x from -20 to 20, y from 0 to "<< (double)(ceil(maximum*10))/10.+.1 << endl;
  233.     out1 << "\\axis bottom label {$\\tau$}" << endl;
  234.     out1 << "ticks numbered from -20 to 20 by 5" << endl;
  235.     out1 << "unlabeled short from -20 to 20 by 1 /" << endl;
  236.     out1 << "\\axis left label {\\begin{turn}{90}$\\nicefrac{I^{\\mathrm{MI}}}{\\mathrm{bit}}$\\end{turn}}" << endl;
  237.     out1 << "ticks numbered from 0 to " << (double)(ceil(maximum*10))/10.+0.1 << " by .2" << endl;
  238.     out1 << "unlabeled short from 0 to " << (double)(ceil(maximum*10))/10. << " by .1 /" << endl;
  239.     out1 << "\\setlinear" << endl;
  240.     out1 << "\\setplotsymbol ({\\circle*{.25}} [Bl])" << endl;
  241.     out1 << "\\plot ";
  242.     for (int i=0; i<final.size(); ++i)  out1 <<  i-20 << " " << final[i] << "\t";
  243.     out1 <<" /" << endl;
  244.     out1 << "\\endpicture" << endl;
  245.  
  246. //  out1 << "tau\tI^MI / bit" << endl;
  247. //  for (int i=0; i<final.size(); ++i)  out1 <<  i-20 << "\t " << final[i] <<  endl;
  248.  
  249.     out1 << "%Dauer in ms: " << stop-start ;
  250.     writeFile1.close();
  251.  
  252.     QVector<QPointF> data_mut;
  253.     for (int i=0; i<final.size(); ++i) data_mut.append(QPointF(i-20 , final.at(i)));
  254.  
  255.     QwtPlotCurve curve_mut;
  256.     curve_mut.setSamples(data_mut);
  257.     curve_mut.attach(plot_mutualinf);
  258.  
  259.     plot_mutualinf -> replot();
  260.     statusBar()->showMessage("Fertig");
  261.  
  262.     return data_mut;
  263. }
  264.  
  265. void window::exportDocument()
  266. {
  267.     QwtPlotCurve curve_mut;
  268.     curve_mut.setSamples(data_mut);
  269.     curve_mut.attach(plot_mutualinf);
  270.  
  271.  
  272. #ifndef QT_NO_PRINTER
  273.     QString fileName = "mutual_inf.pdf";
  274. #else
  275.     QString fileName = "mutual_inf.png";
  276. #endif
  277.  
  278. #ifndef QT_NO_FILEDIALOG
  279.     const QList<QByteArray> imageFormats =
  280.         QImageWriter::supportedImageFormats();
  281.  
  282.     QStringList filter;
  283.     filter += "PDF Documents (*.pdf)";
  284. #ifndef QWT_NO_SVG
  285.     filter += "SVG Documents (*.svg)";
  286. #endif
  287.     filter += "Postscript Documents (*.ps)";
  288.  
  289.     if ( imageFormats.size() > 0 )
  290.     {
  291.         QString imageFilter("Images (");
  292.         for ( int i = 0; i < imageFormats.size(); i++ )
  293.         {
  294.             if ( i > 0 )
  295.                 imageFilter += " ";
  296.             imageFilter += "*.";
  297.             imageFilter += imageFormats[i];
  298.         }
  299.         imageFilter += ")";
  300.  
  301.         filter += imageFilter;
  302.     }
  303.  
  304.     fileName = QFileDialog::getSaveFileName(
  305.         this, "Export File Name", fileName,
  306.         filter.join(";;"), NULL, QFileDialog::DontConfirmOverwrite);
  307. #endif
  308.  
  309.     if ( !fileName.isEmpty() )
  310.     {
  311.         QwtPlotRenderer renderer;
  312. #if 0
  313.         // flags to make the document look like the widget
  314.         renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground, false);
  315.         renderer.setLayoutFlag(QwtPlotRenderer::KeepMargins, true);
  316.         renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames, true);
  317. #endif
  318.         renderer.renderDocument(plot_mutualinf, fileName, QSizeF(300, 200), 85);
  319.     }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement