示例#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);
}
示例#2
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);
    }
 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);
 }
示例#4
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");
}
示例#5
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();

}
示例#6
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();
}
示例#7
0
// Create FFT kernels for template matching
void createFFTKernel(fftwf_complex *kernel, unsigned factor, unsigned fftlen)
{
	unsigned i;

	float values[fftlen];

	memset(values, 0, fftlen * sizeof(fftlen));

	for (i = 0; i < factor / 2 + 1	; i++)
		values[i] += 1;

	if (factor % 2) // Downfactor is odd
		for (i = fftlen - factor / 2; i < fftlen; i++)
			values[i] += 1;

	else if (factor > 2) // Downfactor is even
		for (i = fftlen - (factor / 2 - 1); i < fftlen; i++)
			values[i] += 1;

	for (i = 0; i < fftlen; i++)
		values[i] /= sqrt(factor);

	// Create FFT plan and execute
	fftwf_plan plan = fftwf_plan_dft_r2c_1d(fftlen, values, kernel, FFTW_ESTIMATE);
	fftwf_execute(plan);
	fftwf_destroy_plan(plan);
}
示例#8
0
void rfft(float* buffer,
          float* result,
          int size)
{
  fftwf_plan plan;
  plan = fftwf_plan_dft_r2c_1d(size, buffer, (fftwf_complex*) result,FFTW_ESTIMATE);
  fftwf_execute(plan);
  fftwf_destroy_plan(plan);
}
	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);
	}
示例#10
0
EqAnalyser::EqAnalyser() :
	m_framesFilledUp ( 0 ),
	m_energy ( 0 ),
	m_sampleRate ( 1 ),
	m_active ( true )
{
	m_inProgress=false;
	m_specBuf = ( fftwf_complex * ) fftwf_malloc( ( FFT_BUFFER_SIZE + 1 ) * sizeof( fftwf_complex ) );
	m_fftPlan = fftwf_plan_dft_r2c_1d( FFT_BUFFER_SIZE*2, m_buffer, m_specBuf, FFTW_MEASURE );
	clear();
}
示例#11
0
SpectrumAnalyzer::SpectrumAnalyzer( Model * _parent,
			const Descriptor::SubPluginFeatures::Key * _key ) :
	Effect( &spectrumanalyzer_plugin_descriptor, _parent, _key ),
	m_saControls( this ),
	m_framesFilledUp( 0 ),
	m_energy( 0 )
{
	memset( m_buffer, 0, sizeof( m_buffer ) );

	m_specBuf = (fftwf_complex *) fftwf_malloc( ( FFT_BUFFER_SIZE + 1 ) * sizeof( fftwf_complex ) );
	m_fftPlan = fftwf_plan_dft_r2c_1d( FFT_BUFFER_SIZE*2, m_buffer, m_specBuf, FFTW_MEASURE );
}
示例#12
0
void my_forward_fft(sf_complex **ms,sf_complex **mr,float **shot,float **ds,int nt,float dt,int nx, int padt)
{
  int nw;
  sf_complex *out1a,*out1b;
  float *in1a,*in1b;
  int ntfft,ix,it,iw;
  fftwf_plan p1a,p1b;
  
  ntfft = nt*padt;
  nw = ntfft/2 + 1;
  out1a = sf_complexalloc(nw);
  in1a = sf_floatalloc(ntfft);
  p1a = fftwf_plan_dft_r2c_1d(ntfft, in1a, (fftwf_complex*)out1a, FFTW_ESTIMATE);
  out1b = sf_complexalloc(nw);
  in1b = sf_floatalloc(ntfft);
  p1b = fftwf_plan_dft_r2c_1d(ntfft, in1b, (fftwf_complex*)out1b, FFTW_ESTIMATE);
  for (ix=0;ix<nx;ix++){
  	for(it=0;it<nt;it++) {
		in1a[it] = shot[ix][it];
		in1b[it]=ds[ix][it];
		}
    	for(it=nt;it<ntfft;it++){
		in1a[it] = 0;
		in1b[it]=0;
		}
    fftwf_execute(p1a); 
    fftwf_execute(p1b); 
    for(iw=0;iw<nw;iw++) {
	mr[ix][iw] = out1a[iw]; 
	ms[ix][iw] = out1b[iw]; 
	}
  }
  fftwf_destroy_plan(p1a);
  fftwf_free(in1a); fftwf_free(out1a);
 fftwf_destroy_plan(p1b);
  fftwf_free(in1b); fftwf_free(out1b);
  return;

}
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;
}
示例#14
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);
}
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);
	}
}
示例#16
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;
}
示例#17
0
文件: ofApp.cpp 项目: whaatt/Journey
/**
 * Function: audioIn
 * -----------------
 * Capture audio and process it through
 * beat detection as well as an FFT.
 */
void ofApp::audioIn(float* input, int bufferSize, int nChannels) {
    if (!captureSound) return; // sound capture is currently paused

    // store possibly stereo audio in a mono float buffer
    bufferLock.lock(); // just for correctness

    // update FFT beat detection bins with new data
    beat.audioReceived(input, bufferSize, nChannels);
    beat.update(ofGetElapsedTimeMillis());

    for (int i = 0; i < bufferSize; i += 1) {
        inBuffer[i] = 0; // initialize this

        for (int j = 0; j < nChannels; j += 1) {
            // signal pickup threshold value [ignore ambient noise]
            if (attenuationMode && fabs(input[i * 2 + j]) < 0.05) {
                inBuffer[i] += 0;
                continue;
            }

            // multiply by 1 / nChannels to convert to mono
            inBuffer[i] += input[i * 2 + j] / nChannels;
        }
    }

    // copy the input buffer to FFT storage
    float* fftSamples = new float[bufferSize];
    float* fftWindow = new float[bufferSize];
    copy(inBuffer.begin(), inBuffer.end(), fftSamples);
    bufferLock.unlock(); // once copy is finished

    // execute a complex FFT on a buffer of real numbers, leaving two numbers for bufferSize / 2 frequencies
    fftwf_plan p = fftwf_plan_dft_r2c_1d(bufferSize, fftSamples, (fftwf_complex*) fftSamples, FFTW_ESTIMATE);
    fftwf_execute(p); // repeat as needed
    fftwf_destroy_plan(p);

    // delete old fftBuffers as we go
    if (fftBuffers.size() > maxBufferCount)
        fftBuffers.erase(fftBuffers.begin());

    fftBuffers.push_back(vector<float>(bufferSize));
    vector<float>& fftBuffer = fftBuffers[fftBuffers.size() - 1];
    copy(fftSamples, fftSamples + bufferSize, fftBuffer.begin());

    // deallocate memory
    delete fftSamples;
    delete fftWindow;
}
示例#18
0
int main(){
  cairo_surface_t *cs = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
						    W,H);
  int period=SAMPLES;
  int coeffs = period/4;
  float *square_coeffs;
  float *square_phases;
  int square_coeffs_n;
  int i,j;
  double phase=OFFSET*2*M_PI - (M_PI*CYCLES/period);
  float waveform[W];
  float *work = fftwf_malloc((period+2)*sizeof(*work));
  fftwf_plan plan = fftwf_plan_dft_r2c_1d(period,work,
                                          (fftwf_complex *)work,
                                          FFTW_ESTIMATE);
  for(i=0;i<period/2;i++)
    work[i]=1.;
  for(;i<period;i++)
    work[i]=-1.;
  fftwf_execute(plan);

  square_coeffs_n = coeffs;
  square_coeffs = calloc(coeffs,sizeof(*square_coeffs));
  square_phases = calloc(coeffs,sizeof(*square_phases));

  for(i=1,j=0;j<square_coeffs_n;i+=2,j++){
    square_coeffs[j] = hypotf(work[i<<1],work[(i<<1)+1]) / period;
    square_phases[j] = atan2f(work[(i<<1)+1],work[i<<1]);
  }

  for(i=0;i<W;i++){
    float acc=0.;
    int k;
    for(j=0,k=1;j<square_coeffs_n;j++,k+=2)
      acc += square_coeffs[j] *
        cos( square_phases[j] + k*phase);
    waveform[i]=acc;
    phase += 2*M_PI*CYCLES/W;
    if(phase>=2*M_PI)phase-=2*M_PI;
  }

  transparent_surface(cs);
  draw_overlay(cs,waveform);
  write_frame(cs,0);
  cairo_surface_destroy(cs);
  return 0;
}
示例#19
0
//----------------------------------------------------------------------------
AudioInput::AudioInput(float sRate, int bufferSize, int iNBands, float fResponse,
					   int iAmpScale, int iFreqScale, int iResampling, float fXScale, float fYScale, AudioEffect* effect):
effect(effect),level(0.0f),sampleRate(sRate),bufferSize(bufferSize),nBands(iNBands),halfLife(fResponse),
curFftScaling(iAmpScale),curFreqScaling(iFreqScale),curResampling(iResampling),xScale(fXScale), yScale(fYScale),maxFreq(0),maxMagni(0)
//AudioInput::AudioInput(float sRate, int bufferSize, int iNBands, float fResponse, int iAmpScale):
//level(0.0f),sampleRate(sRate),bufferSize(bufferSize),nBands(iNBands),halfLife(fResponse),curFftScaling(iAmpScale),maxFreq(0),maxMagni(0)

{
	cursor = 0;
	
	buffer = new float[bufferSize];
	memset(buffer, 0, bufferSize*sizeof(float));
	magnitude = NULL;
	audiobars = NULL;
	temp_bars = NULL;

	
	gain = 1/32768.0;

	nSamples = bufferSize;

	nfftResult = (int)(nSamples*0.5+1);

	fftin = new float[nSamples];
	memset(fftin, 0, nSamples*sizeof(float));

	fftout = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * nSamples);
	plan = fftwf_plan_dft_r2c_1d(nSamples, fftin, fftout, FFTW_MEASURE);
	fftMag = new float[nfftResult];
	memset(fftMag, 0, nfftResult*sizeof(float));

	fftBands = new float[nBands];
	memset(fftBands, 0, nBands*sizeof(float));

	fftBandsTemp = new float[nBands];
	memset(fftBandsTemp, 0, nBands*sizeof(float));

	powerN = log10((double)nfftResult)/nBands;

	curResponse = 0.90f;

	lock = false;

	initFilters(nBands);

}
示例#20
0
float* getPowerSpectrum(rednoisemodel_t* model){
    fftwf_complex *spectrum;
    fftwf_plan plan;
    float *data;
    float *power_spectrum;
    spectrum = (fftwf_complex*) fftwf_malloc((model->npt/2+1)*sizeof(fftwf_complex));
    data = (float*) fftwf_malloc((model->npt)*sizeof(fftwf_complex));
    power_spectrum = (float*) malloc((model->npt/2+1)*sizeof(float));

    float t_span=(model->end - model->start)/365.25; // years
    float f_bin=1.0/(t_span); // frequency in yr^-1
    for (int p =0 ; p < (model->npt/2+1); p++){
        power_spectrum[p]=0;
    }
    plan=fftwf_plan_dft_r2c_1d(model->npt,data,spectrum,FFTW_ESTIMATE);
    for (int r =0 ; r < model->nreal; r++){
        int off=r*model->npt;
        for (int p =0 ; p < model->npt; p++){
            data[p]=model->data[p+off];
        }

        float dx=t_span/(float)(model->npt); // yr
        int p;
        for (p =0 ; p < model->npt-1; p++){
            float dy=data[p+1]-data[p];
            data[p] = dy/dx;
        }
        float dy=data[0]-data[p];
        data[p] = 0;
        fftwf_execute(plan);
        for (int p =1 ; p < (model->npt/2+1); p++){
            float factor=1.0/((float)p*f_bin);
            spectrum[p]/=(float)(model->npt/2);
            spectrum[p]*=factor;
            power_spectrum[p]+=crealf(spectrum[p]*conjf(spectrum[p]));
        }
    }
    fftwf_destroy_plan(plan);
    fftwf_free(spectrum);
    fftwf_free(data);
    for (int p =0 ; p < (model->npt/2+1); p++){
        power_spectrum[p]/=(float)(model->nreal);
    }

    return power_spectrum;
}
示例#21
0
inline void rfft(const DEVector<float>::Type &x,
          CDEVector<float>::Type &X, int fftsize)
{
  // zero pad to fftsize
  DEVector<float>::Type xpad(fftsize);
  std::fill_n(xpad.data(), fftsize, 0);
  xpad(_(1,x.length())) = x;

  // calc FFTs
  fftwf_plan p1;
  X.resize(fftsize/2+1);
  p1 = fftwf_plan_dft_r2c_1d(fftsize, &xpad(1),
                            reinterpret_cast<fftwf_complex*>( &X(1) ),
                            FFTW_ESTIMATE);
  fftwf_execute(p1);
  fftwf_destroy_plan(p1);
}
示例#22
0
文件: nidoamp.c 项目: nido/nidolv2
/** 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;
}
示例#23
0
/* 1-D FFT */
void FFTW_(fft)(real *r, real *x, long n)
{
#ifdef USE_FFTW
#if defined(TH_REAL_IS_DOUBLE)
    fftw_complex *out = (fftw_complex*)r;
    fftw_plan p = fftw_plan_dft_r2c_1d(n, x, out, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
#else
    fftwf_complex *out = (fftwf_complex*)r;
    fftwf_plan p = fftwf_plan_dft_r2c_1d(n, x, out, FFTW_ESTIMATE);
    fftwf_execute(p);
    fftwf_destroy_plan(p);
#endif
#else
    THError("fft : FFTW Library was not found in compile time\n");
#endif
}
示例#24
0
void powerSpectrum(std::vector<float>& input, std::vector<float>& output)
{
    fftwf_complex* transform = new fftwf_complex[input.size() / 2 + 1];

    fftwf_plan plan = fftwf_plan_dft_r2c_1d(static_cast<int>(input.size()), &input[0], transform, FFTW_ESTIMATE); // Computationally expensive if not FFTW_ESTIMATE
    fftwf_execute(plan);
    fftwf_destroy_plan(plan);

    output.resize(input.size() / 2 + 1);

    for(int index = 0; index < input.size() / 2 + 1; ++index) // pre increment to avoid copy
    {
        output[index] = sqrt(transform[index][0] * transform[index][0] + transform[index][1] * transform[index][1]) / (input.size() / 2.0f);
    }

    delete[] transform;

}
示例#25
0
文件: cosft.c 项目: 1014511134/src
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
}
示例#26
0
文件: fft.cpp 项目: bitlair/bitvis
void Cfft::Allocate(unsigned int size)
{
  if (size != m_bufsize)
  {
    Free();

    m_bufsize = size;
    m_inbuf = new float[m_bufsize];
    m_fftin = (float*)fftw_malloc(m_bufsize * sizeof(float));
    m_outbuf = (fftwf_complex*)fftw_malloc(m_bufsize * sizeof(fftwf_complex));
    m_window = new float[m_bufsize];

    //create a hamming window
    for (unsigned int i = 0; i < m_bufsize; i++)
      m_window[i] = 0.54f - 0.46f * cosf(2.0f * M_PI * i / (m_bufsize - 1.0f));

    Log("Building fft plan");
    int64_t start = GetTimeUs();
    m_plan = fftwf_plan_dft_r2c_1d(m_bufsize, m_fftin, m_outbuf, FFTW_MEASURE);
    Log("Built fft plan in %.0f ms", (double)(GetTimeUs() - start) / 1000.0f);
  }
}
示例#27
0
文件: fftw_fft.c 项目: KL-Yang/fft
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;
}
PSAnalysis::PSAnalysis(uint32_t n_samples, int nBuffers, const char* wisdomFile) //Construtor
{
	Qcolumn = nBuffers;
	hopa = n_samples;
	N = nBuffers*n_samples;

	frames = new double[N]; fill_n(frames,N,0);
	b = new double*[hopa];

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

	frames2 = fftwf_alloc_real(N);
	fXa = fftwf_alloc_complex(N/2 + 1);
	Xa.zeros(N/2 + 1);
	XaPrevious.zeros(N/2 + 1);
	Xa_arg.zeros(N/2 + 1);
	XaPrevious_arg.zeros(N/2 + 1);
	d_phi.zeros(N/2 + 1); 
	d_phi_prime.zeros(N/2 + 1); 
	d_phi_wrapped.zeros(N/2 + 1); 
	omega_true_sobre_fs.zeros(N/2 + 1); 
	AUX.zeros(N/2 + 1); 
	Xa_abs.zeros(N/2 + 1); 
	w.zeros(N); hann(N,&w); 
	I.zeros(N/2 + 1); I = linspace(0, N/2, N/2 + 1);

	if (fftwf_import_wisdom_from_filename(wisdomFile) != 0)
	{
		p = fftwf_plan_dft_r2c_1d(N, frames2, fXa, FFTW_WISDOM_ONLY);
	}
	else
	{
		p = NULL;
		printf("PSAnalysis: failed to import wisdom file '%s'\n", wisdomFile);
	}
}
示例#29
0
void AudioInput::updateNSamples(int newBufferSize){

	//int tempSamples = (int)(pow((double)2, 9+newNSamples));
	//if (tempSamples != nSamples) {
		//nSamples = tempSamples;

		nSamples = newBufferSize;

		delete[] buffer;

		fftwf_destroy_plan(plan);
		fftwf_free(fftout);
		delete[] fftin;
		delete[] fftMag;

		//delete[] fftBands;
		//delete[] fftBandsTemp;

		nfftResult = (int)(nSamples*0.5+1);
		//fftRawArray.setLength(nfftResult);

		fftin = new float[nSamples];
		fftout = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * nSamples);
		//p = fftwf_plan_dft_r2c_1d(nSamples, fftin, out, FFTW_ESTIMATE);
		plan = fftwf_plan_dft_r2c_1d(nSamples, fftin, fftout, FFTW_MEASURE);
		fftMag = new float[nfftResult];

		//fftBands = new float[nBands];
		//fftBandsTemp = new float[nBands];
		//powerN = log10((double)nfftResult)/nBands;

//		chunkSize = nSamples*nBytes*nChannels;

		//buffer = new short[nSamples*nChannels];
		buffer = new float[newBufferSize];

}
示例#30
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;
}