bpmprocess/fft_waveform.c

Go to the documentation of this file.
00001 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 
00008 #include <bpm/bpm_messages.h>
00009 #include <bpm/bpm_process.h>
00010 
00011 /*
00012   Performs a fast fourier transform of the waveform, after subtracting the 
00013   pedestal
00014   
00015   @param *wf  the waveform
00016   @param  ns  the number of samples
00017   @param  fft the complex returned fft spectrum
00018   
00019   @return BPM_SUCCESS upon success, BPM_FAILURE upon failure
00020 */
00021 int fft_waveform_double( double *wf, int ns, double **fft) {
00022 
00023   double *cp, ped, rms;
00024   int i;
00025   
00026   if ( ! wf || ! fft ) {
00027     bpm_error( "Invalid waveform pointers in fft_waveform(...)",
00028                __FILE__, __LINE__ );
00029     return BPM_FAILURE;
00030   }
00031 
00032 
00033   // first make copy of waveform
00034   cp = (double*) calloc( 2*ns+1, sizeof(double) );
00035   if( ! cp ) {
00036     bpm_error( "Could not allocate memory in fft_waveform(...)",
00037                __FILE__, __LINE__ );
00038     return BPM_FAILURE;
00039   }
00040 
00041   for( i=0; i<ns; i++ ) { 
00042     // translate to what nr_four1 needs, so build up the array + 
00043     // sub tract the pedestal
00044     cp[2*i+1] = wf[i];  // real part
00045     cp[2*i+2] = 0.;     // imaginary part
00046   }
00047 
00048   if ( nr_four1( cp, ns, 1 ) == BPM_FAILURE ) {
00049     free(cp);
00050     bpm_error( "Unable to execute nr_four routine in fft_waveform(...)",
00051                __FILE__, __LINE__ );
00052     return BPM_FAILURE;
00053   }
00054 
00055   // now store in fft...
00056   for( i=0; i<ns; i++ ) {
00057     //    fft[i][Re] = cp[2*i+1];
00058     //    fft[i][Im] = cp[2*i+2];
00059     fft[i][Re] = cp[2*i+1];
00060     fft[i][Im] = cp[2*i+2];
00061   }
00062 
00063   free( cp );
00064 
00065   return BPM_SUCCESS;
00066 }
00067 
00068 
00069 int fft_waveform( int *intwf, int ns, double **fft ) {
00070   int i = 0;
00071   // allocate double waveform
00072   double *wf = (double*)calloc(ns,sizeof(double));
00073 
00074   // copy into double array
00075   for(i=0;i<ns;i++) {
00076     wf[i] = (double)intwf[i];
00077   }
00078 
00079   // perform fft
00080   fft_waveform(wf,ns,fft);
00081 
00082   // free double waveform
00083   free(wf);
00084 }

Generated on Thu Jan 10 10:18:04 2008 for libbpm by  doxygen 1.5.1