示例#1
0
void MainContentComponent::calc_tuning() {								//calculate tuning

	//initialise fft i/o-buffers:									
	in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * file_len);
	out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * file_len);
	p = fftw_plan_dft_1d(file_len, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
	for(long n=0; n<file_len; n++) {									// fill fft input buffer:
		in[n][0] = file_buf[n];		//	real part
		in[n][1] = 0;				//	imag part
	}
	fftw_execute(p);													// execute fft

	file_mX = new float[file_len];										//get mag and phase:
	file_pX = new float[file_len];
	for(long n=0; n<file_len/2; n++) {
		file_mX[n] = c_abs(out[n][0], out[n][1]);
		file_pX[n] = c_arg(out[n][0], out[n][1]);
/*
		std::cout<<"magnitude: "<<file_mX[n];
		std::cout<<"\tphase: "<<file_pX[n]<<std::endl;
*/
	}
		fftw_destroy_plan(p);
	fftw_free(in); fftw_free(out); 
	ladder = new tuning(file_mX, file_len/2, key_count, mirror_tuning, peak_tresh, file_fs);	//(spectrum, length of spectrum (N/2 ivm symmetrie),
																								//	amount of keys, mirror intervals round 3/2?, 
																								//		peak detection treshold)  
	

}
示例#2
0
文件: _root_pol.c 项目: r9y9/SPTK
void output_root_pol(Complex * x, int odr, int form)
{
   int i, k;
   double mag, arg, *a;

   a = dgetmem(2 * odr);

   switch (form) {
   case 1:
      for (k = i = 0; i < odr; i++) {
         a[k++] = c_mag(x[i + 1]);
         a[k++] = c_arg(x[i + 1]);
      }
      break;
   case 2:
   case 3:
      for (k = i = 0; i < odr; i++) {
         mag = 1 / c_mag(x[i + 1]);
         arg = -c_arg(x[i + 1]);
         if (form == 3) {
            a[k++] = mag;
            a[k++] = arg;
         } else {
            a[k++] = mag * cos(arg);
            a[k++] = mag * sin(arg);
         }
      }
      break;
   case 0:
   default:
      for (k = i = 0; i < odr; i++) {
         a[k++] = x[i + 1].re;
         a[k++] = x[i + 1].im;
      }
      break;
   }

   fwritef(a, sizeof(*a), odr * 2, stdout);

   return;
}
示例#3
0
 subpaving::var process_add(app * t, unsigned depth, mpz & n, mpz & d) {
     unsigned num_args = t->get_num_args();
     mpz_buffer ns(qm()), ds(qm());
     var_buffer xs;
     scoped_mpq c(qm()), c_arg(qm());
     scoped_mpz n_arg(qm()), d_arg(qm());
     for (unsigned i = 0; i < num_args; i++) {
         expr * arg           = t->get_arg(i);
         subpaving::var x_arg = process(arg, depth+1, n_arg, d_arg);
         if (x_arg == subpaving::null_var) {
             qm().set(c_arg, n_arg, d_arg);
             qm().add(c, c_arg, c);
         }
         else {
             xs.push_back(x_arg);
             ns.push_back(n_arg);
             ds.push_back(d_arg);
         }
     }
     qm().set(d,  c.get().denominator());
     unsigned sz = xs.size();
     for (unsigned i = 0; i < sz; i++) {
         qm().lcm(d, ds[i], d);
     }
     scoped_mpz & k = d_arg;
     qm().div(d, c.get().denominator(), k);
     scoped_mpz sum_c(qm());
     qm().mul(c.get().numerator(), k, sum_c);
     for (unsigned i = 0; i < sz; i++) {
         qm().div(d, ds[i], k);
         qm().mul(ns[i], k, ns[i]);
     }
     subpaving::var x;
     if (sz == 0) {
         qm().set(n, sum_c);
         x = subpaving::null_var;
     }
     else {
         x = s().mk_sum(sum_c, sz, ns.c_ptr(), xs.c_ptr());
         qm().set(n, 1);
     }
     cache_result(t, x, n, d);
     return x;
 }
示例#4
0
int process_caltone( doublewf_t *signal, bpmconf_t *bpm, bpmproc_t *proc, unsigned int mode ) {

  char msg[128];

  if ( ! bpm || ! signal || ! proc ) {
    bpm_error( "Invalid pointer arguments in process_caltone(...)",
	       __FILE__, __LINE__ );
    return BPM_FAILURE;
  }
  
  // do we have a signal ?
  if ( ! signal ) {
    sprintf( msg, "No signal present for BPM %s in process_waveform(...)", bpm->name );
    bpm_error( msg, __FILE__, __LINE__ );
    return BPM_FAILURE;
  }

  /* ------------------------------- check for saturation ------------------------------------ */
  proc->saturated = check_saturation( signal, bpm->digi_nbits, &(proc->iunsat) );

  // report saturation in caltone... is not good :s
  if ( proc->saturated ) {
    bpm_warning( "Calibration tone is saturated, not updating caltone information...",
		 __FILE__, __LINE__ );
  } else {

    /* ------------------------------- subtract the pedestal ----------------------------------- */
    // determing voltage offset and amplitude noise in adc channels (pedestal)
    if ( get_pedestal( signal, 20, &(proc->voltageoffset) ,&(proc->ampnoise) ) == BPM_FAILURE ) {
      sprintf( msg, "Error getting pedestal of BPM %s in process_waveform(...)", bpm->name );
      bpm_error( msg, __FILE__, __LINE__ );
      return BPM_FAILURE;
    }
    
    // subtract the pedestal
    doublewf_bias( -proc->voltageoffset, signal );


    /* ------------------------------ check whether to do FFT ? -------------------------------- */
    if ( mode & PROC_DO_FFT ) {

      // compute the ft
      if ( fft_waveform( signal, proc->ft ) == BPM_FAILURE ) {
	sprintf( msg, "Could not perform fft for BPM %s in process_caltone(...)", bpm->name );
	bpm_warning( msg, __FILE__, __LINE__ );
	
      } else {
	proc->fft_success = TRUE;
	
	if ( mode & PROC_FIT_FFT ) {
	  // fft done, fit for frequency and tdecay
	  if( fit_fft( proc->ft, &(proc->fft_freq), &(proc->fft_tdecay), NULL, NULL ) 
	      == BPM_FAILURE ) {
	    sprintf( msg, "Could not fit the FFT for BPM %s in process_waveform(...)", bpm->name );
	    bpm_warning( msg, __FILE__, __LINE__ );
	  } 
	  
	}
	
      }

    } /* if ( mode & PROC_DO_FFT ) */


    /* ------------------------------ check whether to do DDC ? -------------------------------- */
    if ( mode & PROC_DO_DDC ) {
    
      // if we must to the full DDC, do it and sample afterwards
      if ( ddc_waveform( signal, bpm->ddc_ct_freq, bpm->ddc_ct_filter, proc->dc,
			 bpm->ddc_buffer_re, bpm->ddc_buffer_im ) == BPM_FAILURE ) {
	sprintf( msg, "Could not ddc BPM %s waveform in process_caltone(...)", bpm->name );
	bpm_warning( msg, __FILE__, __LINE__ );
      } else {
	proc->ddc_success = TRUE;
	
	// Now sample the ddc waveform at the iSample requested, extrapolate amplitude
 	proc->ddc_amp   = c_abs( proc->dc->wf[ bpm->ddc_ct_iSample ] );
	proc->ddc_phase = c_arg( proc->dc->wf[ bpm->ddc_ct_iSample ] );
	norm_phase( &(proc->ddc_phase) );

	// store phase and amplitude in the specially foreseen variables to store them
	// inbetween pulses...
	proc->ddc_ct_amp   = proc->ddc_amp;
	proc->ddc_ct_phase = proc->ddc_phase;
      }
      
    } /* if ( mode & PROC_DO_DDC ) */

  } // no saturation...

  return BPM_SUCCESS;
}