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 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); } }