Bsoft  2.1.0
Bernard's software package
mg_ctf_fit.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "mg_ctf_fit.h"
11 #include "mg_processing.h"
12 #include "simplex.h"
13 #include "moving_average.h"
14 #include "utilities.h"
15 #include "timer.h"
16 
17 #include <sys/stat.h>
18 
19 // Declaration of global variables
20 extern int verbose; // Level of output to the screen
21 
22 // Internal function prototypes
23 double ctf_fit_baseline(Bimage* prad, double real_size, CTFparam& em_ctf, double lores, double hires);
24 double ctf_fit_envelope(Bimage* prad, double real_size, CTFparam& em_ctf, double lores, double hires);
25 
47 {
48  if ( em_ctf.check_defocus() || em_ctf.check_Cs() )
49  cerr << "in img_ctf_radial_average" << endl;
50 
51  long nrad = p->sizeX()/2;
52  Bimage* prad = new Bimage(Double, TSimple, nrad, 1, 1, 1);
53  prad->sampling(p->sampling(0)[0], 1, 1);
54 
55  long i, x, y, z;
56  long iradius;
57  double dx, dy, dz, dr, radius, angle, fraction, cosang, sinang;
58  double smin2 = 1-em_ctf.defocus_deviation()/em_ctf.defocus_average();
59  double smax2 = 1+em_ctf.defocus_deviation()/em_ctf.defocus_average();
60  vector<double> num(nrad);
61  double* rdata = (double *) prad->data_pointer();
62 
63  if ( verbose & VERB_FULL ) {
64  cout << "Radial average for CTF fitting:" << endl;
65  cout << "Size: " << nrad << endl;
66  cout << "Power spectrum origin: " << p->image->origin() << endl;
67  cout << "Defocus, deviation, angle: " <<
68  em_ctf.defocus_average() << " " << em_ctf.defocus_deviation() << " " << em_ctf.astigmatism_angle() << endl;
69  }
70 
71  if ( p->sizeZ() < 2 ) {
72  if ( verbose & VERB_DEBUG )
73  cout << "DEBUG img_ctf_radial_average: doing image " << n+1 << endl;
74  for ( i=0; i<nrad; i++ ) num[i] = 0;
75  for ( y=0; y<p->sizeY(); y++ ) {
76  dy = y - p->image[n].origin()[1];
77  for ( x=0; x<p->sizeX(); x++ ) {
78  dx = x - p->image[n].origin()[0];
79  radius = sqrt(dx*dx + dy*dy);
80  iradius = (long) radius;
81  if ( iradius < nrad ) {
82  angle = atan2(dy, dx) - em_ctf.astigmatism_angle();
83  cosang = cos(angle);
84  sinang = sin(angle);
85  dr = smax2*cosang*cosang + smin2*sinang*sinang;
86  if ( dr >= 0) {
87  radius *= sqrt(dr);
88 // if ( !isfinite(radius) )
89 // cerr << "Warning: radius for " << iradius << " not finite!" << endl;
90  iradius = (long) radius;
91  if ( iradius < nrad - 1 ) {
92  fraction = radius - iradius;
93  num[iradius] += 1.0 - fraction;
94  i = p->index(0,x,y,0,n);
95  rdata[iradius] += (1.0 - fraction)*(*p)[i];
96  iradius++;
97  if ( iradius < nrad ) {
98  num[iradius] += fraction;
99  rdata[iradius] += fraction*(*p)[i];
100  }
101  }
102  }
103  }
104  }
105  }
106  for ( iradius=0; iradius<nrad; iradius++ )
107  if ( num[iradius] ) rdata[iradius] /= num[iradius];
108  else rdata[iradius] = rdata[iradius-1];
109  } else {
110  if ( verbose & VERB_DEBUG )
111  cout << "DEBUG img_ctf_radial_average: calculating 3D RPS" << endl;
112  for ( i=0; i<nrad; i++ ) num[i] = 0;
113  for ( z=0; z<p->sizeZ(); z++ ) {
114  dz = z - p->image[n].origin()[2];
115  for ( y=0; y<p->sizeY(); y++ ) {
116  dy = y - p->image[n].origin()[1];
117  for ( x=0; x<p->sizeX(); x++ ) {
118  dx = x - p->image[n].origin()[0];
119  radius = sqrt(dx*dx + dy*dy + dz*dz);
120  iradius = (long) radius;
121  if ( iradius < nrad - 1 ) {
122  fraction = radius - iradius;
123  num[iradius] += 1.0 - fraction;
124  i = p->index(0,x,y,z,n);
125  rdata[n*nrad+iradius] += (1.0 - fraction)*(*p)[i];
126  iradius++;
127  if ( iradius < nrad ) {
128  num[iradius] += fraction;
129  rdata[iradius] += fraction*(*p)[i];
130  }
131  }
132  }
133  }
134  }
135  for ( iradius=0; iradius<nrad; iradius++ )
136  if ( num[iradius] ) rdata[iradius] /= num[iradius];
137  }
138 
139  prad->statistics();
140 
141  return prad;
142 }
143 
144 double img_ctf_fit_residual(Bimage* p, long n, CTFparam& em_ctf, double lores, double hires)
145 {
146  long rmin = (long) (p->real_size()[0]/lores);
147  long rmax = (long) (p->real_size()[0]/hires);
148 
149  long i, m(0);
150  double s, s2, b, e, c, d, R(0);
151 
152  Bimage* prad = img_ctf_radial_average(p, n, em_ctf);
153 
154  for ( i=rmin; i<=rmax; ++i ) {
155  s = i/p->real_size()[0];
156  s2 = s*s;
157  b = em_ctf.calc_baseline(s);
158  e = em_ctf.calc_envelope(s);
159  c = em_ctf.calculate(s2, 0);
160  c *= c;
161  d = b + e*c - (*prad)[i];
162  d /= b;
163  R += d*d;
164  m++;
165  cout << i << tab << (*prad)[i] << tab << b + e*c << tab << d << endl;
166  }
167 
168  delete prad;
169 
170  R = sqrt(R/m);
171 
172  return R;
173 }
174 
210 double img_ctf_fit(Bimage* p, long n, CTFparam& em_ctf, double lores, double hires,
211  double def_start, double def_end, double def_inc, int flag)
212 {
213  p->check_resolution(hires);
214  if ( lores < hires ) lores = p->real_size()[0];
215  if ( lores > p->real_size()[0] ) lores = p->real_size()[0];
216 
217  long best_tile_size(def_end*em_ctf.lambda()/
218  (hires*p->sampling(0)[0]));
219  if ( p->size()[0] < best_tile_size ) {
220  cerr << "Warning: Tile size too small! (" << p->size()[0] << " < " << best_tile_size << ")" << endl;
221  cerr << "Change the tile size, defocus maximum or high resolution limit." << endl << endl;
222  bexit(-1);
223  }
224 
225  if ( hires > 3 && em_ctf.baseline_type() > 3 )
226  em_ctf.baseline_type(em_ctf.baseline_type()-3);
227 
228  if ( verbose & VERB_PROCESS ) {
229  cout << "Determining CTF parameters:" << endl;
230  cout << "Sampling: " << p->sampling(0)[0] << " A" << endl;
231  em_ctf.show();
232  cout << "Resolution range: " << hires << " - " << lores << " A" << endl;
233  cout << "Baseline type: " << em_ctf.baseline_type() << endl;
234  cout << "Envelope type: " << em_ctf.envelope_type() << endl;
235  cout << "Defocus min, max, inc: " << def_start << " - " << def_end << " ∆ " << def_inc << endl << endl;
236  }
237 
238  if ( em_ctf.defocus_average() < 1 || em_ctf.defocus_average() > 2e5 )
239  em_ctf.defocus_average(2e4);
240 
241  em_ctf.defocus_deviation(0);
242  em_ctf.astigmatism_angle(0);
243  em_ctf.fom(0);
244 
245  double fom = img_ctf_find_defocus(p, n, em_ctf, lores, hires, def_start, def_end, def_inc);
246 
247  img_ctf_fit_baseline(p, n, em_ctf, lores, hires);
248 
249  img_ctf_fit_envelope(p, n, em_ctf, lores, hires);
250 
251  long i(0), imax(10);
252  double pfom(0);
253 // double hires_astig = 2*hires*lores/(lores+hires);
254 // double hires_dec = (hires_astig - hires)/imax;
255 
256  if ( flag ) for ( i=0; i<imax && ( fabs(fom - pfom) > 1e-30 ); ++i ) {
257 // if ( flag ) for ( i=0; i<imax; ++i ) {
258  if ( verbose )
259  cout << "Defocus [" << i << "]: "
260  << em_ctf.defocus_average()
261  << " +- " << em_ctf.defocus_deviation()
262  << " @ " << em_ctf.astigmatism_angle()*180/M_PI
263  << " (" << fom << ")" << endl;
264  pfom = fom;
265 // cout << i << tab << hires_astig << endl;
266  def_start = em_ctf.defocus_average()/10;
267  def_end = em_ctf.defocus_average()*10;
268  def_inc = def_start;
269 // cout << def_start << " - " << def_end << " ∆ " << def_inc << endl;
270  fom = img_ctf_fit_astigmatism(p, n, em_ctf, lores, hires);
271  img_ctf_find_defocus(p, n, em_ctf, lores, hires, def_start, def_end, def_inc);
272  img_ctf_fit_baseline(p, n, em_ctf, lores, hires);
273  img_ctf_fit_envelope(p, n, em_ctf, lores, hires);
274 // fom = img_ctf_fit_residual(p, n, em_ctf, lores, hires);
275 // hires_astig -= hires_dec;
276  }
277 
278  double wri = img_water_ring_index(p, n, em_ctf);
279 
280  if ( verbose ) {
281  cout << "Best defocus: "
282  << em_ctf.defocus_average()
283  << " +- " << em_ctf.defocus_deviation()
284  << " @ " << em_ctf.astigmatism_angle()*180/M_PI
285  << " (" << fom << ")" << endl;
286  cout << "Water ring index: "
287  << wri << endl;
288 // << p->meta_data()["image"][n]["water_ring"].real() << endl;
289  }
290 
291  return wri;
292 }
293 
308 double img_ctf_fit_baseline(Bimage* p, long n, CTFparam& em_ctf, double lores, double hires)
309 {
310  Bimage* prad = img_ctf_radial_average(p, n, em_ctf);
311 
312  if ( !prad ) {
313  cerr << "Error in img_ctf_fit_baseline: No radial power spectrum calculated!" << endl;
314  return -1;
315  }
316 
317  double R = ctf_fit_baseline(prad, p->real_size()[0], em_ctf, lores, hires);
318 
319  delete prad;
320 
321  if ( verbose & VERB_PROCESS )
322  em_ctf.show_baseline();
323 
324  return R;
325 }
326 
341 double img_ctf_fit_envelope(Bimage* p, long n, CTFparam& em_ctf, double lores, double hires)
342 {
343  Bimage* prad = img_ctf_radial_average(p, n, em_ctf);
344 
345  if ( !prad ) {
346  cerr << "Error in img_ctf_fit_envelope: No radial power spectrum calculated!" << endl;
347  return -1;
348  }
349 
350  double R = ctf_fit_envelope(prad, p->real_size()[0], em_ctf, lores, hires);
351 
352  delete prad;
353 
354  if ( verbose & VERB_PROCESS )
355  em_ctf.show_envelope();
356 
357  return R;
358 }
359 
360 double ctf_test_defocus(CTFparam& em_ctf, double def, long nx,
361  double step_size, vector<double>& r, long rmin, long rmax)
362 {
363  long i;
364  long nr(rmax-rmin+1);
365  double fom(0);
366 
367  em_ctf.defocus_average(def);
368 
369  vector<double> c = em_ctf.calculate(nx, 1, step_size);
370 
371  if ( !c.size() ) {
372  cerr << "Error in ctf_test_defocus: CTF not calculated!" << endl;
373  return fom;
374  }
375 
376  for ( i=rmin; i<=rmax; i++ ) fom += (c[i]*c[i] - 0.5)*r[i];
377  fom /= nr;
378 
379  return fom;
380 }
381 
400 double img_ctf_find_defocus(Bimage* p, long n, CTFparam& em_ctf,
401  double lores, double hires,
402  double def_start, double def_end, double def_inc)
403 {
404  if ( def_start < 100 ) def_start = 100;
405  if ( def_end > 2e5 ) def_end = 2e5;
406  if ( em_ctf.defocus_average() < def_start || em_ctf.defocus_average() > def_end )
407  em_ctf.defocus_average((def_end - def_start)/2);
408 
409  p->check_resolution(hires);
410  if ( lores < hires ) lores = 100;
411  if ( lores > 100 ) lores = 100;
412 
413  Bimage* prad = img_ctf_radial_average(p, n, em_ctf);
414 
415  long i, nx(p->sizeX()/2);
416  double step_size = 1/p->real_size()[0];
417  long rmin = (long) (p->real_size()[0]/lores);
418  long rmax = (long) (p->real_size()[0]/hires);
419 
420  for ( i=0; i<rmin; i++ ) prad->set(i, (*prad)[rmin]);
421 
422  vector<double> r = moving_polynomial(2, nx, (double *) prad->data_pointer(), nx/5);
423 
424  for ( i=0; i<nx; i++ )
425  if ( r[i] ) r[i] = (*prad)[i]/r[i] - 1;
426  else r[i] = 0;
427 
428  double def, def_best(em_ctf.defocus_average());
429  double fom, fom_max(-2);
430 
431  if ( verbose & VERB_PROCESS ) {
432  cout << "Defocus range: " << def_start*1e-4 << " - " << def_end*1e-4 << " um" << endl;
433  cout << "Defocus increment: " << def_inc*1e-4 << " um" << endl;
434  cout << "Frequency pixel range: " << rmin << " - " << rmax << endl;
435  cout << "Frequency step size: " << step_size << " 1/A" << endl;
436  }
437 
438  double ds(def_start), de(def_end);
439 
440  if ( verbose & VERB_FULL )
441  cout << "Defocus\tCC" << endl;
442  while ( def_inc >= 50 ) {
443  for ( def=ds; def<=de; def+=def_inc ) {
444  fom = ctf_test_defocus(em_ctf, def, nx, step_size, r, rmin, rmax);
445  if ( fom_max < fom ) {
446  fom_max = fom;
447  def_best = def;
448  }
449  if ( verbose & VERB_FULL )
450  cout << def*1e-4 << tab << fom << endl;
451  }
452  ds = def_best - 2*def_inc;
453  de = def_best + 2*def_inc;
454  def_inc /= 1.6;
455  if ( ds < def_start ) ds = def_start;
456  if ( de > def_end ) de = def_end;
457 // cout << "def_start=" << def_start << endl;
458  }
459 
460  delete prad;
461 
462  em_ctf.defocus_average(def_best);
463  if ( fom_max > -1 ) em_ctf.fom(fom_max);
464 
465  return em_ctf.fom();
466 }
467 
468 //Expects a radial average
469 Bimage* img_ctf_radial_subtract_baseline(Bimage* prad, double real_size, CTFparam& em_ctf)
470 {
471  long i;
472  double s, recip_interval = 1.0/real_size;
473  Bimage* prmb = prad->copy();
474 
475  for ( i=0; i<prmb->sizeX(); i++ ) {
476  s = i*recip_interval;
477  prmb->set(i, (*prmb)[i] - em_ctf.calc_baseline(s));
478  }
479 
480  return prmb;
481 }
482 
483 /*
484  Expects a radial power spectrum
485  Measures the astigmatism between the resolution limits
486  The variable real_size is the box x-size in angstrom
487  Zeroes are calculated, given in spatial frequency units
488  For every two successive zeroes between the resolution limits:
489  the minimum within a kernel is found at each zero: [ib1]:b1 and [ib2]:b2
490  the maximum halfway between the zeroes within a kernel is found: [im]:m
491  Measure: sum(im*(m - (b1+b2)/2))
492  the intent is that this measure should be maximum for the correct astigmatism parameters
493 */
494 double img_ctf_astigmatism_measure(Bimage* p, long n, double real_size, CTFparam& em_ctf, double lores, double hires)
495 {
496  Bimage* prad = img_ctf_radial_average(p, n, em_ctf);
497 
498  if ( !prad ) {
499  cerr << "Error in img_ctf_astigmatism_measure: No radial power spectrum calculated!" << endl;
500  return -1;
501  }
502 
503  double pixel_size = prad->sampling(0)[0];
504  if ( pixel_size <= 0 ) pixel_size = 1;
505 
506  long i, ib1, ib2, im, j, nn(0);
507  long k = prad->sizeX()/100;
508  if ( k < 2 ) k = 2;
509  long ilo = (long) (real_size/lores + 0.5);
510  long ihi = (long) (real_size/hires + 0.5);
511  if ( ihi >= prad->sizeX() ) ihi = prad->sizeX() - 1;
512  double b1, b2, m, OM(0);
513  double max_s = 0.45/pixel_size;
514 
515  Bimage* prmb = img_ctf_radial_subtract_baseline(prad, real_size, em_ctf);
516 
517  vector<double> zero = em_ctf.zeroes(max_s);
518  long nz(zero.size());
519 
520  if ( nz > prad->sizeX() ) nz = prad->sizeX();
521 
522  for ( i=1; i<nz; i++ ) {
523  ib1 = (long) (zero[i-1]*real_size + 0.5);
524  ib2 = (long) (zero[i]*real_size + 0.5);
525  im = (ib1 + ib2)/2;
526  if ( im >= ilo && im <= ihi ) {
527  for ( b1=(*prmb)[ib1], j=ib1-k; j<=ib1+k; j++ )
528  if ( b1 > (*prmb)[j] ) b1 = (*prmb)[j];
529  for ( b2=(*prmb)[ib2], j=ib2-k; j<=ib2+k; j++ )
530  if ( b2 > (*prmb)[j] ) b2 = (*prmb)[j];
531  for ( m=(*prmb)[im], j=im-k; j<=im+k; j++ )
532  if ( m < (*prmb)[j] ) m = (*prmb)[j];
533  OM += im*(m - (b1 + b2)/2);
534  nn += im;
535  }
536  }
537 
538  OM /= nn;
539 
540  delete prad;
541  delete prmb;
542 
543  if ( verbose & VERB_FULL )
544  cout << "Fitting measure: " << OM << endl;
545 
546  return OM;
547 }
548 
566 double img_ctf_fit_astigmatism(Bimage* p, long n, CTFparam& em_ctf, double lores, double hires)
567 {
568  long i, j, na;
569  double real_size = p->real_size()[0];
570  double angle, angle_min(-M_PI_2), angle_max(M_PI_2), angle_range(M_PI), angle_inc(M_PI/9);
571  double def_dev(100), ddiff, prev_fom(0);
572  double best_dev(em_ctf.defocus_deviation()), best_ang(em_ctf.astigmatism_angle());
573  if ( em_ctf.defocus_deviation() > 0 ) def_dev = em_ctf.defocus_deviation(); // Use the input deviation
574  double* fom;
575  CTFparam* ec;
576 
577  double best_fom = img_ctf_astigmatism_measure(p, n, real_size, em_ctf, lores, hires);
578 
579  if ( verbose & VERB_FULL )
580  cout << "Starting astigmatism: " << em_ctf.defocus_average() << " "
581  << em_ctf.defocus_deviation() << " " << em_ctf.astigmatism_angle() << " " << best_fom << endl;
582 
583  long maxiter(20);
584  for ( i=0; i<maxiter && ( best_fom/prev_fom - 1 > 1e-10 ); i++ ) {
585  for ( na=0, angle=angle_min; angle<=angle_max; angle+=angle_inc ) na++;
586  ec = new CTFparam[na];
587  fom = new double[na];
588  em_ctf.defocus_deviation(def_dev);
589  for ( angle=angle_min, j=0; angle<=angle_max; angle+=angle_inc, j++ ) {
590  ec[j] = em_ctf;
591  ec[j].astigmatism_angle(angle);
592  }
593 
594 #ifdef HAVE_GCD
595  dispatch_apply(na, dispatch_get_global_queue(0, 0), ^(size_t k){
596  fom[k] = img_ctf_astigmatism_measure(p, n, real_size, ec[k], lores, hires);
597  });
598 #else
599 #pragma omp parallel for
600  for ( j=0; j<na; j++ ) {
601  fom[j] = img_ctf_astigmatism_measure(p, n, real_size, ec[j], lores, hires);
602  }
603 #endif
604 
605  for ( j=0; j<na; j++ ) {
606  if ( best_fom < fom[j] ) {
607  prev_fom = best_fom;
608  best_fom = fom[j];
609  best_dev = def_dev;
610  best_ang = ec[j].astigmatism_angle();
611  }
612  if ( verbose & VERB_FULL )
613  cout << em_ctf.defocus_average() << tab << def_dev << tab << ec[j].astigmatism_angle() << tab << fom[j] << tab <<
614  em_ctf.defocus_average() << tab << best_dev << tab << best_ang << tab << best_fom << endl;
615  }
616  ddiff = best_dev - def_dev;
617 
618  delete[] ec;
619  delete[] fom;
620 
621  if ( ddiff == 0 ) {
622  def_dev += 10;
623  angle_range /= 1.5;
624  angle_min = best_ang - angle_range/2;
625  angle_max = best_ang + angle_range/2;
626  angle_inc /= 1.5;
627  } else def_dev += ddiff/1.5;
628  }
629 
630  if ( best_ang < -M_PI_2 ) best_ang += M_PI;
631  if ( best_ang > M_PI_2 ) best_ang -= M_PI;
632 
633  em_ctf.defocus_deviation(best_dev);
634  em_ctf.astigmatism_angle(best_ang);
635 
636  if ( verbose & VERB_PROCESS )
637  cout << "Astigmatism refined: " << em_ctf.defocus_average() << " +- "
638  << em_ctf.defocus_deviation() << " @ " << em_ctf.astigmatism_angle()*180.0/M_PI << " (OM = " << best_fom << ")" << endl;
639 
640  return best_fom;
641 }
642 
660 double img_water_ring_index(Bimage* p, long img_num, CTFparam& em_ctf)
661 {
662  Bimage* prad = img_ctf_radial_average(p, img_num, em_ctf);
663 
664  double wri = img_water_ring_index(prad);
665 
666  delete prad;
667 
668  if ( verbose & VERB_FULL )
669  cout << "Water ring index for image " << img_num << ": " << wri << endl;
670 
671  return wri;
672 }
673 
690 {
691  if ( !prad ) {
692  cerr << "Error in img_water_ring_index: No radial power spectrum calculated!" << endl;
693  return -1;
694  }
695 
696  long i, n1(0), n2(0), nw(0);
697  double s, b1(0), b2(0), wp(0), wri(0);
698  double f = 0.5/prad->real_size()[0];
699 
700  for ( i=0, s=f/2; i<prad->sizeX() && s < 0.4; ++i, s+=f ) if ( s > 0.1 ) {
701  if ( s < 0.2 ) {
702  b1 += (*prad)[i];
703  n1++;
704  } else if ( s > 0.3 ) {
705  b2 += (*prad)[i];
706  n2++;
707  } else {
708  wp += (*prad)[i];
709  nw++;
710  }
711  }
712 
713  n1 += n2;
714  if ( n1 ) b1 = (b1 + b2)/n1;
715  if ( nw ) wp /= nw;
716 
717  if ( b1 ) wri = wp/b1 - 1;
718 
719  return wri;
720 }
721 
722 double gaussian_R(Bsimplex& simp)
723 {
724  long i;
725  double R(0), df, s2;
726  vector<double>& f = simp.dependent_values();
727  vector<double>& x = simp.independent_values();
728 
729  for ( i=0; i<simp.points(); i++ ) {
730  s2 = x[i]*x[i];
731  df = f[i] - (simp.parameter(0) + simp.parameter(1)*exp(simp.parameter(2)*s2));
732  R += df*df;
733  }
734 
735  R = sqrt(R/i);
736 
737  return R;
738 }
739 
740 double gaussian1_R(Bsimplex& simp)
741 {
742  long i;
743  double R(0), df, ds, w, wsum(0);
744  double cen(M_PI_2 / ( simp.points() - 1));
745  vector<double>& f = simp.dependent_values();
746  vector<double>& x = simp.independent_values();
747 
748  for ( i=0; i<simp.points(); i++ ) {
749  w = 1 - cos(cen*i);
750  ds = simp.parameter(3) - x[i];
751  df = f[i] - (simp.parameter(0) + simp.parameter(1)*x[i] +
752  simp.parameter(2)*exp(simp.parameter(4)*ds*ds));
753  R += w*df*df;
754  wsum += w;
755  }
756 
757  R = sqrt(R/wsum);
758 
759  return R;
760 }
761 
763 {
764  long i;
765  double R(0), df, s2;
766  vector<double>& f = simp.dependent_values();
767  vector<double>& x = simp.independent_values();
768 
769  for ( i=0; i<simp.points(); i++ ) {
770  s2 = x[i]*x[i];
771  df = f[i] - (simp.parameter(0) + simp.parameter(1)*exp(simp.parameter(2)*s2) +
772  simp.parameter(3)*exp(simp.parameter(4)*s2));
773  R += df*df;
774  }
775 
776  R = sqrt(R/i);
777 
778  return R;
779 }
780 
782 {
783  long i;
784  double R(0), df;
785  vector<double>& f = simp.dependent_values();
786  vector<double>& x = simp.independent_values();
787 
788  for ( i=0; i<simp.points(); i++ ) {
789  df = f[i] - (simp.parameter(0) + simp.parameter(1)*
790  exp(simp.parameter(2)*sqrt(x[i]) + simp.parameter(3)*x[i]*x[i]));
791  R += df*df;
792  }
793 
794  R = sqrt(R/i);
795 
796  return R;
797 }
798 
799 /*
800 @brief Fits a double gaussian function.
801 @param n number of data points.
802 @param *x x array (at least order+1 values).
803 @param *y y array (at least order+1 values).
804 @param coeff array in which 5 coefficients are returned
805  (if NULL, no coefficients returned).
806 @return double R value.
807 
808  Double gaussian function:
809  y = a + b*exp(c*s^2) + d*exp(f*s^2)
810 **/
811 double ctf_fit_polynomial(long n, vector<double>& x, vector<double>& y, vector<double>& coeff)
812 {
813  long nc(5);
814  if ( n < 5 ) nc = n - 1;
815 
816  double R = fit_polynomial(n, x, y, nc, coeff);
817 
818  return R;
819 }
820 
821 /*
822 @brief Fits a double gaussian function.
823 @param n number of data points.
824 @param *x x array (at least order+1 values).
825 @param *y y array (at least order+1 values).
826 @param coeff array in which 5 coefficients are returned
827  (if NULL, no coefficients returned).
828 @return double R value.
829 
830  Double gaussian function:
831  y = a + b*exp(c*s^2) + d*exp(f*s^2)
832 **/
833 double ctf_fit_double_gaussian_baseline(long n, vector<double>& x, vector<double>& y, vector<double>& coeff)
834 {
835  long i;
836 
837  Bsimplex simp(1, 5, 0, n, x, y);
838 
839  simp.parameters(5, coeff);
840  simp.limits(0, 0, 1000);
841  simp.limits(1, 0, 100);
842  simp.limits(2, -1000, -0.1);
843 
844  if ( n > 5 ) {
845  simp.limits(3, 0, 1000);
846  simp.limits(4, -10000, -1);
847  }
848 
849  double R = simp.run(10000, 0.01, double_gaussian_R);
850 
851  for ( i=0; i<5; i++ ) coeff[i] = simp.parameter(i);
852 
853  return R;
854 }
855 
856 /*
857 @brief Fits an EMAN-style baseline function.
858 @param n number of data points.
859 @param *x x array (at least order+1 values).
860 @param *y y array (at least order+1 values).
861 @param coeff array in which 4 coefficients are returned
862  (if NULL, no coefficients returned).
863 @return double R value.
864 
865  EMAN baseline function:
866  y = a + b*exp( c*sqrt(s) + d*s^2 )
867 **/
868 double ctf_fit_eman_to_baseline(long n, vector<double>& x, vector<double>& y, vector<double>& coeff)
869 {
870  long i;
871 
872  Bsimplex simp(1, 4, 0, n, x, y);
873 
874  simp.parameters(4, coeff);
875  simp.limits(0, 0, 1000);
876  simp.limits(1, 0, 1000);
877  simp.limits(2, -100, 100);
878  simp.limits(3, -100, 0);
879 
880  double R = simp.run(10000, 0.001, eman_baseline_R);
881 
882  for ( i=0; i<4; i++ ) coeff[i] = simp.parameter(i);
883 
884  return R;
885 }
886 
887 /*
888 @brief Fits a gaussian function.
889 @param n number of data points.
890 @param *x x array (at least order+1 values).
891 @param *y y array (at least order+1 values).
892 @param coeff array in which 3 coefficients are returned
893  (if NULL, no coefficients returned).
894 @return double R value.
895 
896  Gaussian function:
897  y = a + b*exp(c*s^2)
898 **/
899 double ctf_fit_gaussian(long n, vector<double>& x, vector<double>& y, vector<double>& coeff)
900 {
901  long i;
902 
903  Bsimplex simp(1, 3, 0, n, x, y);
904 
905  simp.parameters(3, coeff);
906  if ( coeff[0] ) simp.limits(0, 0, 1000);
907  simp.limits(1, 0.001, 100);
908  simp.limits(2, -1e4, -0.1);
909 
910  double R = simp.run(10000, 0.01, gaussian_R);
911 
912  for ( i=0; i<3; i++ ) coeff[i] = simp.parameter(i);
913 
914  return R;
915 }
916 
917 double ctf_fit_baseline_bump(long n, vector<double>& x, vector<double>& y, vector<double>& coeff)
918 {
919  long i, ilo(0), ns;
920  double slo(0.2), shi(0.3);
921 
922  coeff[5] = 0;
923  coeff[6] = 0.25;
924  coeff[7] = -1e4;
925 
926  vector<double> xb, yb;
927 
928  for ( i=ns=0; i<n; i++ ) if ( x[i] > slo && x[i] < shi ) {
929  if ( !ilo ) ilo = i;
930  if ( coeff[5] < y[i] ) {
931  coeff[5] = y[i];
932  coeff[6] = x[i];
933  }
934  xb.push_back(x[i]);
935  yb.push_back(y[i]);
936  ns++;
937 // cout << x[i] << tab << y[i] << endl;
938  }
939 
940  double slope((y[ilo+ns-1] - y[ilo])/(x[ilo+ns-1] - x[ilo]));
941  double inter(y[ilo] - slope*x[ilo]);
942 
943  coeff[5] -= 0.5*(y[ilo] + y[ilo+ns-1]);
944 
945  Bsimplex simp(1, 5, 0, ns, xb, yb);
946 
947 // simp.parameters(5, &coeff[3]);
948  simp.parameter(0, inter);
949  simp.parameter(1, slope);
950  simp.parameter(2, coeff[5]);
951  simp.parameter(3, coeff[6]);
952  simp.parameter(4, coeff[7]);
953  simp.limits(0, 0.1*inter, 10*inter);
954  simp.limits(1, -5*fabs(slope), 5*fabs(slope));
955  simp.limits(2, 0.1*coeff[5], 10*coeff[5]);
956  simp.limits(3, 0.2, 0.3);
957  simp.limits(4, -1e5, -1e3);
958 
959 // for ( i=0; i<5; i++ ) cout << tab << simp.parameter(i);
960 // cout << endl;
961 
962  double R = simp.run(10000, 0.0001, gaussian1_R);
963 
964  for ( i=2; i<5; i++ ) coeff[i+3] = simp.parameter(i);
965 
966 // for ( i=0; i<5; i++ ) cout << tab << simp.parameter(i);
967 // cout << tab << R << endl;
968 
969  return R;
970 }
971 
972 /*
973 @brief Fits a double gaussian function.
974 @param n number of data points.
975 @param *x x array (at least order+1 values).
976 @param *y y array (at least order+1 values).
977 @param coeff array in which 5 coefficients are returned
978  (if NULL, no coefficients returned).
979 @return double R value.
980 
981  Double gaussian function:
982  y = a + b*exp(c*s^2) + d*exp(f*s^2)
983  with a = 0
984 **/
985 double ctf_fit_double_gaussian_envelope(long n, vector<double>& x, vector<double>& y, vector<double>& coeff)
986 {
987  long i;
988 
989  Bsimplex simp(1, 5, 0, n, x, y);
990 
991  simp.parameters(5, coeff);
992  if ( coeff[0] ) simp.limits(0, 0, 1000);
993  simp.limits(1, 0.001, 100);
994  simp.limits(2, -500, -0.1);
995 
996  if ( n > 5 ) {
997  simp.limits(3, 0.01, 100);
998  simp.limits(4, -5000, -1);
999  }
1000 
1001  double R = simp.run(10000, 0.01, double_gaussian_R);
1002 
1003  for ( i=0; i<5; i++ ) coeff[i] = simp.parameter(i);
1004 
1005  return R;
1006 }
1007 
1008 double ctf_fit_baseline(Bimage* prad, double real_size, CTFparam& em_ctf, double lores, double hires)
1009 {
1010  double pixel_size = prad->sampling(0)[0];
1011  if ( pixel_size <= 0 ) pixel_size = 1;
1012 
1013  long i, j, ib, nc(4);
1014  long istart = (long) (real_size/lores);
1015  long iend = (long) (real_size/hires);
1016  if ( istart < 1 ) istart = 1;
1017  if ( iend >= prad->sizeX() ) iend = prad->sizeX() - 1;
1018  long w = (iend - istart)/10;
1019 
1020  double recip_interval = 1.0/real_size;
1021  double max_s = 0.45/pixel_size;
1022  long k = prad->sizeX()/100;
1023  if ( k < 1 ) k = 1;
1024 
1025  double R = 1e30;
1026  vector<double> coeff(NCTFPARAM);
1027  for ( i=0; i<NCTFPARAM; i++ ) coeff[i] = em_ctf.baseline(i);
1028 
1029  vector<double> s(prad->sizeX(), 0);
1030  vector<double> d(prad->sizeX(), 0);
1031 
1032  vector<double> zero = em_ctf.zeroes(max_s);
1033  long n(zero.size());
1034 
1035  if ( n > prad->sizeX() ) n = prad->sizeX();
1036 
1037  if ( verbose & VERB_DEBUG )
1038  cout << "DEBUG ctf_fit_baseline: zeroes=" << n << endl;
1039 
1040  double min = 1e10;
1041  double* mavg = NULL;
1042 
1043  if ( n > 3 ) { // Enough zeroes to fit the baseline
1044  for ( i=0; i<n; i++ ) {
1045  ib = (long) (zero[i]*real_size + 0.5);
1046  s[i] = zero[i];
1047  d[i]=(*prad)[ib];
1048  for ( j=(ib>k)?ib-k:0; j<=ib+k && j<prad->sizeX(); j++ ) // minimum within kernel
1049  if ( d[i] > (*prad)[j] ) d[i] = (*prad)[j];
1050  }
1051  } else { // Too few zeroes, use local minima
1052  mavg = moving_average(prad->sizeX(), (double *) prad->data_pointer(), prad->sizeX()/10);
1053  for ( i=istart, n=0; i<iend; i++ ) {
1054  if ( min > (*prad)[i] - mavg[i] ) {
1055  min = (*prad)[i] - mavg[i];
1056  s[n] = recip_interval*i;
1057  d[n] = (*prad)[i];
1058  if ( d[n] > mavg[i] ) d[n] = mavg[i];
1059  }
1060  if ( i%w == 0 ) {
1061  min = 1e10;
1062  n++;
1063  }
1064  }
1065  s[n] = 10;
1066  d[n] = 0;
1067  n++;
1068  delete[] mavg;
1069  }
1070 
1071  if ( verbose & VERB_DEBUG )
1072  cout << "DEBUG ctf_fit_baseline: fitting baseline type " << em_ctf.baseline_type() << endl;
1073 
1074  if ( em_ctf.baseline_type() > 3 ) {
1075  ctf_fit_baseline_bump(n, s, d, coeff);
1076  for ( i=0; i<n; i++ )
1077  d[i] -= coeff[5] * exp(coeff[7] * (coeff[6] - s[i]) * (coeff[6] - s[i]));
1078  }
1079 
1080  switch ( em_ctf.baseline_type() ) {
1081  case 1:
1082  case 4:
1083  if ( n < 5 ) nc = n - 1;
1084  R = fit_polynomial(n, s, d, nc, coeff);
1085  break;
1086  case 2:
1087  case 5:
1088  R = ctf_fit_double_gaussian_baseline(n, s, d, coeff);
1089  break;
1090  case 3:
1091  case 6:
1092  R = ctf_fit_eman_to_baseline(n, s, d, coeff);
1093  break;
1094  default:
1095  cerr << "Error: Baseline type " << em_ctf.baseline_type() << " not supported" << endl;
1096  }
1097 
1098  if ( em_ctf.baseline_type() > 0 && em_ctf.baseline_type() < 7 )
1099  em_ctf.baseline(coeff);
1100 
1101  return R;
1102 }
1103 
1104 double ctf_fit_envelope(Bimage* prad, double real_size, CTFparam& em_ctf, double lores, double hires)
1105 {
1106  double pixel_size = prad->sampling(0)[0];
1107  if ( pixel_size <= 0 ) pixel_size = 1;
1108 
1109  long i, j, ib;
1110  long k = prad->sizeX()/80;
1111  if ( k < 1 ) k = 1;
1112 
1113  double max_s = 0.45/pixel_size;
1114  double R = 1e30;
1115 
1116  vector<double> zero = em_ctf.zeroes(max_s);
1117  long nzero(zero.size());
1118 
1119  if ( nzero > prad->sizeX() ) nzero = prad->sizeX();
1120 
1121  double b, si;
1122  vector<double> coeff(NCTFPARAM);
1123  vector<double> s(nzero);
1124  vector<double> d(nzero);
1125  for ( i=0; i<NCTFPARAM; i++ ) coeff[i] = 0;
1126  for ( i=0; i<nzero; i++ ) s[i] = d[i] = 0;
1127 
1128  ib = (long) (zero[0]*real_size + 0.5);
1129  d[0] = 0;
1130  for ( i=ib/2; i<ib; i++ ) {
1131  si = i/real_size;
1132  b = (*prad)[i] - em_ctf.calc_baseline(si);
1133  if ( d[0] < b ) {
1134  d[0] = b;
1135  s[0] = si;
1136  }
1137  }
1138 
1139  for ( i=1; i<nzero; i++ ) {
1140  s[i] = (zero[i-1]+zero[i])*0.5;
1141  ib = (long) (s[i]*real_size + 0.5);
1142  b = em_ctf.calc_baseline(s[i]);
1143  d[i] = (*prad)[ib] - b;
1144  for ( j=(ib>k)?ib-k:0; j<=ib+k && j<prad->sizeX(); j++ ) // maximum within kernel
1145  if ( d[i] < (*prad)[j] - b ) d[i] = (*prad)[j] - b;
1146  }
1147 
1148  if ( verbose & VERB_DEBUG ) {
1149  cout << "DEBUG ctf_fit_envelope: type " << em_ctf.envelope_type() << endl;
1150  for ( i=0; i<5; i++ ) cout << tab << em_ctf.envelope(i);
1151  cout << endl;
1152  }
1153 
1154  switch ( em_ctf.envelope_type() ) {
1155  case 1: // Single gaussian
1156  for ( i=0; i<2; i++ ) coeff[i+1] = em_ctf.envelope(i);
1157  R = ctf_fit_gaussian(nzero-1, s, d, coeff);
1158  for ( i=0; i<2; i++ ) coeff[i] = coeff[i+1];
1159  coeff[i] = 0;
1160  break;
1161  case 2: // Single gaussian with constant
1162  for ( i=0; i<3; i++ ) coeff[i] = em_ctf.envelope(i);
1163  R = ctf_fit_gaussian(nzero-1, s, d, coeff);
1164  break;
1165  case 3: // Double gaussian
1166  for ( i=0; i<4; i++ ) coeff[i+1] = em_ctf.envelope(i);
1167  R = ctf_fit_double_gaussian_envelope(nzero, s, d, coeff);
1168  for ( i=0; i<4; i++ ) coeff[i] = coeff[i+1];
1169  coeff[i] = 0;
1170  break;
1171  case 4: // Double gaussian with constant
1172  for ( i=0; i<5; i++ ) coeff[i] = em_ctf.envelope(i);
1173  R = ctf_fit_double_gaussian_envelope(nzero, s, d, coeff);
1174  break;
1175  default: break;
1176  }
1177 
1178  if ( verbose & VERB_DEBUG ) {
1179  cout << "DEBUG ctf_fit_envelope:";
1180  for ( i=0; i<5; i++ ) cout << tab << coeff[i];
1181  cout << endl;
1182  }
1183 
1184  em_ctf.envelope(coeff);
1185 
1186  return R;
1187 }
1188 
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
NCTFPARAM
#define NCTFPARAM
Definition: ctf.h:17
gaussian_R
double gaussian_R(Bsimplex &simp)
Definition: mg_ctf_fit.cpp:722
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
ctf_fit_double_gaussian_baseline
double ctf_fit_double_gaussian_baseline(long n, vector< double > &x, vector< double > &y, vector< double > &coeff)
Definition: mg_ctf_fit.cpp:833
CTFparam::calc_envelope
double calc_envelope(double s)
Definition: ctf.h:199
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
img_water_ring_index
double img_water_ring_index(Bimage *p, long img_num, CTFparam &em_ctf)
Calculates the water ring index from a power spectrum.
Definition: mg_ctf_fit.cpp:660
double_gaussian_R
double double_gaussian_R(Bsimplex &simp)
Definition: mg_ctf_fit.cpp:762
img_ctf_fit_residual
double img_ctf_fit_residual(Bimage *p, long n, CTFparam &em_ctf, double lores, double hires)
Definition: mg_ctf_fit.cpp:144
img_ctf_fit_baseline
double img_ctf_fit_baseline(Bimage *p, long n, CTFparam &em_ctf, double lores, double hires)
Fits only the baseline for a given CTF.
Definition: mg_ctf_fit.cpp:308
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
CTFparam::show_envelope
void show_envelope()
Definition: ctf.h:279
img_ctf_fit_astigmatism
double img_ctf_fit_astigmatism(Bimage *p, long n, CTFparam &em_ctf, double lores, double hires)
Fits the astigmatism with a given defocus, baseline and envelope.
Definition: mg_ctf_fit.cpp:566
CTFparam::astigmatism_angle
double astigmatism_angle()
Definition: ctf.h:110
eman_baseline_R
double eman_baseline_R(Bsimplex &simp)
Definition: mg_ctf_fit.cpp:781
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
img_ctf_radial_average
Bimage * img_ctf_radial_average(Bimage *p, long n, CTFparam &em_ctf)
Calculates the power spectrum radial average corrected for astigmatism.
Definition: mg_ctf_fit.cpp:46
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
simplex.h
Nelder and Mead downhill simplex method for generalized parameter fitting.
ctf_fit_gaussian
double ctf_fit_gaussian(long n, vector< double > &x, vector< double > &y, vector< double > &coeff)
Definition: mg_ctf_fit.cpp:899
ctf_fit_envelope
double ctf_fit_envelope(Bimage *prad, double real_size, CTFparam &em_ctf, double lores, double hires)
Definition: mg_ctf_fit.cpp:1104
CTFparam::check_Cs
bool check_Cs()
Definition: ctf.h:137
Bsimplex::dependent_values
vector< double > & dependent_values()
Definition: simplex.h:85
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bsimplex
Structure used in the downhill simplex method.
Definition: simplex.h:44
M_PI_2
#define M_PI_2
Definition: utilities.h:66
ctf_fit_baseline_bump
double ctf_fit_baseline_bump(long n, vector< double > &x, vector< double > &y, vector< double > &coeff)
Definition: mg_ctf_fit.cpp:917
CTFparam::calc_baseline
double calc_baseline(double s)
Definition: ctf.h:171
ctf_fit_eman_to_baseline
double ctf_fit_eman_to_baseline(long n, vector< double > &x, vector< double > &y, vector< double > &coeff)
Definition: mg_ctf_fit.cpp:868
gaussian1_R
double gaussian1_R(Bsimplex &simp)
Definition: mg_ctf_fit.cpp:740
CTFparam::calculate
double calculate(double s2, double angle)
Definition: ctf.h:159
Bsimplex::independent_values
vector< double > & independent_values()
Definition: simplex.h:84
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
ctf_fit_double_gaussian_envelope
double ctf_fit_double_gaussian_envelope(long n, vector< double > &x, vector< double > &y, vector< double > &coeff)
Definition: mg_ctf_fit.cpp:985
moving_average
double * moving_average(long number, double *x, long window)
Calculates a moving average over an array of data.
Definition: moving_average.cpp:28
Bsimplex::points
long points()
Definition: simplex.h:66
img_ctf_find_defocus
double img_ctf_find_defocus(Bimage *p, long n, CTFparam &em_ctf, double lores, double hires, double def_start, double def_end, double def_inc)
Searches for the defocus based on correlation.
Definition: mg_ctf_fit.cpp:400
fit_polynomial
double fit_polynomial(int n, double *x, double *y, int order, double *coeff)
Fits a data set to a polynomial function.
Definition: matrix_linear.cpp:166
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
ctf_fit_polynomial
double ctf_fit_polynomial(long n, vector< double > &x, vector< double > &y, vector< double > &coeff)
Definition: mg_ctf_fit.cpp:811
CTFparam::show
void show()
Definition: ctf.h:242
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bsimplex::run
double run(long maxcycles, double tolerance, double(funk)(Bsimplex &))
Definition: simplex.cpp:76
img_ctf_radial_subtract_baseline
Bimage * img_ctf_radial_subtract_baseline(Bimage *prad, double real_size, CTFparam &em_ctf)
Definition: mg_ctf_fit.cpp:469
CTFparam::check_defocus
bool check_defocus()
Definition: ctf.h:129
ctf_fit_baseline
double ctf_fit_baseline(Bimage *prad, double real_size, CTFparam &em_ctf, double lores, double hires)
Definition: mg_ctf_fit.cpp:1008
CTFparam::defocus_deviation
double defocus_deviation()
Definition: ctf.h:108
Bsimplex::parameter
void parameter(long i, double p)
Definition: simplex.h:71
CTFparam::lambda
double lambda()
Definition: ctf.h:145
CTFparam::zeroes
vector< double > zeroes(double max_s)
Calculates the zeroes of a CTF curve on the spatial frequency scale.
Definition: ctf.cpp:117
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bimage::check_resolution
void check_resolution(double &resolution)
Checks that the resolution falls within reasonable limits.
Definition: Bimage.cpp:635
verbose
int verbose
CTFparam::fom
double fom()
Definition: ctf.h:126
CTFparam::baseline
vector< double > & baseline()
Definition: ctf.h:114
CTFparam
Definition: ctf.h:28
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
CTFparam::show_baseline
void show_baseline()
Definition: ctf.h:256
rwimg.h
Header file for 2D and 3D image I/O.
CTFparam::envelope_type
long envelope_type()
Definition: ctf.h:119
mg_processing.h
Header file for micrograph processing.
moving_average.h
Header file moving average calculations.
img_ctf_astigmatism_measure
double img_ctf_astigmatism_measure(Bimage *p, long n, double real_size, CTFparam &em_ctf, double lores, double hires)
Definition: mg_ctf_fit.cpp:494
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:106
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
ctf_test_defocus
double ctf_test_defocus(CTFparam &em_ctf, double def, long nx, double step_size, vector< double > &r, long rmin, long rmax)
Definition: mg_ctf_fit.cpp:360
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
mg_ctf_fit.h
Header file for CTF (contrast transfer function) functions.
Bsimplex::parameters
long parameters()
Definition: simplex.h:67
Double
@ Double
Definition: utilities.h:162
Bsimplex::limits
void limits(long i, double vlo, double vhi)
Definition: simplex.h:77
moving_polynomial
vector< double > moving_polynomial(long order, long number, double *x, long window)
Calculates a moving polynomial fit over an array of data.
Definition: moving_average.cpp:122
CTFparam::envelope
vector< double > & envelope()
Definition: ctf.h:121
img_ctf_fit_envelope
double img_ctf_fit_envelope(Bimage *p, long n, CTFparam &em_ctf, double lores, double hires)
Fits only the envelope for a given CTF.
Definition: mg_ctf_fit.cpp:341
img_ctf_fit
double img_ctf_fit(Bimage *p, long n, CTFparam &em_ctf, double lores, double hires, double def_start, double def_end, double def_inc, int flag)
Determines the CTF parameters from a power spectrum.
Definition: mg_ctf_fit.cpp:210
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
CTFparam::baseline_type
long baseline_type()
Definition: ctf.h:112