Beispiel #1
1
int sync_init() {
	int i, arraysize;
	nreceivers = conf.nreceivers;
	corrlen = conf.sync_len;
	if(nreceivers > NRECEIVERS_MAX) return -1;
	
	/* half of fft input will be zero-padded */
	fft1n = corrlen*2;
	fft2n = fft1n * CORRELATION_OVERSAMPLE;
	
	arraysize = nreceivers * fft1n;
	fft1in  = fftwf_malloc(arraysize * sizeof(*fft1in));
	fft1out = fftwf_malloc(arraysize * sizeof(*fft1out));
	for(i = 0; i < arraysize; i++)
		fft1in[i] = fft1out[i] = 0;
	
	arraysize = (nreceivers-1) * fft2n;
	fft2in  = fftwf_malloc(arraysize * sizeof(*fft2in));
	fft2out = fftwf_malloc(arraysize * sizeof(*fft2out));
	for(i = 0; i < arraysize; i++)
		fft2in[i] = fft2out[i] = 0;
	
	fft1plan = fftwf_plan_many_dft(
		1, &fft1n, nreceivers,
		fft1in,  NULL, 1, fft1n,
		fft1out, NULL, 1, fft1n,
		FFTW_FORWARD, FFTW_ESTIMATE);

	fft2plan = fftwf_plan_many_dft(
		1, &fft2n, nreceivers-1,
		fft2in,  NULL, 1, fft2n,
		fft2out, NULL, 1, fft2n,
		FFTW_BACKWARD, FFTW_ESTIMATE);
	return 0;
}
Beispiel #2
0
equalizer::equalizer(int bands,
                     const int hnSize,
                     const int HwSize)
  : size_(bands),hnSize_(hnSize),HwSize_(HwSize),verbose_(false),wnd_(0) {

  bands_ = new float[size_];
  freqs_ = new float[size_];
  for (int i=0;i<size_;++i) {
    bands_[i]=1.0;
    freqs_[i]=0.0;
  }

  // initialize FFT transformers
  // Buffer that holds the frequency domain data
  Hw_ = reinterpret_cast<fftwf_complex*>
        (fftwf_malloc(sizeof(fftwf_complex)*HwSize_));

  memset(Hw_,0,HwSize_*sizeof(fftwf_complex));

  // Even if the size of h(n) is hnSize_, we use HwSize because zero
  // padding is to be performed
  hn_ = reinterpret_cast<float*>(fftwf_malloc(sizeof(float)*HwSize_));
  memset(hn_,0,sizeof(float)*HwSize_);

  ifft_ = fftwf_plan_dft_c2r_1d(HwSize_,Hw_,hn_,FFTW_MEASURE);
  fft_  = fftwf_plan_dft_r2c_1d(HwSize_,hn_,Hw_,FFTW_MEASURE);

  hanning();
  //rectangular();

}
Beispiel #3
0
/* --------------------------------------------------------------------
 * IFFT for floating complex number data
 * Comparation with Matlab Command :
 * b = ifft(A) --> b = ifftwf_data(A, lenA, lenA)
 * b = ifft(A, nfft) --> b = ifftwf_data(A, lenA, nfft)
 * -------------------------------------------------------------------- */
fftwf_complex *ifftwf_data(fftwf_complex *fdata, int ndata, int nfft)
{
	fftwf_complex *idata;
	fftwf_complex *idatapad;
	fftwf_plan plan_backward;
	int i;

	if(nfft<ndata) {
		fprintf(stderr, "nfft < ndata \n");
		exit(0);
	}

	//allocate memory for data
	idata = ( fftwf_complex* ) fftwf_malloc( sizeof( fftwf_complex ) * ndata );

	//allocate memory for inversfft + padding
	idatapad = ( fftwf_complex* ) fftwf_malloc( sizeof( fftwf_complex ) * nfft );

	plan_backward = fftwf_plan_dft_1d( nfft, fdata, idatapad, FFTW_BACKWARD, FFTW_ESTIMATE );
	fftwf_execute( plan_backward ); //execute invers fft

	//get invers fft with length ndata and divide the value with nfft
	for( i=0; i<ndata; i++ ){
		idata[i][0] = idatapad[i][0]/nfft;	//real data
		idata[i][1] = idatapad[i][1]/nfft;	//imaginer data
	}

	//free allocate memory
	fftwf_destroy_plan( plan_backward );
	fftwf_free( idatapad );

	return(idata);
}
Beispiel #4
0
FFTX_FN_PREFIX
void fftx_init(struct FFTAnalysis *ft, uint32_t window_size, double rate, double fps) {
	ft->rate        = rate;
	ft->window_size = window_size;
	ft->data_size   = window_size / 2;
	ft->hann_window = NULL;
	ft->rboff = 0;
	ft->smps = 0;
	ft->step = 0;
	ft->sps = (fps > 0) ? ceil(rate / fps) : 0;
	ft->freq_per_bin = ft->rate / ft->data_size / 2.f;
	ft->phasediff_step = M_PI / ft->data_size;
	ft->phasediff_bin = 0;

	ft->ringbuf = (float *) malloc(window_size * sizeof(float));
	ft->fft_in  = (float *) fftwf_malloc(sizeof(float) * window_size);
	ft->fft_out = (float *) fftwf_malloc(sizeof(float) * window_size);
	ft->power   = (float *) malloc(ft->data_size * sizeof(float));
	ft->phase   = (float *) malloc(ft->data_size * sizeof(float));
	ft->phase_h = (float *) malloc(ft->data_size * sizeof(float));

	fftx_reset(ft);

	ft->fftplan = fftwf_plan_r2r_1d(window_size, ft->fft_in, ft->fft_out, FFTW_R2HC, FFTW_MEASURE);
}
void SpectrumVisualProcessor::setup(int fftSize_in) {
    busy_run.lock();

    fftSize = fftSize_in;
    desiredInputSize.store(fftSize);
    
    if (fftwInput) {
        free(fftwInput);
    }
    fftwInput = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * fftSize);
    if (fftInData) {
        free(fftInData);
    }
    fftInData = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * fftSize);
    if (fftLastData) {
        free(fftLastData);
    }
    fftLastData = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * fftSize);
    if (fftwOutput) {
        free(fftwOutput);
    }
    fftwOutput = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * fftSize);
    if (fftw_plan) {
        fftwf_destroy_plan(fftw_plan);
    }
    fftw_plan = fftwf_plan_dft_1d(fftSize, fftwInput, fftwOutput, FFTW_FORWARD, FFTW_ESTIMATE);
    busy_run.unlock();
}
Beispiel #6
0
/* --------------------------------------------------------------------
 * FFT for floating complex number data
 * Comparation with Matlab Command :
 * b = fft(A) --> b = fftwf_data(A, lenA, lenA)
 * b = fft(A, nfft) --> b = fftwf_data(A, lenA, nfft)
 * -------------------------------------------------------------------- */
fftwf_complex *fftwf_data(fftwf_complex *input, int ndata, int nfft)
{
	fftwf_complex *paddata;
	fftwf_complex *fdata;
	fftwf_plan plan_forward;

	if(nfft<ndata) {
		fprintf(stderr, "nfft < ndata \n");
		exit(0);
	}

	//allocate memory for data+padding
	paddata = ( fftwf_complex* ) fftwf_malloc( sizeof( fftwf_complex ) * nfft );

	//allocate data for output fft process
	fdata = ( fftwf_complex* ) fftwf_malloc( sizeof( fftwf_complex ) * nfft );

	//padding input data
	memset(paddata[0], 0, nfft*sizeof(fftwf_complex));
	memcpy(paddata[0], input[0], ndata*sizeof(fftwf_complex));

	plan_forward  = fftwf_plan_dft_1d( nfft, paddata, fdata, FFTW_FORWARD, FFTW_ESTIMATE );
	fftwf_execute( plan_forward ); //execute fft

	//free allocate memory
	fftwf_destroy_plan( plan_forward );
	fftwf_free( paddata );

	return(fdata);
}
Beispiel #7
0
void AKnockout::AllocateNewBuffers(unsigned int fftSize) {
	unsigned int fftSize2=fftSize/2+1;
	gInFIFO = new float [fftSize];
	FFTRealBuffer=(float*)fftwf_malloc(sizeof(float)*fftSize);
	gFFTworksp = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize2);
	gOutputAccum = new float [fftSize];
	gOutputAccum2 = new float [fftSize];
	gAnaPhase1 = new float [fftSize2];
	gAnaPhase2 = new float [fftSize2];
	gAnaMagn = new float [fftSize2];
	gInFIFO2 = new float [fftSize];
	gFFTworksp2 = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize2);
	gAnaMagn2 = new float [fftSize2];
	gDecay = new float [fftSize2];
	gDecay2 = new float [fftSize2];
	window = new float [fftSize];

	forward_sp1= fftwf_plan_dft_r2c_1d(fftSize, FFTRealBuffer , gFFTworksp,
                                    FFTW_ESTIMATE);
	forward_sp2= fftwf_plan_dft_r2c_1d(fftSize,FFTRealBuffer, gFFTworksp2,
                                    FFTW_ESTIMATE);	
	backward_sp1=fftwf_plan_dft_c2r_1d(fftSize, gFFTworksp, FFTRealBuffer,
                                    FFTW_ESTIMATE);
	backward_sp2=fftwf_plan_dft_c2r_1d(fftSize, gFFTworksp2, FFTRealBuffer,
                                    FFTW_ESTIMATE);
	makelookup(fftSize);
}
void AFComplexVector<fftwf_complex, float>::SetLength(const AFSampleCount smpcLength)
{
    assert(smpcLength > 0.0);
    
    if(m_smpcLength == 0)
    {
        m_smpcLength = smpcLength;
        if(m_pcpxUf) fftwf_free(m_pcpxUf);
        m_pcpxUf = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * m_smpcLength);
    }
    else
    {
        // create a temporary vector for storing purposes
        fftwf_complex* pcpxTmp = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * m_smpcLength);
        memcpy(pcpxTmp, m_pcpxUf, sizeof(fftwf_complex) * m_smpcLength);
        fftwf_free(m_pcpxUf);
        // enlarge the vector
        m_pcpxUf = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * smpcLength);
        memset(m_pcpxUf, 0, sizeof(fftwf_complex) * smpcLength);
        // copy backup to new vector
        memcpy(m_pcpxUf, pcpxTmp, sizeof(fftwf_complex) * m_smpcLength);
        fftwf_free(pcpxTmp);
        // set new length attribute
        m_smpcLength = smpcLength;
    }
}
Beispiel #9
0
InputSource::InputSource(const char *filename, int fft_size) {
    m_fft_size = fft_size;

    m_file = fopen(filename, "rb");
    if (m_file == nullptr)
        throw "Error opening file";

    struct stat sb;
    if (fstat(fileno(m_file), &sb) != 0)
        throw "Error fstating file";
    m_file_size = sb.st_size;

    m_data = (fftwf_complex*)mmap(NULL, m_file_size, PROT_READ, MAP_SHARED, fileno(m_file), 0);
    if (m_data == 0)
        throw "Error mmapping file";

    m_fftw_in = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * m_fft_size);
    m_fftw_out = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * m_fft_size);
    m_fftw_plan = fftwf_plan_dft_1d(m_fft_size, m_fftw_in, m_fftw_out, FFTW_FORWARD, FFTW_MEASURE);

    m_window.reset(new float[m_fft_size]);
    for (int i = 0; i < m_fft_size; i++) {
        m_window[i] = 0.5f * (1.0f - cos(Tau * i / (m_fft_size - 1)));
    }

    m_zoom = 0;
    m_max_zoom = floor(log2(m_fft_size));
}
temporaryMemFFT::temporaryMemFFT(long size)
{
    long alocSize;
    alocSize = 2*size;

#ifndef FFT3D_ACC
#ifdef SINGLE
	temp1_ = (fftwf_complex *)fftwf_malloc(alocSize*sizeof(fftwf_complex));
	temp2_ = (fftwf_complex *)fftwf_malloc(alocSize*sizeof(fftwf_complex));


#endif
	
#ifndef SINGLE
	temp1_ = (fftw_complex *)fftw_malloc(alocSize*sizeof(fftw_complex));
	temp2_ = (fftw_complex *)fftw_malloc(alocSize*sizeof(fftw_complex));
#endif
#else
#ifdef SINGLE
        temp1_ = (fftwf_complex *)malloc(alocSize*sizeof(fftwf_complex));
        temp2_ = (fftwf_complex *)malloc(alocSize*sizeof(fftwf_complex));
#endif

#ifndef SINGLE
        temp1_ = (fftw_complex *)malloc(alocSize*sizeof(fftw_complex));
        temp2_ = (fftw_complex *)malloc(alocSize*sizeof(fftw_complex));
#endif
#endif
	allocated_=size;
#pragma acc enter data copyin(this)
#pragma acc enter data create(temp1_[0:alocSize][0:2])
#pragma acc enter data create(temp2_[0:alocSize][0:2])
}
Beispiel #11
0
void wavelet_prepare(PluginData *pd)
{
	int w = pd->image_width, h = pd->image_height,
		pw = w/2+1; // physical width
	fftwf_complex *multiplied = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * pw * h);
	float *image_temp = (float*)fftwf_malloc(sizeof(float) * w * h);
	float diagonal = sqrt(h*h + w*w)/2;
	pd->image_wavelet = (char*)fftwf_malloc(WAVELET_DEPTH * w * h * sizeof(char));
	// printf("Wavelet layers occupy %lu MB.\n", (WAVELET_DEPTH * w * h * sizeof(short)) >> 20);
	// TODO: keep only the selected part of the image (->save memory)

	int lower = 0, peak = 1, upper = scale_to_dist(1, diagonal);
	for (int scale = 0; scale < WAVELET_DEPTH; scale ++)
	{
		float above = upper-peak, below = peak-lower;
		for (int i=0; i < pw*h; i++){
			multiplied[i][0] = multiplied[i][1] = 0.0;
		}
		for (int i=0; i < pw*h; i++)
		{
			float dist = index_to_dist(i, pw, h);
			if (dist <= upper){
				if (dist > lower){
					if (dist > peak){
						for(int channel=0; channel < pd->channel_count; channel ++)
						{
							multiplied[i][0] += pd->image_freq[channel][i][0];
							multiplied[i][1] += pd->image_freq[channel][i][1];
						}
						float coef = (1.0 - (dist-peak)/above) / pd->channel_count;
						multiplied[i][0] *= coef;
						multiplied[i][1] *= coef;
					}
					else {
						for(int channel=0; channel < pd->channel_count; channel ++)
						{
							multiplied[i][0] += pd->image_freq[channel][i][0];
							multiplied[i][1] += pd->image_freq[channel][i][1];
						}
						float coef = (1.0 - (peak-dist)/below) / pd->channel_count;
						multiplied[i][0] *= coef;
						multiplied[i][1] *= coef;
					}
				}
			}
		}
		// apply inverse FFT
		fftwf_execute_dft_c2r(pd->plan, multiplied, image_temp);
		for (int i=0; i < w*h; i++)
		{
			pd->image_wavelet[i*WAVELET_DEPTH + scale] = CLAMPED(image_temp[i], -127, 127);
		}
		lower = peak;
		peak = upper;
		upper = scale_to_dist(scale+2, diagonal);
	}
	fftwf_free(multiplied);
	fftwf_free(image_temp);
}
Beispiel #12
0
void grav_fft_init()
{
	int xblock2 = XRES/CELL*2;
	int yblock2 = YRES/CELL*2;
	int x, y, fft_tsize = (xblock2/2+1)*yblock2;
	float distance, scaleFactor;
	fftwf_plan plan_ptgravx, plan_ptgravy;
	if (grav_fft_status) return;

	//use fftw malloc function to ensure arrays are aligned, to get better performance
	th_ptgravx = (float*)fftwf_malloc(xblock2*yblock2*sizeof(float));
	th_ptgravy = (float*)fftwf_malloc(xblock2*yblock2*sizeof(float));
	th_ptgravxt = (fftwf_complex*)fftwf_malloc(fft_tsize*sizeof(fftwf_complex));
	th_ptgravyt = (fftwf_complex*)fftwf_malloc(fft_tsize*sizeof(fftwf_complex));
	th_gravmapbig = (float*)fftwf_malloc(xblock2*yblock2*sizeof(float));
	th_gravmapbigt = (fftwf_complex*)fftwf_malloc(fft_tsize*sizeof(fftwf_complex));
	th_gravxbig = (float*)fftwf_malloc(xblock2*yblock2*sizeof(float));
	th_gravybig = (float*)fftwf_malloc(xblock2*yblock2*sizeof(float));
	th_gravxbigt = (fftwf_complex*)fftwf_malloc(fft_tsize*sizeof(fftwf_complex));
	th_gravybigt = (fftwf_complex*)fftwf_malloc(fft_tsize*sizeof(fftwf_complex));

	//select best algorithm, could use FFTW_PATIENT or FFTW_EXHAUSTIVE but that increases the time taken to plan, and I don't see much increase in execution speed
	plan_ptgravx = fftwf_plan_dft_r2c_2d(yblock2, xblock2, th_ptgravx, th_ptgravxt, FFTW_MEASURE);
	plan_ptgravy = fftwf_plan_dft_r2c_2d(yblock2, xblock2, th_ptgravy, th_ptgravyt, FFTW_MEASURE);
	plan_gravmap = fftwf_plan_dft_r2c_2d(yblock2, xblock2, th_gravmapbig, th_gravmapbigt, FFTW_MEASURE);
	plan_gravx_inverse = fftwf_plan_dft_c2r_2d(yblock2, xblock2, th_gravxbigt, th_gravxbig, FFTW_MEASURE);
	plan_gravy_inverse = fftwf_plan_dft_c2r_2d(yblock2, xblock2, th_gravybigt, th_gravybig, FFTW_MEASURE);

	//(XRES/CELL)*(YRES/CELL)*4 is size of data array, scaling needed because FFTW calculates an unnormalized DFT
	scaleFactor = -M_GRAV/((XRES/CELL)*(YRES/CELL)*4);
	//calculate velocity map caused by a point mass
	for (y=0; y<yblock2; y++)
	{
		for (x=0; x<xblock2; x++)
		{
			if (x==XRES/CELL && y==YRES/CELL) continue;
			distance = sqrtf(pow(x-(XRES/CELL), 2) + pow(y-(YRES/CELL), 2));
			th_ptgravx[y*xblock2+x] = scaleFactor*(x-(XRES/CELL)) / pow(distance, 3);
			th_ptgravy[y*xblock2+x] = scaleFactor*(y-(YRES/CELL)) / pow(distance, 3);
		}
	}
	th_ptgravx[yblock2*xblock2/2+xblock2/2] = 0.0f;
	th_ptgravy[yblock2*xblock2/2+xblock2/2] = 0.0f;

	//transform point mass velocity maps
	fftwf_execute(plan_ptgravx);
	fftwf_execute(plan_ptgravy);
	fftwf_destroy_plan(plan_ptgravx);
	fftwf_destroy_plan(plan_ptgravy);
	fftwf_free(th_ptgravx);
	fftwf_free(th_ptgravy);

	//clear padded gravmap
	memset(th_gravmapbig,0,xblock2*yblock2*sizeof(float));

	grav_fft_status = 1;
}
	Convolver::Convolver(unsigned long int size) : _size(size), _fourier_size(size/2+1)
	{
		this->real = (float *) fftwf_malloc(sizeof(float) * this->_size);
		this->fourier = (fftwf_complex *) fftwf_malloc(sizeof(fftwf_complex) * this->_fourier_size);
		this->forward = fftwf_plan_dft_r2c_1d(this->_size, this->real, this->fourier,
                FFTW_MEASURE);
		this->backward = fftwf_plan_dft_c2r_1d(this->_size, this->fourier, this->real,
                FFTW_MEASURE);
	}
void PPFChanneliser::_createFFTWPlan(unsigned nChannels, fftwf_plan& plan)
{
    size_t fftSize = nChannels * sizeof(fftwf_complex);
    fftwf_complex* in  = (fftwf_complex*) fftwf_malloc(fftSize);
    fftwf_complex* out = (fftwf_complex*) fftwf_malloc(fftSize);
    plan = fftwf_plan_dft_1d(_nChannels, in, out, FFTW_FORWARD, FFTW_MEASURE);
    fftwf_free(in);
    fftwf_free(out);
}
Beispiel #15
0
void hcInitTripple(HConvTripple *filter, float *h, int hlen, int sflen, int mflen, int lflen)
{
	int size;
	float *h2 = NULL;
	int h2len;

	// sanity check: minimum impulse response length
	h2len = mflen + 2 * lflen + 1;
	if (hlen < h2len)
	{
		size = sizeof(float) * h2len;
		h2 = (float*)fftwf_malloc(size);
		memset(h2, 0, size);
		size = sizeof(float) * hlen;
		memcpy(h2, h, size);
		h = h2;
		hlen = h2len;
	}

	// processing step counter
	filter->step = 0;

	// number of processing steps per medium audio frame
	filter->maxstep = mflen / sflen;

	// number of samples per medium audio frame
	filter->flen_medium = mflen;

	// number of samples per short audio frame
	filter->flen_short = sflen;

	// input buffer (medium frame)
	size = sizeof(float) * mflen;
	filter->in_medium = (float *)fftwf_malloc(size);
	memset(filter->in_medium, 0, size);

	// output buffer (medium frame)
	size = sizeof(float) * mflen;
	filter->out_medium = (float *)fftwf_malloc(size);
	memset(filter->out_medium, 0, size);

	// convolution filter (short segments)
	size = sizeof(HConvSingle);
	filter->f_short = (HConvSingle *)malloc(size);
	hcInitSingle(filter->f_short, h, mflen, sflen, 1);

	// convolution filter (medium segments)
	size = sizeof(HConvDual);
	filter->f_medium = (HConvDual *)malloc(size);
	hcInitDual(filter->f_medium, &(h[mflen]), hlen - mflen, mflen, lflen);

	if (h2 != NULL)
	{
		fftwf_free(h2);
	}
}
Beispiel #16
0
void SingleParticle2dx::Utilities::FFTCalculator::performBackwardFFT (fft_array2d_type* fourier_data, real_array2d_type* real_data)
{
	size_type nx = fourier_data->shape()[0];
	size_type ny = fourier_data->shape()[1];
	size_type n_part = SingleParticle2dx::ConfigContainer::Instance()->getParticleSize();
	
	SingleParticle2dx::Utilities::FFTPlanContainer* plan_cont = SingleParticle2dx::Utilities::FFTPlanContainer::Instance();
		
	fftwf_complex* in = (fftwf_complex *) fftwf_malloc ( sizeof(fftwf_complex)*nx*ny);
	fftwf_complex* out = (fftwf_complex *) fftwf_malloc ( sizeof(fftwf_complex)*nx*ny);
		
	fftwf_complex* out_pointer = out;
	
	for (size_type i=0; i<nx; i++)
	{
		for (size_type j=0; j<ny; j++)
		{
			out_pointer[j+i*ny][0] = (*fourier_data)[i][j].real();
			out_pointer[j+i*ny][1] = (*fourier_data)[i][j].imag();
		}
	}
	
	fftwf_plan p2_bw;
	real_array2d_type* pow_array;
	
	if( nx == 4*n_part )
	{
		p2_bw = plan_cont->getBW_4_2d();
		pow_array = plan_cont->getLargePowArray2D();
	}
	else
	{
		p2_bw = plan_cont->getBW_2d();
		pow_array = plan_cont->getSmallPowArray2D();
	}
	
//	fftw3_mkl.verbose = 10;
	fftwf_execute_dft(p2_bw, out, in);
	
	fftwf_complex* in_pointer = in;
	value_type power;
	
	for (size_type i=0; i<nx; i++)
	{
		for (size_type j=0; j<ny; j++)
		{
			power = (*pow_array)[i][j];
			//((*real_data)[i][j]) = in.get()[j+i*ny][0] * powf(-1.0,i+j) / n;
			((*real_data)[i][j]) = in_pointer[j+i*ny][0] * power;
		}
	}
	
	fftwf_free(in);
	fftwf_free(out);
}
int temporaryMemFFT::setTemp(long size)
{
	if(size>allocated_)
	{

	    if(allocated_!=0){

#pragma acc exit data delete(temp1_)
#pragma acc exit data delete(temp2_)

#ifndef FFT3D_ACC
#ifdef SINGLE
		fftwf_free(temp1_);
		fftwf_free(temp2_);
#endif
#ifndef SINGLE
		fftw_free(temp1_);
		fftw_free(temp2_);
#endif
#else
		free(temp1_);
		free(temp2_);
#endif
	    }

	    long alocSize;
	    alocSize = 2*size;

#ifndef FFT3D_ACC
#ifdef SINGLE
	    temp1_ = (fftwf_complex *)fftwf_malloc(alocSize*sizeof(fftwf_complex));
	    temp2_ = (fftwf_complex *)fftwf_malloc(alocSize*sizeof(fftwf_complex));
#endif  

#ifndef SINGLE
	    temp1_ = (fftw_complex *)fftw_malloc(alocSize*sizeof(fftw_complex));
	    temp2_ = (fftw_complex *)fftw_malloc(alocSize*sizeof(fftw_complex));
#endif
#else
#ifdef SINGLE
	    temp1_ = (fftwf_complex *)malloc(alocSize*sizeof(fftwf_complex));
	    temp2_ = (fftwf_complex *)malloc(alocSize*sizeof(fftwf_complex));
#endif

#ifndef SINGLE
	    temp1_ = (fftw_complex *)malloc(alocSize*sizeof(fftw_complex));
	    temp2_ = (fftw_complex *)malloc(alocSize*sizeof(fftw_complex));
#endif
#endif
/// maybe add temp2_ if segfault
#pragma acc enter data create(temp1_[0:alocSize][0:2])
#pragma acc enter data create(temp2_[0:alocSize][0:2])
	}
	return 1;
}
Beispiel #18
0
void fft_prepare(PluginData *pd)
{
	gint         w = pd->image_width, h = pd->image_height;
	gint         channel_count = pd->channel_count;
	int          x, y;
	float      **image;
	guchar      *img_pixels;
	float        norm;
	image = pd->image = (float**) malloc(sizeof(float*) * channel_count);
	pd->image_freq = (fftwf_complex**) malloc(sizeof(fftwf_complex*) * channel_count);
  img_pixels = pd->img_pixels = g_new (guchar, w * h * channel_count);
  //allocate an array for each channel
  for (int channel = 0; channel < channel_count; channel ++){
	  image[channel] = (float*) fftwf_malloc(sizeof(float) * w * h);
		pd->image_freq[channel] = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * (w/2+1) * h);
	}
	// printf("Image data occupies %lu MB.\n", (sizeof(float) * w * h * channel_count) >> 20);
	// printf("Frequency data occupies %lu MB.\n", (sizeof(fftwf_complex) * (w/2+1) * h * channel_count) >> 20);
	
	// forward plan
	fftwf_plan plan = fftwf_plan_dft_r2c_2d(pd->image_height, pd->image_width, *image, *pd->image_freq, FFTW_ESTIMATE);
	// inverse plan (to be reused)
	pd->plan = fftwf_plan_dft_c2r_2d(pd->image_height, pd->image_width, *pd->image_freq, *image, FFTW_ESTIMATE);

	// set image region to reading mode
	gimp_pixel_rgn_init (&pd->region, pd->drawable, 0, 0, w, h, FALSE, FALSE);
	gimp_pixel_rgn_get_rect(&pd->region, img_pixels, 0, 0, w, h);
	
	// execute forward FFT once
	int pw = w/2+1; // physical width
	float diagonal = sqrt(h*h + w*w)/2.0;
	norm = 1.0/(w*h);
	for(int channel=0; channel<channel_count; channel++)
	{
		// convert one color channel to float[]
		for(int i=0; i < w*h; i ++)
		{
			 image[channel][i] =  (float) img_pixels[(i)*channel_count + channel] * norm;
		}
		// transform the channel
		fftwf_execute_dft_r2c(plan, image[channel], pd->image_freq[channel]);
		for(int i=0; i < w*h; i ++)
		{
			 image[channel][i] =  (float) img_pixels[(i)*channel_count + channel] * norm;
		}
		// copy the channel again, for preview
		for(int i=0; i < w*h; i ++)
		{
			 image[channel][i] =  (float) img_pixels[(i)*channel_count + channel];
		}
	}
	fftwf_destroy_plan(plan);
}
Beispiel #19
0
void SingleParticle2dx::Utilities::FFTCalculator::performForwardFFT(real_array2d_type* real_data, fft_array2d_type* fourier_data)
{
	size_type nx = fourier_data->shape()[0];
	size_type ny = fourier_data->shape()[1];
	
	size_type n_part = SingleParticle2dx::ConfigContainer::Instance()->getParticleSize();
	
	SingleParticle2dx::Utilities::FFTPlanContainer* plan_cont = SingleParticle2dx::Utilities::FFTPlanContainer::Instance();
	
	fftwf_plan p2_fw;
	real_array2d_type* pow_array;
	
	if( nx == 4*n_part )
	{
		p2_fw = plan_cont->getFW_4_2d();
		pow_array = plan_cont->getLargePowArray2D();
	}
	else
	{
		p2_fw = plan_cont->getFW_2d();
		pow_array = plan_cont->getSmallPowArray2D();
	}
	
	fftwf_complex* in = (fftwf_complex *) fftwf_malloc (sizeof(fftwf_complex)*nx*ny);
	fftwf_complex* out = (fftwf_complex *) fftwf_malloc (sizeof(fftwf_complex)*nx*ny);

	for (size_type i=0; i<nx; i++)
	{
		for (size_type j=0; j<ny; j++)
		{
			in[j+i*ny][0] = ((*real_data)[i][j]) * ((*pow_array)[i][j]);
			in[j+i*ny][1] = 0;
		}
	}
	
	fftwf_execute_dft(p2_fw, in, out);
	
	fft_type value2set(0,0);
	
	for (size_type i=0; i<nx; i++)
	{
		for (size_type j=0; j<ny; j++)
		{
			value2set.real(out[j+i*ny][0]);
			value2set.imag(out[j+i*nx][1]);
			(*fourier_data)[i][j] = value2set;
		}
	}
	
	fftwf_free(in);
	fftwf_free(out);
}
Beispiel #20
0
void SingleParticle2dx::Utilities::FFTCalculator::performForwardFFT(real_array3d_type* real_data, fft_array3d_type* fourier_data)
{
	size_type nx = fourier_data->shape()[0];
	size_type ny = fourier_data->shape()[1];
	size_type nz = fourier_data->shape()[2];
	
	value_type n = getScalingFactor3d (nx, ny, nz);
	
	SingleParticle2dx::Utilities::FFTPlanContainer* plan_cont = SingleParticle2dx::Utilities::FFTPlanContainer::Instance();
	
	fftwf_complex* in = (fftwf_complex *) fftwf_malloc (sizeof(fftwf_complex)*nx*ny*nz);
	fftwf_complex* out = (fftwf_complex *) fftwf_malloc (sizeof(fftwf_complex)*nx*ny*nz);
	
	real_array3d_type* power_array = plan_cont->getPowArray3D();

	value_type power;
	for (size_type i=0; i<nx; i++)
	{
		for (size_type j=0; j<ny; j++)
		{
			for (size_type k=0; k<nz; k++)
			{
				power = (*power_array)[i][j][k];
				//in.get()[k+j*ny+i*nx*ny][0] = ((*real_data)[i][j][k]) * powf(-1,i+j+k) / n;
				in[k+j*ny+i*nx*ny][0] = ((*real_data)[i][j][k]) * power / n;
				in[k+j*ny+i*nx*ny][1] = 0;
			}
		}
	}
	
	fftwf_plan p3_fw = plan_cont->getFW_3d();
	fftwf_execute_dft(p3_fw, in, out);
	
	fft_type value2set(0,0);
	for (size_type i=0; i<nx; i++)
	{
		for (size_type j=0; j<ny; j++)
		{
			for (size_type k=0; k<nz; k++)
			{
				value2set.real(out[k+j*ny+i*nx*ny][0]);
				value2set.imag(out[k+j*ny+i*nx*ny][1]);
				(*fourier_data)[i][j][k] = value2set;
			}
		}
	}
	
	fftwf_free(in);
	fftwf_free(out);
}
Beispiel #21
0
static cfftw_info *cfftw_getplan(int n,int fwd)
{
    cfftw_info *info;
    int logn = ilog2(n);
    if (logn < MINFFT || logn > MAXFFT)
        return (0);
    info = (fwd?cfftw_fwd:cfftw_bwd)+(logn-MINFFT);
    if (!info->plan)
    {
        info->in = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * n);
        info->out = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * n);
        info->plan = fftwf_plan_dft_1d(n, info->in, info->out, fwd?FFTW_FORWARD:FFTW_BACKWARD, FFTW_MEASURE);
    }
    return info;
}
Beispiel #22
0
static rfftw_info *rfftw_getplan(int n,int fwd)
{
    rfftw_info *info;
    int logn = ilog2(n);
    if (logn < MINFFT || logn > MAXFFT)
        return (0);
    info = (fwd?rfftw_fwd:rfftw_bwd)+(logn-MINFFT);
    if (!info->plan)
    {
        info->in = (float*) fftwf_malloc(sizeof(float) * n);
        info->out = (float*) fftwf_malloc(sizeof(float) * n);
        info->plan = fftwf_plan_r2r_1d(n, info->in, info->out, fwd?FFTW_R2HC:FFTW_HC2R, FFTW_MEASURE);
    }
    return info;
}
Beispiel #23
0
static void
fftw_init(struct fftw_state *state, const int logN)
{
	const int N = 1 << logN;
	int i;

	state->in  = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * N);
	state->out = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * N);

	state->p = fftwf_plan_dft_1d(N, state->in, state->out, FFTW_FORWARD, FFTW_MEASURE);

	srandom(0);
	for (i=0; i<N; i++)
		state->in[i] = (float)(random() & 0xfff) / 256.0f;
}
Beispiel #24
0
epicsShareFunc void epicsShareAPI fftw_1d (float *argv1, int *argv2, int *argv3)
{

        float *data = (float *)argv1;
        int n = *(int *)argv2;
        int isign  = *(int *)argv3;

        static int n_prev;
        static fftwf_complex *in, *out;
        static fftwf_plan forward_plan, backward_plan;

        if (n != n_prev) {
           /* Create plans */
           if (n_prev != 0) fftwf_free(in);
           in = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex)*n);
           out = in;
           //printf("fft_test1f: creating plans, n=%d, n_prev=%d\n", n, n_prev);
           n_prev = n;
           forward_plan = fftwf_plan_dft_1d(n, in, out, FFTW_FORWARD, FFTW_MEASURE);
           backward_plan = fftwf_plan_dft_1d(n, in, out, FFTW_BACKWARD, FFTW_MEASURE);
        }
        memcpy(in, data, n*sizeof(fftwf_complex));
        if (isign == -1) fftwf_execute(forward_plan);
        else             fftwf_execute(backward_plan);
        memcpy(data, in, n*sizeof(fftwf_complex));
}
Beispiel #25
0
epicsShareFunc void epicsShareAPI fftw_2d (float *argv1, int *argv2, int *argv3,  int *argv4)
{

        float *data = (float *)argv1;
        int nx  = *(int *)argv2;
        int ny  = *(int *)argv3;
        int isign  = *(int *)argv4;

        static int nx_prev, ny_prev;
        static fftwf_complex *in, *out;
        static fftwf_plan forward_plan, backward_plan;

        if ((nx != nx_prev) || (ny != ny_prev)) {
           /* Create plans */
           if (nx_prev != 0) fftwf_free(in);
           in = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex)*nx*ny);
           out = in;
           //printf("fft_test2f: creating plans, nx=%d, ny=%d, nx_prev=%d, ny_prev=%d\n",
           //       nx, ny, nx_prev, ny_prev);
           nx_prev = nx;
           ny_prev = ny;
           forward_plan = fftwf_plan_dft_2d(ny, nx, in, out, FFTW_FORWARD, FFTW_MEASURE);
           backward_plan = fftwf_plan_dft_2d(ny, nx, in, out, FFTW_BACKWARD, FFTW_MEASURE);
        }
        memcpy(in, data, nx*ny*sizeof(fftwf_complex));
        if (isign == -1) fftwf_execute(forward_plan);
        else             fftwf_execute(backward_plan);
        memcpy(data, in, nx*ny*sizeof(fftwf_complex));
}
Beispiel #26
0
int
padsynth_init(void)
{
    global.padsynth_table_size = -1;
    global.padsynth_outfreqs = NULL;
    global.padsynth_outsamples = NULL;
    global.padsynth_fft_plan = NULL;
    global.padsynth_ifft_plan = NULL;

    /* allocate input FFT buffer */
    global.padsynth_inbuf = (float *)fftwf_malloc(WAVETABLE_POINTS * sizeof(float));
    if (!global.padsynth_inbuf) {
        return 0;
    }

    /* create input FFTW plan */
#ifdef FFTW_VERSION_2
    global.padsynth_fft_plan  = (void *)rfftw_create_plan(WAVETABLE_POINTS,
                                                          FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE);
#else
    global.padsynth_fft_plan  = (void *)fftwf_plan_r2r_1d(WAVETABLE_POINTS,
                                                          global.padsynth_inbuf,
                                                          global.padsynth_inbuf,
                                                          FFTW_R2HC, FFTW_ESTIMATE);
#endif
    if (!global.padsynth_fft_plan) {
        padsynth_fini();
        return 0;
    }

    return 1;
}
Beispiel #27
0
/**
 * Set up the worker task and start an activity.
 *
 * Description:\n
 * 	Sets up the worker task for a new data collection.  The sample buffer
 * 	is allocated if the existing buffer is too small.
 * 	This serves as a "start collection" for the worker.
 */
void
WorkerTask::startActivity(Activity *act)
{
	Assert(act);
	activity = act;
	channel = activity->getChannel();
	Assert(channel);

	lock();
	spectrometer->stopSpectrometry(activity);
	activityId = activity->getActivityId();
	// allocate a working buffer for the sample input; data will be transferred
	// into this buffer before performing the DFB
	// if a smaller buffer has already been allocated, free it
	uint32_t size = channel->getThreshold() * sizeof(ComplexFloat32);
	if (sampleBuf && sampleBufSize < size) {
		fftwf_free(sampleBuf);
		sampleBuf = 0;
	}
	if (!sampleBuf) {
		sampleBuf = static_cast<ComplexFloat32 *> (fftwf_malloc(size));
		sampleBufSize = size;
	}
	Assert(sampleBuf);

	// initialize the spectrometer
	spectrometer->setup(activity);
	unlock();
}
Beispiel #28
0
/* arr_alloc1(): allocate a new one-dimensional hypercomplex array.
 *
 * arguments:
 *  @n1: size of the array.
 *  @n2: unused.
 *  @n3: unused.
 *
 * returns:
 *  pointer to a newly allocated one-dimensional hypercomplex
 *  array structure, or NULL on failure.
 */
arr1 *arr_alloc1 (int n1, int n2, int n3) {
  /* declare required variables:
   *  @a: pointer to the newly allocated structure.
   */
  arr1 *a;

  /* check that the array size is valid. */
  if (n1 < 1) {
    /* if not, output an error message and return null. */
    failf("size '%d' is invalid", n1);
    return NULL;
  }

  /* allocate a new structure pointer. */
  a = (arr1*) malloc(sizeof(arr1));
  if (!a)
    return NULL;

  /* store the array size in the structure. */
  a->n = n1;

  /* allocate a new array of coefficients. */
  a->x = (hx1*) fftwf_malloc(sizeof(hx1) * a->n);
  if (!a->x)
    return NULL;

  /* zero the contents of the array. */
  arr_zero1(a);

  /* return the newly allocated structure pointer. */
  return a;
}
Beispiel #29
0
SpectrometryTask::SpectrometryTask(string name_) :
	QTask(name_, SPECTROMETRY_PRIO), activity(0), CTBufsFilled(0),
			CTBufsEmptied(0), initWave(false)
{
	float twopi, w;
	twopi = 8.0 * atan(1.0);
	ObsData obsData;

#ifdef DO_TIMING_TESTS
	tvE.tv_usec = 0;
	tvL.tv_usec = 0;
#endif

	// allocate the frame array
	int32_t size = FRAME_SAMPLES * sizeof(complexFloat);
	Frame = static_cast<complexFloat *> (fftwf_malloc(size));

	if (!initWave) {
		/* initialize the fake signal array */
		for (int i=0; i<FRAME_SAMPLES; i++) {
			w = (i+0.5)/FRAME_SAMPLES * twopi;
			Wave[i] = sinf(w);
		}
		initWave = true;

		// create the fftw plans
		plan1024 = fftwf_plan_dft_1d(FRAME_SAMPLES, (fftwf_complex *) Frame,
				(fftwf_complex *) Frame, FFTW_FORWARD, FFTW_MEASURE);
		plan512 = fftwf_plan_dft_1d(FRAME_SAMPLES / 2, (fftwf_complex *) Frame,
				(fftwf_complex *) Frame, FFTW_FORWARD, FFTW_MEASURE);
	}
}
Beispiel #30
0
void fft_apply(PluginData *pd)
{
	int w = pd->image_width, h = pd->image_height,
		pw = w/2+1; // physical width
	fftwf_complex *multiplied = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * pw * h);
	float diagonal = sqrt(h*h + w*w)/2.0;
	// save current state of the curve
	curve_copy(&pd->curve_user, &pd->curve_fft);
	for(int channel=0; channel < pd->channel_count; channel++)
	{
		//skip DC value
		multiplied[0][0] = pd->image_freq[channel][0][0];
		multiplied[0][1] = pd->image_freq[channel][0][1];
		// apply convolution
		for (int i=1; i < pw*h; i++){
			float dist = index_to_dist(i, pw, h);
			float coef = curve_get_value(dist_to_graph(dist), &pd->curve_fft);
			multiplied[i][0] = pd->image_freq[channel][i][0] * coef;
			multiplied[i][1] = pd->image_freq[channel][i][1] * coef;
		}
		// apply inverse FFT
		fftwf_execute_dft_c2r(pd->plan, multiplied, pd->image[channel]);
		// pack results for GIMP
		for(int x=0; x < w; x ++)
		{
			for(int y=0; y < h; y ++)
			{
				float v = pd->image[channel][y*w + x];
				pd->img_pixels[(y*w + x)*pd->channel_count + channel] = CLAMPED(v,0,255);
			}
		}
	}
	fftwf_free(multiplied);
}