bpmcalibration/setup_calibration.c

Go to the documentation of this file.
00001 
00005 #include <bpm/bpm_messages.h>
00006 #include <bpm/bpm_calibration.h>
00007 
00008 int setup_calibration( bpmconf_t *cnf, bpmproc_t *proc, int npulses, int startpulse, 
00009                        int stoppulse, double angle, double startpos, double endpos, 
00010                        int num_steps, beamconf_t *beam ) {
00011 
00012   if ( ! proc || ! beam || ! cnf ) {
00013     bpm_error( "Invalid pointer arguments in setup_calibration(...)",
00014                __FILE__, __LINE__ );
00015     return BPM_FAILURE;
00016   }
00017 
00018   int ns = 5, nerr = 0, nstep, i;
00019   double mean = 0, stddev = 0, val, prev_mean, prev_stddev, cnt = 0;
00020   double curr_beam_pos = startpos, stepsize = (endpos - startpos) / (double) num_steps;
00021 
00022   for ( nstep = 0; nstep < num_steps; nstep++) { 
00023     
00024     while ( nerr < 3 ){
00025       
00026       mean = 0;
00027       stddev = 0;
00028       cnt = 0;
00029       
00030       // Calculate mean and std. dev. from first ns pulses
00031       for (i = startpulse; i < ns + startpulse; i++ ) {
00032         val = sqrt( pow( ( proc[i].ddc_I - proc[stoppulse].ddc_I ) , 2) + 
00033                     pow( ( proc[i].ddc_Q - proc[stoppulse].ddc_Q ) , 2) );
00034         
00035         if ( fabs( val - prev_mean ) < ( prev_stddev * 3.) ) {
00036           mean += sqrt( pow( ( proc[i].ddc_I - proc[stoppulse].ddc_I ) , 2) + 
00037                         pow( ( proc[i].ddc_Q - proc[stoppulse].ddc_Q ) , 2) );
00038         }
00039         
00040         cnt++;
00041       }
00042       
00043       mean /= cnt;
00044       
00045       for (i = startpulse; i < ns + startpulse; i++ ) {
00046         val = sqrt( pow( ( proc[i].ddc_I - proc[stoppulse].ddc_I ) , 2) + 
00047                     pow( ( proc[i].ddc_Q - proc[stoppulse].ddc_Q ) , 2) );
00048         
00049         if ( fabs( val - prev_mean ) < ( prev_stddev * 3.) ) {
00050           stddev += (val - mean) * (val - mean);
00051         }
00052       }
00053       
00054       stddev = sqrt( stddev / cnt );
00055       
00056       prev_mean = mean;
00057       prev_stddev = stddev;
00058       
00059       // Add the next event?
00060       val = sqrt( pow( ( proc[ns + startpulse].ddc_I - proc[stoppulse].ddc_I ) , 2) + 
00061                   pow( ( proc[ns + startpulse].ddc_Q - proc[stoppulse].ddc_Q ) , 2) );
00062       
00063       if ( fabs( val - mean ) > ( stddev * 3.) ) nerr++;
00064       
00065       ns++;
00066     }
00067     
00068     // Add the beam position to the beamconf array
00069     for (i = startpulse; i < ns + startpulse; i++ ) {
00070       
00071       beam[i].bpmhit[0] = cos( angle ) * curr_beam_pos;
00072       beam[i].bpmhit[1] = sin( angle ) * curr_beam_pos;
00073     }
00074     
00075     curr_beam_pos += stepsize;
00076     startpulse += ns; 
00077   }
00078 
00079   return BPM_SUCCESS;
00080 }
00081 
00082 

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