void Fit::setDataCurve(QwtPlotCurve *curve, double start, double end) { Filter::setDataCurve(curve, start, end); if (!d_w){ d_w = (double *)malloc(d_n*sizeof(double)); if (!d_w){ memoryErrorMessage(); return; } } if (d_graph && d_curve && ((PlotCurve *)d_curve)->type() != Graph::Function) { QList<ErrorBarsCurve *> lst = ((DataCurve *)d_curve)->errorBarsList(); foreach (ErrorBarsCurve *er, lst){ if (!er->xErrors()){ d_weighting = Instrumental; for (int i=0; i<d_n; i++){ double e = er->errorValue(i); d_w[i] = 1.0/(e*e); } weighting_dataset = er->title().text(); return; } } }
void FFT::fftTable() { double *amp = (double *)malloc(d_n*sizeof(double)); gsl_fft_complex_wavetable *wavetable = gsl_fft_complex_wavetable_alloc (d_n); gsl_fft_complex_workspace *workspace = gsl_fft_complex_workspace_alloc (d_n); if(!amp || !wavetable || !workspace){ memoryErrorMessage(); return; } double df = 1.0/(double)(d_n*d_sampling);//frequency sampling double aMax = 0.0;//max amplitude if(d_inverse) gsl_fft_complex_inverse (d_y, 1, d_n, wavetable, workspace); else gsl_fft_complex_forward (d_y, 1, d_n, wavetable, workspace); gsl_fft_complex_wavetable_free (wavetable); gsl_fft_complex_workspace_free (workspace); if (d_shift_order) { int n2 = d_n/2; for(int i = 0; i < d_n; i++) { d_x[i] = (i - n2)*df; int j = i + d_n; double aux = d_y[i]; d_y[i] = d_y[j]; d_y[j] = aux; } } else { for(int i = 0; i < d_n; i++) d_x[i] = i*df; } for(int i = 0; i < d_n; i++) { int i2 = 2*i; double a = sqrt(d_y[i2]*d_y[i2] + d_y[i2+1]*d_y[i2+1]); amp[i]= a; if (a > aMax) aMax = a; } ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); int prec = app->d_decimal_digits; for (int i = 0; i < d_n; i++) { int i2 = 2*i; d_result_table->setText(i, 0, locale.toString(d_x[i], 'g', prec)); d_result_table->setText(i, 1, locale.toString(d_y[i2], 'g', prec)); d_result_table->setText(i, 2, locale.toString(d_y[i2 + 1], 'g', prec)); if (d_normalize) d_result_table->setText(i, 3, locale.toString(amp[i]/aMax, 'g', prec)); else d_result_table->setText(i, 3, locale.toString(amp[i], 'g', prec)); d_result_table->setText(i, 4, locale.toString(atan(d_y[i2 + 1]/d_y[i2]), 'g', prec)); } free(amp); }
bool Correlation::setDataFromTable(Table *t, const QString& colName1, const QString& colName2, int startRow, int endRow) { if (!t) return false; d_table = t; int col1 = d_table->colIndex(colName1); int col2 = d_table->colIndex(colName2); if (col1 < 0){ QMessageBox::warning((ApplicationWindow *)parent(), tr("QtiPlot") + " - " + tr("Error"), tr("The data set %1 does not exist!").arg(colName1)); d_init_err = true; return false; } else if (col2 < 0){ QMessageBox::warning((ApplicationWindow *)parent(), tr("QtiPlot") + " - " + tr("Error"), tr("The data set %1 does not exist!").arg(colName2)); d_init_err = true; return false; } if (d_n > 0)//delete previousely allocated memory freeMemory(); startRow--; endRow--; if (startRow < 0 || startRow >= t->numRows()) startRow = 0; if (endRow < 0 || endRow >= t->numRows()) endRow = t->numRows() - 1; int from = QMIN(startRow, endRow); int to = QMAX(startRow, endRow); int rows = abs(to - from) + 1; d_n = 16; // tmp number of points while (d_n < rows) d_n *= 2; d_x = new double[d_n]; d_y = new double[d_n]; if(d_y && d_x){ memset( d_x, 0, d_n * sizeof( double ) ); // zero-pad the two arrays... memset( d_y, 0, d_n * sizeof( double ) ); for(int i = 0; i< d_n; i++){ int j = i + from; d_x[i] = d_table->cell(j, col1); d_y[i] = d_table->cell(j, col2); } } else { memoryErrorMessage(); d_n = 0; return false; } return true; }
bool Fit::setDataFromTable(Table *t, const QString& xColName, const QString& yColName, int from, int to, bool sort) { if (Filter::setDataFromTable(t, xColName, yColName, from, to, sort)){ if (d_w) free(d_w); d_w = (double *)malloc(d_n*sizeof(double)); if (!d_w){ memoryErrorMessage(); return false; } for (int i = 0; i < d_n; i++)//initialize the weighting data to 1.0 d_w[i] = 1.0; return true; } else return false; }
void FFT::fftCurve() { int n2 = d_n/2; double *amp = (double *)malloc(d_n*sizeof(double)); double *result = (double *)malloc(2*d_n*sizeof(double)); if(!amp || !result){ memoryErrorMessage(); return; } double df = 1.0/(double)(d_n*d_sampling);//frequency sampling double aMax = 0.0;//max amplitude if(!d_inverse){ gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(d_n); gsl_fft_real_wavetable *real = gsl_fft_real_wavetable_alloc(d_n); if(!work || !real){ memoryErrorMessage(); return; } gsl_fft_real_transform(d_y, 1, d_n, real, work); gsl_fft_halfcomplex_unpack (d_y, result, 1, d_n); gsl_fft_real_wavetable_free(real); gsl_fft_real_workspace_free(work); } else { gsl_fft_real_unpack (d_y, result, 1, d_n); gsl_fft_complex_wavetable *wavetable = gsl_fft_complex_wavetable_alloc (d_n); gsl_fft_complex_workspace *workspace = gsl_fft_complex_workspace_alloc (d_n); if(!workspace || !wavetable){ memoryErrorMessage(); return; } gsl_fft_complex_inverse (result, 1, d_n, wavetable, workspace); gsl_fft_complex_wavetable_free (wavetable); gsl_fft_complex_workspace_free (workspace); } if (d_shift_order){ for(int i = 0; i < d_n; i++){ d_x[i] = (i - n2)*df; int j = i + d_n; double aux = result[i]; result[i] = result[j]; result[j] = aux; } } else { for(int i = 0; i < d_n; i++) d_x[i] = i*df; } for(int i = 0; i < d_n; i++) { int i2 = 2*i; double real_part = result[i2]; double im_part = result[i2+1]; double a = sqrt(real_part*real_part + im_part*im_part); amp[i]= a; if (a > aMax) aMax = a; } ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); int prec = app->d_decimal_digits; for (int i = 0; i < d_n; i++){ int i2 = 2*i; d_result_table->setText(i, 0, locale.toString(d_x[i], 'g', prec)); d_result_table->setText(i, 1, locale.toString(result[i2], 'g', prec)); d_result_table->setText(i, 2, locale.toString(result[i2 + 1], 'g', prec)); if (d_normalize) d_result_table->setText(i, 3, locale.toString(amp[i]/aMax, 'g', prec)); else d_result_table->setText(i, 3, locale.toString(amp[i], 'g', prec)); d_result_table->setText(i, 4, locale.toString(atan(result[i2 + 1]/result[i2]), 'g', prec)); } free(amp); free(result); }
bool FFT::setDataFromTable(Table *t, const QString& realColName, const QString& imagColName, int from, int to) { d_init_err = true; if (!t) return false; d_real_col = d_table->colIndex(realColName); if (d_real_col < 0 || t->columnType(d_real_col) != Table::Numeric) return false; if (!imagColName.isEmpty()){ d_imag_col = d_table->colIndex(imagColName); if (d_imag_col < 0 || t->columnType(d_imag_col) != Table::Numeric) return false; } from--; to--; if (from < 0 || from >= t->numRows()) from = 0; if (to < 0 || to >= t->numRows()) to = t->numRows() - 1; if (t && d_table != t) d_table = t; if (d_n > 0)//delete previousely allocated memory freeMemory(); d_graph = 0; d_curve = 0; d_init_err = false; d_n = abs(to - from) + 1; int n2 = 2*d_n; d_y = (double *)malloc(n2*sizeof(double)); if (!d_y){ memoryErrorMessage(); return false; }; d_x = (double *)malloc(d_n*sizeof(double)); if (!d_x){ memoryErrorMessage(); free(d_y); return false; }; if(d_y && d_x) {// zero-pad data array memset( d_y, 0, n2* sizeof( double ) ); for(int i=0; i<d_n; i++) { int i2 = 2*i; d_y[i2] = d_table->cell(i, d_real_col); if (d_imag_col >= 0) d_y[i2+1] = d_table->cell(i, d_imag_col); } } else { memoryErrorMessage(); return false; } return true; }
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); } }