/* Allocate the necessary arrays, note that we put everything in the
   first element of the fftonthreadparams structure array. All the
   other elements will point to this one later. This structure will be
   given to threads to run two times with a fixed set of parameters,
   that is why we are doing this here to facilitate the job. */
void
fftinitializer(struct convolveparams *p, struct fftonthreadparams **outfp)
{
  size_t i;
  struct fftonthreadparams *fp;

  /* Allocate the fftonthreadparams array.  */
  errno=0;
  *outfp=fp=malloc(p->cp.numthreads*sizeof *fp);
  if(fp==NULL)
    error(EXIT_FAILURE, errno, "%s: allocating %zu bytes for fp",
          __func__, p->cp.numthreads*sizeof *fp);

  /* Initialize the gsl_fft_wavetable structures (these are thread
     safe): */
  fp[0].ps0wave=gsl_fft_complex_wavetable_alloc(p->ps0);
  fp[0].ps1wave=gsl_fft_complex_wavetable_alloc(p->ps1);

  /* Set the values for all the other threads: */
  for(i=0;i<p->cp.numthreads;++i)
    {
      fp[i].p=p;
      fp[i].ps0wave=fp[0].ps0wave;
      fp[i].ps1wave=fp[0].ps1wave;
      fp[i].ps0work=gsl_fft_complex_workspace_alloc(p->ps0);
      fp[i].ps1work=gsl_fft_complex_workspace_alloc(p->ps1);
    }
}
Beispiel #2
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;
}
Beispiel #3
0
static VALUE rb_gsl_fft_complex_wavetable_new(VALUE klass, VALUE n)
{
  CHECK_FIXNUM(n);
  return Data_Wrap_Struct(cgsl_fft_complex_wavetable, 0, 
			  gsl_fft_complex_wavetable_free,
			  gsl_fft_complex_wavetable_alloc(FIX2INT(n)));
}
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;
}
Beispiel #5
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);
}
Beispiel #6
0
FFTCalc::FFTCalc(unsigned int length)
  : N_(length)
  , data_(2 * N_)
  , wavetable_(0)
  , workspace_(0)
{
  assert(N_ > 0);
  wavetable_ = gsl_fft_complex_wavetable_alloc(N_);
  workspace_ = gsl_fft_complex_workspace_alloc(N_);
}
Beispiel #7
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);
};
Beispiel #8
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;
}
Beispiel #9
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;
}
Beispiel #10
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);
};
Beispiel #11
0
// Parse argc, argv.  obj must be GSL::Vector::Complex.
// This can be simplified at some point.
// See comments preceding get_complex_stride_n()
static int gsl_fft_get_argv_complex(int argc, VALUE *argv, VALUE obj,
			    gsl_vector_complex ** vin,
			    gsl_complex_packed_array *data, size_t *stride,
			    size_t *n, gsl_fft_complex_wavetable **table,
			    gsl_fft_complex_workspace **space)
{
  int flag = NONE_OF_TWO, flagtmp, i, itmp = argc, itmp2 = 0, ccc;
  int flagw = 0;

  CHECK_VECTOR_COMPLEX(obj);

  ccc = argc;
  flagtmp = 0;
  flagw = 0;
  for (i = argc-1; i >= itmp2; i--) {
    if (rb_obj_is_kind_of(argv[i], cgsl_fft_complex_workspace)) {
      Data_Get_Struct(argv[i], gsl_fft_complex_workspace, *space);
      flagtmp = 1;
      flagw = 1;
      itmp = i;
      ccc--;
      break;
    }
  }
  flagtmp = 0;
  for (i = itmp-1; i >= itmp2; i--) {
    if (rb_obj_is_kind_of(argv[i], cgsl_fft_complex_wavetable)) {
      Data_Get_Struct(argv[i], gsl_fft_complex_wavetable, *table);
      flagtmp = 1;
      ccc--;
      break;
    }
  }
  get_complex_stride_n(obj, vin, data, stride, n);
  if (flagw == 0) {
    *space = gsl_fft_complex_workspace_alloc(*n);
    flag += ALLOC_SPACE;
  }
  if (flagtmp == 0) {
    *table = gsl_fft_complex_wavetable_alloc(*n);
    flag += ALLOC_TABLE;
  }
  if (*table == NULL) {
    rb_raise(rb_eRuntimeError, "something wrong with wavetable");
  }
  if (*space == NULL) {
    rb_raise(rb_eRuntimeError, "something wrong with workspace");
  }
  return flag;
}
/*
 * Initialize the FFT Band-pass filter
 */
BandPassFilterFFT::BandPassFilterFFT(uint32_t sample_rate, uint32_t chunk_size){
	length = chunk_size;
	samples = length/4;
	freqs = samples/2;

	/* allocate fast Fourier transform stuff */
	workspace = gsl_fft_complex_workspace_alloc (samples);
	wavetable = gsl_fft_complex_wavetable_alloc (samples);

	/* frequency array */
	double delta = 1./(sample_rate-1);
	f = new double[samples];
	f[0] = 0;
	for (int i=1; i<=(int)freqs; i++){
		f[samples-i]=-i/(samples*delta);
		f[i]=i/(samples*delta);
	}

	faccel = gsl_interp_accel_alloc () ;
	fcache = new double[2*samples];
}
Beispiel #13
0
static PyObject* 
PyGSL_transform_space_init(PyObject *self, PyObject *args, const enum pygsl_transform_space_type type)
{
	PyGSL_transform_space *o=NULL;
	size_t n;
	FUNC_MESS_BEGIN();
	o =  (PyGSL_transform_space *) PyObject_NEW(PyGSL_transform_space, &PyGSL_transform_space_pytype);
	if(o == NULL){
		return NULL;
	}

	if (0==PyArg_ParseTuple(args,"l", &n))
		return NULL;


	if (n<=0) {
	     pygsl_error("dimension must be >0", filename, __LINE__, GSL_EINVAL);
	     return NULL;
	}
	o->type = type;
	switch(type){
	case COMPLEX_WORKSPACE:           o->space.cws  = gsl_fft_complex_workspace_alloc(n);            break;
	case COMPLEX_WAVETABLE:           o->space.cwt  = gsl_fft_complex_wavetable_alloc(n);            break;
	case REAL_WORKSPACE:              o->space.rws  = gsl_fft_real_workspace_alloc(n);               break;
	case REAL_WAVETABLE:	          o->space.rwt  = gsl_fft_real_wavetable_alloc(n);               break;
	case HALFCOMPLEX_WAVETABLE:       o->space.hcwt = gsl_fft_halfcomplex_wavetable_alloc(n);        break;
	case COMPLEX_WORKSPACE_FLOAT:     o->space.cwsf  = gsl_fft_complex_workspace_float_alloc(n);     break;
	case COMPLEX_WAVETABLE_FLOAT:     o->space.cwtf  = gsl_fft_complex_wavetable_float_alloc(n);     break;
	case REAL_WORKSPACE_FLOAT:        o->space.rwsf  = gsl_fft_real_workspace_float_alloc(n);        break;
	case REAL_WAVETABLE_FLOAT:	  o->space.rwtf  = gsl_fft_real_wavetable_float_alloc(n);        break;
	case HALFCOMPLEX_WAVETABLE_FLOAT: o->space.hcwtf = gsl_fft_halfcomplex_wavetable_float_alloc(n); break;
#ifdef _PYGSL_GSL_HAS_WAVELET
	case WAVELET_WORKSPACE          : o->space.wws   = gsl_wavelet_workspace_alloc(n);               break;
#endif
	default: pygsl_error("Got unknown switch", filename, __LINE__, GSL_ESANITY); return NULL; break;
	}
	assert(o->space.v);
	FUNC_MESS_END();
	return (PyObject *) o;
}
Beispiel #14
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;
}
Beispiel #15
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;
}
Beispiel #16
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;
}
Beispiel #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;
}
Beispiel #18
0
int
main (int argc, char *argv[])
{
  int status, factor_sum;
  size_t i, start, end, n;
  double *complex_data, *complex_tmp;
  double rms, total;

  gsl_fft_complex_wavetable * cw;

  if (argc == 2)
    {
      start = strtol (argv[1], NULL, 0);
      end = start + 1;
    }
  else
    {
      start = 1 ;
      end = 1000 ;
    }

  for (n = start; n < end; n++)
    {

      complex_data = (double *) malloc (n * 2 * sizeof (double));
      complex_tmp = (double *) malloc (n * 2 * sizeof (double));

      cw = gsl_fft_complex_wavetable_alloc (n);
      status = gsl_fft_complex_init (n, cw);
      status = gsl_fft_complex_generate (n, cw);

      for (i = 0; i < n; i++)
        {
          REAL(complex_data,1,i) = urand();
          IMAG(complex_data,1,i) = urand();
        }

      memcpy (complex_tmp, complex_data, n * 2 * sizeof (double));
      gsl_fft_complex_forward (complex_data, 1, n, cw);
      gsl_fft_complex_inverse (complex_data, 1, n, cw);

      total = 0.0;
      for (i = 0; i < n; i++)
        {
          double dr = REAL(complex_data,1,i) - REAL(complex_tmp,1,i);
          double di = IMAG(complex_data,1,i) - IMAG(complex_tmp,1,i);
          total += dr * dr + di * di;
        }

      rms = sqrt (total / n);

      factor_sum = 0;
      for (i = 0; i < cw->nf; i++)
        {
          int j = cw->factor[i];
          factor_sum += j;
        }

      printf ("n = %d factor_sum = %d rms = %e\n", n, factor_sum, rms);

      free (complex_data);
      free (complex_tmp);

    }

  return 0;
}
char* oph_gsl_ifft(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error)
{
	if (*error)
	{
	        *length=0;
	        *is_null=0;
	        *error=1;
	        return NULL;
	}
	if (*is_null || !args->lengths[2])
	{
	        *length=0;
	        *is_null=1;
	        *error=0;
	        return NULL;
	}

    if (!initid->ptr) {

        initid->ptr=(char *)calloc(1,sizeof(oph_string));
        if(!initid->ptr){
            pmesg(1,  __FILE__, __LINE__, "Error allocating result\n");
            *length=0;
            *is_null=1;
            *error=1;
            return NULL;
        }

        oph_stringPtr output = (oph_stringPtr) initid->ptr;

        core_set_type(output,args->args[1],&(args->lengths[1]));
        if (output->type!=OPH_COMPLEX_DOUBLE) {
        	pmesg(1,  __FILE__, __LINE__, "Invalid type: oph_complex_double required\n");
        	*length=0;
        	*is_null=1;
        	*error=1;
        	return NULL;
        }

        core_set_type(output,args->args[0],&(args->lengths[0]));
        if (output->type!=OPH_COMPLEX_DOUBLE) {
        	pmesg(1,  __FILE__, __LINE__, "Invalid type: oph_complex_double or oph_double required\n");
        	*length=0;
        	*is_null=1;
        	*error=1;
        	return NULL;
        }

        output->length = (unsigned long *)calloc(1,sizeof(unsigned long));
        if (!output->length) {
            pmesg(1,  __FILE__, __LINE__, "Error allocating length\n");
            *length=0;
            *is_null=1;
            *error=1;
            return NULL;
        }

        *(output->length) = args->lengths[2];

        if(core_set_elemsize(output)){
            pmesg(1,  __FILE__, __LINE__, "Error on setting element size\n");
            *length=0;
            *is_null=0;
            *error=1;
            return NULL;
        }

        if(core_set_numelem(output)){
            pmesg(1,  __FILE__, __LINE__, "Error on counting result elements\n");
            *length=0;
            *is_null=0;
            *error=1;
            return NULL;
        }

        output->content = (char *)calloc(1,*(output->length));
        if(!output->content){
            pmesg(1,  __FILE__, __LINE__, "Error allocating result string\n");
            *length=0;
            *is_null=1;
            *error=1;
            return NULL;
        }

        initid->extension = calloc(1,sizeof(oph_gsl_fft_extraspace));
        if (!initid->extension) {
            pmesg(1,  __FILE__, __LINE__, "Error allocating extra space\n");
            *length=0;
            *is_null=1;
            *error=1;
            return NULL;
        }

        oph_gsl_fft_extraspace *extra = (oph_gsl_fft_extraspace *) initid->extension;

        extra->wt = gsl_fft_complex_wavetable_alloc(output->numelem);
        if (!extra->wt) {
            pmesg(1,  __FILE__, __LINE__, "Error allocating wavetable\n");
            *length=0;
            *is_null=1;
            *error=1;
            return NULL;
        }

        extra->ws = gsl_fft_complex_workspace_alloc(output->numelem);
        if (!extra->ws) {
            pmesg(1,  __FILE__, __LINE__, "Error allocating workspace\n");
            *length=0;
            *is_null=1;
            *error=1;
            return NULL;
        }

    }

    oph_stringPtr output = (oph_stringPtr) initid->ptr;
    oph_gsl_fft_extraspace *extra = (oph_gsl_fft_extraspace *) initid->extension;

    gsl_set_error_handler_off();

    memcpy(output->content,args->args[2],*(output->length));
    if (gsl_fft_complex_inverse((gsl_complex_packed_array) output->content,1,output->numelem,extra->wt,extra->ws)) {
        pmesg(1,  __FILE__, __LINE__, "Error computing ifft\n");
        *length=0;
        *is_null=1;
        *error=1;
        return NULL;
    }

    *length = *(output->length);
    *error=0;
    *is_null=0;

    return output->content;
}
Beispiel #20
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);
}
Beispiel #21
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;
}
Beispiel #22
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;
}