bpmsimulation/digitise.c

Go to the documentation of this file.
00001 
00005 #include <bpm/bpm_messages.h>
00006 #include <bpm/bpm_simulation.h>
00007 #include <bpm/bpm_rf.h>
00008 #include <bpm/bpm_nr.h>
00009 
00023 int digitise( double *IF, int nbits, double fs, double range_min,
00024               double range_max, int ns, int *wf ) {
00025 
00026   if (( ! IF ) || ( ! wf )) {
00027     bpm_error( "Invalid pointer arguments in digitise(...)", 
00028                __FILE__, __LINE__ );
00029     return BPM_FAILURE;
00030   }
00031   
00032   if ( nbits <= 0 ) {
00033     bpm_error( "Invalid number of ADC bits to digitise(...)",
00034                __FILE__, __LINE__);
00035     return BPM_FAILURE;
00036   }
00037 
00038   double range, t_adc, amp, t_rf;
00039   int i,j;
00040 
00041   range = pow( 2., nbits ); // range of the ADC
00042   
00043   // find trigger time in the waveform to get things started
00044   i = 0;
00045   t_rf = 0.;
00046 
00047   // now at first sample
00048   for ( j = 0; j < ns; j++ ) {
00049 
00050     // ADC timestamp of this sample, (without time jitter)
00051     t_adc = (double) j / fs;
00052     
00053     // add time jitter here when required
00054     //t_adc = nr_rangauss( t_adc, jit );
00055 
00056     // find the two samples inbetween which this ADC sampling time is located
00057     while( t_rf < t_adc ) {
00058       t_rf = i / rf_samplefreq;
00059       i++;
00060     }
00061 
00062     // Interpolate where necessary
00063     if ( i >= rf_nsamples ) { 
00064 
00065       // in this case we just fill the array with the pedestal 
00066       // but to overcome weirdness in say the FFT, we add 0.5 count RMS noise 
00067       wf[j] = (int) range/2 + nr_rangauss( 0., .5 );
00068 
00069     } else {
00070 
00071       if ( i > 0 ) {
00072 
00073         // interpolate IF[i][Re] linearily between these two values at t_adc
00074         // so here we get the signal amplitude
00075         amp = IF[i-1] + ( IF[i] - IF[i-1] ) * 
00076           ( t_adc - (t_rf - 1. / rf_samplefreq ) ) * rf_samplefreq;
00077 
00078       } else {
00079 
00080         amp = IF[i];
00081 
00082       }
00083 
00084       // now convert this value to an ADC value, adding half of the range
00085       // as pedestal
00086       wf[j] = (int) ( amp * range/(range_max-range_min) + range/2) + nr_rangauss( 0., .5 );
00087 
00088       // now address saturation in the ADC 
00089       if( wf[j] < 0 ) wf[j] = 0;
00090       if( wf[j] > ( range - 1 ) ) wf[j] = range - 1;
00091 
00092     }
00093   }
00094 
00095   return BPM_SUCCESS;
00096 }
00097 
00098 // end of file

Generated on Fri Nov 9 21:17:11 2007 for libbpm by  doxygen 1.5.1