/*============================================================================= Name: meaENoload Abs: Measure the Klystron Energy E-Noload by doing an unweighted linear fit of the SLED output. Return Enoload and results that can be used to plot the fitted data. Args: Type Name Access Description ----------------- ----------- --------- ---------------------------- double [] scaled_data_a read-only waveform of scaled SLED output that we will fit double [] scaled_x_a read-only time in nanoseconds for waveform sample (850ns) int scaled_count read-only count of data points in scaled_data and scaled_x int fit_x_start read-only optional start Xunits for the linear fit int fit_x_end read-only optional (non-zero) end Xunits for the linear fit int sled_tuned read-only 1 indicates Klystron is SLED tuned double sigma_tol read-only sigma tolerance (SIGMA_TOL from IOC db) double scaled_data_lopr read-only LOPR of scaled data, used in EDM plot results double scaled_data_hopr read-only HOPR of scaled data, used in EDM plot results double scaled_data_egu read-only EGU of scaled data, used in EDM plot results int debug_flag read-only indicates whether to errlog diagnostic messages double * energy_gain_p write-only calculated energy gain (EACT) double * slope_p write-only Slope of linear fit (SLOPE) double * sigma_p write-only sigma of fit (SIGMA) double * energy_slope_p write-only fall time calculation from fit (ESLO) double * fitted_edge_p write-only right edge of fitted data (FITTED_EDGE) double * fitted_width_p write_only width of fitted data (FITTED_WIDTH) double [] fitted_data_a write-only results as used by William's EDM plots (FITTED_DATA) double [] fitted_x_a write-only results as used by William's EDM plots (FITTED_X) int fitted_count write-only count of fitted_data_a and fitted_x_a string * cursor_string_p write-only "Cursor =" string on EDM plot (CURSOR_STRING) double [] cursor_y_a write-only range of y-axis (CURSOR_Y) [0..1] double [] cursor_x_a write-only range of x-axis (CURSOR_X) [0..1] double [] window_y_a write-only range of y-axis (WINDOW_Y) [0..1] double [] window_x_a write-only range of x-axis (WINDOW_X) [0..1] Rem: Measure the Klystron Energy E-Noload by doing a linear fit of the SLED output. Return Enoload and results that can be used to plot the fitted data. Use gsl_multifit_linear (gnu scientific library) to fit with an unweighted linear fit. Side: if debugFlag is nonzero, prints diagnostic messages (using errlogPrintf). Ret: Integer indicating status of measuring Energy E Noload =============================================================================*/ #include // memset prototype #include // sprintf #include // errlogPrintf #include // std #include // sleep #include "mulparmlinfit.h" // MultiParamLinearFit int meaENoload (double scaled_data_a[], double scaled_x_a[], int scaled_count, int fit_x_start, int fit_x_end, int sled_tuned, double sigma_tol, double scaled_data_lopr, double scaled_data_hopr, const char *scaled_data_egu, int debug_flag, double *energy_gain_p, double *slope_p, double *sigma_p, double *energy_slope_p, double *fitted_edge_p, double *fitted_width_p, double fitted_data_a[], double fitted_x_a[], int fitted_count, char cursor_string_p[], double cursor_y_a[], double cursor_x_a[], double window_y_a[], double window_x_a[] ) { #define SCALE_119MHZ 8.4 // 119MHz rate = ~8.4 nsec ticks (1/119MHz) #define DEGREE_OF_FIT 2 // we are doing a linear fit #define MIN(a,b) ((ab) ? a : b) #define interpolate(X0, X, X1, Y0, Y1) ((0 == ((X1) - (X0))) ? (Y0) : ((Y0) + (((X) - (X0)) * (((Y1) - (Y0))/((X1) - (X0)))))) #define ABS(a) (((a)<0) ? (-1.0 * (a)) : (a)) #define SIGN(a,b) (((b)<0) ? (-1.0 * ABS(a)) : (ABS(a))) #define CURSOR_STRING_SIZE 50 // size of cursor string PV HOW DO I GET THIS FROM THE DATABASE? #define MIN_FIT_POINTS 6 // minimun number of points that we need to do a fit /* local variables */ int iss; // return status of executing this function int min_x; // min index on x-axis to look for area to fit int max_x; // max index on x-axis to look for area to fit int i; // index of for loop int num_points; // number of points in area to fit double y_fullscale; double t_psk_xaxis; // Usec start of sample time double t_psk_peak; double peak; int b_peak; int b_half; double y_psk_peak; double r_1; double r_2; double m_rise; int lo_bin; int hi_bin; double fourteen; int sleep_time = 1; // sleep time used after debug print statements /********************** resolve these entries: for now copy values from KLYS:LI28 Are these variables needed for the new hardware? ************************/ double pado = -43; // PAD Offset? force to -43 for now double ECVT[2] = {0.039296,20,1}; // fake values so I can test /* For fit.c */ int ndata; /* number of data points that we will fit */ double fit_results[2]; // the results of the fit double fit_offset; // intercept of linear fit /*************************************************************/ /* initialize results before fitting the data */ memset (fitted_data_a, 0, fitted_count*8); memset (fitted_x_a, 0, fitted_count*8); *fitted_edge_p = 0; *fitted_width_p = 0; *slope_p = 0; *sigma_p = 0; /* find the x-axis range of where look for the peak that we will fit */ if (fit_x_end != 0) { /* user specified what portion of data to find peak & fit */ min_x = fit_x_start/SCALE_119MHZ; max_x = fit_x_end/SCALE_119MHZ; } else { min_x = 0; max_x = scaled_count; /* use all the data points */ } num_points = max_x - min_x; if ((num_points < MIN_FIT_POINTS) || (num_points > scaled_count)) { /* must have a minimum number of points to fit */ iss = 0; goto error_egress; } /* how do I find y_fullscale? */ y_fullscale = sqrt(MAX(.0001,ECVT[0])) * ECVT[1] * ECVT[2] * 100.0; if (sled_tuned) { // figure out when psk came // find the peak bin in our data set. note that b_peak=0 for no data. peak = 0; b_peak = 0; for (i = min_x; i <= max_x; i++) if (scaled_data_a[i] > peak) { peak = scaled_data_a[i]; b_peak = i; } if ((peak < y_fullscale/4) || (b_peak < 7) || (b_peak > 50)) { b_peak = MAX(MIN(b_peak, 50), 7); y_psk_peak = peak; // this is the best we can do w/ bad data errlogPrintf("Measure ENoLoad: waveform peak not found"); } else { // peak ok b_half = b_peak; // best guess // find the bin whose value is close to peak/2. we do this by finding a // bin whose n point is > half and n-1 is < half. choose the [n-1|n] // closest to half. for (i = b_peak; i >= (b_peak-5); i=i-1) { r_1 = scaled_data_a[i-1] - peak/2; r_2 = scaled_data_a[i] - peak/2; if ((r_1 < 0) && (r_2 >= 0)) { if (ABS(r_1) < ABS(r_2)) { b_half = i-1; } else { b_half = i; } break; } } // from the peak, predict our leading edge slope (units/time) m_rise = peak / .100; // 100 ns rise time // extrapolate to find the x-axis intercept and peak time t_psk_xaxis = scaled_x_a[b_half] - scaled_data_a[b_half] / m_rise; t_psk_peak = t_psk_xaxis + .100; } // peak ok } // sled_tuned else { // not sled_tuned t_psk_xaxis = -0.825 - 0.100; // us start of sample time t_psk_peak = -0.825; b_half = 1; // this has no real meaning, but is used later. } // not sled_tuned if (debug_flag) errlogPrintf("Measure ENoLoad: peak=%f b_peak=%d b_half=%d\n", peak, b_peak, b_half); // figure out which points fall on the falling edge of the sled waveform. // or are the sample points for non-sleded operation. same critera. // critera are: // a) assume 100 ns rise time // b) no points within 50 ns of the peak (pad doesn't measure these well // c) no points beyond 825 ns of peak // d) no points which saturated the adc. (<= 92.5% full scale) r_1 = t_psk_xaxis + .100 + .050; r_2 = t_psk_xaxis + .100 + .825; for (i = b_half; i= (y_fullscale*.925)) lo_bin = i+1; } // make sure there are at least 6 points left. (400 ns of data) if ((hi_bin - lo_bin + 1) < 6) { errlogPrintf("Measure ENoLoad unable to find rise/fall from peak"); } else { // rise fall peak ok // find the slope and sigma ndata = hi_bin - lo_bin + 1; *slope_p = 0; *sigma_p = 0; if (debug_flag) { errlogPrintf("Measure ENoLoad about to fit using gsl_multifit_linear, ndata=%d\n", ndata); for (i=0; i .5)) || ((!sled_tuned) && (ABS(*energy_slope_p) < 10))) { errlogPrintf("Measure ENoLoad analysis fall time unreasonable"); } else { // fall time reasonable // and sigma should be very good if ((*sigma_p/MAX(.1,y_psk_peak)) > sigma_tol) { errlogPrintf("Measure ENoLoad analysis sigma unreasonable, sigma=%f y_psk+peak=%f calc=%f > %f\n", *sigma_p, y_psk_peak, (*sigma_p/MAX(.1,y_psk_peak)), sigma_tol); } else { // sigma ok // calculate the no-load energy gain. we do this as follows: // sled: // john sheppard reports that the no-load energy gain of a // typical non-sledded station is ~20 times the sqrt(power), // and about e4.6 times the peak power (sqrt). the constant ~20 // is station geometry dependent, and for now, we assume that the // ratio of 14... to 20 is constant. by now, the result...ydata(n) // contains the instantious power (the 20 number) from the station. // we compute the ~14.n number (pulse length dependent) from the, // and scale dependent on the ~20 (list.ecvt(2)) value. from the peak // we get the no-load gain. // non-sled // john reports that the "y_psk_peak" (which for non-sled, is the // 825ns average) is multiplied by the 20 number (above). (note: // data has been pre-scaled wrt to the 20 number.) // john reported values of the constant 14 as a function of plen in a // memo dated 1/27/86 "peak rf power to energy conversion". the following // fit computes 14 to a sigma of 0.0027 over the range of 2.5 to 5 us. if (sled_tuned) { fourteen = 13.070 + 0.89678 * SCALE_119MHZ*1.e6 - 0.17678 * pow((SCALE_119MHZ*1.e6),2) + 0.012623 * pow((SCALE_119MHZ*1.e6),3); *energy_gain_p = y_psk_peak * fourteen / 20; } else { *energy_gain_p = y_psk_peak; } if (debug_flag) { errlogPrintf("Measure ENoLoad eact=%f eslo=%f y_psk_peak=%f t_psk_peak=%f m_rise=%f\n", *energy_gain_p, *energy_slope_p, y_psk_peak, t_psk_peak, m_rise); } if (fitted_count < 4) { iss = 0; goto error_egress; } fitted_x_a[0] = t_psk_xaxis + .1; fitted_x_a[1] = t_psk_xaxis + .1; fitted_x_a[2] = t_psk_xaxis + .925; fitted_x_a[3] = t_psk_xaxis + .925; fitted_data_a[0] = scaled_data_lopr; fitted_data_a[1] = y_psk_peak; fitted_data_a[2] = y_psk_peak + .825 * *slope_p; fitted_data_a[3] = scaled_data_lopr; *fitted_edge_p = fitted_x_a[3]; *fitted_width_p = fitted_x_a[3] - fitted_x_a[1]; // to make display look nice, we'll draw the last point repeatedly if (fitted_count >= 5) for (i=4; i= pado/119.0) { sprintf (cursor_string_p, "cursor = %.3f %s at %.4f us", interpolate (scaled_x_a[i-1], pado/119.0, scaled_x_a[i], scaled_data_a[i-1], scaled_data_a[i]), scaled_data_egu, pado/119.0); break; // exit for loop after we find the first x-axis value > PADO & set value of cursor_string } cursor_x_a[0] = pado/119.0; cursor_x_a[1] = cursor_x_a[0]; cursor_y_a[0] = scaled_data_lopr; cursor_y_a[1] = scaled_data_hopr; /* i don't really know how to calculate the "fill" time. scp uses a function called tnominal given micro & beam code {} */ window_y_a[0] = scaled_data_lopr; window_y_a[1] = scaled_data_lopr; window_x_a[0] = -0.825; /* us */ window_x_a[1] = 0; *energy_gain_p += 1; /* force success for now */ iss = 1; error_egress: return iss; }