bpmcalibration/calibrate.c

Go to the documentation of this file.
00001 
00005 #include <bpm/bpm_messages.h>
00006 #include <bpm/bpm_calibration.h>
00007 #include <bpm/bpm_nr.h>
00008 
00009 int calibrate( bpmconf_t *bpm, beamconf_t *beam, bpmproc_t *proc, int npulses, bpmcalib_t *cal ) {
00010 
00011   if ( ! cal || ! proc || ! beam ) {
00012     bpm_error( "Invalid pointer arguments in calibrate(...)",
00013                __FILE__, __LINE__ );
00014     return BPM_FAILURE;
00015   }
00016 
00017   // First, fit a staright line to the data to get the IQ phase
00018   double *ival = calloc( npulses, sizeof(double) );
00019   double *qval = calloc( npulses, sizeof(double) );
00020   int i;
00021 
00022   for (i = 0; i < npulses; i++) {
00023     ival[i] = proc[i].ddc_I;
00024     qval[i] = proc[i].ddc_Q;
00025   }
00026 
00027   double intercept, gradient, siga, sigb, chi2, q;
00028   if ( nr_fit( ival, qval, npulses, NULL, 0, &intercept, &gradient, 
00029                &siga, &sigb, &chi2, &q ) == BPM_FAILURE ) {
00030     bpm_error( "Straight line fit failed in calibrate(...)", __FILE__, __LINE__ );
00031     return BPM_FAILURE;
00032   }
00033   
00034   cal->IQphase = gradient;
00035 
00036   // Now calculate the scale
00037   double pos;
00038   double *scale = calloc( npulses, sizeof(double) );
00039 
00040   for (i = 0; i < npulses; i++) {
00041 
00042     get_pos( proc[i].ddc_Q, proc[i].ddc_I, cal->IQphase, 1., &pos );
00043 
00044     if ( bpm->cav_polarisation == horiz ) {
00045       scale[i] = beam[i].bpmhit[0] / pos;
00046     } else {
00047       scale[i] = beam[i].bpmhit[1] / pos;
00048     }
00049  
00050   }
00051 
00052   cal->posscale = nr_median(npulses, scale);
00053 
00054   return BPM_SUCCESS;
00055 }
00056 
00057 

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