Exemplo n.º 1
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);
}
 FFTWProxyImplFloat(int n, float* timespace, genfloat2* freqspace)
 {
     forwardPlan = fftwf_plan_dft_r2c_1d(
             n, timespace, (fftwf_complex*)freqspace, FFTW_ESTIMATE);
     inversePlan = fftwf_plan_dft_c2r_1d(
             n, (fftwf_complex*)freqspace, timespace, FFTW_ESTIMATE);
 }
Exemplo n.º 3
0
int main() {
      float start[SIZE];
      int i;
      
      for (i=0; i<SIZE; i++) start[i] = (float)(i+1);
      
      printf("Starting out: ");
      for (i=0; i<SIZE; i++) printf(" %f ", (start[i])); printf("\n");

      _Complex float middle[SIZE/2 + 1];
      
      fftwf_plan plan = fftwf_plan_dft_r2c_1d(SIZE, start, (fftwf_complex*)middle, FFTW_ESTIMATE);
      fftwf_execute(plan);
      fftwf_destroy_plan(plan);

      printf("Done with forward transform: ");
      for (i=0; i< SIZE/2 + 1; i++) printf(" %g+%gi ", __real__ (middle[i]),  __imag__ (middle[i]));
      printf("\n");

      float end[SIZE];

      fftwf_plan plan2 = fftwf_plan_dft_c2r_1d(SIZE, (fftwf_complex*)middle, end, FFTW_ESTIMATE);
      fftwf_execute(plan2);
      fftwf_destroy_plan(plan2);

      printf("Done with reverse transform transform: ");
      for (i=0; i<SIZE; i++) printf(" %fi ", (end[i]));
      printf("\n");
}
Exemplo n.º 4
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();

}
Exemplo n.º 5
0
GOSoundReverbPartition::GOSoundReverbPartition(unsigned size, unsigned cnt, unsigned start_pos) :
	m_PartitionSize(size),
	m_PartitionCount(cnt),
	m_fftwTmpReal(0),
	m_fftwTmpComplex(0),
	m_TimeToFreq(0),
	m_FreqToTime(0),
	m_Input(0),
	m_Output(0),
	m_InputPos(start_pos),
	m_InputStartPos(start_pos),
	m_OutputPos(0),
	m_InputHistory(),
	m_IRData(),
	m_InputHistoryPos(0)
{
	m_fftwTmpReal = new float[m_PartitionSize * 2];
	m_fftwTmpComplex = new fftwf_complex[m_PartitionSize + 1];

	m_TimeToFreq = fftwf_plan_dft_r2c_1d(2 * m_PartitionSize, m_fftwTmpReal, m_fftwTmpComplex, FFTW_ESTIMATE);
	m_FreqToTime = fftwf_plan_dft_c2r_1d(2 * m_PartitionSize, m_fftwTmpComplex, m_fftwTmpReal, FFTW_ESTIMATE);
	assert(m_TimeToFreq);
	assert(m_FreqToTime);

	m_Input = new float[m_PartitionSize];
	m_Output = new float[2 * m_PartitionSize];

	for(unsigned i = 0; i < m_PartitionCount; i++)
		m_InputHistory.push_back(new fftwf_complex[m_PartitionSize + 1]);

	for(unsigned i = 0; i < m_PartitionCount; i++)
		m_IRData.push_back(NULL);

	Reset();
}
PSSinthesis::PSSinthesis(PSAnalysis *obj, const char* wisdomFile) //Construtor
{
	Qcolumn = obj->Qcolumn;
	hopa = obj->hopa;
	N = obj->N;
	omega_true_sobre_fs = &obj->omega_true_sobre_fs;
	Xa_abs = &obj->Xa_abs;
	w = &obj->w;

	first = true;
	hops = new int[Qcolumn];                       fill_n(hops,Qcolumn,hopa);
	ysaida = new double[2*N + 4*(Qcolumn-1)*hopa]; fill_n(ysaida,2*N + 4*(Qcolumn-1)*hopa,0);
	yshift = new double[hopa];                     fill_n(yshift,hopa,0);
	q = fftwf_alloc_real(N);
	fXs = fftwf_alloc_complex(N/2 + 1);
	Xs.zeros(N/2 + 1);
	Phi.zeros(N/2 + 1);
	PhiPrevious.zeros(N/2 + 1);

	if (fftwf_import_wisdom_from_filename(wisdomFile) != 0)
	{
		p2 = fftwf_plan_dft_c2r_1d(N, fXs, q, FFTW_WISDOM_ONLY);
	}
	else
	{
		p2 = NULL;
		printf("PSSinthesis: failed to import wisdom file '%s'\n", wisdomFile);
	}
}
Exemplo n.º 7
0
	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);
	}
Exemplo n.º 8
0
void ifft(float* buffer,
	  float* result,
          int size)
{
  fftwf_plan plan;
  plan = fftwf_plan_dft_c2r_1d(size, (fftwf_complex*) buffer,
			       result,FFTW_ESTIMATE|FFTW_PRESERVE_INPUT);
  fftwf_execute(plan);
  fftwf_destroy_plan(plan);
}
Exemplo n.º 9
0
    // create an instance of the decoder
    //  blocksize is fixed over the lifetime of this object for performance reasons
    decoder_impl(unsigned blocksize=8192): N(blocksize), halfN(blocksize/2) {
#ifdef USE_FFTW3
        // create FFTW buffers
        lt = (float*)fftwf_malloc(sizeof(float)*N);
        rt = (float*)fftwf_malloc(sizeof(float)*N);
        dst = (float*)fftwf_malloc(sizeof(float)*N);
        dftL = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex)*N);
        dftR = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex)*N);
        src = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex)*N);
        loadL = fftwf_plan_dft_r2c_1d(N, lt, dftL,FFTW_MEASURE);
        loadR = fftwf_plan_dft_r2c_1d(N, rt, dftR,FFTW_MEASURE);
        store = fftwf_plan_dft_c2r_1d(N, src, dst,FFTW_MEASURE);    
#else
        // create lavc fft buffers
        lt = (float*)av_malloc(sizeof(FFTSample)*N);
        rt = (float*)av_malloc(sizeof(FFTSample)*N);
        dftL = (FFTComplexArray*)av_malloc(sizeof(FFTComplex)*N*2);
        dftR = (FFTComplexArray*)av_malloc(sizeof(FFTComplex)*N*2);
        src = (FFTComplexArray*)av_malloc(sizeof(FFTComplex)*N*2);
        fftContextForward = (FFTContext*)av_malloc(sizeof(FFTContext));
        memset(fftContextForward, 0, sizeof(FFTContext));
        fftContextReverse = (FFTContext*)av_malloc(sizeof(FFTContext));
        memset(fftContextReverse, 0, sizeof(FFTContext));
        ff_fft_init(fftContextForward, 13, 0);
        ff_fft_init(fftContextReverse, 13, 1);
#endif
        // resize our own buffers
        frontR.resize(N);
        frontL.resize(N);
        avg.resize(N);
        surR.resize(N);
        surL.resize(N);
        trueavg.resize(N);
        xfs.resize(N);
        yfs.resize(N);
        inbuf[0].resize(N);
        inbuf[1].resize(N);
        for (unsigned c=0;c<6;c++) {
            outbuf[c].resize(N);
            filter[c].resize(N);
        }
        sample_rate(48000);
        // generate the window function (square root of hann, b/c it is applied before and after the transform)
        wnd.resize(N);
        for (unsigned k=0;k<N;k++)
            wnd[k] = sqrt(0.5*(1-cos(2*PI*k/N))/N);
        current_buf = 0;
        memset(inbufs, 0, sizeof(inbufs));
        memset(outbufs, 0, sizeof(outbufs));
        // set the default coefficients
        surround_coefficients(0.8165,0.5774);
        phase_mode(0);
        separation(1,1);
        steering_mode(true);
    }
Exemplo n.º 10
0
inline void irfft(CDEVector<float>::Type &X, DEVector<float>::Type &x)
{
  int fftsize = 2*(X.length()-1);

  // calc IFFT
  x.resize(fftsize);
  fftwf_plan p1 = fftwf_plan_dft_c2r_1d(fftsize, 
                          reinterpret_cast<fftwf_complex*>( &X(1) ),
                          &x(1), FFTW_ESTIMATE);
  fftwf_execute(p1);
  fftwf_destroy_plan(p1);
}
Exemplo n.º 11
0
FFTwrapper::FFTwrapper(int fftsize_)
{
    fftsize  = fftsize_;
    time     = new fftw_real[fftsize];
    fft      = new fftwf_complex[fftsize + 1];
    planfftw = fftwf_plan_dft_r2c_1d(fftsize,
                                    time,
                                    fft,
                                    FFTW_ESTIMATE);
    planfftw_inv = fftwf_plan_dft_c2r_1d(fftsize,
                                        fft,
                                        time,
                                        FFTW_ESTIMATE);
}
LV2_Handle PitchShifter::instantiate(const LV2_Descriptor* descriptor, double samplerate, const char* bundle_path, const LV2_Feature* const* features)
{
    PitchShifter *plugin = new PitchShifter();
    
    plugin->nBuffers = 20;
    plugin->Qcolumn = plugin->nBuffers;
    plugin->hopa = TAMANHO_DO_BUFFER;
    plugin->N = plugin->nBuffers*plugin->hopa;
    plugin->cont = 0;
    
    plugin->s = 0;
    plugin->g = 1;    
    
    plugin->Hops = (int*)calloc(plugin->Qcolumn,sizeof(int)); memset(plugin->Hops, plugin->hopa, plugin->Qcolumn );
    plugin->frames = (double*)calloc(plugin->N,sizeof(double));
    plugin->ysaida = (double*)calloc(2*(plugin->N + 2*(plugin->Qcolumn-1)*plugin->hopa),sizeof(double));
    plugin->yshift = (double*)calloc(plugin->hopa,sizeof(double));
    plugin->b = (double**)calloc(plugin->hopa,sizeof(double*));
    
    plugin->frames2 = fftwf_alloc_real(plugin->N);
    plugin->q = fftwf_alloc_real(plugin->N);
    plugin->fXa = fftwf_alloc_complex(plugin->N/2 + 1);
	plugin->fXs = fftwf_alloc_complex(plugin->N/2 + 1);
    
    plugin->Xa.zeros(plugin->N/2 + 1); 
	plugin->Xs.zeros(plugin->N/2 + 1); 
	plugin->XaPrevious.zeros(plugin->N/2 + 1);
	plugin->Xa_arg.zeros(plugin->N/2 + 1);
	plugin->XaPrevious_arg.zeros(plugin->N/2 + 1);
	plugin->Phi.zeros(plugin->N/2 + 1);
	plugin->PhiPrevious.zeros(plugin->N/2 + 1);
    plugin->d_phi.zeros(plugin->N/2 + 1);
	plugin->d_phi_prime.zeros(plugin->N/2 + 1);
	plugin->d_phi_wrapped.zeros(plugin->N/2 + 1);
	plugin->omega_true_sobre_fs.zeros(plugin->N/2 + 1);
	plugin->AUX.zeros(plugin->N/2 + 1);
	plugin->Xa_abs.zeros(plugin->N/2 + 1);
	plugin->w.zeros(plugin->N); hann(plugin->N,&plugin->w);
	plugin->I.zeros(plugin->N/2 + 1); plugin->I = linspace(0, plugin->N/2,plugin->N/2 + 1);
    
    for (int i=1 ; i<= (plugin->nBuffers); i++)
    {
		plugin->b[i-1] = &plugin->frames[(i-1)*plugin->hopa];
	}
	
	plugin->p = fftwf_plan_dft_r2c_1d(plugin->N, plugin->frames2, plugin->fXa, FFTW_ESTIMATE);
	plugin->p2 = fftwf_plan_dft_c2r_1d(plugin->N, plugin->fXs, plugin->q, FFTW_ESTIMATE);
	
    return (LV2_Handle)plugin;
}
Exemplo n.º 13
0
PitchDetection::PitchDetection(uint32_t n_samples, int nBuffers, double SampleRate, const char* wisdomFile) //Constructor
{
	hopa = n_samples;
	N = nBuffers*n_samples;
	fs = SampleRate;

	frames = fftwf_alloc_real(2*N); memset(frames, 0, 2*N );
	b = new float*[hopa];

	for (int i=0 ; i< nBuffers; i++)
	{
		b[i] = &frames[i*hopa];
	}

	q = fftwf_alloc_real(2*N);	
	fXa = fftwf_alloc_complex(N + 1);
	fXs = fftwf_alloc_complex(N + 1);

	Xa.zeros(N + 1);
	Xs.zeros(N + 1);
	R.zeros(N);
	NORM.zeros(N);
	F.zeros(N);
	AUTO.zeros(N);

	if (fftwf_import_wisdom_from_filename(wisdomFile) != 0)
	{
		p  = fftwf_plan_dft_r2c_1d(2*N, frames, fXa, FFTW_WISDOM_ONLY);
		p2 = fftwf_plan_dft_c2r_1d(2*N, fXs, q, FFTW_WISDOM_ONLY);
	}
	else
	{
		p  = fftwf_plan_dft_r2c_1d(2*N, frames, fXa, FFTW_ESTIMATE);
		p2 = fftwf_plan_dft_c2r_1d(2*N, fXs, q, FFTW_ESTIMATE);
		printf("PitchDetection: failed to import wisdom file '%s', using estimate instead\n", wisdomFile);
	}
}
Exemplo n.º 14
0
scfft * scfft_create(size_t fullsize, size_t winsize, SCFFT_WindowFunction wintype,
					 float *indata, float *outdata, SCFFT_Direction forward, SCFFT_Allocator & alloc)
{
	char * chunk = (char*) alloc.alloc(sizeof(scfft) + scfft_trbufsize(fullsize));
	if (!chunk)
		return NULL;

	scfft * f = (scfft*)chunk;
	float *trbuf = (float*)(chunk + sizeof(scfft));

	f->nfull = fullsize;
	f->nwin  =  winsize;
	f->log2nfull = LOG2CEIL(fullsize);
	f->log2nwin  = LOG2CEIL( winsize);
	f->wintype = wintype;
	f->indata  = indata;
	f->outdata = outdata;
	f->trbuf   = trbuf;

	// Buffer is larger than the range of sizes we provide for at startup; we can get ready just-in-time though
	if (fullsize > SC_FFT_MAXSIZE)
		scfft_ensurewindow(f->log2nfull, f->log2nwin, wintype);

#if SC_FFT_FFTW
	if(forward)
		f->plan = fftwf_plan_dft_r2c_1d(fullsize, trbuf, (fftwf_complex*) trbuf, FFTW_ESTIMATE);
	else
		f->plan = fftwf_plan_dft_c2r_1d(fullsize, (fftwf_complex*) trbuf, outdata, FFTW_ESTIMATE);
#endif

	// The scale factors rescale the data to unity gain. The old Green lib did this itself, meaning scalefacs would here be 1...
	if(forward){
#if SC_FFT_VDSP
		f->scalefac = 0.5f;
#else // forward FFTW and Green factor
		f->scalefac = 1.f;
#endif
	} else { // backward FFTW and VDSP factor
#if SC_FFT_GREEN
		f->scalefac = 1.f;
#else  // fftw, vdsp
		f->scalefac = 1.f / fullsize;
#endif
	}

	memset(trbuf, 0, scfft_trbufsize(fullsize));

	return f;
}
Exemplo n.º 15
0
CDecoder::CDecoder(IDecodeResult* _listener)
{
	dump = 0;
	_decodeResultListener = _listener;
	s32CentreFreq=RX_CARRIER_FREQ;
	fSampleRate=SAMPLE_RATE;
	fVCOPhase=0.0F;
	
	nFIRPos=RX_MATCHED_FILTER_SIZE-1;
	nPeakPos=0;
	nNewPeakPos=0;
	nBitPos=0;
	fBitPhasePos=0.0F;
	csLastIQ.fRe = 0.0F;
	csLastIQ.fIm = 0.0F;	
	fEnergyOut=1.0F;
	u32BitAcc=0;
	nBitCount=0;
	nByteCount=0;
	nDownSampleHistIndex=0;
	nDownSampleCount=0;
	_centreBin = 0;

	_isDecoded = FALSE;

	
	_fftInvIn = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * INVERSE_FFT_SIZE);
	_fftInvOut = (float*) fftwf_malloc(sizeof(float) * INVERSE_FFT_SIZE);
	_fftInvPlan = fftwf_plan_dft_c2r_1d(INVERSE_FFT_SIZE, _fftInvIn, _fftInvOut, FFTW_MEASURE);

	for (int i=0;i<RX_SINCOS_SIZE;i++)
	{
		afSin[i]=(FLOAT)sin(i*2.0*PI/RX_SINCOS_SIZE);
		afCos[i]=(FLOAT)cos(i*2.0*PI/RX_SINCOS_SIZE);
	}

	for (int i=0;i<RX_MATCHED_FILTER_SIZE;i++)
	{
		acsHistory[i].fRe=0.0F;
		acsHistory[i].fIm=0.0F;
	}

	for (int i=0;i<SAMPLES_PER_BIT+2;i++)
	{
		afSyncEnergyAvg[i]=0.0F;
	}
}
Exemplo n.º 16
0
/** Instantiate the plugin.
 *
 * This function initialises the plugin. It includes allocating memory
 * and fftw plans and saving them to a datastructure for later
 * reference.
 *
 * @param descriptor argument of unknown significance
 * @param rate the amount of samples per second thius plusin operates at
 * @param bundle_path argument of unknownn significance
 * @param features argument of unknown significance
 *
 * @return an LV2_Handle representation of the datastructure
 */
LV2_Handle instantiate(/*@unused@*/ const LV2_Descriptor *
                       descriptor, /*@unused@*/ double rate,
                       /*@unused@*/ const char *bundle_path,
                       /*@unused@*/ const LV2_Feature* const *features)
{
    Amp *amp = malloc(sizeof(Amp));
    assert(amp != NULL);

    amp->complex_buffer =
        fftwf_malloc(sizeof(fftwf_complex) * COMPLEX_SIZE);
    assert(amp->complex_buffer != NULL);

    amp->kernel_buffer =
        fftwf_malloc(sizeof(fftwf_complex) * COMPLEX_SIZE);
    assert(amp->kernel_buffer != NULL);

    amp->fourier_buffer = fftwf_malloc(sizeof(float) * FOURIER_SIZE);
    assert(amp->fourier_buffer != NULL);

    amp->in_buffer = init_buffer(BUFFER_SIZE, FOURIER_SIZE);
    assert(amp->in_buffer != NULL);

    amp->previous_buffer = malloc(sizeof(float) * FOURIER_SIZE);
    assert(amp->previous_buffer != NULL);

    amp->out_buffer = init_buffer(BUFFER_SIZE, FOURIER_SIZE);
    assert(amp->out_buffer != NULL);

    amp->buffer_index = 0;
    set_inner_product(&(amp->convolve_func));
    amp->forward =
        fftwf_plan_dft_r2c_1d(FOURIER_SIZE, amp->fourier_buffer,
                              amp->complex_buffer, FFTW_ESTIMATE);
    assert(amp->forward != NULL);

    amp->backward =
        fftwf_plan_dft_c2r_1d(FOURIER_SIZE, amp->kernel_buffer,
                              amp->fourier_buffer, FFTW_ESTIMATE);
    assert(amp->backward != NULL);

#ifdef __OPENMP__
    omp_set_num_threads(omp_get_num_procs());
#endif
    return (LV2_Handle) amp;
}
Exemplo n.º 17
0
/* 1-D IFFT */
void FFTW_(ifft)(real *r, real *x, long n)
{
#ifdef USE_FFTW
#if defined(TH_REAL_IS_DOUBLE)
    fftw_complex *in = (fftw_complex*)x;
    fftw_plan p = fftw_plan_dft_c2r_1d(n, in, r, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
#else
    fftwf_complex *in = (fftwf_complex*)x;
    fftwf_plan p = fftwf_plan_dft_c2r_1d(n, in, r, FFTW_ESTIMATE);
    fftwf_execute(p);
    fftwf_destroy_plan(p);
#endif
#else
    THError("ifft : FFTW Library was not found in compile time\n");
#endif
}
Exemplo n.º 18
0
void sf_cosft_init(int n1_in)
/*< initialize >*/ 
{
    n1 = n1_in;
    nt = 2*kiss_fft_next_fast_size(n1-1);
    nw = nt/2+1;
    p  = sf_floatalloc (nt);
    pp = (kiss_fft_cpx*) sf_complexalloc(nw);

#ifdef SF_HAS_FFTW
    cfg = fftwf_plan_dft_r2c_1d(nt, p, (fftwf_complex *) pp,
				FFTW_ESTIMATE);
    icfg = fftwf_plan_dft_c2r_1d(nt, (fftwf_complex *) pp, p,
				 FFTW_ESTIMATE);
#else
    forw = kiss_fftr_alloc(nt,0,NULL,NULL);
    invs = kiss_fftr_alloc(nt,1,NULL,NULL);
#endif
}
Exemplo n.º 19
0
void fftw1d_plan(fftwplan_h *h, int n)
{
    fftwplan_t *tt;
    void *pi; void *po; int nr, nc;
    tt = calloc(1, sizeof(fftwplan_t));

    //1. real/complex
    nr = n; nc=nr/2+1;
    pi = calloc(nr, sizeof(float));
    po = calloc(nc, sizeof(complex float));
    tt->r2c = fftwf_plan_dft_r2c_1d(nr, pi, po, FFTW_PATIENT);
    tt->c2r = fftwf_plan_dft_c2r_1d(nr, po, pi, FFTW_PATIENT);
    free(pi); free(po);

    //2. complex/complex
    pi = calloc(n, sizeof(complex float));
    po = calloc(n, sizeof(complex float));
    tt->fwd = fftwf_plan_dft_1d(n, pi, po, FFTW_FORWARD, FFTW_PATIENT);
    tt->rvs = fftwf_plan_dft_1d(n, pi, po, FFTW_BACKWARD, FFTW_PATIENT);
    free(pi); free(po);

    *h = (fftwplan_h)tt;
}
Exemplo n.º 20
0
int scfft_create(scfft *f, unsigned int fullsize, unsigned int winsize, short wintype, float *indata, float *outdata, float *trbuf, bool forward){
	f->nfull = fullsize;
	f->nwin  =  winsize;
	f->log2nfull = LOG2CEIL(fullsize);
	f->log2nwin  = LOG2CEIL( winsize);
	f->wintype = wintype;
	f->indata  = indata;
	f->outdata = outdata;
	f->trbuf   = trbuf;

	// Buffer is larger than the range of sizes we provide for at startup; we can get ready just-in-time though
	if (fullsize > SC_FFT_MAXSIZE){
		scfft_ensurewindow(f->log2nfull, f->log2nwin, wintype);
	}

	#if SC_FFT_FFTW
		if(forward)
			f->plan = fftwf_plan_dft_r2c_1d(fullsize, trbuf, (fftwf_complex*) trbuf, FFTW_ESTIMATE);
		else
			f->plan = fftwf_plan_dft_c2r_1d(fullsize, (fftwf_complex*) trbuf, outdata, FFTW_ESTIMATE);
	#endif

	// The scale factors rescale the data to unity gain. The old Green lib did this itself, meaning scalefacs would here be 1...
	if(forward){
		#if SC_FFT_VDSP
			f->scalefac = 0.5f;
		#else // forward FFTW factor
			f->scalefac = 1.f;
		#endif
	}else{ // backward FFTW and VDSP factor
		f->scalefac = 1.f / fullsize;
	}

	memset(trbuf, 0, scfft_trbufsize(fullsize));

	return 0;
}
Exemplo n.º 21
0
// Processes a sound file to include the response in the recorder
// track. The response is not interpolated with a successive
// response. A Fast Fourier Transform is used to transfer both the
// dry signal and the impulse response to the frequency domain to
// reduce the complexity (= speed up) of the convolution operation.
// An additional argument specifies whether to optionally fade in
// or fade out the input signal to help with the interpolation
// of successive key-frames.
RecorderTrack* RecorderTrack::Process(SoundFile* const sound_file,
                                      Fade fade) const {

	const RecorderTrack& _this = *this;
	const unsigned int M = this->getLength();
	const unsigned int N = sound_file->sample_length;
	const unsigned int MN = M+N+1;
	const unsigned int MNh = MN/2+1;

	float* a = (float*) fftwf_malloc(sizeof (float) * MN);	
	memset(a,0,sizeof(float)*MN);
	memcpy(a,&_this[0],sizeof(float)*M);

	fftwf_complex* A = (fftwf_complex *) fftwf_malloc (
		sizeof (fftwf_complex) * MNh);
	memset(A,0,sizeof (fftwf_complex) * MNh);
	fftwf_plan fft_plan1 = fftwf_plan_dft_r2c_1d(
		MN,a,A,FFTW_ESTIMATE);
	fftwf_execute(fft_plan1);
	fftwf_free(a);

	float* b = (float*) fftwf_malloc(sizeof (float) * MN);
	memset(b,0,sizeof(float)*MN);
	memcpy(b,sound_file->data,sizeof(float)*N);
	if ( fade != CONSTANT ) {
		float factor = fade == FADE_OUT ? 1.0f : 0.0f;
		float df = (fade == FADE_OUT ? -1.0f : 1.0f) /
			(float)sound_file->sample_length;
		for ( unsigned int i = 0;
			i < sound_file->sample_length;
			++ i ) {
				b[i] *= factor;
				factor += df;
		}
	}
	fftwf_complex* B = (fftwf_complex *) fftwf_malloc (
		sizeof (fftwf_complex) * MNh);
	memset(B,0,sizeof (fftwf_complex) * MNh);
	fftwf_plan fft_plan2 = fftwf_plan_dft_r2c_1d(
		MN,b,B,FFTW_ESTIMATE);	
	fftwf_execute(fft_plan2);
	fftwf_free(b);

	float scale = 1.0f / (float)MN;
	for ( unsigned int i = 0; i < MNh; ++ i ) {
		float re = (A[i][0] * B[i][0] - A[i][1] * B[i][1]) * scale;
		float im = (A[i][0] * B[i][1] + A[i][1] * B[i][0]) * scale;
		A[i][0] = re;
		A[i][1] = im;
	}

	fftwf_free(B);

	float* c = (float*) fftwf_malloc(sizeof (float) * MN);
	memset(c,0,sizeof(float)*MN);
	fftwf_plan inv_fft_plan = fftwf_plan_dft_c2r_1d(
		MN,A,c,FFTW_ESTIMATE);

	fftwf_execute(inv_fft_plan);

	fftwf_free(A);

	RecorderTrack* result = new RecorderTrack();
	RecorderTrack& _result = *result;

	for ( unsigned int i = 0; i < MN; ++ i ) {
		_result[i+sound_file->offset] = c[i];
	}

	fftwf_free(c);

	fftwf_destroy_plan (fft_plan1);
	fftwf_destroy_plan (fft_plan2);
	fftwf_destroy_plan (inv_fft_plan);	

	return result;
}
Exemplo n.º 22
0
/**
  @param hFactor corresponds to the bandwidth factor
 @param dataSrc is the trace of the raw data
 @remarks the FFTW function transform the area such that the area output is the area input multiplied by fftLen. So we have to corret it.
 **/
QMap<double, double> MetropolisVariable::generateHisto(QVector<double>& dataSrc, int fftLen, double hFactor, double tmin, double tmax)
{
    int inputSize = fftLen;
    int outputSize = 2 * (inputSize / 2 + 1);
    
    double sigma = dataStd(dataSrc);
    QMap<double, double> result;
    if (sigma==0) {
        qDebug()<<"MetropolisVariable::generateHisto sigma=0";
        return result;
    }

    double h = hFactor * 1.06 * sigma * pow(dataSrc.size(), -1.f/5.f);
    double a = vector_min_value(dataSrc) - 4.f * h;
    double b = vector_max_value(dataSrc) + 4.f * h;
    double delta = (b - a) / fftLen;
    
    float* input = generateBufferForHisto(dataSrc, fftLen, hFactor);
    float* output = (float*) fftwf_malloc(outputSize * sizeof(float));
    /*
    double areaTot = 0.;
    for(int i=0; i<inputSize; ++i) {
        areaTot += input[i];
    }
    
        qDebug()<<"MetropolisVariable::generateHisto areaTot ="<<areaTot<<" a="<<a<<" b="<<b;
     qDebug()<<areaTot;
     */
    if(input != 0) {
        // ----- FFT -----
        
        fftwf_plan plan_forward = fftwf_plan_dft_r2c_1d(inputSize, input, (fftwf_complex*)output, FFTW_ESTIMATE);
        fftwf_execute(plan_forward);
        
        for(int i=0; i<outputSize/2; ++i) {
            double s = 2.f * M_PI * i / (b-a);
            double factor = expf(-0.5f * s * s * h * h);
            
            output[2*i] *= factor;
            output[2*i + 1] *= factor;
        }
        
        fftwf_plan plan_backward = fftwf_plan_dft_c2r_1d(inputSize, (fftwf_complex*)output, input, FFTW_ESTIMATE);
        fftwf_execute(plan_backward);
        
        // ----- FFT Buffer to result map -----
        /*
        areaTot =0.;
        for(int i=0; i<inputSize; ++i) {
            areaTot += input[i];
        }
        
        //qDebug()<<"MetropolisVariable::generateHisto areaTot ="<<areaTot<<" a="<<a<<" b="<<b;
        //qDebug()<<areaTot/inputSize;
        */
        for(int i=0; i<inputSize; ++i)  {
            double t = a + (double)i * delta;
            if(t >= tmin && t<= tmax) {
                result[t] = input[i];
            }
        }
        fftwf_free(input);
        fftwf_free(output);
        
        result = equal_areas(result, 1.); // normalize the output area du to the fftw and the case (t >= tmin && t<= tmax)
        
    }
    
    return result; // return a map between a and b with a step delta = (b - a) / fftLen;
}
Exemplo n.º 23
0
// new
void *firbank_new(t_symbol *s, short argc, t_atom *argv) {
    
    // scope vars
    t_firbank *x;
    t_symbol* default_buffer;
    int default_filters;
    int default_iomap;
    int default_channel_1;
    int default_channel_2;
    
    int i, j, c;
    
    // instantiate
    x = object_alloc(firbank_class);
    if(!x){
	    return NULL;
    }
    
    // setup defaults
    /*
     x->autosplit = 0;
     x->overlap = 0.5;
     x->overlap_power = 1.0;
     x->rc = 0.9;
     x->template = NULL;
     x->bands = NULL;
     x->iomap = NULL;
     */
    
    x->framesize = 512;
    x->n = 1;
    x->m = 16;
    x->k = 16;
    x->v = 0;
    
    x->filters = NULL;
    x->filter_states = NULL;
    x->input_copy = NULL;
    x->input = NULL;
    x->output = NULL;
    x->w = NULL;
    
    // initialization parsing state
    default_buffer = NULL;
    
    default_iomap = 1;
    default_filters = 1;
    
    default_channel_1 = 0;
    default_channel_2 = 1;
    
    // read arguments
    for(i = 0; i < argc; i++) {
        
        if(argv[i].a_type == A_SYM) {
            
            // look for @n
            if(strcmp(argv[i].a_w.w_sym->s_name, "@n") == 0) {
                i++;
                if(i < argc && argv[i].a_type == A_LONG) {
                    x->n = argv[i].a_w.w_long;
                } else {
                    object_post((t_object *)x, "firbank~: expected int for @n");
                }
            }
            
            // look for @k
            else if(strcmp(argv[i].a_w.w_sym->s_name, "@k") == 0) {
                i++;
                if(i < argc && argv[i].a_type == A_LONG) {
                    x->k = argv[i].a_w.w_long;
                } else {
                    object_post((t_object *)x, "firbank~: expected int for @k");
                }
            }
            
            // look for @m
            else if(strcmp(argv[i].a_w.w_sym->s_name, "@m") == 0) {
                i++;
                if(i < argc && argv[i].a_type == A_LONG) {
                    x->m = argv[i].a_w.w_long;
                } else {
                    object_post((t_object *)x, "firbank~: expected int for @m");
                }
            }
            
            // look for @framesize
            else if(strcmp(argv[i].a_w.w_sym->s_name, "@framesize") == 0) {
                i++;
                if(i < argc && argv[i].a_type == A_LONG) {
                    x->framesize = argv[i].a_w.w_long;
                } else {
                    object_post((t_object *)x, "firbank~: expected int for @framesize");
                }
            }
            
            // look for @buffer
            else if(strcmp(argv[i].a_w.w_sym->s_name, "@buffer") == 0) {
                i++;
                if(i < argc && argv[i].a_type == A_SYM) {
                    default_buffer = argv[i].a_w.w_sym; // _sym_to_buffer(argv[i].a_w.w_sym);
                    //x->autosplit = 0;
                } else {
                    object_post((t_object *)x, "firbank~: expected symbol for @buffer");
                }
            }
            
            // look for @channel
            else if(strcmp(argv[i].a_w.w_sym->s_name, "@channel") == 0) {
                default_channel_1 = -1;
                default_channel_2 = -1;
                
                i++;
                
                if(i < argc && argv[i].a_type == A_LONG) {
                    default_channel_1 = argv[i].a_w.w_long;
                    
                    if(i + 1 < argc && argv[i+1].a_type == A_LONG) {
                        i++;
                        default_channel_2 = argv[i].a_w.w_long;
                    } else {
                        default_channel_2 = -1;
                    }
                } else {
                    object_post((t_object *)x, "firbank~: expected int for @channel");
                }
            }
            
            // look for @filter
            /*
             if(strcmp(argv[i].a_w.w_sym->s_name, "@filter") == 0) {
             // @todo
             }
             */
            
            // look for @mode
            /*
             if(strcmp(argv[i].a_w.w_sym->s_name, "@mode") == 0) {
             i++;
             if(strcmp(argv[i].a_w.w_sym->s_name, "complex") == 0) {
             default_mode = FIRBANK_MODE_COMPLEX;
             } else if(strcmp(argv[i].a_w.w_sym->s_name, "polar") == 0) {
             default_mode = FIRBANK_MODE_POLAR;
             } else if(strcmp(argv[i].a_w.w_sym->s_name, "timedomain") == 0) {
             default_mode = FIRBANK_MODE_POLAR;
             } else {
             object_post((t_object *)x, "firbank~: expected symbol, 'complex', 'polar', or 'timedomain' for @mode");
             }
             }
             */
            
            /*
             // look for @offset
             if(strcmp(argv[i].a_w.w_sym->s_name, "@offset") == 0) {
             // todo
             }
             
             // look for @iomap
             if(strcmp(argv[i].a_w.w_sym->s_name, "@iomap") == 0) {
             
             }
             
             // look for @autosplit
             if(strcmp(argv[i].a_w.w_sym->s_name, "@autosplit") == 0) {
             
             }
             
             // look for @overlap
             if(strcmp(argv[i].a_w.w_sym->s_name, "@overlap") == 0) {
             
             }
             
             // look for @rc
             if(strcmp(argv[i].a_w.w_sym->s_name, "@rc") == 0) {
             
             }
             */
            
        }
        
    }
    
    if(x->m) {
        // post("firbank~: allocating %d filter states, tail %d samples", x->m, x->framesize / 2);
        x->filter_states = (t_fir_state*)malloc(sizeof(t_fir_state)*x->m);
        for(i = 0; i < x->m; i++) {
            x->filter_states[i].tail = (float*)fftwf_malloc(sizeof(float) * x->framesize / 2);
            memset(x->filter_states[i].tail, 0, sizeof(float) * x->framesize / 2);
        }
    }
    
    /*
     if(x->autosplit) {
     // configure...
     object_post((t_object *)x, "firbank~: autosplit mode not supported yet");
     }
     */
    
    // setup filters if not from initialization
    if(default_filters && default_buffer != NULL) {
        // post("firbank~: setting up %d filters with framesize %d, channels (%d, %d)", x->k, x->framesize, default_channel_1, default_channel_2);
        
        x->filters = (t_fir*)malloc(sizeof(t_fir)*x->k);
        
        for(i = 0; i < x->k; i++) {
            x->filters[i].buffer = default_buffer;
            x->filters[i].channel_1 = default_channel_1;
            x->filters[i].channel_2 = default_channel_2;
            x->filters[i].offset = i * x->framesize;
        }
    } else {
        object_post((t_object *)x, "firbank~: no buffer specified");
    }
    
    // setup iomap if not from initialization
    if(default_iomap) {
        // post("firbank~: initializing input-output map...");
        for(i = 0; i < x->n; i++) {
            for(j = 0; j < x->k; j++) {
                c = (j*(x->m/x->k))+i;
                if(c < x->m) {
                    x->filter_states[c].o = c;      // output channel
                    x->filter_states[c].k = j;      // filter
                    x->filter_states[c].i = i;      // input channel
                    // post("firbank~: input: %d, filter %d, output %d", x->filter_states[c].i, x->filter_states[c].k, x->filter_states[c].o);
                }
            }
        }
    }
    
    // setup fftw
    x->x_forward_t = (float*)fftwf_malloc(sizeof(float) * (x->framesize + 2)); // two extra so fftw can work its in-place magic
    x->x_forward_c = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * (x->framesize / 2 + 1));  // n/2+1 complex numbers (dc is first, nyquist last)
    
    x->x_inverse_t = (float*)fftwf_malloc(sizeof(float) * (x->framesize + 2));
    x->x_inverse_c = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * (x->framesize / 2 + 1));  // n/2+1 complex numbers (dc is first, nyquist last)
    
    x->input_copy = (float*)fftwf_malloc(sizeof(float) * (x->framesize / 2) * x->n);
    
    memset(x->x_forward_t, 0, sizeof(float) * (x->framesize));
    memset(x->x_inverse_t, 0, sizeof(float) * (x->framesize));
    
    // post("firbank~: preparing fftw plan...");
    
    x->x_forward = fftwf_plan_dft_r2c_1d(x->framesize,
                                         x->x_forward_t, x->x_forward_c,
                                         FFTW_MEASURE
                                         );
    
    x->x_inverse = fftwf_plan_dft_c2r_1d(x->framesize,
                                         x->x_inverse_c, x->x_inverse_t,
                                         FFTW_MEASURE
                                         );
    
    // post("firbank~: done.");
    
    x->w = (t_int**)malloc(sizeof(t_int*) * (x->n + x->m + 2));
    x->input = (float**)malloc(sizeof(float*) * x->n);
    x->output = (float**)malloc(sizeof(float*) * x->m);
    
    // don't share inlet and outlet buffers
    // this doesn't work!
    // x->x_obj.z_misc = Z_NO_INPLACE;
    
    // allocate inlets
    dsp_setup((t_pxobject *)x, x->n);
    
    // allocate outlets
    for(i = 0; i < x->m; i++) {
        outlet_new((t_object *)x, "signal");	// type of outlet: "signal"
    }
    
    return (x);
    
}
Exemplo n.º 24
0
int main()
{

    if (FILE* wisdomfile = fopen("wisdom.wis","r"))
    {
        fftwf_import_wisdom_from_file(wisdomfile);
        fclose(wisdomfile);
    }
 

    const int N[] = {32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072};
    const int sizeN = 13;
    int cfft[sizeN];
    int nfft[sizeN];
    double t;
    double tavg;
    double tfprev;
    double tbprev;

    for (int l = 0; l < sizeN; l++)
    {
        cfft[l] = N[l]+1;
        nfft[l] = N[l]*2;
    }

    FILE *fp = fopen("fftw.csv","w");

    for (int l = 0; l < sizeN; l++)
    {
        printf("\n\nTiming N = %u, cfft = %u\n",N[l],cfft[l]);
        float *a = new float[nfft[l]];
        fftwf_complex *b = (fftwf_complex*)fftwf_malloc(cfft[l]*sizeof(fftwf_complex));

        fftwf_plan fwd;
        fftwf_plan bck;

        int trial;
        float temp;

        fprintf(fp,"%u, ",N[l]);

        //ESTIMATE --------------- ---------------
        temp = (float)get_time();
        for (int i = 0; i < nfft[i]; i++)
            a[i] = temp;
        float* bptr = (float*)b;
        for (int i = 0; i < 2*cfft[i]; i++)
            bptr[i] = temp;

        printf("ESTIMATE: \n");
        fwd = fftwf_plan_dft_r2c_1d(nfft[l],a,b,FFTW_ESTIMATE);
        bck = fftwf_plan_dft_c2r_1d(nfft[l],b,a,FFTW_ESTIMATE);

        t = 0;
        for (trial = 0; trial < MAX_TRIALS; trial++)
        {

            if (t > MAX_TIME)
                break;

            t -= get_time();
            fftwf_execute(fwd);
            t += get_time();
        }
        tavg = t/trial;
        printf("FWD trials: %u, total time: %5g, avg time: %5g\n",trial,t,tavg);
        fprintf(fp,"%g, ",tavg);
        tfprev = tavg;

        t = 0;
        for (trial = 0; trial < MAX_TRIALS; trial++)
        {

            if (t > MAX_TIME)
                break;

            t -= get_time();
            fftwf_execute(bck);
            t += get_time();
        }
        tavg = t/trial;
        printf("BCK trials: %u, total time: %5g, avg time: %5g\n",trial,t,tavg);
        fprintf(fp,"%g, ",tavg);
        tbprev = tavg;

        fftwf_destroy_plan(fwd);
        fftwf_destroy_plan(bck);

        //MEASURE --------------- ---------------
        temp = (float)get_time();
        for (int i = 0; i < nfft[i]; i++)
            a[i] = temp;
        bptr = (float*)b;
        for (int i = 0; i < 2*cfft[i]; i++)
            bptr[i] = temp;

        printf("MEASURE: \n");
        fwd = fftwf_plan_dft_r2c_1d(nfft[l],a,b,FFTW_MEASURE);
        bck = fftwf_plan_dft_c2r_1d(nfft[l],b,a,FFTW_MEASURE);

        t = 0;
        for (trial = 0; trial < MAX_TRIALS; trial++)
        {

            if (t > MAX_TIME)
                break;

            t -= get_time();
            fftwf_execute(fwd);
            t += get_time();
        }
        tavg = t/trial;
        printf("FWD trials: %u, total time: %5g, avg time: %5g (%5g,%5g)\n",trial,t,tavg,100*tavg/tfprev,100*tfprev/tavg);
        fprintf(fp,"%g, ",tavg);
        tfprev = tavg;

        t = 0;
        for (trial = 0; trial < MAX_TRIALS; trial++)
        {

            if (t > MAX_TIME)
                break;

            t -= get_time();
            fftwf_execute(bck);
            t += get_time();
        }
        tavg = t/trial;
        printf("BCK trials: %u, total time: %5g, avg time: %5g (%5g,%5g)\n",trial,t,tavg,100*tavg/tbprev,100*tbprev/tavg);
        fprintf(fp,"%g, ",tavg);
        tbprev = tavg;

        fftwf_destroy_plan(fwd);
        fftwf_destroy_plan(bck);

        //PATIENT --------------- ---------------
        temp = (float)get_time();
        for (int i = 0; i < nfft[i]; i++)
            a[i] = temp;
        bptr = (float*)b;
        for (int i = 0; i < 2*cfft[i]; i++)
            bptr[i] = temp;

        printf("PATIENT: \n");
        fwd = fftwf_plan_dft_r2c_1d(nfft[l],a,b,FFTW_PATIENT);
        bck = fftwf_plan_dft_c2r_1d(nfft[l],b,a,FFTW_PATIENT);

        t = 0;
        for (trial = 0; trial < MAX_TRIALS; trial++)
        {

            if (t > MAX_TIME)
                break;

            t -= get_time();
            fftwf_execute(fwd);
            t += get_time();
        }
        tavg = t/trial;
        printf("FWD trials: %u, total time: %5g, avg time: %5g (%5g,%5g)\n",trial,t,tavg,100*tavg/tfprev,100*tfprev/tavg);
        fprintf(fp,"%g, ",tavg);

        t = 0;
        for (trial = 0; trial < MAX_TRIALS; trial++)
        {

            if (t > MAX_TIME)
                break;

            t -= get_time();
            fftwf_execute(bck);
            t += get_time();
        }
        tavg = t/trial;
        printf("BCK trials: %u, total time: %5g, avg time: %5g (%5g,%5g)\n",trial,t,tavg,100*tavg/tbprev,100*tbprev/tavg);
        fprintf(fp,"%g\n",tavg);

        fftwf_destroy_plan(fwd);
        fftwf_destroy_plan(bck);

        delete[] a;
        fftwf_free(b);

    }

    if (FILE* wisdomfile = fopen("wisdom.wis","w"))
    {
        fftwf_export_wisdom_to_file(wisdomfile);
        fclose(wisdomfile);
    }
    

    fclose(fp);

    return 0;

}
Exemplo n.º 25
0
void hcInitSingle(HConvSingle *filter, float *h, int hlen, int flen, int steps)
{
	int i, j, size, num, pos;
	float gain;

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

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

	// current frame index
	filter->mixpos = 0;

	// number of samples per audio frame
	filter->framelength = flen;

	// DFT buffer (time domain)
	size = sizeof(float) * 2 * flen;
	filter->dft_time = (float *)fftwf_malloc(size);

	// DFT buffer (frequency domain)
	size = sizeof(fftwf_complex) * (flen + 1);
	filter->dft_freq = (fftwf_complex*)fftwf_malloc(size);

	// input buffer (frequency domain)
	size = sizeof(float) * (flen + 1);
	filter->in_freq_real = (float*)fftwf_malloc(size);
	filter->in_freq_imag = (float*)fftwf_malloc(size);

	// number of filter segments
	filter->num_filterbuf = (hlen + flen - 1) / flen;

	// processing tasks per step
	size = sizeof(int) * (steps + 1);
	filter->steptask = (int *)malloc(size);
	num = filter->num_filterbuf / steps;
	for (i = 0; i <= steps; i++)
		filter->steptask[i] = i * num;
	if (filter->steptask[1] == 0)
		pos = 1;
	else
		pos = 2;
	num = filter->num_filterbuf % steps;
	for (j = pos; j < pos + num; j++)
	{
		for (i = j; i <= steps; i++)
			filter->steptask[i]++;
	}

	// filter segments (frequency domain)
	size = sizeof(float*) * filter->num_filterbuf;
	filter->filterbuf_freq_real = (float**)fftwf_malloc(size);
	filter->filterbuf_freq_imag = (float**)fftwf_malloc(size);
	for (i = 0; i < filter->num_filterbuf; i++)
	{
		size = sizeof(float) * (flen + 1);
		filter->filterbuf_freq_real[i] = (float*)fftwf_malloc(size);
		filter->filterbuf_freq_imag[i] = (float*)fftwf_malloc(size);
	}

	// number of mixing segments
	filter->num_mixbuf = filter->num_filterbuf + 1;

	// mixing segments (frequency domain)
	size = sizeof(float*) * filter->num_mixbuf;
	filter->mixbuf_freq_real = (float**)fftwf_malloc(size);
	filter->mixbuf_freq_imag = (float**)fftwf_malloc(size);
	for (i = 0; i < filter->num_mixbuf; i++)
	{
		size = sizeof(float) * (flen + 1);
		filter->mixbuf_freq_real[i] = (float*)fftwf_malloc(size);
		filter->mixbuf_freq_imag[i] = (float*)fftwf_malloc(size);
		memset(filter->mixbuf_freq_real[i], 0, size);
		memset(filter->mixbuf_freq_imag[i], 0, size);
	}

	// history buffer (time domain)
	size = sizeof(float) * flen;
	filter->history_time = (float *)fftwf_malloc(size);
	memset(filter->history_time, 0, size);

	// FFT transformation plan
	filter->fft = fftwf_plan_dft_r2c_1d(2 * flen, filter->dft_time, filter->dft_freq, FFTW_ESTIMATE|FFTW_PRESERVE_INPUT);

	// IFFT transformation plan
	filter->ifft = fftwf_plan_dft_c2r_1d(2 * flen, filter->dft_freq, filter->dft_time, FFTW_ESTIMATE|FFTW_PRESERVE_INPUT);

	// generate filter segments
	gain = 0.5f / flen;
	size = sizeof(float) * 2 * flen;
	memset(filter->dft_time, 0, size);
	for (i = 0; i < filter->num_filterbuf - 1; i++)
	{
		for (j = 0; j < flen; j++)
			filter->dft_time[j] = gain * h[i * flen + j];
		fftwf_execute(filter->fft);
		for (j = 0; j < flen + 1; j++)
		{
			filter->filterbuf_freq_real[i][j] = filter->dft_freq[j][0];
			filter->filterbuf_freq_imag[i][j] = filter->dft_freq[j][1];
		}
	}
	for (j = 0; j < hlen - i * flen; j++)
		filter->dft_time[j] = gain * h[i * flen + j];
	size = sizeof(float) * ((i + 1) * flen - hlen);
	memset(&(filter->dft_time[hlen - i * flen]), 0, size);
	fftwf_execute(filter->fft);
	for (j = 0; j < flen + 1; j++)
	{
		filter->filterbuf_freq_real[i][j] = filter->dft_freq[j][0];
		filter->filterbuf_freq_imag[i][j] = filter->dft_freq[j][1];
	}
}
Exemplo n.º 26
0
int main()
{
	unsigned overlap = (fftlen - chunklen) / 2;

	// Create data
	float *input = (float *) malloc(nsamp * ndms * sizeof(float));

    // LOAD FILE: FOR TESTING ONLY
//	FILE *fp = fopen("/home/lessju/Code/MDSM/src/prototypes/TestingCCode.dat", "rb");
//	printf("Read: %ld\n", fread(input, sizeof(float), nsamp, fp));
//	fclose(fp);

	// Initialise templating
	unsigned numDownFacts;
	for(numDownFacts = 0; numDownFacts < 12; numDownFacts++)
		if (downfactors[numDownFacts] > maxDownfact)
			break;

	// Allocate kernels
	fftwf_complex **kernels = (fftwf_complex **) malloc(numDownFacts * sizeof(fftwf_complex *));
	for(unsigned i = 0; i < numDownFacts; i++)
		kernels[i] = (fftwf_complex *) fftwf_malloc(fftlen / 2 * sizeof(fftwf_complex));

	// Create kernels
	for(unsigned i = 0; i < numDownFacts; i++)
		createFFTKernel(kernels[i], downfactors[i], fftlen);

	// Start timing
	struct timeval start, end;
	long mtime, seconds, useconds;
	gettimeofday(&start, NULL);

	// Set number of OpenMP threads
	omp_set_num_threads(threads);

	// Create candidate container
	std::vector<Candidate> **candidates = (std::vector<Candidate> **) malloc(threads * sizeof(std::vector<Candidate> *));

	unsigned nchunks = nsamp / chunklen;

	#pragma omp parallel \
		shared(kernels, input, ndms, nsamp, fftlen, chunklen, numDownFacts, tsamp, \
			   overlap, downfactors, threshold, nchunks, candidates)
	{
		// Get thread details
		unsigned numThreads = omp_get_num_threads();
		unsigned threadId = omp_get_thread_num();

		// Allocate memory to be used in processing
		candidates[threadId] = new std::vector<Candidate>();

        float *chunk = (float *) fftwf_malloc(fftlen * sizeof(float)); // Store input chunk
		fftwf_complex *fftChunk = (fftwf_complex *)
                fftwf_malloc(fftlen / 2 * sizeof(fftwf_complex));      // Store FFT'ed input chunk
        fftwf_complex *convolvedChunk = (fftwf_complex *)
                fftwf_malloc(fftlen / 2 * sizeof(fftwf_complex));      // Store FFT'ed, convolved input chunk
        InitialCandidate *initialCands = (InitialCandidate *)
                malloc(fftlen * sizeof(InitialCandidate));             // Store initial Candidate list

		// Create FFTW plans (these calls are note thread safe, place in critical section)
		fftwf_plan chunkPlan, convPlan;
		#pragma omp critical
		{
			chunkPlan = fftwf_plan_dft_r2c_1d(fftlen, chunk, fftChunk, FFTW_ESTIMATE);
		    convPlan  = fftwf_plan_dft_c2r_1d(fftlen, convolvedChunk, chunk, FFTW_ESTIMATE) ;
		}

		// Process all DM buffer associated with this thread
		for(unsigned j = 0; j < ndms / numThreads; j++)
		{
			unsigned d = ndms / numThreads * threadId + j;

			std::vector<Candidate> dmCandidates;

			// Process all data chunks
			for (unsigned c = 0; c < nchunks; c++)
			{
				int beg = d * nsamp + c * chunklen - overlap;
				if (c == 0)                // First chunk, we need to insert 0s at the beginning
				{
					memset(chunk, 0, overlap * sizeof(float));
                    memcpy(chunk + overlap, input, (fftlen - overlap) * sizeof(float));
				}
				else if (c == nchunks - 1) // Last chunk, insert 0s at the end
				{
					memset(chunk + fftlen - overlap, 0, overlap * sizeof(float));
					memcpy(chunk, input + beg, (fftlen - overlap) * sizeof(float));
				}
				else
					memcpy(chunk, input + beg, fftlen * sizeof(float));

				// Search non-downsampled data first
				for(unsigned i = overlap; i < chunklen; i++)
					if (chunk[i] >= threshold)
					{
						candidate newCand = { d, chunk[i], 25, c*chunklen+i, 1 };
						dmCandidates.push_back(newCand);
					}

				// FFT current chunk
				fftwf_execute(chunkPlan);

				// Loop over all downfactor levels
				for(unsigned s = 0; s < numDownFacts; s++)
				{
                    // Reset inital Candidate List
                    memset(initialCands, 0, fftlen * sizeof(InitialCandidate));

					// Perform convolution
					convolve(fftChunk, kernels[s], convolvedChunk, chunk, fftlen, convPlan);

					// Threshold results and build preliminary candidate list
                    unsigned numCands = 0;
					for(unsigned i = overlap; i < chunklen; i++)
					{
						if (chunk[i] >= threshold)
                        {
						//	printf("We have something %d %d \n", c, s);
						    initialCands[numCands].bin = i;
						    initialCands[numCands].value = chunk[i];
                            numCands++;
                        }
					}

                    if (numCands != 0)
                    {
                        // Prune candidate list
                        pruneRelated(initialCands, downfactors[s], numCands);

                        // Store candidate list
                        for(unsigned k = 0; k < numCands; k++)
							if (initialCands[k].value != 0)
							{
                                Candidate newCand = { d, initialCands[j].value, 5, c * chunklen + k, downfactors[s] };
                                dmCandidates.push_back(newCand);
                            }
                    }
				}
			}

			// Remove redundate candidates across downsampling levels
			if (dmCandidates.size() > 0)
			{
				char *mask = (char *) malloc(dmCandidates.size() * sizeof(char));
		        pruneRelatedDownfactors(dmCandidates, mask, numDownFacts);

	            // Append to final candidate list
	            for(j = 0; j < dmCandidates.size(); j++)
	                if (mask[j])
	                    candidates[threadId] -> push_back(dmCandidates[j]);

				free(mask);
			}
		}

		free(convolvedChunk);
		free(fftChunk);
		free(chunk);
	}

    gettimeofday(&end, NULL);
    seconds  = end.tv_sec  - start.tv_sec;
    useconds = end.tv_usec - start.tv_usec;

    mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
	printf("Processed everything in %ld ms\n", mtime);

	// Now write everything to disk...
	FILE *fp2 = fopen("output.dat", "w");
	for(unsigned i = 0; i < threads; i++)
		for(unsigned j = 0; j < candidates[i] -> size(); j++)
		{
			Candidate cand = candidates[i] -> at(j);
			fprintf(fp2, "%f,%f,%f,%ld,%d\n", cand.dm, cand.value, cand.time, cand.bin, cand.downfact);
		}
	fflush(fp2);
	fclose(fp2);
}
Exemplo n.º 27
0
void benchmark_ffts(int N, int cplx) {
  int Nfloat = (cplx ? N*2 : N);
  int Nbytes = Nfloat * sizeof(float);
  float *X = pffft_aligned_malloc(Nbytes), *Y = pffft_aligned_malloc(Nbytes), *Z = pffft_aligned_malloc(Nbytes);

  double t0, t1, flops;

  int k;
  int max_iter = 5120000/N*4;
#ifdef __arm__
  max_iter /= 4;
#endif
  int iter;

  for (k = 0; k < Nfloat; ++k) {
    X[k] = 0; //sqrtf(k+1);
  }

  // FFTPack benchmark
  {
    float *wrk = malloc(2*Nbytes + 15*sizeof(float));
    int max_iter_ = max_iter/pffft_simd_size(); if (max_iter_ == 0) max_iter_ = 1;
    if (cplx) cffti(N, wrk);
    else      rffti(N, wrk);
    t0 = uclock_sec();  
    
    for (iter = 0; iter < max_iter_; ++iter) {
      if (cplx) {
        cfftf(N, X, wrk);
        cfftb(N, X, wrk);
      } else {
        rfftf(N, X, wrk);
        rfftb(N, X, wrk);
      }
    }
    t1 = uclock_sec();
    free(wrk);
    
    flops = (max_iter_*2) * ((cplx ? 5 : 2.5)*N*log((double)N)/M_LN2); // see http://www.fftw.org/speed/method.html
    show_output("FFTPack", N, cplx, flops, t0, t1, max_iter_);
  }

#ifdef HAVE_VECLIB
  int log2N = (int)(log(N)/log(2) + 0.5f);
  if (N == (1<<log2N)) {
    FFTSetup setup;

    setup = vDSP_create_fftsetup(log2N, FFT_RADIX2);
    DSPSplitComplex zsamples;
    zsamples.realp = &X[0];
    zsamples.imagp = &X[Nfloat/2];
    t0 = uclock_sec();  
    for (iter = 0; iter < max_iter; ++iter) {
      if (cplx) {
        vDSP_fft_zip(setup, &zsamples, 1, log2N, kFFTDirection_Forward);
        vDSP_fft_zip(setup, &zsamples, 1, log2N, kFFTDirection_Inverse);
      } else {
        vDSP_fft_zrip(setup, &zsamples, 1, log2N, kFFTDirection_Forward); 
        vDSP_fft_zrip(setup, &zsamples, 1, log2N, kFFTDirection_Inverse);
      }
    }
    t1 = uclock_sec();
    vDSP_destroy_fftsetup(setup);

    flops = (max_iter*2) * ((cplx ? 5 : 2.5)*N*log((double)N)/M_LN2); // see http://www.fftw.org/speed/method.html
    show_output("vDSP", N, cplx, flops, t0, t1, max_iter);
  } else {
    show_output("vDSP", N, cplx, -1, -1, -1, -1);
  }
#endif
  
#ifdef HAVE_FFTW
  {
    fftwf_plan planf, planb;
    fftw_complex *in = (fftw_complex*) fftwf_malloc(sizeof(fftw_complex) * N);
    fftw_complex *out = (fftw_complex*) fftwf_malloc(sizeof(fftw_complex) * N);
    memset(in, 0, sizeof(fftw_complex) * N);
    int flags = (N < 40000 ? FFTW_MEASURE : FFTW_ESTIMATE);  // measure takes a lot of time on largest ffts
    //int flags = FFTW_ESTIMATE;
    if (cplx) {
      planf = fftwf_plan_dft_1d(N, (fftwf_complex*)in, (fftwf_complex*)out, FFTW_FORWARD, flags);
      planb = fftwf_plan_dft_1d(N, (fftwf_complex*)in, (fftwf_complex*)out, FFTW_BACKWARD, flags);
    } else {
      planf = fftwf_plan_dft_r2c_1d(N, (float*)in, (fftwf_complex*)out, flags);
      planb = fftwf_plan_dft_c2r_1d(N, (fftwf_complex*)in, (float*)out, flags);
    }

    t0 = uclock_sec();  
    for (iter = 0; iter < max_iter; ++iter) {
      fftwf_execute(planf);
      fftwf_execute(planb);
    }
    t1 = uclock_sec();

    fftwf_destroy_plan(planf);
    fftwf_destroy_plan(planb);
    fftwf_free(in); fftwf_free(out);

    flops = (max_iter*2) * ((cplx ? 5 : 2.5)*N*log((double)N)/M_LN2); // see http://www.fftw.org/speed/method.html
    show_output((flags == FFTW_MEASURE ? "FFTW (meas.)" : " FFTW (estim)"), N, cplx, flops, t0, t1, max_iter);
  }
#endif  

  // PFFFT benchmark
  {
    PFFFT_Setup *s = pffft_new_setup(N, cplx ? PFFFT_COMPLEX : PFFFT_REAL);
    if (s) {
      t0 = uclock_sec();  
      for (iter = 0; iter < max_iter; ++iter) {
        pffft_transform(s, X, Z, Y, PFFFT_FORWARD);
        pffft_transform(s, X, Z, Y, PFFFT_BACKWARD);
      }
      t1 = uclock_sec();
      pffft_destroy_setup(s);
    
      flops = (max_iter*2) * ((cplx ? 5 : 2.5)*N*log((double)N)/M_LN2); // see http://www.fftw.org/speed/method.html
      show_output("PFFFT", N, cplx, flops, t0, t1, max_iter);
    }
  }

  if (!array_output_format) {
    printf("--\n");
  }

  pffft_aligned_free(X);
  pffft_aligned_free(Y);
  pffft_aligned_free(Z);
}
Exemplo n.º 28
0
int main(int argc, char* argv[])
{
    map4 str;
    bool verb;
    int i1,i2, n1,n2,n3, nw, nx,ny,nv, ix,iy,iv;
    float d1,o1,d2,o2, eps, w,x,y, v0,v2,v,dv, dx,dy, t, x0,y0, dw;
    float *trace, *strace, *t2;
    sf_complex *ctrace, *ctrace2, shift;
    sf_file in, out;

#ifdef SF_HAS_FFTW
    fftwf_plan forw, invs;
#else
    kiss_fftr_cfg forw, invs;
#endif

    sf_init (argc,argv);
    in = sf_input("in");
    out = sf_output("out");

    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&nx)) sf_error("No n2= in input");
    if (!sf_histint(in,"n3",&ny)) sf_error("No n3= in input");

    if (!sf_getfloat("eps",&eps)) eps=0.01; /* regularization */
    if (!sf_getint("pad",&n2)) n2=n1; /* padding for stretch */
    if (!sf_getint("pad2",&n3)) n3=2*kiss_fft_next_fast_size((n2+1)/2);
    /* padding for FFT */

    if (!sf_getbool("verb",&verb)) verb=true;
    /* verbosity flag */

    nw = n3/2+1;

    strace = sf_floatalloc(n3);
    ctrace  = sf_complexalloc(nw);
    ctrace2 = sf_complexalloc(nw);

#ifdef SF_HAS_FFTW
    forw = fftwf_plan_dft_r2c_1d(n3, strace, (fftwf_complex *) ctrace,
				 FFTW_ESTIMATE);
    invs = fftwf_plan_dft_c2r_1d(n3, (fftwf_complex *) ctrace2, strace,
				 FFTW_ESTIMATE);
#else
    forw = kiss_fftr_alloc(n3,0,NULL,NULL);
    invs = kiss_fftr_alloc(n3,1,NULL,NULL);
#endif
    if (NULL == forw || NULL == invs) sf_error("FFT allocation error");

    if (!sf_histfloat(in,"o1",&o1)) o1=0.;  
    o2 = o1*o1;

    if(!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input");
    d2 = o1+(n1-1)*d1;
    d2 = (d2*d2 - o2)/(n2-1);
    dw = 16*SF_PI/(d2*n3); /* 2pi * 8 */

    if (!sf_getint("nv",&nv)) sf_error("Need nv=");
    /* velocity steps */
    if (!sf_getfloat("dv",&dv)) sf_error("Need dv=");
    /* velocity step size */
    if (!sf_getfloat("v0",&v0) && 
	!sf_histfloat(in,"v0",&v0)) sf_error("Need v0=");
    /*( v0 starting velocity )*/

    if(!sf_histfloat(in,"d2",&dx)) sf_error("No d2= in input");
    if(!sf_histfloat(in,"o2",&x0)) x0=0.;

    if(!sf_histfloat(in,"d3",&dy)) sf_error("No d3= in input");
    if(!sf_histfloat(in,"o3",&y0)) y0=0.;

    sf_putfloat(out,"o2",v0+dv);
    sf_putfloat(out,"d2",dv);
    sf_putint(out,"n2",nv);

    sf_putstring(out,"label2","Velocity");

    sf_shiftdim(in, out, 2);

    dx *= 2.*SF_PI;
    x0 *= 2.*SF_PI;

    dy *= 2.*SF_PI;
    y0 *= 2.*SF_PI;

    trace = sf_floatalloc(n1);
    t2 = sf_floatalloc(n2);

    str = stretch4_init (n1, o1, d1, n2, eps);

    for (i2=0; i2 < n2; i2++) {
	t = o2+i2*d2;
	t2[i2] = sqrtf(t);
    }    

    stretch4_define (str,t2);

    for (iy=0; iy < ny; iy++) {
	if (verb) sf_warning("wavenumber %d of %d;", iy+1,ny);

	y = y0+iy*dy; 
	y *= y * 0.5;

	for (ix=0; ix < nx; ix++) {
	    x = x0+ix*dx; 
	    x *= x * 0.5;

	    x += y;

	    sf_floatread(trace,n1,in);
	    for (i1=0; i1 < n1; i1++) {
		trace[i1] /= n1;
	    }
	    stretch4_invert (false,str,strace,trace);
	    for (i2=n2; i2 < n3; i2++) {
		strace[i2] = 0.;
	    }

#ifdef SF_HAS_FFTW
	    fftwf_execute(forw);
#else
	    kiss_fftr(forw,strace, (kiss_fft_cpx *) ctrace);
#endif

	    for (iv=0; iv < nv; iv++) {
		v = v0 + (iv+1)*dv;
		v2 = x * ((v0*v0) - (v*v));
		
		ctrace2[0] = sf_cmplx(0.0f,0.0f); /* dc */
		
		for (i2=1; i2 < nw; i2++) {
		    w = i2*dw;
		    w = v2/w;
		    
		    shift = sf_cmplx(cosf(w),sinf(w));
		    
#ifdef SF_HAS_COMPLEX_H
		    ctrace2[i2] = ctrace[i2] * shift;
#else
		    ctrace2[i2] = sf_cmul(ctrace[i2],shift);
#endif
		} /* w */

#ifdef SF_HAS_FFTW
		fftwf_execute(invs);
#else
		kiss_fftri(invs,(const kiss_fft_cpx *) ctrace2, strace);
#endif
		stretch4_apply(false,str,strace,trace);
		sf_floatwrite (trace,n1,out);
	    } /* v  */
	} /* x */
    } /* y */
    if (verb) sf_warning(".");

    exit (0);
}
void PitchShifter::run(LV2_Handle instance, uint32_t n_samples)
{
    PitchShifter *plugin;
    plugin = (PitchShifter *) instance;

    float media = 0;
    
    for (uint32_t i=1; i<n_samples; i++)
    {
		media = media + abs(plugin->in[i-1]);
	}
	
	if (media == 0)
	{
		for (uint32_t i=1; i<n_samples; i++)
		{
			plugin->out_1[i-1] = 0;
		}
	}
	else
	{
		
    int hops;
    
    double g_before = plugin->g;
    plugin->g = pow(10, (float)(*(plugin->gain))/20.0);
    plugin->s = (double)(*(plugin->step));
    hops = round(plugin->hopa*(pow(2,(plugin->s/12))));
    
	for (int k=1; k<= plugin->Qcolumn-1; k++)
    {
		plugin->Hops[k-1] = plugin->Hops[k];
	}
    
    plugin->Hops[plugin->Qcolumn-1] = hops;
    
    if ( ((plugin->hopa) != (int)n_samples) )
    {
		plugin->hopa = n_samples;
		plugin->N = plugin->nBuffers*plugin->hopa;
		
		plugin->frames = (double*)realloc(plugin->frames,plugin->N*sizeof(double));                                          memset(plugin->frames, 0, plugin->N );
		plugin->ysaida = (double*)realloc(plugin->ysaida,2*(plugin->N + 2*(plugin->Qcolumn-1)*plugin->hopa)*sizeof(double)); memset(plugin->ysaida, 0, 2*(plugin->N + 2*(plugin->Qcolumn-1)*plugin->hopa) );
		plugin->yshift = (double*)realloc(plugin->yshift,plugin->hopa*sizeof(double));                                       memset(plugin->yshift, 0, plugin->hopa );
		plugin->b = (double**)realloc(plugin->b,plugin->hopa*sizeof(double*));
		
		fftwf_free(plugin->frames2); plugin->frames2 = fftwf_alloc_real(plugin->N);
		fftwf_free(plugin->q);       plugin->q = fftwf_alloc_real(plugin->N);
		fftwf_free(plugin->fXa);     plugin->fXa = fftwf_alloc_complex(plugin->N/2 + 1);
		fftwf_free(plugin->fXs);     plugin->fXs = fftwf_alloc_complex(plugin->N/2 + 1);
		
		plugin->Xa.zeros(plugin->N/2 + 1); 
		plugin->Xs.zeros(plugin->N/2 + 1); 
		plugin->XaPrevious.zeros(plugin->N/2 + 1);
		plugin->Xa_arg.zeros(plugin->N/2 + 1);
		plugin->XaPrevious_arg.zeros(plugin->N/2 + 1);
		plugin->Phi.zeros(plugin->N/2 + 1);
		plugin->PhiPrevious.zeros(plugin->N/2 + 1);
		plugin->d_phi.zeros(plugin->N/2 + 1);
		plugin->d_phi_prime.zeros(plugin->N/2 + 1);
		plugin->d_phi_wrapped.zeros(plugin->N/2 + 1);
		plugin->omega_true_sobre_fs.zeros(plugin->N/2 + 1);
		plugin->AUX.zeros(plugin->N/2 + 1);
		plugin->Xa_abs.zeros(plugin->N/2 + 1);
		plugin->w.zeros(plugin->N); hann(plugin->N,&plugin->w);
		plugin->I.zeros(plugin->N/2 + 1); plugin->I = linspace(0, plugin->N/2,plugin->N/2 + 1);		
		
		for (int i=1 ; i<= plugin->nBuffers; i++)
		{
			plugin->b[i-1] = &plugin->frames[(i-1)*plugin->hopa];
		}
		
		fftwf_destroy_plan(plugin->p);  plugin->p = fftwf_plan_dft_r2c_1d(plugin->N, plugin->frames2, plugin->fXa, FFTW_ESTIMATE);
		fftwf_destroy_plan(plugin->p2); plugin->p2 = fftwf_plan_dft_c2r_1d(plugin->N, plugin->fXs, plugin->q, FFTW_ESTIMATE);
		
		return;
	}
    
		for (int i=1; i<=plugin->hopa; i++)
		{
			for (int j=1; j<=(plugin->nBuffers-1); j++)
			{
				plugin->b[j-1][i-1] = plugin->b[j][i-1];
			}
			plugin->b[plugin->nBuffers-1][i-1] = plugin->in[i-1];
		}
		
		if ( plugin->cont < plugin->nBuffers-1)
		{
			plugin->cont = plugin->cont + 1;
		}
		else
		{
			shift(plugin->N, plugin->hopa, plugin->Hops, plugin->frames, plugin->frames2, &plugin->w, &plugin->XaPrevious, &plugin->Xa_arg, &plugin->Xa_abs, &plugin->XaPrevious_arg, &plugin->PhiPrevious, plugin->yshift, &plugin->Xa, &plugin->Xs, plugin->q, &plugin->Phi, plugin->ysaida, plugin->ysaida2,  plugin->Qcolumn, &plugin->d_phi, &plugin->d_phi_prime, &plugin->d_phi_wrapped, &plugin->omega_true_sobre_fs, &plugin->I, &plugin->AUX, plugin->p, plugin->p2, plugin->fXa, plugin->fXs);
			
				for (int i=1; i<=plugin->hopa; i++)
				{
				plugin->out_1[i-1] = (g_before + ((plugin->g - g_before)/(plugin->hopa - 1))*(i-1) )*(float)plugin->yshift[i-1];
				}
			
		}
		
	}

}
Exemplo n.º 30
0
void populateRedNoiseModel(rednoisemodel_t* model,long seed){
    int t_npts;
    int i;
    double freq,A,index;
    double t_samp,f_bin,t_span;
    float *data;
    fftwf_plan plan;
    fftwf_complex *spectrum;
    double secperyear=365*86400.0;

    t_samp=(model->end - model->start)/(float)model->npt;

    model->start-=t_samp;
    model->end+=t_samp*2.0;

    if (seed == 0){
        seed=TKsetSeed();
    }else if (seed > 0){
        seed=-seed;
    }

    t_npts=model->npt*model->nreal;

    spectrum = (fftwf_complex*) fftwf_malloc((t_npts/2+1)*sizeof(fftwf_complex));
    data = (float*) fftwf_malloc(t_npts*sizeof(float));

    t_span=(model->end - model->start)/365.25; // years

    t_samp=t_span/(float)model->npt;
    model->tres=t_samp*365.25;
    f_bin=1.0/(t_span*model->nreal); // frequency in yr^-1


    // To convert PSD to FFT power, need to multiply by N^2/T.
    // BUT FFTW does not divide by N^2 on inverse transform
    // so we need to divide by N^2.
    // The factor of 4, converts one-sided P to 2-sided P as
    // we input one-sided, and FFTW expected 2-sided.
    //
    // I am now 99% sure this is correct. George looked at it too!
    // M. Keith 2013.
    A=model->pwr_1yr /(4*t_span*model->nreal);


    // we are forming "amplitudes" not powers
    // so square root.
    index=model->index/2.0;
    A = sqrt(A);

    // form a complex spectrum, then do a c2r transform.
    spectrum[0]=0;
    for (i=1; i < t_npts/2+1; i++){
        freq=(double)i*f_bin;
        double scale=0;
        if(model->mode==MODE_T2CHOL){
            // same definition as the Cholesky code (except index is negative)
            scale=A*pow(1.0+pow(fabs(freq)/model->flatten,2),index/2.0);
        }
        if(model->mode==MODE_SIMPLE){
            if (freq < model->flatten)
                freq=model->flatten;

            scale = A*pow(freq,index);
            if (freq < model->cutoff)scale=0;
        }
        // complex spectrum
        spectrum[i]=(scale*TKgaussDev(&seed) + I*scale*TKgaussDev(&seed));
    }

    plan=fftwf_plan_dft_c2r_1d(t_npts,spectrum,data,FFTW_ESTIMATE);
    fftwf_execute(plan);
    fftwf_destroy_plan(plan);
    fftwf_free(spectrum);

    model->data=data;
}