Пример #1
0
void
freefp(struct fftonthreadparams *fp)
{
  size_t i;
  gsl_fft_complex_wavetable_free(fp[0].ps0wave);
  gsl_fft_complex_wavetable_free(fp[0].ps1wave);
  for(i=0;i<fp->p->cp.numthreads;++i)
    {
      gsl_fft_complex_workspace_free(fp[i].ps0work);
      gsl_fft_complex_workspace_free(fp[i].ps1work);
    }
  free(fp);
}
Пример #2
0
static void
PyGSL_transform_space_dealloc(PyGSL_transform_space * self)
{
     FUNC_MESS_BEGIN();
     assert(PyGSL_transform_space_check(self));     
     assert(self->space.v);
     switch(self->type){
     case COMPLEX_WORKSPACE:           gsl_fft_complex_workspace_free(self->space.cws);       break;
     case COMPLEX_WAVETABLE:           gsl_fft_complex_wavetable_free(self->space.cwt);       break;
     case REAL_WORKSPACE:              gsl_fft_real_workspace_free(self->space.rws);          break;
     case REAL_WAVETABLE:	       gsl_fft_real_wavetable_free(self->space.rwt);          break;
     case HALFCOMPLEX_WAVETABLE:       gsl_fft_halfcomplex_wavetable_free(self->space.hcwt);  break;
     case COMPLEX_WORKSPACE_FLOAT:     gsl_fft_complex_workspace_float_free(self->space.cwsf);      break;
     case COMPLEX_WAVETABLE_FLOAT:     gsl_fft_complex_wavetable_float_free(self->space.cwtf);      break;
     case REAL_WORKSPACE_FLOAT:        gsl_fft_real_workspace_float_free(self->space.rwsf);         break;
     case REAL_WAVETABLE_FLOAT:	       gsl_fft_real_wavetable_float_free(self->space.rwtf);         break;
     case HALFCOMPLEX_WAVETABLE_FLOAT: gsl_fft_halfcomplex_wavetable_float_free(self->space.hcwtf); break;
#ifdef _PYGSL_GSL_HAS_WAVELET
     case WAVELET_WORKSPACE          : gsl_wavelet_workspace_free(self->space.wws);                 break;
#endif
     default: pygsl_error("Got unknown switch", filename, __LINE__, GSL_ESANITY); break;
     }
     self->space.v = NULL;
     FUNC_MESS_END();
}
Пример #3
0
/* calculates the fast Fourier transform of a complex packed array, and stores
 * it into fft_results. The length of the array is 2*N, and the storage
 * convention is that the real and imaginary parts of the complex number are
 * stored in consecutive locations */
int complex_fft (size_t N, double *data, double *fft_results) {
  size_t i;
  int retcode;

  /* initialize the data for fft */
  for (i=0; i<2*N; i++) fft_results [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_complex_radix2_forward (fft_results, 1, N);
  }
  else {
    /* alloc memory for wavetable and workspace */
    gsl_fft_complex_wavetable * wavetable = gsl_fft_complex_wavetable_alloc (N);
    gsl_fft_complex_workspace * workspace = gsl_fft_complex_workspace_alloc (N);

    /* perform the fft */
    retcode = gsl_fft_complex_forward (fft_results, 1, N, wavetable, workspace);

    /* free memory */
    gsl_fft_complex_wavetable_free (wavetable);
    gsl_fft_complex_workspace_free (workspace);
  }
  return retcode;
}
Пример #4
0
vector<double> powerSpectrum(vector<double> input)
{
    size_t length = input.size();
    if(length != size)
    {
        if(size >= 0)
        {
            gsl_fft_complex_workspace_free (work);
            gsl_fft_complex_wavetable_free (complex);
        }
        complex = gsl_fft_complex_wavetable_alloc(length);
        work = gsl_fft_complex_workspace_alloc(length);
        size  = length;

    }
    double reals[length*2];
    for(size_t i = 0; i <length; i++)
    {
        reals[i*2] = input[i];
        reals[i*2+1] = 0;
    }


    gsl_fft_complex_transform (reals, 1, length, complex, work, gsl_fft_forward);

    for(int i = 0; i< length; i++)
    {
        input[i] = sqrt(reals[i*2]*reals[i*2] + reals[i*2+1]*reals[i*2+1])/length;
    }
    return input;
}
Пример #5
0
/*
 * Free the allocated data types
 */
BandPassFilterFFT::~BandPassFilterFFT() {
	delete[] f;
	delete[] fcache;
	gsl_interp_accel_free(faccel);
	gsl_fft_complex_wavetable_free (wavetable);
	gsl_fft_complex_workspace_free (workspace);
}
void oph_gsl_ifft_deinit(UDF_INIT *initid)
{
    //Free allocated space
	if(initid->ptr){
		oph_stringPtr output = (oph_stringPtr) initid->ptr;
		if(output->content){
			free(output->content);
			output->content=NULL;
		}
		if(output->length){
			free(output->length);
			output->length=NULL;
		}
		free(initid->ptr);
		initid->ptr=NULL;
	}
	if(initid->extension){
		oph_gsl_fft_extraspace *extra = (oph_gsl_fft_extraspace *) initid->extension;
		if(extra->ws){
			gsl_fft_complex_workspace_free(extra->ws);
			extra->ws=NULL;
		}
		if(extra->wt){
			gsl_fft_complex_wavetable_free(extra->wt);
			extra->wt=NULL;
		}
		free(initid->extension);
		initid->extension=NULL;
	}
}
Пример #7
0
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);
}
Пример #8
0
void cnoise_generate_colored_noise( double *x, int N, double alpha, double std ){
	gsl_fft_complex_wavetable * wavetable;
	gsl_fft_complex_workspace * workspace;
	
	int i, j;
	double tmp;
	double gauss_second, gauss_u, gauss_v;
	double * fh = malloc( 4*N*sizeof(double) );
	double * fw = malloc( 4*N*sizeof(double) );
	
	fh[0] = 1.0; fh[1] = 0.0;
	for( i=1; i<N; i++ ){
		fh[2*i] = fh[2*(i-1)] * ( 0.5 * alpha + (double)(i-1) ) / ((double) i );
		fh[2*i+1] = 0.0;
	};
	for( i=0; i<N; i+=2 ){
		gauss_u = cnoise_uniform01(); gauss_v = cnoise_uniform01();
		fw[2*i] = std * sqrt( - 2 * log( gauss_u ) ) * cos( 2 * _CNOISE_PI * gauss_v ); fw[2*i+1] = 0.0;
		fw[2*i+2] = std * sqrt( - 2 * log( gauss_u ) ) * sin( 2 * _CNOISE_PI * gauss_v ); fw[2*i+3] = 0.0;
	};
	for( i=2*N; i<4*N; i++ ){ fh[i] = 0.0; fw[i] = 0.0; };
	
	wavetable = gsl_fft_complex_wavetable_alloc(2*N);
	workspace = gsl_fft_complex_workspace_alloc(2*N);

	gsl_fft_complex_forward (fh, 1, 2*N, wavetable, workspace);
	gsl_fft_complex_forward (fw, 1, 2*N, wavetable, workspace);
	
	for( i=0; i<N+1; i++ ){
		tmp = fw[2*i];
		fw[2*i]   = tmp*fh[2*i] - fw[2*i+1]*fh[2*i+1];
		fw[2*i+1] = tmp*fh[2*i+1] + fw[2*i+1]*fh[2*i];
	};

	fw[0] /= 2.0; fw[1] /= 2.0;
	fw[2*N] /= 2.0; fw[2*N+1] /= 2.0;
	
	for( i=N+1; i<2*N; i++ ){
		fw[2*i] = 0.0; fw[2*i+1] = 0.0;
	};
	
	gsl_fft_complex_inverse( fw, 1, 2*N, wavetable, workspace);
	
	for( i=0; i<N; i++ ){
		x[i] = 2.0*fw[2*i];
	};

	free( fh );
	free( fw );

	gsl_fft_complex_wavetable_free (wavetable);
	gsl_fft_complex_workspace_free (workspace);
};
Пример #9
0
int
main (void)
{
  int i;
  const int n = 630;
  double data[2*n];

  gsl_fft_complex_wavetable * wavetable;
  gsl_fft_complex_workspace * workspace;

  for (i = 0; i < n; i++)
    {
      REAL(data,i) = 0.0;
      IMAG(data,i) = 0.0;
    }

  data[0] = 1.0;

  for (i = 1; i <= 10; i++)
    {
      REAL(data,i) = REAL(data,n-i) = 1.0;
    }

  for (i = 0; i < n; i++)
    {
      printf ("%d: %e %e\n", i, REAL(data,i), 
                                IMAG(data,i));
    }
  printf ("\n");

  wavetable = gsl_fft_complex_wavetable_alloc (n);
  workspace = gsl_fft_complex_workspace_alloc (n);

  for (i = 0; i < wavetable->nf; i++)
    {
       printf ("# factor %d: %d\n", i, 
               wavetable->factor[i]);
    }

  gsl_fft_complex_forward (data, 1, n, 
                           wavetable, workspace);

  for (i = 0; i < n; i++)
    {
      printf ("%d: %e %e\n", i, REAL(data,i), 
                                IMAG(data,i));
    }

  gsl_fft_complex_wavetable_free (wavetable);
  gsl_fft_complex_workspace_free (workspace);
  return 0;
}
Пример #10
0
void
Fft<double>::inverse(
  const std::vector<double>&                data,
        unsigned int                        fftSize,
        std::vector<std::complex<double> >& inverseResult)
{
  if (inverseResult.size() != fftSize) {
    inverseResult.resize(fftSize,std::complex<double>(0.,0.));
    std::vector<std::complex<double> >(inverseResult).swap(inverseResult);
  }

  std::vector<double> internalData(2*fftSize,0.); // Yes, twice the fftSize
  unsigned int minSize = std::min((unsigned int) data.size(),fftSize);
  for (unsigned int j = 0; j < minSize; ++j) {
    internalData[2*j] = data[j];
  }

  //if (m_subDisplayFile()) {
  //  *m_subDisplayFile() << "In Fft<double>::inverse()"
  //                     << ": about to call gsl_fft_complex_inverse()"
  //                     << " with fftSize = "         << fftSize
  //                     << "; internalData.size() = " << internalData.size()
  //                     << std::endl;
  //}

  gsl_fft_complex_workspace* complexWkSpace = gsl_fft_complex_workspace_alloc(fftSize);
  gsl_fft_complex_wavetable* complexWvTable = gsl_fft_complex_wavetable_alloc(fftSize);

  gsl_fft_complex_inverse(&internalData[0],
                          1,
                          fftSize,
                          complexWvTable,
                          complexWkSpace);

  gsl_fft_complex_wavetable_free(complexWvTable);
  gsl_fft_complex_workspace_free(complexWkSpace);

  //if (m_subDisplayFile()) {
  //  *m_subDisplayFile() << "In Fft<double>::inverse()"
  //                     << ": returned from gsl_fft_complex_inverse()"
  //                     << " with fftSize = "          << fftSize
  //                     << "; inverseResult.size() = " << inverseResult.size()
  //                     << std::endl;
  //}

  for (unsigned int j = 0; j < fftSize; ++j) {
    inverseResult[j] = std::complex<double>(internalData[2*j],internalData[2*j+1]);
  }

  return;
}
Пример #11
0
void cnoise_generate_colored_noise_truncated( double *x, int N, double alpha, double std, double range ){
	gsl_fft_complex_wavetable * wavetable;
	gsl_fft_complex_workspace * workspace;
	
	int i, j;
	double tmp;
	double gauss_second, gauss_u, gauss_v;
	double * fh = malloc( 4*N*sizeof(double) );
	double * fw = malloc( 4*N*sizeof(double) );
	
	fh[0] = 1.0; fh[1] = 0.0;
	for( i=1; i<N; i++ ){
		fh[2*i] = fh[2*(i-1)] * ( 0.5 * alpha + (double)(i-1) ) / ((double) i );
		fh[2*i+1] = 0.0;
	};
	for( i=0; i<N; i+=2 ){
		cnoise_two_gaussian_truncated( &fw[2*i], &fw[2*i+2], std, range );
	};
	for( i=2*N; i<4*N; i++ ){ fh[i] = 0.0; fw[i] = 0.0; };
	
	wavetable = gsl_fft_complex_wavetable_alloc(2*N);
	workspace = gsl_fft_complex_workspace_alloc(2*N);

	gsl_fft_complex_forward (fh, 1, 2*N, wavetable, workspace);
	gsl_fft_complex_forward (fw, 1, 2*N, wavetable, workspace);
	
	for( i=0; i<N+1; i++ ){
		tmp = fw[2*i];
		fw[2*i]   = tmp*fh[2*i] - fw[2*i+1]*fh[2*i+1];
		fw[2*i+1] = tmp*fh[2*i+1] + fw[2*i+1]*fh[2*i];
	};

	fw[0] /= 2.0; fw[1] /= 2.0;
	fw[2*N] /= 2.0; fw[2*N+1] /= 2.0;
	
	for( i=N+1; i<2*N; i++ ){
		fw[2*i] = 0.0; fw[2*i+1] = 0.0;
	};
	
	gsl_fft_complex_inverse( fw, 1, 2*N, wavetable, workspace);
	
	for( i=0; i<N; i++ ){
		x[i] = 2.0*fw[2*i];
	};

	free( fh );
	free( fw );

	gsl_fft_complex_wavetable_free (wavetable);
	gsl_fft_complex_workspace_free (workspace);
};
Пример #12
0
void
Fft<std::complex<double> >::inverse(
  const std::vector<std::complex<double> >& data,
        unsigned int                        fftSize,
        std::vector<std::complex<double> >& inverseResult)
{
  if (inverseResult.size() != fftSize) {
    inverseResult.resize(fftSize,std::complex<double>(0.,0.));
    std::vector<std::complex<double> >(inverseResult).swap(inverseResult);
  }

  std::vector<double> internalData(2*fftSize,0.);                          // Yes, twice the fftSize
  unsigned int minSize = 2 * std::min((unsigned int) data.size(),fftSize); // Yes, 2*
  for (unsigned int j = 0; j < minSize; ++j) {
    internalData[2*j  ] = data[j].real();
    internalData[2*j+1] = data[j].imag();
  }

  gsl_fft_complex_workspace* complexWkSpace = gsl_fft_complex_workspace_alloc(fftSize);
  gsl_fft_complex_wavetable* complexWvTable = gsl_fft_complex_wavetable_alloc(fftSize);

  gsl_fft_complex_inverse(&internalData[0],
                          1,
                          fftSize,
                          complexWvTable,
                          complexWkSpace);

  gsl_fft_complex_wavetable_free(complexWvTable);
  gsl_fft_complex_workspace_free(complexWkSpace);

  for (unsigned int j = 0; j < fftSize; ++j) {
    inverseResult[j] = std::complex<double>(internalData[2*j],internalData[2*j+1]);
  }

  return;
}
Пример #13
0
QString FFT::fftTable()
{
    int i;
	double *amp = new double[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(!amp || !wavetable || !workspace){
		QMessageBox::critical((ApplicationWindow *)parent(), tr("MantidPlot") + " - " + tr("Error"),
                        tr("Could not allocate memory, operation aborted!"));
        d_init_err = true;
        return "";
	}

	double df = 1.0/(double)(d_n*d_sampling);//frequency sampling
	double aMax = 0.0;//max amplitude
	QString text;
	if(!d_inverse) {
		d_explanation = tr("Forward") + " " + tr("FFT") + " " + tr("of") + " " + d_table->colName(d_real_col);
		text = tr("Frequency");
		gsl_fft_complex_forward (d_y, 1, d_n, wavetable, workspace);
	} else {
		d_explanation = tr("Inverse") + " " + tr("FFT") + " " + tr("of") + " " + d_table->colName(d_real_col);
		text = tr("Time");
		gsl_fft_complex_inverse (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(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(i=0; i<d_n; i++)
			d_x[i] = i*df;
	}

	for(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;

	text += "\t"+tr("Real")+"\t"+tr("Imaginary")+"\t"+tr("Amplitude")+"\t"+tr("Angle")+"\n";
	for (i=0; i<d_n; i++) {
		int i2 = 2*i;
		text += locale.toString(d_x[i], 'g', prec)+"\t";
		text += locale.toString(d_y[i2], 'g', prec)+"\t";
		text += locale.toString(d_y[i2+1], 'g', prec)+"\t";
		if (d_normalize)
			text += locale.toString(amp[i]/aMax, 'g', prec)+"\t";
		else
			text += locale.toString(amp[i], 'g', prec)+"\t";
		text += locale.toString(atan(d_y[i2+1]/d_y[i2]), 'g', prec)+"\n";
	}
	delete[] amp;
    return text;
}
Пример #14
0
static void GSL_FFT_Wavetable_free(GSL_FFT_Wavetable *table)
{
  gsl_fft_complex_wavetable_free((gsl_fft_complex_wavetable *) table);
}
Пример #15
0
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);
}
Пример #16
0
QString FFT::fftTable()
{
    int i;
	int rows = d_table->tableRows();
	double *amp = new double[rows];

	gsl_fft_complex_wavetable *wavetable = gsl_fft_complex_wavetable_alloc (rows);
	gsl_fft_complex_workspace *workspace = gsl_fft_complex_workspace_alloc (rows);

	if(!amp || !wavetable || !workspace)
	{
		QMessageBox::critical((ApplicationWindow *)parent(), tr("QtiPlot") + " - " + tr("Error"),
                        tr("Could not allocate memory, operation aborted!"));
        d_init_err = true;
        return "";
	}

	double df = 1.0/(double)(rows*d_sampling);//frequency sampling
	double aMax = 0.0;//max amplitude
	QString text;
	if(!d_inverse)
	{
		text = tr("Frequency");
		gsl_fft_complex_forward (d_y, 1, rows, wavetable, workspace);
	}
	else
	{
		text = tr("Time");
		gsl_fft_complex_inverse (d_y, 1, rows, wavetable, workspace);
	}

	gsl_fft_complex_wavetable_free (wavetable);
	gsl_fft_complex_workspace_free (workspace);

	if (d_shift_order)
	{
		int n2 = rows/2;
		for(i=0; i<rows; i++)
		{
			d_x[i] = (i-n2)*df;
			int j = i + rows;
			double aux = d_y[i];
			d_y[i] = d_y[j];
			d_y[j] = aux;
		}
	}
	else
	{
		for(i=0; i<rows; i++)
			d_x[i] = i*df;
	}

	for(i=0; i<rows; 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;
	}

	text += "\t"+tr("Real")+"\t"+tr("Imaginary")+"\t"+tr("Amplitude")+"\t"+tr("Angle")+"\n";
	for (i=0; i<rows; i++)
	{
		int i2 = 2*i;
		text += QString::number(d_x[i])+"\t";
		text += QString::number(d_y[i2])+"\t";
		text += QString::number(d_y[i2+1])+"\t";
		if (d_normalize)
			text += QString::number(amp[i]/aMax)+"\t";
		else
			text += QString::number(amp[i])+"\t";
		text += QString::number(atan(d_y[i2+1]/d_y[i2]))+"\n";
	}
	delete[] amp;
    return text;
}
Пример #17
0
QString FFT::fftCurve()
{
    int i, i2;
	int n2 = d_n/2;
	double *amp = new double[d_n];
	double *result = new double[2*d_n];

	if(!amp || !result){
		QMessageBox::critical((ApplicationWindow *)parent(), tr("MantidPlot") + " - " + tr("Error"),
                        tr("Could not allocate memory, operation aborted!"));
        d_init_err = true;
        return "";
	}

	double df = 1.0/(double)(d_n*d_sampling);//frequency sampling
	double aMax = 0.0;//max amplitude
	QString text;
	if(!d_inverse){
        d_explanation = tr("Forward") + " " + tr("FFT") + " " + tr("of") + " " + d_curve->title().text();
		text = tr("Frequency");

		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){
			QMessageBox::critical((ApplicationWindow *)parent(), tr("MantidPlot") + " - " + tr("Error"),
                        tr("Could not allocate memory, operation aborted!"));
            d_init_err = true;
			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 {
        d_explanation = tr("Inverse") + " " + tr("FFT") + " " + tr("of") + " " + d_curve->title().text();
		text = tr("Time");

		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){
			QMessageBox::critical((ApplicationWindow *)parent(), tr("MantidPlot") + " - " + tr("Error"),
                        tr("Could not allocate memory, operation aborted!"));
            d_init_err = true;
			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(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(i=0; i<d_n; i++)
			d_x[i] = i*df;
	}

	for(i=0;i<d_n;i++) {
		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;

	text += "\t"+tr("Real")+"\t"+tr("Imaginary")+"\t"+ tr("Amplitude")+"\t"+tr("Angle")+"\n";
	for (i=0; i<d_n; i++){
		i2 = 2*i;
		text += locale.toString(d_x[i], 'g', prec)+"\t";
		text += locale.toString(result[i2], 'g', prec)+"\t";
		text += locale.toString(result[i2+1], 'g', prec)+"\t";
		if (d_normalize)
			text += locale.toString(amp[i]/aMax, 'g', prec)+"\t";
		else
			text += locale.toString(amp[i], 'g', prec)+"\t";
		text += locale.toString(atan(result[i2+1]/result[i2]), 'g', prec)+"\n";
	}
	delete[] amp;
	delete[] result;
    return text;
}
Пример #18
0
QList<Column *> FFT::fftCurve() {
  int i, i2;
  int n2 = d_n / 2;
  double *amp = new double[d_n];
  double *result = new double[2 * d_n];

  if (!amp || !result) {
    QMessageBox::critical((ApplicationWindow *)parent(),
                          tr("AlphaPlot") + " - " + tr("Error"),
                          tr("Could not allocate memory, operation aborted!"));
    d_init_err = true;
    return QList<Column *>();
  }

  double df = 1.0 / (double)(d_n * d_sampling);  // frequency sampling
  double aMax = 0.0;  // max amplitude
  QList<Column *> columns;
  if (!d_inverse) {
    d_explanation = tr("Forward") + " " + tr("FFT") + " " + tr("of") + " " +
                    d_curve->title().text();
    columns << new Column(tr("Frequency"), AlphaPlot::Numeric);

    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) {
      QMessageBox::critical(
          (ApplicationWindow *)parent(), tr("AlphaPlot") + " - " + tr("Error"),
          tr("Could not allocate memory, operation aborted!"));
      d_init_err = true;
      return QList<Column *>();
    }

    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 {
    d_explanation = tr("Inverse") + " " + tr("FFT") + " " + tr("of") + " " +
                    d_curve->title().text();
    columns << new Column(tr("Time"), AlphaPlot::Numeric);

    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) {
      QMessageBox::critical(
          (ApplicationWindow *)parent(), tr("AlphaPlot") + " - " + tr("Error"),
          tr("Could not allocate memory, operation aborted!"));
      d_init_err = true;
      return QList<Column *>();
    }

    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 (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 (i = 0; i < d_n; i++) d_x[i] = i * df;
  }

  for (i = 0; i < d_n; i++) {
    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();

  columns << new Column(tr("Real"), AlphaPlot::Numeric);
  columns << new Column(tr("Imaginary"), AlphaPlot::Numeric);
  columns << new Column(tr("Amplitude"), AlphaPlot::Numeric);
  columns << new Column(tr("Angle"), AlphaPlot::Numeric);
  for (i = 0; i < d_n; i++) {
    i2 = 2 * i;
    columns.at(0)->setValueAt(i, d_x[i]);
    columns.at(1)->setValueAt(i, result[i2]);
    columns.at(2)->setValueAt(i, result[i2 + 1]);
    if (d_normalize)
      columns.at(3)->setValueAt(i, amp[i] / aMax);
    else
      columns.at(3)->setValueAt(i, amp[i]);
    columns.at(4)->setValueAt(i, atan(result[i2 + 1] / result[i2]));
  }
  delete[] amp;
  delete[] result;
  columns.at(0)->setPlotDesignation(AlphaPlot::X);
  columns.at(1)->setPlotDesignation(AlphaPlot::Y);
  columns.at(2)->setPlotDesignation(AlphaPlot::Y);
  columns.at(3)->setPlotDesignation(AlphaPlot::Y);
  columns.at(4)->setPlotDesignation(AlphaPlot::Y);
  return columns;
}
Пример #19
0
FFTCalc::~FFTCalc()
{
  gsl_fft_complex_wavetable_free(wavetable_);
  gsl_fft_complex_workspace_free(workspace_);
}
Пример #20
0
QList<Column *> FFT::fftTable() {
  int i;
  int rows = d_table->numRows();
  double *amp = new double[rows];

  gsl_fft_complex_wavetable *wavetable = gsl_fft_complex_wavetable_alloc(rows);
  gsl_fft_complex_workspace *workspace = gsl_fft_complex_workspace_alloc(rows);

  if (!amp || !wavetable || !workspace) {
    QMessageBox::critical((ApplicationWindow *)parent(),
                          tr("AlphaPlot") + " - " + tr("Error"),
                          tr("Could not allocate memory, operation aborted!"));
    d_init_err = true;
    return QList<Column *>();
  }

  double df = 1.0 / (double)(rows * d_sampling);  // frequency sampling
  double aMax = 0.0;  // max amplitude
  QList<Column *> columns;
  if (!d_inverse) {
    columns << new Column(tr("Frequency"), AlphaPlot::Numeric);
    gsl_fft_complex_forward(d_y, 1, rows, wavetable, workspace);
  } else {
    columns << new Column(tr("Time"), AlphaPlot::Numeric);
    gsl_fft_complex_inverse(d_y, 1, rows, wavetable, workspace);
  }

  gsl_fft_complex_wavetable_free(wavetable);
  gsl_fft_complex_workspace_free(workspace);

  if (d_shift_order) {
    int n2 = rows / 2;
    for (i = 0; i < rows; i++) {
      d_x[i] = (i - n2) * df;
      int j = i + rows;
      double aux = d_y[i];
      d_y[i] = d_y[j];
      d_y[j] = aux;
    }
  } else {
    for (i = 0; i < rows; i++) d_x[i] = i * df;
  }

  for (i = 0; i < rows; 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;
  }

  columns << new Column(tr("Real"), AlphaPlot::Numeric);
  columns << new Column(tr("Imaginary"), AlphaPlot::Numeric);
  columns << new Column(tr("Amplitude"), AlphaPlot::Numeric);
  columns << new Column(tr("Angle"), AlphaPlot::Numeric);
  for (i = 0; i < rows; i++) {
    int i2 = 2 * i;
    columns.at(0)->setValueAt(i, d_x[i]);
    columns.at(1)->setValueAt(i, d_y[i2]);
    columns.at(2)->setValueAt(i, d_y[i2 + 1]);
    if (d_normalize)
      columns.at(3)->setValueAt(i, amp[i] / aMax);
    else
      columns.at(3)->setValueAt(i, amp[i]);
    columns.at(4)->setValueAt(i, atan(d_y[i2 + 1] / d_y[i2]));
  }
  delete[] amp;
  columns.at(0)->setPlotDesignation(AlphaPlot::X);
  columns.at(1)->setPlotDesignation(AlphaPlot::Y);
  columns.at(2)->setPlotDesignation(AlphaPlot::Y);
  columns.at(3)->setPlotDesignation(AlphaPlot::Y);
  columns.at(4)->setPlotDesignation(AlphaPlot::Y);
  return columns;
}