void FFT::output(const QString &text) { ApplicationWindow *app = (ApplicationWindow *)parent(); QString tableName = app->generateUniqueName(QString(objectName())); d_result_table = app->newHiddenTable(tableName, d_explanation, d_n, 5, text); if (d_graphics_display){ MultiLayer *ml = 0; if (!d_output_graph){ ml = createOutputGraph(); d_output_graph = ml->activeGraph(); } d_output_graph->setTitle(QString()); if (!d_inverse) d_output_graph->setXAxisTitle(tr("Frequency") + " (" + tr("Hz") + ")"); else d_output_graph->setXAxisTitle(tr("Time") + + " (" + tr("s") + ")"); d_output_graph->setYAxisTitle(tr("Amplitude")); d_output_graph->insertCurve(d_result_table, 0, tableName + "_" + tr("Amplitude"), 0); d_output_graph->setCurvePen(d_output_graph->curves() - 1, QPen(ColorBox::color(d_curveColorIndex), 1)); d_output_graph->replot(); if (ml) ml->showMaximized(); } }
QwtPlotCurve* Filter::addResultCurve(double *x, double *y) { ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); const QString tableName = app->generateUniqueName(QString(objectName())); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; d_result_table = app->newHiddenTable(tableName, d_explanation + " " + tr("of") + " " + dataSet, d_points, 2); for (int i=0; i<d_points; i++){ d_result_table->setText(i, 0, locale.toString(x[i], 'e', app->d_decimal_digits)); d_result_table->setText(i, 1, locale.toString(y[i], 'e', app->d_decimal_digits)); } DataCurve *c = 0; if (d_graphics_display){ c = new DataCurve(d_result_table, tableName + "_1", tableName + "_2"); c->setData(x, y, d_points); c->setPen(QPen(ColorBox::color(d_curveColorIndex), 1)); if (!d_output_graph) d_output_graph = createOutputGraph()->activeGraph(); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->updatePlot(); } return (QwtPlotCurve*)c; }
void Differentiation::output() { double *result = new double[d_n-1]; for (int i = 1; i < d_n-1; i++) result[i]=0.5*((d_y[i+1]-d_y[i])/(d_x[i+1]-d_x[i]) + (d_y[i]-d_y[i-1])/(d_x[i]-d_x[i-1])); ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); QString tableName = app->generateUniqueName(QString(objectName())); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; int prec = app->d_decimal_digits; d_result_table = app->newHiddenTable(tableName, tr("Derivative") + " " + tr("of","Derivative of") + " " + dataSet, d_n-2, 2); for (int i = 1; i < d_n-1; i++) { int aux = i - 1; d_result_table->setText(aux, 0, locale.toString(d_x[i], 'g', prec)); d_result_table->setText(aux, 1, locale.toString(result[i], 'g', prec)); } delete[] result; if (d_graphics_display){ if (!d_output_graph) createOutputGraph(); d_output_graph->insertCurve(d_result_table, tableName + "_2", 0); d_output_graph->updatePlot(); } }
void Differentiation::output() { double *result = new double[d_n - 1]; for (int i = 1; i < d_n - 1; i++){ double xl = d_x[i - 1]; double xc = d_x[i]; double xr = d_x[i + 1]; if (xr != xc && xl != xc) result[i] = 0.5*((d_y[i + 1] - d_y[i])/(xr - xc) + (d_y[i] - d_y[i - 1])/(xc - xl)); else if (xr != xc) result[i] = (d_y[i + 1] - d_y[i])/(xr - xc); else if (xl != xc) result[i] = (d_y[i] - d_y[i - 1])/(xc - xl); else result[i] = result[i - 1]; } ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); QString tableName = app->generateUniqueName(QString(objectName())); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; int prec = app->d_decimal_digits; int rows = d_n - 2; int col = 1; if (!d_result_table || d_result_table->numRows() < rows){ d_result_table = app->newHiddenTable(tableName, tr("Derivative") + " " + tr("of", "Derivative of") + " " + dataSet, rows, 2); for (int i = 1; i < d_n-1; i++) { int aux = i - 1; d_result_table->setText(aux, 0, locale.toString(d_x[i], 'g', prec)); d_result_table->setText(aux, 1, locale.toString(result[i], 'g', prec)); } } else { col = d_result_table->numCols(); d_result_table->addCol(); for (int i = 1; i < d_n-1; i++) d_result_table->setText(i - 1, col, locale.toString(result[i], 'g', prec)); } delete[] result; if (d_graphics_display){ if (!d_output_graph){ createOutputGraph(); d_output_graph->removeLegend(); } d_output_graph->insertCurve(d_result_table, d_result_table->colLabel(col), 0); if (d_update_output_graph) d_output_graph->updatePlot(); } }
void Filter::enableGraphicsDisplay(bool on, Graph *g) { d_graphics_display = on; if (on){ if (g) d_output_graph = g; else d_output_graph = createOutputGraph()->activeGraph(); } }
void Correlation::addResultCurve() { ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent()); if (!app) return; QLocale locale = app->locale(); if (d_n > d_table->numRows()) d_table->setNumRows(d_n); int cols = d_table->numCols(); int cols2 = cols+1; d_table->addCol(); d_table->addCol(); int n = d_n/2; QVarLengthArray<double> x_temp(d_n), y_temp(d_n);//double x_temp[d_n], y_temp[d_n]; for (int i = 0; i<d_n; i++){ double x = i - n; x_temp[i] = x; double y; if(i < n) y = d_x[n + i]; else y = d_x[i - n]; y_temp[i] = y; d_table->setText(i, cols, QString::number(x)); d_table->setText(i, cols2, locale.toString(y, 'g', app->d_decimal_digits)); } QStringList l = d_table->colNames().grep(tr("Lag")); QString id = QString::number((int)l.size()+1); QString label = objectName() + id; d_table->setColName(cols, tr("Lag") + id); d_table->setColName(cols2, label); d_table->setColPlotDesignation(cols, Table::X); d_table->setHeaderColType(); if (d_graphics_display){ if (!d_output_graph) d_output_graph = createOutputGraph()->activeGraph(); DataCurve *c = new DataCurve(d_table, d_table->colName(cols), d_table->colName(cols2)); c->setData(x_temp.data(), y_temp.data(), d_n);//c->setData(x_temp, y_temp, d_n); c->setPen(QPen(ColorBox::color(d_curveColorIndex), 1)); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->updatePlot(); } }
void Convolution::addResultCurve() { ApplicationWindow *app = (ApplicationWindow *)parent(); if (!app) return; int cols = d_table->numCols(); int cols2 = cols+1; d_table->addCol(); d_table->addCol(); #ifdef Q_CC_MSVC QVarLengthArray<double> x_temp(d_n); #else double x_temp[d_n]; #endif QLocale locale = app->locale(); for (int i = 0; i<d_n; i++){ double x = i+1; x_temp[i] = x; d_table->setText(i, cols, QString::number(x)); d_table->setText(i, cols2, locale.toString(d_x[i], 'g', app->d_decimal_digits)); } QStringList l = d_table->colNames().grep(tr("Index")); QString id = QString::number((int)l.size()+1); QString label = objectName() + id; d_table->setColName(cols, tr("Index") + id); d_table->setColName(cols2, label); d_table->setColPlotDesignation(cols, Table::X); d_table->setHeaderColType(); if (d_graphics_display){ if (!d_output_graph) createOutputGraph(); DataCurve *c = new DataCurve(d_table, d_table->colName(cols), d_table->colName(cols2)); #ifdef Q_CC_MSVC c->setData(x_temp.data(), d_x, d_n); #else c->setData(x_temp, d_x, d_n); #endif c->setPen(QPen(ColorBox::color(d_curveColorIndex), 1)); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->updatePlot(); } }
void Differentiation::output() { std::vector<double> result(d_n - 1); for (int i = 1; i < d_n - 1; i++) result[i] = 0.5 * ((d_y[i + 1] - d_y[i]) / (d_x[i + 1] - d_x[i]) + (d_y[i] - d_y[i - 1]) / (d_x[i] - d_x[i - 1])); ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent()); if (!app) { throw std::logic_error( "Parent of Differentiation is not ApplicationWindow as expected."); } QLocale locale = app->locale(); QString tableName = app->generateUniqueName(QString(objectName())); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; d_result_table = app->newHiddenTable( tableName, tr("Derivative") + " " + tr("of", "Derivative of") + " " + dataSet, d_n - 2, 2); for (int i = 1; i < d_n - 1; i++) { d_result_table->setText( i - 1, 0, locale.toString(d_x[i], 'g', app->d_decimal_digits)); d_result_table->setText( i - 1, 1, locale.toString(result[i], 'g', app->d_decimal_digits)); } if (d_graphics_display) { if (!d_output_graph) d_output_graph = createOutputGraph()->activeGraph(); d_output_graph->insertCurve(d_result_table, tableName + "_2", 0); QString legend = "\\l(1)" + tr("Derivative") + " " + tr("of", "Derivative of") + " " + dataSet; LegendWidget *l = d_output_graph->legend(); if (l) { l->setText(legend); l->repaint(); } else d_output_graph->newLegend(legend); } }
void Differentiation::output() { double *result = new double[d_n-1]; for (int i = 1; i < d_n-1; i++) result[i]=0.5*((d_y[i+1]-d_y[i])/(d_x[i+1]-d_x[i]) + (d_y[i]-d_y[i-1])/(d_x[i]-d_x[i-1])); ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); QString tableName = app->generateUniqueName(QString(objectName())); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; d_result_table = app->newHiddenTable(tableName, tr("Derivative") + " " + tr("of","Derivative of") + " " + dataSet, d_n-2, 2); for (int i = 1; i < d_n-1; i++) { d_result_table->setText(i-1, 0, locale.toString(d_x[i], 'g', app->d_decimal_digits)); d_result_table->setText(i-1, 1, locale.toString(result[i], 'g', app->d_decimal_digits)); } delete[] result; if (d_graphics_display){ if (!d_output_graph) d_output_graph = createOutputGraph()->activeLayer(); d_output_graph->insertCurve(d_result_table, tableName + "_2", 0); QString legend = "\\l(1)" + tr("Derivative") + " " + tr("of","Derivative of") + " " + dataSet; LegendWidget *l = d_output_graph->legend(); if (l){ l->setText(legend); l->repaint(); } else d_output_graph->newLegend(legend); } }
void MultiPeakFit::generateFitCurve() { ApplicationWindow *app = (ApplicationWindow *)parent(); if (!d_gen_function) d_points = d_n; gsl_matrix * m = gsl_matrix_alloc (d_points, d_peaks); if (!m){ QMessageBox::warning(app, tr("MantidPlot - Fit Error"), tr("Could not allocate enough memory for the fit curves!")); return; } QVarLengthArray<double> X(d_points), Y(d_points);//double X[d_points], Y[d_points]; int i, j; int peaks_aux = d_peaks; if (d_peaks == 1) peaks_aux--; if (d_gen_function){ double step = (d_x[d_n-1] - d_x[0])/(d_points-1); for (i = 0; i<d_points; i++){ double x = d_x[0] + i*step; X[i] = x; double yi = 0; for (j=0; j<d_peaks; j++){ double y = evalPeak(d_results, x, j); gsl_matrix_set(m, i, j, y + d_results[d_p - 1]); yi += y; } Y[i] = yi + d_results[d_p - 1];//add offset } customizeFitResults(); if (d_graphics_display){ if (!d_output_graph) d_output_graph = createOutputGraph()->activeGraph(); if (d_peaks > 1) insertFitFunctionCurve(QString(objectName()) + tr("Fit"), X.data(), Y.data(), 2);//insertFitFunctionCurve(QString(objectName()) + tr("Fit"), X, Y, 2); else insertFitFunctionCurve(QString(objectName()) + tr("Fit"), X.data(), Y.data());//insertFitFunctionCurve(QString(objectName()) + tr("Fit"), X, Y); if (generate_peak_curves){ for (i=0; i<peaks_aux; i++){//add the peak curves for (j=0; j<d_points; j++) Y[j] = gsl_matrix_get (m, j, i); insertPeakFunctionCurve(X.data(), Y.data(), i);//insertPeakFunctionCurve(X, Y, i); } } d_output_graph->replot(); } } else { QString tableName = app->generateUniqueName(tr("Fit")); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; QString label = d_explanation + " " + tr("fit of") + " " + dataSet; d_result_table = app->newHiddenTable(tableName, label, d_points, peaks_aux + 2); QStringList header = QStringList() << "1"; for (i = 0; i<peaks_aux; i++) header << tr("peak") + QString::number(i+1); header << "2"; d_result_table->setHeader(header); QLocale locale = app->locale(); for (i = 0; i<d_points; i++){ X[i] = d_x[i]; d_result_table->setText(i, 0, locale.toString(X[i], 'e', d_prec)); double yi=0; for (j=0; j<d_peaks; j++){ double diff = X[i] - d_results[3*j + 1]; double w = d_results[3*j + 2]; double y_aux = 0; if (d_profile == Gauss) y_aux += sqrt(M_2_PI)*d_results[3*j]/w*exp(-2*diff*diff/(w*w)); else y_aux += M_2_PI*d_results[3*j]*w/(4*diff*diff+w*w); yi += y_aux; y_aux += d_results[d_p - 1]; d_result_table->setText(i, j+1, locale.toString(y_aux, 'e', d_prec)); gsl_matrix_set(m, i, j, y_aux); } Y[i] = yi + d_results[d_p - 1];//add offset if (d_peaks > 1) d_result_table->setText(i, d_peaks+1, locale.toString(Y[i], 'e', d_prec)); } customizeFitResults(); if (d_graphics_display){ if (!d_output_graph) d_output_graph = createOutputGraph()->activeGraph(); label = tableName + "_2"; DataCurve *c = new DataCurve(d_result_table, tableName + "_1", label); if (d_peaks > 1) c->setPen(QPen(ColorBox::color(d_curveColorIndex), 2)); else c->setPen(QPen(ColorBox::color(d_curveColorIndex), 1)); c->setData(X.data(), Y.data(), d_points);//c->setData(X, Y, d_points); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->addFitCurve(c); if (generate_peak_curves){ for (i=0; i<peaks_aux; i++){//add the peak curves for (j=0; j<d_points; j++) Y[j] = gsl_matrix_get (m, j, i); label = tableName + "_" + tr("peak") + QString::number(i+1); c = new DataCurve(d_result_table, tableName + "_1", label); c->setPen(QPen(ColorBox::color(d_peaks_color), 1)); c->setData(X.data(), Y.data(), d_points);//c->setData(X, Y, d_points); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->addFitCurve(c); } } d_output_graph->replot(); } } gsl_matrix_free(m); }
void FFT::outputGraphs() { createOutputGraph(); MultiLayer *ml = d_output_graph->multiLayer(); d_output_graph->setTitle(QString::null); d_output_graph->setYAxisTitle(tr("Angle (deg)")); d_output_graph->enableAxis(QwtPlot::xTop, true); d_output_graph->enableAxis(QwtPlot::yRight, true); if (!d_inverse) d_output_graph->setAxisTitle(QwtPlot::xTop, tr("Frequency") + " (" + tr("Hz") + ")"); else d_output_graph->setAxisTitle(QwtPlot::xTop, tr("Time") + + " (" + tr("s") + ")"); ScaleDraw *sd = (ScaleDraw *)d_output_graph->axisScaleDraw(QwtPlot::yLeft); if (sd) sd->setShowTicksPolicy(ScaleDraw::HideBegin); sd = (ScaleDraw *)d_output_graph->axisScaleDraw(QwtPlot::yRight); if (sd) sd->setShowTicksPolicy(ScaleDraw::HideBegin); sd = (ScaleDraw *)d_output_graph->axisScaleDraw(QwtPlot::xBottom); if (sd){ sd->setShowTicksPolicy(ScaleDraw::HideBeginEnd); sd->enableComponent(QwtAbstractScaleDraw::Backbone, false); } QString tableName = d_result_table->objectName(); PlotCurve *pc = d_output_graph->insertCurve(d_result_table, 0, tableName + "_" + tr("Angle"), 0); pc->setPen(QPen(d_curveColor, 1)); d_output_graph->removeLegend(); d_output_graph->updatePlot(); Graph *g = ml->addLayer(0, 0, 0, 0, true); g->setTitle(QString::null); if (!d_inverse) g->setXAxisTitle(tr("Frequency") + " (" + tr("Hz") + ")"); else g->setXAxisTitle(tr("Time") + + " (" + tr("s") + ")"); g->setYAxisTitle(tr("Amplitude")); g->removeLegend(); sd = (ScaleDraw *)g->axisScaleDraw(QwtPlot::xTop); if (sd) sd->setShowTicksPolicy(ScaleDraw::HideBeginEnd); PlotCurve *c = g->insertCurve(d_result_table, 0, tableName + "_" + tr("Amplitude"), 0); c->setPen(QPen(d_curveColor, 1)); g->updatePlot(); double rb = g->axisScaleDiv(QwtPlot::xBottom)->upperBound(); d_output_graph->setAxisScale(QwtPlot::xBottom, 0, rb); d_output_graph->setAxisScale(QwtPlot::xTop, 0, rb); g->setAxisScale(QwtPlot::xBottom, 0, rb); g->setAxisScale(QwtPlot::xTop, 0, rb); ml->setAlignPolicy(MultiLayer::AlignCanvases); ml->setRows(2); ml->setCols(1); ml->setSpacing(0, 0); ml->linkXLayerAxes(); ml->setCommonLayerAxes(false, true); ml->arrangeLayers(false, false); }
void MultiPeakFit::generateFitCurve() { ApplicationWindow *app = (ApplicationWindow *)parent(); if (!d_gen_function) d_points = d_n; int i, j; int peaks_aux = d_peaks; if (d_peaks == 1) peaks_aux--; if (d_gen_function){ customizeFitResults(); if (d_graphics_display){ if (!d_output_graph) createOutputGraph(); if (d_peaks > 1) insertFitFunctionCurve(QString(objectName()) + tr("Fit"), 2); else insertFitFunctionCurve(QString(objectName()) + tr("Fit")); if (generate_peak_curves){ for (i = 0; i < peaks_aux; i++)//add the peak curves insertPeakFunctionCurve(i); } d_output_graph->replot(); } } else { gsl_matrix * m = gsl_matrix_alloc (d_points, d_peaks); if (!m){ QMessageBox::warning(app, tr("QtiPlot - Fit Error"), tr("Could not allocate enough memory for the fit curves!")); return; } double *X = (double *)malloc(d_points*sizeof(double)); if (!X){ memoryErrorMessage(); return; } double *Y = (double *)malloc(d_points*sizeof(double)); if (!Y){ memoryErrorMessage(); free(X); return; } QString tableName = app->generateUniqueName(tr("Fit")); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; QString label = d_explanation + " " + tr("fit of") + " " + dataSet; d_result_table = app->newHiddenTable(tableName, label, d_points, peaks_aux + 2); QStringList header = QStringList() << "1"; for (i = 0; i<peaks_aux; i++) header << tr("peak") + QString::number(i+1); header << "2"; d_result_table->setHeader(header); QLocale locale = app->locale(); for (i = 0; i<d_points; i++){ X[i] = d_x[i]; d_result_table->setText(i, 0, locale.toString(X[i], 'e', d_prec)); double yi=0; for (j=0; j<d_peaks; j++){ double diff = X[i] - d_results[3*j + 1]; double w = d_results[3*j + 2]; double y_aux = 0; if (d_profile == Gauss) y_aux += sqrt(M_2_PI)*d_results[3*j]/w*exp(-2*diff*diff/(w*w)); else y_aux += M_2_PI*d_results[3*j]*w/(4*diff*diff+w*w); yi += y_aux; y_aux += d_results[d_p - 1]; d_result_table->setText(i, j+1, locale.toString(y_aux, 'e', d_prec)); gsl_matrix_set(m, i, j, y_aux); } Y[i] = yi + d_results[d_p - 1];//add offset if (d_peaks > 1) d_result_table->setText(i, d_peaks+1, locale.toString(Y[i], 'e', d_prec)); } customizeFitResults(); if (d_graphics_display){ if (!d_output_graph) createOutputGraph(); label = tableName + "_2"; DataCurve *c = new DataCurve(d_result_table, tableName + "_1", label); if (d_curve){ c->setCurveType(d_curve->curveType()); c->setAxis(d_curve->xAxis(), d_curve->yAxis()); } if (d_peaks > 1) c->setPen(QPen(d_curveColor, 2)); else c->setPen(QPen(d_curveColor, 1)); if (c->curveType() == QwtPlotCurve::Xfy) c->setData(Y, X, d_points); else c->setData(X, Y, d_points); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->addFitCurve(c); if (generate_peak_curves){ for (i=0; i<peaks_aux; i++){//add the peak curves for (j=0; j<d_points; j++) Y[j] = gsl_matrix_get (m, j, i); label = tableName + "_" + tr("peak") + QString::number(i+1); c = new DataCurve(d_result_table, tableName + "_1", label); c->setPen(QPen(d_peaks_color, 1)); if (d_curve){ c->setCurveType(d_curve->curveType()); c->setAxis(d_curve->xAxis(), d_curve->yAxis()); } if (c->curveType() == QwtPlotCurve::Xfy) c->setData(Y, X, d_points); else c->setData(X, Y, d_points); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->addFitCurve(c); } } d_output_graph->replot(); } gsl_matrix_free(m); free(X); free(Y); } }