Exemple #1
0
static void
run_regression(int new_sample,
               int *valid,
               time_t *ref,
               double *fast,
               double *slope)
{
  double rtc_rel[MAX_SAMPLES]; /* Relative times on RTC axis */
  double offsets[MAX_SAMPLES]; /* How much the RTC is fast of the system clock */
  int i;
  double est_intercept, est_slope;
  int best_new_start;

  if (n_samples > 0) {

    for (i=0; i<n_samples; i++) {
      rtc_rel[i] = rtc_trim[i] + (double)(rtc_sec[i] - rtc_ref);
      offsets[i] = ((double) (rtc_ref - system_times[i].tv_sec) -
                    (1.0e-6 * (double) system_times[i].tv_usec) +
                    rtc_rel[i]);

    }

    if (RGR_FindBestRobustRegression
        (rtc_rel, offsets,
         n_samples, 1.0e-9,
         &est_intercept, &est_slope,
         &n_runs,
         &best_new_start)) {

      /* Calculate and store coefficients.  We don't do any error
         bounds processing on any of these. */
      *valid = 1;
      *ref = rtc_ref;
      *fast = est_intercept;
      *slope = est_slope;

      if (best_new_start > 0) {
        discard_samples(best_new_start);
      }


    } else {
      /* Keep existing coefficients. */
    }
  } else {
    /* Keep existing coefficients. */
  }

}
Exemple #2
0
static void
estimate_and_set_system(struct timeval *now, int offset_provided, double offset, long *offset_cs, double *dfreq_ppm, double *new_afreq_ppm)
{
  double agos[MAX_SAMPLES], offsets[MAX_SAMPLES];
  double b0, b1;
  int n_runs, best_start; /* Unused results from regression analyser */
  int i;
  double freq = 0.0;
  double skew = 0.099999999; /* All 9's when printed to log file */
  int found_freq;
  double slew_by;

  if (n_samples > 1) {
    for (i=0; i<n_samples; i++) {
      UTI_DiffTimevalsToDouble(&agos[i], &samples[n_samples-1].when, &samples[i].when);
      offsets[i] = samples[i].offset;
    }
    
    RGR_FindBestRobustRegression(agos, offsets, n_samples,
                                 1.0e-8, /* 0.01ppm easily good enough for this! */
                                 &b0, &b1, &n_runs, &best_start);
    
    
    /* Ignore b0 from regression; treat offset as being the most
       recently entered value.  (If the administrator knows he's put
       an outlier in, he will rerun the settime operation.)   However,
       the frequency estimate comes from the regression. */
    
    freq = -b1;
    found_freq = 1;
  } else {
    if (offset_provided) {
      b0 = offset;
    } else {
      b0 = 0.0;
    }
    b1 = freq = 0.0;
    found_freq = 0;
    agos[0] = 0.0;
    offsets[0] = b0;
  }

  if (offset_provided) {
    slew_by = offset;
  } else {
    slew_by = b0;
  }
  
  if (found_freq) {
    LOG(LOGS_INFO, LOGF_Manual,
        "Making a frequency change of %.3f ppm and a slew of %.6f",
        1.0e6 * freq, slew_by);
    
    REF_SetManualReference(now,
                           slew_by,
                           freq, skew);
  } else {
    LOG(LOGS_INFO, LOGF_Manual, "Making a slew of %.6f", slew_by);
    REF_SetManualReference(now,
                           slew_by,
                           0.0, skew);
  }
  
  if (offset_cs) *offset_cs = (long)(0.5 + 100.0 * b0);
  if (dfreq_ppm) *dfreq_ppm = 1.0e6 * freq;
  if (new_afreq_ppm) *new_afreq_ppm = LCL_ReadAbsoluteFrequency();
  
  /* Calculate residuals to store them */
  for (i=0; i<n_samples; i++) {
    samples[i].residual = offsets[i] - (b0 + agos[i] * b1);
  }
  
}