void Correlation::output() { // calculate the FFTs of the two functions if( gsl_fft_real_radix2_transform( d_x, 1, d_n ) == 0 && gsl_fft_real_radix2_transform( d_y, 1, d_n ) == 0) { for(int i=0; i<d_n/2; i++ ){// multiply the FFT by its complex conjugate if( i==0 || i==(d_n/2)-1 ) d_x[i] *= d_x[i]; else{ int ni = d_n-i; double dReal = d_x[i] * d_y[i] + d_x[ni] * d_y[ni]; double dImag = d_x[i] * d_y[ni] - d_x[ni] * d_y[i]; d_x[i] = dReal; d_x[ni] = dImag; } } } else { QMessageBox::warning((ApplicationWindow *)parent(), tr("QtiPlot") + " - " + tr("Error"), tr("Error in GSL forward FFT operation!")); return; } gsl_fft_halfcomplex_radix2_inverse(d_x, 1, d_n ); //inverse FFT addResultCurve(); d_result_table = d_table; }
void Convolution::convlv(double *sig, int n, double *dres, int m, int sign) { double *res = new double[n]; memset(res,0,n*sizeof(double)); int i, m2 = m/2; for (i=0;i<m2;i++) {//store the response in wrap around order, see Numerical Recipes doc res[i] = dres[m2+i]; res[n-m2+i] = dres[i]; } if(m2%2==1) res[m2]=dres[m-1]; // calculate ffts gsl_fft_real_radix2_transform(res,1,n); gsl_fft_real_radix2_transform(sig,1,n); double re, im, size; for (i=0;i<n/2;i++) {// multiply/divide both ffts if(i==0 || i==n/2-1) { if(sign == 1) sig[i] = res[i]*sig[i]; else sig[i] = sig[i]/res[i]; } else { int ni = n-i; if(sign == 1) { re = res[i]*sig[i]-res[ni]*sig[ni]; im = res[i]*sig[ni]+res[ni]*sig[i]; } else { size = res[i]*res[i]+res[ni]*res[ni]; re = res[i]*sig[i]+res[ni]*sig[ni]; im = res[i]*sig[ni]-res[ni]*sig[i]; re /= size; im /= size; } sig[i] = re; sig[ni] = im; } } delete[] res; gsl_fft_halfcomplex_radix2_inverse(sig,1,n);// inverse fft }
/* * Perform a type-II DCT using GSL's FFT function. * Assumes len is a power of 2 */ void fDCT2_fft(const unsigned int input[], double output[], size_t len) { double *fft_data; int i; /* * David, please check this -- do you mean to call fDCT2 and then return? * ISO C forbids a return with expression in a void function. */ if (len <= 4) { fDCT2(input, output, len); return; } /* Allocate the new vector and zero all of the elements. * The even elements will remain zero. */ fft_data = (double *) malloc(sizeof(double) * 4 * len); memset(fft_data, 0, sizeof(double) * 4 * len); for (i = 0; i < len; i++) fft_data[2*i + 1] = input[i]; for (i = 1; i < 2*len; i++) fft_data[4*len - i] = fft_data[i]; gsl_fft_real_radix2_transform(fft_data, 1, 4*len); for (i = 0; i < len; i++) output[i] = fft_data[i] / 2; free(fft_data); }
/* calculates the fast Fourier transform of signal data, stores it into fft_results */ int fft (size_t N, double *data, double *fft_results) { size_t i; double *fft_data = (double *) calloc (N, sizeof (double)); int retcode; /* initialize the data for fft */ for (i=0; i<N; i++) fft_data [i] = data [i]; /* use the corresponding routine if N is power of 2 */ if (is_power_of_n (N,2)) { /* perform the fft */ retcode = gsl_fft_real_radix2_transform (fft_data, 1, N); gsl_fft_halfcomplex_radix2_unpack (fft_data, fft_results, 1, N); } else { /* alloc memory for real and half-complex wavetables, and workspace */ gsl_fft_real_wavetable *real_wavetable = gsl_fft_real_wavetable_alloc (N); gsl_fft_real_workspace *ws = gsl_fft_real_workspace_alloc (N); /* perform the fft */ retcode = gsl_fft_real_transform (fft_data, 1, N, real_wavetable, ws); gsl_fft_halfcomplex_unpack (fft_data, fft_results, 1, N); /* free memory */ gsl_fft_real_wavetable_free (real_wavetable); gsl_fft_real_workspace_free (ws); } free (fft_data); return retcode; }
void TimeSeriesMotion::fft( const QVector<double>& in, QVector<std::complex<double> >& out ) const { /* // The number of elements in the double array is 2 * n, but only the first // n are filled with data. For the complex array, n/2 + 1 elements are // required. // Copy the input QVector into a double array double* inArray = (double*) fftw_malloc( sizeof(double) * 2 * in.size() ); for (int i = 0; i < in.size(); i++) { inArray[i] = in.at(i); } // Allocate the space for the output int n = in.size() / 2 + 1; fftw_complex* outArray = (fftw_complex*)fftw_malloc( sizeof(fftw_complex) * n); // Create the plan and execute the FFT fftw_plan p = fftw_plan_dft_r2c_1d( in.size(), inArray, outArray, FFTW_ESTIMATE); fftw_execute(p); // Copy the data to the output QVector out.resize(n); for (int i = 0; i < n; i++) { out[i] = std::complex<double>(outArray[i][0], outArray[i][1]); } // Free the memory fftw_destroy_plan(p); fftw_free(inArray); fftw_free(outArray); */ // Load the buffer with the initial values const int n = in.size(); double *d = new double[n]; // Load the data memcpy(d, in.data(), n * sizeof(double)); #ifdef USE_FFTW fftw_plan p = fftw_plan_r2r_1d(n, d, d, FFTW_R2HC, FFTW_ESTIMATE); fftw_execute(p); #else // Execute FFT gsl_fft_real_radix2_transform(d, 1, n); #endif // Load the data into out out.resize(1 + n / 2); out[0] = std::complex<double>(d[0], 0); for (int i = 1; i < (out.size() - 1); ++i) { out[i] = std::complex<double>(d[i], d[n - i]); } out[out.size() - 1] = std::complex<double>(d[n - 1], 0); // Delete the buffer delete [] d; }
//using namespace std; double bandpass_filter(double data, double lp_cutoff, double hp_cutoff,double sample_rate, int num_samples) { int fft_size = FFT_SIZE; double filtered_samples [num_samples]; double zero_array [432] = {0}; //std::fill( array, array+432, 0); double *buffer_data = new double[fft_size]; std::copy(data,data+num_samples,buffer_data); std::copy(zero_array,zero_array+432,buffer_data+num_samples); gsl_fft_real_radix2_transform (buffer_data,1,fft_size); int lc_index = (int)((lp_cutoff*num_samples/sample_rate)+0.5); int hc_index = (int)((hp_cutoff*num_samples/sample_rate)+0.5); for (int i = 0; i < fft_size; ++i) { if ((i >= lc_index) && (i <= hc_index)) { filtered_samples[i] = buffer_data[i]; } else if ((i <= (fft_size-lc_index)) && (i >= (fft_size-hc_index))) { filtered_samples[i] = buffer_data[i]; } } gsl_fft_real_radix2_inverse (filtered_samples,1,fft_size); return filtered_samples; }
int compute_itegral_r(const mu_data_fit *mu, const fit_params fp, gsl_vector *fftR_abs){ size_t vsize= mu->k->size; gsl_vector *mu_tmp=gsl_vector_alloc(vsize); gsl_vector_set_zero(mu_tmp); size_t ikmin=search_min(mu->k, mu->kmin - 0.5*mu->dwk); size_t ikmax=search_min(mu->k, mu->kmax + 0.5*mu->dwk); gsl_vector_view kw = gsl_vector_subvector(mu->k, ikmin-1, ikmax-ikmin-1); gsl_vector_view muw = gsl_vector_subvector(mu_tmp, ikmin-1, ikmax-ikmin-1); gsl_vector *ktmp=gsl_vector_alloc((&kw.vector)->size); gsl_vector_memcpy(ktmp, &kw.vector); gsl_vector_add_constant(ktmp, fp.kshift); compute_itegral(ktmp, &fp, &muw.vector); hanning(mu_tmp, mu->k, mu->kmin, mu->kmax, mu->dwk); //FFT transform double *data = (double *) malloc(vsize*sizeof(double)); memcpy(data, mu_tmp->data, vsize*sizeof(double)); gsl_fft_real_radix2_transform(data, 1, vsize); //Unpack complex vector gsl_vector_complex *fourier_data = gsl_vector_complex_alloc (vsize); gsl_fft_halfcomplex_radix2_unpack(data, fourier_data->data, 1, vsize); gsl_vector *fftR_real = gsl_vector_alloc(vsize/2); gsl_vector *fftR_imag = gsl_vector_alloc(vsize/2); //gsl_vector *fftR_abs = gsl_vector_alloc(vsize/2); complex_vector_parts(fourier_data, fftR_real, fftR_imag); complex_vector_abs(fftR_abs, fftR_real, fftR_imag); hanning(fftR_abs, mu->r, mu->rmin, mu->rmax, mu->dwr); gsl_vector_free(fftR_real); gsl_vector_free(fftR_imag); gsl_vector_complex_free(fourier_data); gsl_vector_free(mu_tmp); free(data); }
bool CrossCorrelate::algorithm() { KstVectorPtr array_one = inputVector(ARRAY_ONE); KstVectorPtr array_two = inputVector(ARRAY_TWO); KstVectorPtr step_value = outputVector(STEP_VALUE); KstVectorPtr correlated = outputVector(CORRELATED); if (array_one->length() <= 0 || array_two->length() <= 0 || array_one->length() != array_two->length()) { return false; } double* pdArrayOne; double* pdArrayTwo; double* pdResult[2]; double dReal; double dImag; int iLength; int iLengthNew; bool iReturn = false; // // zero-pad the array... // iLength = array_one->length(); iLength *= 2; step_value->resize(array_one->length(), false); correlated->resize(array_two->length(), false); // // round iLength up to the nearest power of two... // iLengthNew = 64; while( iLengthNew < iLength && iLengthNew > 0) { iLengthNew *= 2; } iLength = iLengthNew; if (iLength <= 0) return false; pdArrayOne = new double[iLength]; pdArrayTwo = new double[iLength]; if (pdArrayOne != NULL && pdArrayTwo != NULL) { // // zero-pad the two arrays... // memset( pdArrayOne, 0, iLength * sizeof( double ) ); memcpy( pdArrayOne, array_one->value(), array_one->length() * sizeof( double ) ); memset( pdArrayTwo, 0, iLength * sizeof( double ) ); memcpy( pdArrayTwo, array_two->value(), array_two->length() * sizeof( double ) ); // // calculate the FFTs of the two functions... // if (gsl_fft_real_radix2_transform( pdArrayOne, 1, iLength ) == 0) { if (gsl_fft_real_radix2_transform( pdArrayTwo, 1, iLength ) == 0) { // // multiply one FFT by the complex conjugate of the other... // for (int i=0; i<iLength/2; i++) { if (i==0 || i==(iLength/2)-1) { pdArrayOne[i] = pdArrayOne[i] * pdArrayTwo[i]; } else { dReal = pdArrayOne[i] * pdArrayTwo[i] + pdArrayOne[iLength-i] * pdArrayTwo[iLength-i]; dImag = pdArrayOne[i] * pdArrayTwo[iLength-i] - pdArrayOne[iLength-i] * pdArrayTwo[i]; pdArrayOne[i] = dReal; pdArrayOne[iLength-i] = dImag; } } // // do the inverse FFT... // if (gsl_fft_halfcomplex_radix2_inverse( pdArrayOne, 1, iLength ) == 0) { if (step_value->length() != array_one->length()) { pdResult[0] = (double*)realloc( step_value->value(), array_one->length() * sizeof( double ) ); } else { pdResult[0] = step_value->value(); } if (correlated->length() != array_two->length()) { pdResult[1] = (double*)realloc( correlated->value(), array_two->length() * sizeof( double ) ); } else { pdResult[1] = correlated->value(); } if (pdResult[0] != NULL && pdResult[1] != NULL) { for (int i = 0; i < array_one->length(); ++i) { step_value->value()[i] = pdResult[0][i]; } for (int i = 0; i < array_two->length(); ++i) { correlated->value()[i] = pdResult[1][i]; } for (int i = 0; i < array_one->length(); i++) { step_value->value()[i] = (double)( i - ( array_one->length() / 2 ) ); } memcpy( &(correlated->value()[array_one->length() / 2]), &(pdArrayOne[0]), ( ( array_one->length() + 1 ) / 2 ) * sizeof( double ) ); memcpy( &(correlated->value()[0]), &(pdArrayOne[iLength - (array_one->length() / 2)]), ( array_one->length() / 2 ) * sizeof( double ) ); iReturn = true; } } } } } delete[] pdArrayOne; delete[] pdArrayTwo; return iReturn; }
bool AutoCorrelate::algorithm() { KstVectorPtr array = inputVector(ARRAY); KstVectorPtr step_value = outputVector(STEP_VALUE); KstVectorPtr auto_correlated = outputVector(AUTO_CORRELATED); if (array->length() <= 0) { return false; } double* pdArrayOne; double* pdResult; double* pdCorrelate; double dReal; double dImag; double sigmaSquared = 0.0; int iLength; int iLengthNew; bool iReturn = false; // // zero-pad the array... // iLength = array->length(); iLength *= 2; step_value->resize(array->length(), false); auto_correlated->resize(array->length(), false); // // round iLength up to the nearest power of two... // iLengthNew = 64; while( iLengthNew < iLength && iLengthNew > 0) { iLengthNew *= 2; } iLength = iLengthNew; if (iLength <= 0) { return false; } pdArrayOne = new double[iLength]; if (pdArrayOne != NULL) { // // zero-pad the two arrays... // memset( pdArrayOne, 0, iLength * sizeof( double ) ); memcpy( pdArrayOne, array->value(), array->length() * sizeof( double ) ); // // calculate the FFTs of the two functions... // if (gsl_fft_real_radix2_transform( pdArrayOne, 1, iLength ) == 0) { // // multiply the FFT by its complex conjugate... // for (int i=0; i<iLength/2; i++) { if (i==0 || i==(iLength/2)-1) { pdArrayOne[i] *= pdArrayOne[i]; } else { dReal = pdArrayOne[i] * pdArrayOne[i] + pdArrayOne[iLength-i] * pdArrayOne[iLength-i]; dImag = pdArrayOne[i] * pdArrayOne[iLength-i] - pdArrayOne[iLength-i] * pdArrayOne[i]; pdArrayOne[i] = dReal; pdArrayOne[iLength-i] = dImag; } } // // do the inverse FFT... // if (gsl_fft_halfcomplex_radix2_inverse( pdArrayOne, 1, iLength ) == 0) { if (step_value->length() != array->length()) { pdResult = (double*)realloc( step_value->value(), array->length() * sizeof( double ) ); } else { pdResult = step_value->value(); } if (auto_correlated->length() != array->length()) { pdCorrelate = (double*)realloc( auto_correlated->value(), array->length() * sizeof( double ) ); } else { pdCorrelate = auto_correlated->value(); } if (pdResult != NULL && pdCorrelate != NULL) { sigmaSquared = pdArrayOne[0]; memcpy( &(auto_correlated->value()[array->length() / 2]), &(pdArrayOne[0]), ( ( array->length() + 1 ) / 2 ) * sizeof( double ) ); memcpy( &(auto_correlated->value()[0]), &(pdArrayOne[iLength - (array->length() / 2)]), ( array->length() / 2 ) * sizeof( double ) ); for (int i = 0; i < array->length(); i++) { auto_correlated->value()[i] /= sigmaSquared; step_value->value()[i] = (double)( i - ( array->length() / 2 ) ); } iReturn = true; } } } } delete[] pdArrayOne; return iReturn; }
int crosscorrelate(const double *const inArrays[], const int inArrayLens[], const double inScalars[], double *outArrays[], int outArrayLens[], double outScalars[]) { double* pdArrayOne; double* pdArrayTwo; double* pdResult[2]; double dReal; double dImag; int i = 0; int iLength; int iLengthNew; int iReturn = -1; if( inArrayLens[0] > 0 && inArrayLens[1] > 0 && inArrayLens[0] == inArrayLens[1] ) { // // zero-pad the array... // iLength = inArrayLens[0]; iLength *= 2; // // round iLength up to the nearest power of two... // iLengthNew = 64; while( iLengthNew < iLength && iLengthNew > 0 ) { iLengthNew *= 2; } iLength = iLengthNew; if( iLength > 0 ) { pdArrayOne = new double[iLength]; pdArrayTwo = new double[iLength]; if( pdArrayOne != NULL && pdArrayTwo != NULL ) { // // zero-pad the two arrays... // memset( pdArrayOne, 0, iLength * sizeof( double ) ); memcpy( pdArrayOne, inArrays[0], inArrayLens[0] * sizeof( double ) ); memset( pdArrayTwo, 0, iLength * sizeof( double ) ); memcpy( pdArrayTwo, inArrays[1], inArrayLens[1] * sizeof( double ) ); // // calculate the FFTs of the two functions... // if( gsl_fft_real_radix2_transform( pdArrayOne, 1, iLength ) == 0 ) { if( gsl_fft_real_radix2_transform( pdArrayTwo, 1, iLength ) == 0 ) { // // multiply one FFT by the complex conjugate of the other... // for( i=0; i<iLength/2; i++ ) { if( i==0 || i==(iLength/2)-1 ) { pdArrayOne[i] = pdArrayOne[i] * pdArrayTwo[i]; } else { dReal = pdArrayOne[i] * pdArrayTwo[i] + pdArrayOne[iLength-i] * pdArrayTwo[iLength-i]; dImag = pdArrayOne[i] * pdArrayTwo[iLength-i] - pdArrayOne[iLength-i] * pdArrayTwo[i]; pdArrayOne[i] = dReal; pdArrayOne[iLength-i] = dImag; } } // // do the inverse FFT... // if( gsl_fft_halfcomplex_radix2_inverse( pdArrayOne, 1, iLength ) == 0 ) { if( outArrayLens[0] != inArrayLens[0] ) { pdResult[0] = (double*)realloc( outArrays[0], inArrayLens[0] * sizeof( double ) ); } else { pdResult[0] = outArrays[0]; } if( outArrayLens[1] != inArrayLens[1] ) { pdResult[1] = (double*)realloc( outArrays[1], inArrayLens[1] * sizeof( double ) ); } else { pdResult[1] = outArrays[1]; } if( pdResult[0] != NULL && pdResult[1] != NULL ) { outArrays[0] = pdResult[0]; outArrayLens[0] = inArrayLens[0]; outArrays[1] = pdResult[1]; outArrayLens[1] = inArrayLens[1]; for( i=0; i<inArrayLens[0]; i++ ) { outArrays[0][i] = (double)( i - ( inArrayLens[0] / 2 ) ); } memcpy( &(outArrays[1][inArrayLens[0] / 2]), &(pdArrayOne[0]), ( ( inArrayLens[0] + 1 ) / 2 ) * sizeof( double ) ); memcpy( &(outArrays[1][0]), &(pdArrayOne[iLength - (inArrayLens[0] / 2)]), ( inArrayLens[0] / 2 ) * sizeof( double ) ); iReturn = 0; } } } } delete[] pdArrayOne; delete[] pdArrayTwo; } } } return iReturn; }
int main(){ const int max_mu_size=601; const int zero_pad_size=pow(2,15); FILE *in; in= fopen("mean.chi", "r"); gsl_matrix *e = gsl_matrix_alloc(max_mu_size, 4); gsl_vector * kvar=gsl_vector_alloc(max_mu_size); gsl_vector * muvar=gsl_vector_alloc(max_mu_size); gsl_vector * mu_0pad=gsl_vector_alloc(zero_pad_size); gsl_vector * r_0pad=gsl_vector_alloc(zero_pad_size/2); //half of lenght gsl_vector * kvar_0pad=gsl_vector_alloc(zero_pad_size); gsl_matrix_fscanf(in, e); fclose(in); gsl_matrix_get_col(kvar,e,0); gsl_matrix_get_col(muvar,e,1); gsl_vector_set_zero(mu_0pad); gsl_matrix_free(e); double dk=gsl_vector_get (kvar, 1)-gsl_vector_get (kvar, 0); double dr=M_PI/float(zero_pad_size-1)/dk; for (int i = 0; i < zero_pad_size; i++) { gsl_vector_set (kvar_0pad, i, dk*i); } for (int i = 0; i < zero_pad_size/2; i++) { gsl_vector_set (r_0pad, i, dr*i); } for (int i = 0; i < max_mu_size; i++) { gsl_vector_set (mu_0pad, i, gsl_vector_get (muvar, i)); } gsl_vector *mu_widowed=gsl_vector_alloc(zero_pad_size); gsl_vector_memcpy (mu_widowed, mu_0pad); double kmin=4.0, kmax=17.0, dwk=0.8; hanning(mu_widowed, kvar_0pad, kmin, kmax, dwk); //FFT transform double *data = (double *) malloc(zero_pad_size*sizeof(double)); //new double [zero_pad_size] ; memcpy(data, mu_widowed->data, zero_pad_size*sizeof(double)); gsl_fft_real_radix2_transform(data, 1, zero_pad_size); //Unpack complex vector gsl_vector_complex *fourier_data = gsl_vector_complex_alloc (zero_pad_size); gsl_fft_halfcomplex_radix2_unpack(data, fourier_data->data, 1, zero_pad_size); gsl_vector *fftR_real = gsl_vector_alloc(fourier_data->size/2); gsl_vector *fftR_imag = gsl_vector_alloc(fourier_data->size/2); gsl_vector *fftR_abs = gsl_vector_alloc(fourier_data->size/2); complex_vector_parts(fourier_data, fftR_real, fftR_imag); complex_vector_abs(fftR_abs, fftR_real, fftR_imag); gsl_vector *first_shell=gsl_vector_alloc(fftR_abs->size); gsl_vector_memcpy (first_shell, fftR_abs); double rmin=0.2, rmax=3.0, dwr=0.1; hanning(first_shell, r_0pad, rmin, rmax, dwr); //feff0001.dat const int path_lines=68; e = gsl_matrix_alloc(path_lines, 7); gsl_vector * k_p =gsl_vector_alloc(path_lines); gsl_vector * phc_p=gsl_vector_alloc(path_lines); gsl_vector * mag_p=gsl_vector_alloc(path_lines); gsl_vector * pha_p=gsl_vector_alloc(path_lines); gsl_vector * lam_p=gsl_vector_alloc(path_lines); in= fopen("feff0001.dat", "r"); gsl_matrix_fscanf(in, e); fclose(in); gsl_matrix_get_col(k_p ,e,0); gsl_matrix_get_col(phc_p,e,1); gsl_matrix_get_col(mag_p,e,2); gsl_matrix_get_col(pha_p,e,3); gsl_matrix_get_col(lam_p,e,5); gsl_matrix_free(e); gsl_interp_accel *acc = gsl_interp_accel_alloc (); gsl_spline *k_spline = gsl_spline_alloc (gsl_interp_cspline, path_lines); gsl_spline *phc_spline = gsl_spline_alloc (gsl_interp_cspline, path_lines); gsl_spline *mag_spline = gsl_spline_alloc (gsl_interp_cspline, path_lines); gsl_spline *pha_spline = gsl_spline_alloc (gsl_interp_cspline, path_lines); gsl_spline *lam_spline = gsl_spline_alloc (gsl_interp_cspline, path_lines); gsl_spline_init (k_spline , k_p->data, k_p->data , path_lines); gsl_spline_init (phc_spline, k_p->data, phc_p->data, path_lines); gsl_spline_init (mag_spline, k_p->data, mag_p->data, path_lines); gsl_spline_init (pha_spline, k_p->data, pha_p->data, path_lines); gsl_spline_init (lam_spline, k_p->data, lam_p->data, path_lines); gsl_vector * mu_p =gsl_vector_alloc(path_lines); //struct fit_params { student_params t; double kshift; double S02; double N; inter_path splines; }; //student_params t = {2.45681867, 0.02776907, -21.28920008, 9.44741797, 0.0, 0.0, 0.0}; splines.acc=acc; splines.phc_spline=phc_spline; splines.mag_spline=mag_spline; splines.pha_spline=pha_spline; splines.lam_spline=lam_spline; fit_params fp = { 2.45681867, 0.02776907, -21.28920008, 9.44741797, 1.0, 0.0}; compute_itegral(k_p, &fp, mu_p); //mu_data_fit params = { k_p, mu_p}; mu_data.k = kvar_0pad; mu_data.mu = mu_0pad; mu_data.mu_ft = first_shell; mu_data.r = r_0pad; mu_data.kmin = kmin; mu_data.kmax = kmax; mu_data.rmin = rmin; mu_data.rmax = rmax; mu_data.dwk = dwk; mu_data.dwr = dwr; // initialize the solver size_t Nparams=6; gsl_vector *guess0 = gsl_vector_alloc(Nparams); gsl_vector_set(guess0, 0, 2.4307); gsl_vector_set(guess0, 1, 0.040969); gsl_vector_set(guess0, 2, 0.001314); gsl_vector_set(guess0, 3, 7835); gsl_vector_set(guess0, 4, 1.0); gsl_vector_set(guess0, 5, 0.0); gsl_vector *fit_r = gsl_vector_alloc(r_0pad->size); compute_itegral_r(&mu_data, fp, fit_r); gsl_matrix *plotting = gsl_matrix_calloc(r_0pad->size, 3); gsl_matrix_set_col (plotting, 0, r_0pad); gsl_matrix_set_col (plotting, 1, first_shell); gsl_matrix_set_col (plotting, 2, fit_r); plot_matplotlib(plotting); gsl_matrix_free (plotting); gsl_multifit_function_fdf fit_mu_k; fit_mu_k.f = &resudial_itegral_r; fit_mu_k.n = MAX_FIT_POINTS; fit_mu_k.p = Nparams; fit_mu_k.params = &mu_data; fit_mu_k.df = NULL; fit_mu_k.fdf = NULL; gsl_multifit_fdfsolver *solver = gsl_multifit_fdfsolver_alloc(gsl_multifit_fdfsolver_lmsder, MAX_FIT_POINTS, Nparams); gsl_multifit_fdfsolver_set(solver, &fit_mu_k, guess0); size_t iter=0, status; do{ iter++; //cout << solver->x->data[0] << " " << solver->x->data[1] <<endl; status = gsl_multifit_fdfsolver_iterate (solver); //printf("%12.4f %12.4f %12.4f\n", solver->J->data[0,0], solver->J->data[1,1], solver->J->data[2,2] ); //gsl_multifit_fdfsolver_dif_df (k_p, &fit_mu_k, mu_p, solver->J); //gsl_multifit_fdfsolver_dif_fdf (k_p, &fit_mu_k, mu_p, solver->J); for (int i =0; i< solver->x->size; i++){ printf("%14.5f", gsl_vector_get (solver->x, i)) ; } printf("\n") ; if (status) break; status = gsl_multifit_test_delta (solver->dx, solver->x, 1e-4, 1e-4); }while (status == GSL_CONTINUE && iter < 100); gsl_vector * mu_fit =gsl_vector_alloc(path_lines); fit_params fitp = { solver->x->data[0], solver->x->data[1],\ solver->x->data[2], solver->x->data[3],\ solver->x->data[4], solver->x->data[5]}; compute_itegral(k_p, &fitp, mu_fit); fp.mu=gsl_vector_get (solver->x, 0); fp.sig=gsl_vector_get (solver->x, 1); fp.skew=gsl_vector_get (solver->x, 2); fp.nu=gsl_vector_get (solver->x, 3); fp.S02=gsl_vector_get (solver->x, 4); fp.kshift=gsl_vector_get (solver->x, 5); compute_itegral_r(&mu_data, fp, fit_r); //gsl_matrix *plotting = gsl_matrix_calloc(r_0pad->size, 3); gsl_matrix_set_col (plotting, 0, r_0pad); gsl_matrix_set_col (plotting, 1, first_shell); gsl_matrix_set_col (plotting, 2, fit_r); int min_r=search_max(r_0pad, 0.); int max_r=search_max(r_0pad, 4.); gsl_matrix_view plotting_lim = gsl_matrix_submatrix (plotting, min_r, 0, max_r-min_r, plotting->size2); plot_matplotlib(&plotting_lim.matrix); gsl_matrix_free (plotting); //cout << gsl_spline_eval (k_spline, 1.333, acc) << endl; //cout << gsl_spline_eval (phc_spline, 1.333, acc) << endl; //cout << data[0] << "\t" << data[1] << "\t" << data[2] << "\t" << endl; //cout << fourier_data->data[0] << "\t" << fourier_data->data[1] << "\t" << fourier_data->data[2] << "\t" << endl; //Plotting /* gsl_matrix *plotting = gsl_matrix_calloc(zero_pad_size, 3); gsl_matrix_set_col (plotting, 0, kvar_0pad); gsl_matrix_set_col (plotting, 1, mu_0pad); gsl_matrix_set_col (plotting, 2, mu_widowed); int max_k=search_max(kvar_0pad, 35.); int min_k=search_max(kvar_0pad, 1.0); gsl_matrix_view plotting_lim = gsl_matrix_submatrix (plotting, min_k, 0, max_k-min_k, 3); plot_matplotlib(&plotting_lim.matrix); gsl_matrix_free (plotting); */ /* gsl_matrix *plotting = gsl_matrix_calloc(zero_pad_size, 2); gsl_matrix_set_col (plotting, 0, r_0pad); gsl_matrix_set_col (plotting, 1, mu_0pad); int max_k=search_max(kvar_0pad, 35.); int min_k=search_max(kvar_0pad, 1.0); gsl_matrix_view plotting_lim = gsl_matrix_submatrix (plotting, min_k, 0, max_k-min_k, 3); plot_matplotlib(&plotting_lim.matrix); gsl_matrix_free (plotting); */ /* gsl_matrix *plotting = gsl_matrix_calloc(r_0pad->size, 5); gsl_matrix_set_col (plotting, 0, r_0pad); gsl_matrix_set_col (plotting, 1, fftR_abs); gsl_matrix_set_col (plotting, 2, fftR_real); gsl_matrix_set_col (plotting, 3, fftR_imag); gsl_matrix_set_col (plotting, 4, first_shell); int min_r=search_max(r_0pad, 0.); int max_r=search_max(r_0pad, 5.); gsl_matrix_view plotting_lim = gsl_matrix_submatrix (plotting, min_r, 0, max_r-min_r, plotting->size2); plot_matplotlib(&plotting_lim.matrix); //plot_matplotlib(plotting); gsl_matrix_free (plotting); */ //cout << "Done" << endl; //cout << data[1] <<"\t" << data[2] << endl; //for (int i = 0; i < kvar->size; i++) //{ // cout << gsl_vector_get (kvar, i) <<"\t" << gsl_vector_get (muvar, i) << endl; //} }