Bsoft  2.1.0
Bernard's software package
ctf.cpp
Go to the documentation of this file.
1 
9 #include "ctf.h"
10 #include "utilities.h"
11 
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14 
21 {
22  int i;
23 
24  if ( ctf.volt() ) volt(ctf.volt()); // Acceleration voltage (volts)
25  if ( ctf.focal_length() ) fl = ctf.focal_length(); // Focal length (angstrom)
26  if ( ctf.objective_aperture() ) oa = ctf.objective_aperture(); // Objective aperture (angstrom)
27  if ( ctf.Cs() ) Cs(ctf.Cs()); // Spherical aberation (angstrom)
28  if ( ctf.Cc() ) cc = ctf.Cc(); // Chromatic aberation (angstrom)
29  if ( ctf.alpha() ) a = ctf.alpha(); // Illumination half-angle (radians)
30  if ( ctf.dE() ) de = ctf.dE(); // Energy spread (eV)
31  if ( ctf.amp_shift() ) amp_shift(ctf.amp_shift()); // CTF amplitude phase shift (radians)
32  if ( ctf.defocus_average() ) def_avg = ctf.defocus_average(); // CTF defocus average (angstrom)
33  if ( ctf.defocus_deviation() ) def_dev = ctf.defocus_deviation(); // CTF defocus deviation (angstrom)
34  if ( ctf.astigmatism_angle() ) ast_ang = ctf.astigmatism_angle(); // CTF astigmatism angle (radians)
35  if ( ctf.baseline_type() ) { // Baseline equation
36  bt = ctf.baseline_type();
37  for ( i=0; i<NCTFPARAM; i++ ) base[i] = ctf.baseline(i);
38  }
39  if ( ctf.envelope_type() ) { // Envelope equation
40  et = ctf.envelope_type();
41  for ( i=0; i<NCTFPARAM; i++ ) env[i] = ctf.envelope(i);
42  }
43 
44  update_terms();
45  zero(1);
46 
47  return 0;
48 }
49 
69 vector<double> CTFparam::calculate(int nrad, int npsi, double step_size)
70 {
71  vector<double> ctf;
72 
73  if ( check_defocus() || check_Cs() ) {
74  cerr << "in CTFparam::calculate" << endl;
75  return ctf;
76  }
77 
78  if ( npsi < 1 ) npsi = 1;
79 
80  int i, j, k;
81  double s2, dphi, defocus;
82  double dpsi = M_PI*2.0/npsi;
83 
84  ctf.resize(nrad*npsi);
85 
86  defocus = def_avg;
87  for ( j=k=0; j<npsi; j++ ) {
88  if ( npsi > 1 ) defocus = def_avg + def_dev*cos(2*(j*dpsi-ast_ang));
89  for ( i=0; i<nrad; i++, k++ ) {
90  s2 = i*step_size;
91  s2 *= s2;
92  dphi = (t1*s2 - t2*defocus)*s2 - ac;
93  ctf[k] = sin(dphi);
94 // cout << k << tab << ctf[i] << endl;
95  }
96  }
97 
98  zero(1);
99 
100  return ctf;
101 }
102 
117 vector<double> CTFparam::zeroes(double max_s)
118 {
119  vector<double> zero;
120 
121  if ( check_defocus() || check_Cs() ) {
122  cerr << "in CTFparam::zeroes" << endl;
123  return zero;
124  }
125 
126  long i;
127  double t, sm2(max_s*max_s);
128  double l = lambda();
129  double l3Cs = l*l*l*cs/2.0;
130  double fz = def_avg/(l*l*cs), fz2 = fz*fz;
131  double ps = ac/M_PI;
132 
133 // cout << l*def_avg*sm2 << tab << l3Cs*sm2*sm2 << tab << ps << endl;
134 
135  long nzero = (long) (l*def_avg*sm2 - l3Cs*sm2*sm2 + ps);
136 
137  if ( nzero < 1 ) nzero = 1;
138 
139  if ( verbose & VERB_DEBUG )
140  cout << "DEBUG CTFparam::zeroes: " << nzero << tab << max_s << endl;
141 
142  zero.resize(nzero);
143 
144  for ( i=0; i<nzero; i++ ) {
145  t = (i+1.0-ps)/l3Cs;
146  if ( fz2 > t )
147  zero[i] = sqrt(fz - sqrt(fz2 - t));
148  else
149  zero[i] = -1;
150  if ( !isfinite(zero[i]) ) zero[i] = 0.0001;
151  }
152 
153  if ( verbose & VERB_DEBUG )
154  for ( i=0; i<nzero; i++ ) cout << i+1 << tab << zero[i] << tab << 1.0/zero[i] << endl;
155 
156  return zero;
157 }
158 
168 {
169  if ( base[0] <= 0 && base[1] == 0 ) base[0] = 1; // Default baseline
170 
171  Bstring bleq(base[0], "%.4g");
172 
173  switch ( bt ) {
174  case 1: // Polynomial
175  case 4: // Polynomial
176  bleq += Bstring(base[1], " + %.4g*$s") + Bstring(base[2], " + %.4g*$s2")
177  + Bstring(base[3], " + %.4g*$s3") + Bstring(base[4], " + %.4g*$s4");
178  break;
179  case 2: // Double gaussian
180  case 5: // Double gaussian
181  bleq += Bstring(base[1], " + %.4g") + Bstring(base[2], "*exp(%.4g*$s2)")
182  + Bstring(base[3], " + %.4g") + Bstring(base[4], "*exp(%.4g*$s2)");
183  break;
184  case 3: // EMAN style
185  case 6: // EMAN style
186  bleq += Bstring(base[1], " + %.4g") + Bstring(base[2], "*exp(%.4g*sqrt($s)")
187  + Bstring(base[3], " + %.4g*$s2)");
188  break;
189  default: break;
190  }
191 
192 // for ( int i=5; i<8; ++i ) cout << tab << base[i];
193 // cout << endl;
194 
195  if ( bt > 3 )
196  bleq += Bstring(base[5], " + %.4g") + Bstring(base[7], "*exp(%.4g")
197  + Bstring(base[6], "*(%.4g - $s)") + Bstring(base[6], "*(%.4g - $s))");
198 
199 // cout << bleq << endl;
200 
201  return bleq;
202 }
203 
212 {
213  Bstring enveq(env[0], "%.4g");
214 
215  switch ( et ) {
216  case 1: // Single gaussian
217  enveq += Bstring(env[1], "*exp(%.4g*$s2)");
218  break;
219  case 2: // Single gaussian with constant
220  enveq += Bstring(env[1], " + %.4g") + Bstring(env[2], "*exp(%.4g*$s2)");
221  break;
222  case 3: // Double gaussian
223  enveq += Bstring(env[1], "*exp(%.4g*$s2)") +
224  Bstring(env[2], " + %.4g") + Bstring(env[3], "*exp(%.4g*$s2)");
225  break;
226  case 4: // Double gaussian with constant
227  enveq += Bstring(env[1], " + %.4g") + Bstring(env[2], "*exp(%.4g*$s2)") +
228  Bstring(env[3], " + %.4g") + Bstring(env[4], "*exp(%.4g*$s2)");
229  break;
230  default: break;
231  }
232 
233  return enveq;
234 }
235 
245 {
246  base_eq = base_eq.remove('\"');
247 
248  int n(0);
249 
250  if ( base_eq.contains("s4") ) { // Polynomial
251  bt = 1;
252  sscanf(base_eq.c_str(), "%lf + %lf*$s + %lf*$s2 + %lf*$s3 + %lf*$s4%n",
253  &base[0], &base[1], &base[2], &base[3], &base[4], &n);
254  } else if ( base_eq.contains("sqrt") ) { // EMAN style
255  bt = 3;
256  sscanf(base_eq.c_str(), "%lf + %lf*exp(%lf*sqrt($s) + %lf*$s2)%n",
257  &base[0], &base[1], &base[2], &base[3], &n);
258  } else if ( base_eq.contains("exp") ) { // Double gaussian
259  bt = 2;
260  sscanf(base_eq.c_str(), "%lf + %lf*exp(%lf*$s2) + %lf*exp(%lf*$s2)%n",
261  &base[0], &base[1], &base[2], &base[3], &base[4], &n);
262  }
263 
264  char* ptr = &base_eq[n];
265  if ( strstr(ptr, "exp") ) {
266 // cout << ptr << endl;
267  sscanf(ptr, " + %lf*exp(%lf*(%lf - $s)*(%lf - $s))",
268  &base[5], &base[7], &base[6], &base[6]);
269  bt += 3;
270 // cout << base[5] << tab << base[6] << tab << base[7] << endl;
271  }
272 
273  if ( verbose & VERB_DEBUG )
274  cout << "DEBUG parse_baseline_equation: bt=" << bt << " baseline=" <<
275  base[0] << "," << base[1] << "," << base[2] << "," <<
276  base[3] << "," << base[4] << endl;
277 
278  return bt;
279 }
280 
290 {
291  env_eq = env_eq.remove('\"');
292 
293  et = env_eq.count('+') + env_eq.count('s');
294 
295  switch ( et ) {
296  case 1: // Single gaussian
297  sscanf(env_eq.c_str(), "%lf*exp(%lf*$s2)",
298  &env[0], &env[1]);
299  break;
300  case 2: // Single gaussian with constant
301  sscanf(env_eq.c_str(), "%lf + %lf*exp(%lf*$s2)",
302  &env[0], &env[1], &env[2]);
303  break;
304  case 3: // Double gaussian
305  sscanf(env_eq.c_str(), "%lf*exp(%lf*$s2) + %lf*exp(%lf*$s2)",
306  &env[0], &env[1], &env[2], &env[3]);
307  break;
308  case 4: // Double gaussian with constant
309  sscanf(env_eq.c_str(), "%lf + %lf*exp(%lf*$s2) + %lf*exp(%lf*$s2)",
310  &env[0], &env[1], &env[2], &env[3], &env[4]);
311  break;
312  default: break;
313  }
314 
315 // if ( env[0] <= 0 ) env[0] = 1; // Default envelope
316 // if ( env[1] >= 0 ) env[1] = -10;
317 // if ( env[2] <= 0 ) env[2] = 0;
318 // if ( env[3] >= 0 ) env[3] = -10;
319 
320  return 0;
321 }
322 
340 vector<double> CTFparam::envelope_partial_coherence(long n, double freq_step)
341 {
342  long i;
343  double l = lambda(), Csl2(cs*l*l);
344  double s, arg, pal(M_PI*a);
345  vector<double> curve(n);
346 
347  for ( i=0; i<n; i++ ) {
348  s = i*freq_step;
349  arg = pal*(Csl2*s*s - def_avg)*s;
350  curve[i] = exp(-arg*arg);
351  }
352 
353  return curve;
354 }
355 
374 vector<double> CTFparam::envelope_energy_spread(long n, double freq_step)
375 {
376  long i;
377  double s, arg;
378 // double fac(1.0/(16*log(2.0)));
379  double fac(0.5);
380  double l = lambda(), deccl(M_PI*l*cc*de/av);
381  vector<double> curve(n);
382 
383  for ( i=0; i<n; i++ ) {
384  s = i*freq_step;
385  arg = deccl*s*s;
386  curve[i] = exp(-fac*arg*arg);
387  }
388 
389  return curve;
390 }
391 
398 {
399  JSvalue js(JSobject);
400 
401  js["Volt"] = cp.volt();
402  js["Wavelength"] = cp.lambda();
403  js["Focal_length"] = cp.focal_length();
404  js["Objective_aperture"] = cp.objective_aperture();
405  js["Slit_width"] = cp.slit_width();
406  js["Cs"] = cp.Cs();
407  js["Cc"] = cp.Cc();
408  js["Beam_convergence"] = cp.alpha();
409  js["Energy_spread"] = cp.dE();
410  js["Amplitude_phase"] = cp.amp_shift();
411  js["Defocus_average"] = cp.defocus_average();
412  js["Defocus_deviation"] = cp.defocus_deviation();
413  js["Astigmatism_angle"] = cp.astigmatism_angle();
414 
415  return js;
416 }
417 
424 {
425  CTFparam cp;
426 
427  if ( js.exists("Volt") ) cp.volt(js["Volt"].real());
428 // if ( js.exists("Wavelength") ) cp.lambda(js["Wavelength"].real());
429  if ( js.exists("Focal_length") ) cp.focal_length(js["Focal_length"].real());
430  if ( js.exists("Objective_aperture") ) cp.objective_aperture(js["Objective_aperture"].real());
431  if ( js.exists("Slit_width") ) cp.slit_width(js["Slit_width"].real());
432  if ( js.exists("Cs") ) cp.Cs(js["Cs"].real());
433  if ( js.exists("Cc") ) cp.Cc(js["Cc"].real());
434  if ( js.exists("Beam_convergence") ) cp.alpha(js["Beam_convergence"].real());
435  if ( js.exists("Energy_spread") ) cp.dE(js["Energy_spread"].real());
436  if ( js.exists("Amplitude_phase") ) cp.amp_shift(js["Amplitude_phase"].real());
437  if ( js.exists("Defocus_average") ) cp.defocus_average(js["Defocus_average"].real());
438  if ( js.exists("Defocus_deviation") ) cp.defocus_deviation(js["Defocus_deviation"].real());
439  if ( js.exists("Astigmatism_angle") ) cp.astigmatism_angle(js["Astigmatism_angle"].real());
440 
441  return cp;
442 }
456 double electron_wavelength(double volt)
457 {
458  if ( volt < 1 ) {
459  error_show("Error in electron_wavelength", __FILE__, __LINE__);
460  cerr << "The acceleration voltage is below 1 V! (" << volt << " V)" << endl;
461  return -1;
462  }
463 
464  // return 12.2642596/sqrt(volt*(1+volt*0.978475598e-6));
465 
466  return 1e10*PLANCK/sqrt(2*EMASS*ECHARGE*volt*(1+ECHARGE*volt/(2*EMASS*LIGHTSPEED*LIGHTSPEED)));
467 }
468 
479 double beta2(double volt)
480 {
481  double f, b2(0);
482 
483  f = 1 + ECHARGE*volt/(EMASS*LIGHTSPEED*LIGHTSPEED);
484 
485  b2 = 1 - 1/(f*f);
486 
487  return b2;
488 }
489 
500 double beta(double volt)
501 {
502  return sqrt(beta2(volt));
503 }
504 
505 
515 vector<double> C_curve(long n, double freq_step)
516 {
517  long i;
518  double s2;
519  vector<double> curve(n);
520 
521  for ( i=0; i<n; i++ ) {
522  s2 = i*freq_step;
523  s2 *= s2;
524  curve[i] = 0.893*exp(-0.2465*s2) + 0.2563*exp(-1.7100*s2) +
525  0.7570*exp(-6.4094*s2) + 1.0487*exp(-18.6113*s2) +
526  0.3575*exp(-50.2523*s2);
527  }
528 
529  return curve;
530 }
531 
544 vector<double> defocus_range_profile(CTFparam& ctf, double freq_step, double def_min, double def_max, double def_step)
545 {
546  double scut = ctf.frequency_cutoff();
547  long n(scut/freq_step+1);
548 
549  if ( def_min < 1 ) {
550  vector<double> c(n, 0.5);
551  return c;
552  }
553 
554  long i, m(0);
555  double def, s2, v;
556  vector<double> c(n, 0);
557 
558  double def_ori = ctf.defocus_average();
559 
560  for ( def = def_min; def <= def_max; def += def_step, ++m ) {
561  ctf.defocus_average(def);
562  for ( i=0; i<n; ++i ) {
563  s2 = i*freq_step;
564  s2 *= s2;
565  v = ctf.calculate(s2, 0);
566  c[i] += v*v;
567  }
568  }
569 
570  ctf.defocus_average(def_ori); // Preserve the original defocus
571 
572  for ( i=0; i<n; ++i )
573  c[i] /= m;
574 
575  return c;
576 }
577 
589 double defocus_factor(CTFparam& ctf, double def_min, double def_max, double def_step)
590 {
591  if ( def_min < 1 ) return 0.5;
592 
593  double def_fac(0);
594  vector<double> c = defocus_range_profile(ctf, 0.001, def_min, def_max, def_step);
595 
596  for ( auto it = c.begin(); it != c.end(); ++it ) def_fac += *it;
597 
598  def_fac /= c.size();
599 
600  return def_fac;
601 }
602 
603 
ECHARGE
#define ECHARGE
Definition: utilities.h:52
NCTFPARAM
#define NCTFPARAM
Definition: ctf.h:17
Bstring::remove
Bstring remove(const char c)
Definition: Bstring.cpp:473
CTFparam::zero
double zero(int i)
Definition: ctf.h:228
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
verbose
int verbose
CTFparam::Cc
double Cc()
Definition: ctf.h:93
CTFparam::dE
double dE()
Definition: ctf.h:97
Bstring::count
long count(const char c)
Definition: Bstring.cpp:269
Bstring
Definition: Bstring.h:29
CTFparam::update
int update(CTFparam *ctf)
Definition: ctf.h:81
CTFparam::objective_aperture
double objective_aperture()
Definition: ctf.h:102
LIGHTSPEED
#define LIGHTSPEED
Definition: utilities.h:54
CTFparam::astigmatism_angle
double astigmatism_angle()
Definition: ctf.h:110
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
defocus_range_profile
vector< double > defocus_range_profile(CTFparam &ctf, double freq_step, double def_min, double def_max, double def_step)
Calculates a weighting profile associated with a range of defocus values.
Definition: ctf.cpp:544
CTFparam::check_Cs
bool check_Cs()
Definition: ctf.h:137
CTFparam::envelope_energy_spread
vector< double > envelope_energy_spread(long n, double freq_step)
Calculates the envelope curve based on energy spread.
Definition: ctf.cpp:374
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
CTFparam::parse_baseline_equation
int parse_baseline_equation(Bstring base_eq)
Extracts the coefficients from the baseline string.
Definition: ctf.cpp:244
ctf_to_json
JSvalue ctf_to_json(CTFparam &cp)
Converts microscope parameters to a JSON object list.
Definition: ctf.cpp:397
CTFparam::calculate
double calculate(double s2, double angle)
Definition: ctf.h:159
PLANCK
#define PLANCK
Definition: utilities.h:51
JSvalue::exists
bool exists(string tag)
Definition: json.h:221
C_curve
vector< double > C_curve(long n, double freq_step)
Calculates the carbon scattering curve.
Definition: ctf.cpp:515
ctf.h
Header file for CTF (contrast transfer function) functions.
CTFparam::envelope_equation
Bstring envelope_equation()
Generates an envelope equation string from 4 coefficients.
Definition: ctf.cpp:211
CTFparam::envelope_partial_coherence
vector< double > envelope_partial_coherence(long n, double freq_step)
Calculates the envelope curve based on partial coherence.
Definition: ctf.cpp:340
ctf_from_json
CTFparam ctf_from_json(JSvalue &js)
Converts a JSON object list to microscope parameters.
Definition: ctf.cpp:423
CTFparam::parse_envelope_equation
int parse_envelope_equation(Bstring env_eq)
Extracts the coefficients from the envelope string.
Definition: ctf.cpp:289
JSvalue
Definition: json.h:53
CTFparam::check_defocus
bool check_defocus()
Definition: ctf.h:129
electron_wavelength
double electron_wavelength(double volt)
Calculates the wavelength in angstrom from the acceleration voltage.
Definition: ctf.cpp:456
CTFparam::volt
double volt()
Definition: ctf.h:87
CTFparam::defocus_deviation
double defocus_deviation()
Definition: ctf.h:108
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.
CTFparam::slit_width
double slit_width()
Definition: ctf.h:104
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
isfinite
bool isfinite(complex< T > &value)
CTFparam::baseline
vector< double > & baseline()
Definition: ctf.h:114
CTFparam
Definition: ctf.h:28
CTFparam::envelope_type
long envelope_type()
Definition: ctf.h:119
JSobject
@ JSobject
Definition: json.h:26
CTFparam::frequency_cutoff
double frequency_cutoff()
Definition: ctf.h:150
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:106
CTFparam::alpha
double alpha()
Definition: ctf.h:95
CTFparam::focal_length
double focal_length()
Definition: ctf.h:89
CTFparam::amp_shift
double amp_shift()
Definition: ctf.h:99
defocus_factor
double defocus_factor(CTFparam &ctf, double def_min, double def_max, double def_step)
Calculates an integrated defocus weighting factor for a raneg of defocus values.
Definition: ctf.cpp:589
beta2
double beta2(double volt)
Calculates the square of the relative electron velocity.
Definition: ctf.cpp:479
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
CTFparam::baseline_equation
Bstring baseline_equation()
Generates a baseline equation string from 4 to 10 coefficients.
Definition: ctf.cpp:167
CTFparam::envelope
vector< double > & envelope()
Definition: ctf.h:121
beta
double beta(double volt)
Calculates the relative electron velocity.
Definition: ctf.cpp:500
CTFparam::Cs
double Cs()
Definition: ctf.h:91
CTFparam::baseline_type
long baseline_type()
Definition: ctf.h:112
EMASS
#define EMASS
Definition: utilities.h:53