void LinearSlopeFit::fit()
{
    if (d_init_err)
        return;

	if (d_p > d_n){
  		QMessageBox::critical((ApplicationWindow *)parent(), tr("QtiPlot - Fit Error"),
  	    tr("You need at least %1 data points for this fit operation. Operation aborted!").arg(d_p));
  		return;
  	}

	double c1, cov11;
	if (d_weighting == NoWeighting)
		gsl_fit_mul(d_x, 1, d_y, 1, d_n, &c1, &cov11, &chi_2);
	else
		gsl_fit_wmul(d_x, 1, d_w, 1, d_y, 1, d_n, &c1, &cov11, &chi_2);

	d_results[0] = c1;

	gsl_matrix_set(covar, 0, 0, cov11);
	generateFitCurve();

	ApplicationWindow *app = (ApplicationWindow *)parent();
	if (app->writeFitResultsToLog)
		app->updateLog(logFitInfo(0, 0));
}
Esempio n. 2
0
CAMLprim value ml_gsl_fit_mul(value wo, value x, value y)
{
  value r;
  size_t N=Double_array_length(x);
  double c1,cov11,sumsq;
  
  if(Double_array_length(y) != N)
    GSL_ERROR("array sizes differ", GSL_EBADLEN);

  if(wo == Val_none)
    gsl_fit_mul(Double_array_val(x), 1, Double_array_val(y), 1, N,
		&c1, &cov11, &sumsq);
  else {
    value w=Field(wo, 0);
    if(Double_array_length(w) != N)
      GSL_ERROR("array sizes differ", GSL_EBADLEN);
    gsl_fit_wmul(Double_array_val(x), 1, 
		 Double_array_val(w), 1,
		 Double_array_val(y), 1, N,
		 &c1, &cov11, &sumsq);
  }
  r=alloc_small(3 * Double_wosize, Double_array_tag);
  Store_double_field(r, 0, c1);
  Store_double_field(r, 1, cov11);
  Store_double_field(r, 2, sumsq);
  return r;
}
Esempio n. 3
0
static VALUE rb_gsl_fit_mul(int argc, VALUE *argv, VALUE obj)
{
  double *ptrx, *ptry;
  double c1, cov11, sumsq;
  int status;
  size_t n, stridex, stridey;
  switch (argc) {
  case 2:
    ptrx = get_vector_ptr(argv[0], &stridex, &n);
    ptry = get_vector_ptr(argv[1], &stridey, &n);
    break;
  case 3:
    CHECK_FIXNUM(argv[2]);
    ptrx = get_vector_ptr(argv[0], &stridex, &n);
    ptry = get_vector_ptr(argv[1], &stridey, &n);
    n = FIX2INT(argv[2]);
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)", argc);
    break;
  }
  status = gsl_fit_mul(ptrx, stridex, ptry, stridey, n, &c1, &cov11, &sumsq);
  return rb_ary_new3(4, rb_float_new(c1), 
		     rb_float_new(cov11), rb_float_new(sumsq), INT2FIX(status));
}
Esempio n. 4
0
pure_expr* wrap_gsl_fit_mul(double* x, double* y, size_t n)
{
  double c1, cov11, sumsq;
  
  gsl_fit_mul(x, 1, y, 1, n, &c1, &cov11, &sumsq);
  return pure_listl(3, pure_double(c1), pure_double(cov11), pure_double(sumsq));
}
int main()
{
  int num_phasecodes[MAX_CARDS],num_cards[MAX_CARDS],num_freqs[MAX_CARDS],active[MAX_CARDS];
  int i,b,c,ii,cc,count,summary_freqs;
  int f,d,p,o;
  double offset,slope,slope_variance,sumsq;
  double best_sumsq[MAX_PHASES],best_offset[MAX_PHASES],best_slope[MAX_PHASES],best_var[MAX_PHASES];
  char tmp;
  int lowest_pwr_mag_index[3]={-1,-1,-1}; // freq,card,phasecode
  double lowest_pwr_mag=1E10; // freq,card,phasecode
  int highest_time_delay_card[MAX_FREQS]; // freq
  double highest_time_delay[MAX_FREQS];
  double *freq[MAX_CARDS],*phase[MAX_FREQS][MAX_CARDS];
  double *pwr_mag[MAX_FREQS][MAX_CARDS];
  double *timedelay1[MAX_FREQS][MAX_CARDS];    
  double *timedelay2[MAX_FREQS][MAX_CARDS];    
  double minimum_timedelay=0;
  double Y[MAX_FREQS],time0[MAX_FREQS];    
  char filename1[120];
  char filename2[120];
  double max_Y;
  double diff,var,max_var[13],max_var0;
  int  hmm;
  for(i=0;i<MAX_FREQS;i++) {
    highest_time_delay_card[i]=-1;
    highest_time_delay[i]=-1000;
  }
  printf("Nulling arrays\n");
  for(c=0;c<MAX_CARDS;c++) {
    active[c]=0;
    freq[c]=NULL;
    num_freqs[c]=0;
    for(i=0;i<MAX_FREQS;i++) {
        phase[i][c]=NULL;
        pwr_mag[i][c]=NULL;
        timedelay1[i][c]=NULL;
        timedelay2[i][c]=NULL;
    }
  }
  for(c=CARD;c<=CARD;c++) {
    sprintf(filename1,"/tmp/phasing_cal_%s_%d.dat",radar_name1,c);
    calfile1=fopen(filename1,"r");
    printf("1: %p %s\n",calfile1,filename1); 
    if (calfile1!=NULL ) {
      fread(&num_phasecodes[c],sizeof(int),1,calfile1);
      fread(&num_cards[c],sizeof(int),1,calfile1);
      fread(&num_freqs[c],sizeof(int),1,calfile1);
      if (num_freqs[c]>MAX_FREQS) {
        printf("Too many stored frequencies...up the MAX_FREQS define!\n");
        exit(0);
      }

      printf("Allocating arrays\n");
      for(i=0;i<num_freqs[c];i++) {
        if (freq[c]!=NULL) free(freq[c]);
        freq[c]=calloc(num_freqs[c],sizeof(double));
        if (phase[i][c]!=NULL) free(phase[i][c]);
        phase[i][c]=calloc(num_phasecodes[c],sizeof(double));
        if (pwr_mag[i][c]!=NULL) free(pwr_mag[i][c]);
        pwr_mag[i][c]=calloc(num_phasecodes[c],sizeof(double));
        if (timedelay1[i][c]!=NULL) free(timedelay1[i][c]);
        timedelay1[i][c]=calloc(num_phasecodes[c],sizeof(double));
      }


      printf("Reading frequency array\n");

      count=fread(freq[c],sizeof(double),num_freqs[c],calfile1);
      count=1;
      printf("Reading in data\n");
      while(count>0) {
          count=fread(&ii,sizeof(int),1,calfile1);
          if (count==0) {
            break;
          }
          count=fread(phase[ii][c],sizeof(double),num_phasecodes[c],calfile1);
          if (verbose > 1) printf("Freq index: %d Phase Count: %d\n",ii,count);
          count=fread(pwr_mag[ii][c],sizeof(double),num_phasecodes[c],calfile1);
          if (verbose > 1) printf("Freq index: %d Pwr-mag Count: %d\n",ii,count);
          if (count==0) {
            break;
          }
      }
      if (count==0) {
        if (feof(calfile1)) printf("End of File!\n");
      }
      fclose(calfile1);
      printf("Processing Phase Information for Radar: %s Card: %d\n",radar_name1,c);
      for (p=0;p<num_phasecodes[c];p++) {
//        printf("Phase Code: %d\n",p);

        best_offset[p]=1000;
        best_sumsq[p]=1E100;
        for (o=-20;o<20;o++) {
          offset=360.0*o;
          max_Y=-1000;
          for (i=0;i<num_freqs[c];i++) {
            Y[i]=phase[i][c][p]+offset;
            if (Y[i]> max_Y) max_Y=Y[i];
          }
/*
*  Linear Regression Y=slope*freq
*/
          gsl_fit_mul (freq[c], 1, Y, 1, num_freqs[c], &slope, &slope_variance, &sumsq);
          if (max_Y < 0 ) {
            if (sumsq < best_sumsq[p]) {
              best_offset[p]=offset; 
              best_sumsq[p]=sumsq;
              best_var[p]=slope_variance;
              best_slope[p]=slope;
            }
          }
        } // end offset loop
        printf("%d :: Best Offset: %lf  Phase0: %lf\n",p,best_offset[p],phase[0][c][p]);
/*
*  Build the timedelays arrays using the best phase offset from the linear regression
*/
        for (i=0;i<num_freqs[c];i++) {
          time0[i]=phase_to_timedelay(phase[i][c][0]+best_offset[0],freq[c][i]);
          timedelay1[i][c][p]=phase_to_timedelay(phase[i][c][p]+best_offset[p],freq[c][i]);
          if((phase[i][c][p]+best_offset[p]) > 0 ) {
            printf("ERROR: Phase error: %d %d %d %lfi %lf\n",i,c,p,phase[i][c][p],best_offset[p]);
            exit(0);
      
          }
//          printf("Phase: %d :: Freq %e :: Time Delay %% Diff  %lf\n",
//                p,freq[c][i],fabs((timedelay[i][c][p]-expected_timedelay(p)-time0[i])/(expected_timedelay(p)+time0[i]))*100);
        }  //freq loop
        if(timedelay1[0][c][p] < minimum_timedelay) {
          printf("ERROR: Timedelay1 switch error: %d %lf\n",p,timedelay1[0][c][p]);
          exit(0);
        }
        if(pwr_mag[0][c][p] < pwr_threshold) {
          printf("ERROR: pwr_mag switch error: %d\n",p);
          exit(0);
        }

      }  //phasecode loop
    } // end calfile1 check

    sprintf(filename2,"/tmp/phasing_cal_%s_%d.dat",radar_name2,c);
    calfile2=fopen(filename2,"r");
    printf("1: %p %s\n",calfile2,filename2); 
    if (calfile2!=NULL ) {
      fread(&num_phasecodes[c],sizeof(int),1,calfile2);
      fread(&num_cards[c],sizeof(int),1,calfile2);
      fread(&num_freqs[c],sizeof(int),1,calfile2);
      if (num_freqs[c]>MAX_FREQS) {
        printf("Too many stored frequencies...up the MAX_FREQS define!\n");
        exit(0);
      }

      printf("Allocating arrays\n");
      for(i=0;i<num_freqs[c];i++) {
        if (freq[c]!=NULL) free(freq[c]);
        freq[c]=calloc(num_freqs[c],sizeof(double));
        if (phase[i][c]!=NULL) free(phase[i][c]);
        phase[i][c]=calloc(num_phasecodes[c],sizeof(double));
        if (pwr_mag[i][c]!=NULL) free(pwr_mag[i][c]);
        pwr_mag[i][c]=calloc(num_phasecodes[c],sizeof(double));
        if (timedelay2[i][c]!=NULL) free(timedelay2[i][c]);
        timedelay2[i][c]=calloc(num_phasecodes[c],sizeof(double));
      }


      printf("Reading frequency array\n");

      count=fread(freq[c],sizeof(double),num_freqs[c],calfile2);
      count=1;
      printf("Reading in data\n");
      while(count>0) {
          count=fread(&ii,sizeof(int),1,calfile2);
          if (count==0) {
            break;
          }
          count=fread(phase[ii][c],sizeof(double),num_phasecodes[c],calfile2);
          if (verbose > 1) printf("Freq index: %d Phase Count: %d\n",ii,count);
          count=fread(pwr_mag[ii][c],sizeof(double),num_phasecodes[c],calfile2);
          if (verbose > 1) printf("Freq index: %d Pwr-mag Count: %d\n",ii,count);
          if (count==0) {
            break;
          }
      }
      if (count==0) {
        if (feof(calfile2)) printf("End of File!\n");
      }
      fclose(calfile2);
      printf("Processing Phase Information for Radar: %s Card: %d\n",radar_name2,c);
      for (p=0;p<num_phasecodes[c];p++) {
//        printf("Phase Code: %d\n",p);

        best_offset[p]=1000;
        best_sumsq[p]=1E100;
        for (o=-20;o<20;o++) {
          offset=360.0*o;
          max_Y=-1000;
          for (i=0;i<num_freqs[c];i++) {
            Y[i]=phase[i][c][p]+offset;
            if (Y[i]> max_Y) max_Y=Y[i];
          }
/*
*  Linear Regression Y=slope*freq
*/
          gsl_fit_mul (freq[c], 1, Y, 1, num_freqs[c], &slope, &slope_variance, &sumsq);
          if (max_Y < 0 ) {
            if (sumsq < best_sumsq[p]) {
            
              best_offset[p]=offset; 
              best_sumsq[p]=sumsq;
              best_var[p]=slope_variance;
              best_slope[p]=slope;
            }
          }
        } // end offset loop
        printf("%d :: Best Offset: %lf  Phase0: %lf\n",p,best_offset[p],phase[0][c][p]);
/*
*  Build the timedelays arrays using the best phase offset from the linear regression
*/
        for (i=0;i<num_freqs[c];i++) {
          time0[i]=phase_to_timedelay(phase[i][c][0]+best_offset[0],freq[c][i]);
          timedelay2[i][c][p]=phase_to_timedelay(phase[i][c][p]+best_offset[p],freq[c][i]);
//          printf("Phase: %d :: Freq %e :: Time Delay %% Diff  %lf\n",
//                p,freq[c][i],fabs((timedelay[i][c][p]-expected_timedelay(p)-time0[i])/(expected_timedelay(p)+time0[i]))*100);
        }  //freq loop
        if(timedelay2[0][c][p] < minimum_timedelay) {
          printf("ERROR: Timedelay2 switch error: %d %lf\n",p,timedelay2[0][c][p]);
          exit(0);
        }
        if(pwr_mag[0][c][p] < pwr_threshold) {
          printf("ERROR: pwr_mag switch error: %d\n",p);
          exit(0);
        }

      }  //phasecode loop


    } // end calfile2 check
    max_var0=-1000;
    for (b=0;b<13;b++) {
      max_var[b]=-1000.0;
    }
    for (p=0;p<num_phasecodes[c];p++) {
      for (i=0;i<num_freqs[c];i++) {
        diff=timedelay1[i][c][p]-timedelay2[i][c][p];
        var=fabs(diff);
        if (p == 0 ) {
         if (var > max_var0) max_var0=var;
        }
        for (b=0;b<13;b++) {
          hmm=pow(2,b);
          if( p == hmm ) {
            if (var > max_var[b]) max_var[b]=var;
          }
        }
      }
    }

    printf("Code: %d Max Diff (ns): %lf\n",0,max_var0);
    for (b=0;b<13;b++) {
      printf("Code: %d Max Diff (ns): %lf\n",(int)pow(2,b),max_var[b]);
    }
  }  // End card Loop

} // end of main
Esempio n. 6
0
int
main (void)
{


  double x[1000], y[1000], w[1000];

  size_t xstride = 2, wstride = 3, ystride = 5;
  size_t i;

  for (i = 0; i < norris_n; i++) 
    {
      x[i*xstride] = norris_x[i];
      w[i*wstride] = 1.0;
      y[i*ystride] = norris_y[i];
    }

  gsl_ieee_env_setup();

  {
    double c0, c1, cov00, cov01, cov11, sumsq;
       
    double expected_c0 = -0.262323073774029;
    double expected_c1 =  1.00211681802045; 
    double expected_cov00 = pow(0.232818234301152, 2.0);
    double expected_cov01 = -7.74327536339570e-05;  /* computed from octave */
    double expected_cov11 = pow(0.429796848199937E-03, 2.0);
    double expected_sumsq = 26.6173985294224;
    
    gsl_fit_linear (x, xstride, y, ystride, norris_n, 
                    &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
    
    /* gsl_fit_wlinear (x, xstride, w, wstride, y, ystride, norris_n, 
                     &c0, &c1, &cov00, &cov01, &cov11, &sumsq); */
  
    gsl_test_rel (c0, expected_c0, 1e-10, "norris gsl_fit_linear c0") ;
    gsl_test_rel (c1, expected_c1, 1e-10, "norris gsl_fit_linear c1") ;
    gsl_test_rel (cov00, expected_cov00, 1e-10, "norris gsl_fit_linear cov00") ;
    gsl_test_rel (cov01, expected_cov01, 1e-10, "norris gsl_fit_linear cov01") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "norris gsl_fit_linear cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "norris gsl_fit_linear sumsq") ;
  }

  {
    double c0, c1, cov00, cov01, cov11, sumsq;
       
    double expected_c0 = -0.262323073774029;
    double expected_c1 =  1.00211681802045; 
    double expected_cov00 = 6.92384428759429e-02;  /* computed from octave */
    double expected_cov01 = -9.89095016390515e-05; /* computed from octave */
    double expected_cov11 = 2.35960747164148e-07;  /* computed from octave */
    double expected_sumsq = 26.6173985294224;
    
    gsl_fit_wlinear (x, xstride, w, wstride, y, ystride, norris_n, 
                     &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
  
    gsl_test_rel (c0, expected_c0, 1e-10, "norris gsl_fit_wlinear c0") ;
    gsl_test_rel (c1, expected_c1, 1e-10, "norris gsl_fit_wlinear c1") ;
    gsl_test_rel (cov00, expected_cov00, 1e-10, "norris gsl_fit_wlinear cov00") ;
    gsl_test_rel (cov01, expected_cov01, 1e-10, "norris gsl_fit_wlinear cov01") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "norris gsl_fit_wlinear cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "norris gsl_fit_wlinear sumsq") ;
  }

  for (i = 0; i < noint1_n; i++) 
    {
      x[i*xstride] = noint1_x[i];
      w[i*wstride] = 1.0;
      y[i*ystride] = noint1_y[i];
    }

  {
    double c1, cov11, sumsq;
       
    double expected_c1 = 2.07438016528926; 
    double expected_cov11 = pow(0.165289256198347E-01, 2.0);  
    double expected_sumsq = 127.272727272727;
    
    gsl_fit_mul (x, xstride, y, ystride, noint1_n, &c1, &cov11, &sumsq);
  
    gsl_test_rel (c1, expected_c1, 1e-10, "noint1 gsl_fit_mul c1") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "noint1 gsl_fit_mul cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "noint1 gsl_fit_mul sumsq") ;
  }

  {
    double c1, cov11, sumsq;
       
    double expected_c1 = 2.07438016528926; 
    double expected_cov11 = 2.14661371686165e-05; /* computed from octave */
    double expected_sumsq = 127.272727272727;
    
    gsl_fit_wmul (x, xstride, w, wstride, y, ystride, noint1_n, &c1, &cov11, &sumsq);

    gsl_test_rel (c1, expected_c1, 1e-10, "noint1 gsl_fit_wmul c1") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "noint1 gsl_fit_wmul cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "noint1 gsl_fit_wmul sumsq") ;
  }


  for (i = 0; i < noint2_n; i++) 
    {
      x[i*xstride] = noint2_x[i];
      w[i*wstride] = 1.0;
      y[i*ystride] = noint2_y[i];
    }

  {
    double c1, cov11, sumsq;
       
    double expected_c1 = 0.727272727272727; 
    double expected_cov11 = pow(0.420827318078432E-01, 2.0);  
    double expected_sumsq = 0.272727272727273;
    
    gsl_fit_mul (x, xstride, y, ystride, noint2_n, &c1, &cov11, &sumsq);
  
    gsl_test_rel (c1, expected_c1, 1e-10, "noint2 gsl_fit_mul c1") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "noint2 gsl_fit_mul cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "noint2 gsl_fit_mul sumsq") ;
  }

  {
    double c1, cov11, sumsq;
       
    double expected_c1 = 0.727272727272727; 
    double expected_cov11 = 1.29870129870130e-02 ; /* computed from octave */
    double expected_sumsq = 0.272727272727273;
    
    gsl_fit_wmul (x, xstride, w, wstride, y, ystride, noint2_n, &c1, &cov11, &sumsq);

    gsl_test_rel (c1, expected_c1, 1e-10, "noint2 gsl_fit_wmul c1") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "noint2 gsl_fit_wmul cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "noint2 gsl_fit_wmul sumsq") ;
  }

  /* now summarize the results */

  exit (gsl_test_summary ());
}