00001
00005 #include <math.h>
00006 #include <limits.h>
00007
00008 #include <bpm/bpm_messages.h>
00009 #include <bpm/bpm_process.h>
00010
00011 int check_saturation( doublewf_t *w, int nbits, int *iunsat ) {
00012
00013 int i;
00014 double min_value, max_value;
00015 double thresh = 15.;
00016
00017 *iunsat = -INT_MAX;
00018
00019 if ( ! w ) {
00020 bpm_error( "Invalid waveform pointer in check_saturation(...)",
00021 __FILE__, __LINE__ );
00022 return -1;
00023 }
00024
00025 min_value = thresh;
00026 max_value = (double) ( 1 << nbits ) - thresh;
00027
00028 if ( max_value <= min_value ) {
00029 bpm_error( "Check number of bits in ADC and threshold for check_saturation(...)",
00030 __FILE__, __LINE__ );
00031 return -1;
00032 }
00033
00034
00035 for ( i = w->ns-1; i >= 0; --i ) {
00036 if ( ( w->wf[i] > max_value ) || ( w->wf[i] < min_value ) ) break;
00037 }
00038
00039
00040 if ( i > 0 ) {
00041 if ( i < w->ns - 1 ) *iunsat = i + 1;
00042 return 1;
00043 }
00044
00045
00046 *iunsat = 0;
00047 return 0;
00048 }