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 #include <bpm/bpm_wf.h>
00010 
00011 int digitise( doublewf_t *IF, int nbits, double range_min, double range_max,
00012               double clock_jitter, double digi_noise, unsigned int ipmode, 
00013               intwf_t *wf ) {
00014   
00015   double range, ti, amp;
00016   int    i;
00017 
00018   if (( ! IF ) || ( ! wf )) {
00019     bpm_error( "Invalid pointer arguments in digitise(...)", 
00020                __FILE__, __LINE__ );
00021     return BPM_FAILURE;
00022   }
00023   
00024   if ( nbits <= 0 ) {
00025     bpm_error( "Invalid number of ADC bits in digitise(...)",
00026                __FILE__, __LINE__);
00027     return BPM_FAILURE;
00028   }
00029 
00030   if ( range_max <= range_min ) {
00031     bpm_error( "Invalid range setting in digitise(...)",
00032                __FILE__, __LINE__);
00033     return BPM_FAILURE;
00034   }
00035 
00036   range = pow( 2., nbits ); // range of the ADC
00037   
00038   // now at first sample
00039   for ( i = 0; i < wf->ns; i++ ) {
00040 
00041     // ADC timestamp of this sample, (without time jitter)
00042     ti = (double) i / wf->fs;
00043     
00044     // add time jitter here when required
00045     if ( clock_jitter != 0 ) ti = nr_rangauss( ti, clock_jitter );
00046 
00047     // are we already beyond the IF time ?
00048     if ( ti > (double) (IF->ns-1) / IF->fs ) {
00049       // We are already past the last sample in the IF waveform
00050       // In this case we just fill the array with the pedestal 
00051       wf->wf[i] = (int) range / 2;
00052     } else {
00053 
00054       // resample the waveform
00055       amp = doublewf_getvalue( IF, ti, ipmode );
00056 
00057       // now convert this value to an ADC value, adding half of the range
00058       // as pedestal
00059       wf->wf[i] = (int) ( amp * range / ( range_max - range_min ) + range / 2 );
00060     }
00061 
00062   }
00063 
00064   // Add digitiser noise
00065   intwf_add_ampnoise( wf, digi_noise );
00066 
00067   // Address saturation in the ADC 
00068   for ( i = 0; i < wf->ns; i++ ) {
00069     if( wf->wf[i] < 0 ) wf->wf[i] = 0;
00070     if( wf->wf[i] > ( range - 1 ) ) wf->wf[i] = range - 1;
00071   }
00072 
00073   return BPM_SUCCESS;
00074 }
00075 
00076 // end of file

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