Bsoft  2.1.0
Bernard's software package
ctf.h
Go to the documentation of this file.
1 
9 #include <cmath>
10 #include <vector>
11 
12 #include "Bstring.h"
13 #include "Complex.h"
14 #include "json.h"
15 #include "utilities.h"
16 
17 #define NCTFPARAM 10
18 
19 #ifndef _CTFparam_
20 /************************************************************************
21 @Object: class CTFparam
22 @Description:
23  Contrast transfer function parameter class.
24 @Features:
25  This defines the contrast transfer parameters for a microscope at
26  a particular combination of settings.
27 *************************************************************************/
28 class CTFparam {
29 private:
30  double av; // Acceleration voltage (V)
31  double fl; // Focal length (angstrom)
32  double cs; // Spherical aberration coefficient (angstrom)
33  double cc; // Chromatic aberation (angstrom)
34  double a; // Illumination half-angle (radians)
35  double de; // Energy spread (eV)
36  double oa; // Objective aperture radius (angstrom)
37  double sw; // Energy filter slit width (eV), 0 if not used
38  double ac; // Amplitude contrast phase shift (radian)
39  double def_avg; // Defocus average (angstrom)
40  double def_dev; // Defocus deviation (angstrom)
41  double ast_ang; // Astigmatism angle (radian)
42  double firstzero; // CTF first zero (angstrom)
43  long bt; // Baseline type (1=poly, 2=double_gauss, 3=eman)
44  vector<double> base; // Baseline (up to 10 coefficients)
45  long et; // Envelope type (1=single_gauss, 2=c+single_gauss, 3=double_gauss, 4=c+double_gauss)
46  vector<double> env; // Envelope (up to 10 coefficients)
47  double f; // CTF fit figure-of-merit
48  double wl; // Wave length
49  double t1, t2; // Terms in CTF
50  void initialize() {
51  a = de = sw = def_dev = ast_ang = firstzero = f = 0;
52  def_avg = 20000;
53  av = 300000;
54  fl = 3.5e7;
55  oa = 2e6;
56  cs = 2e7;
57  cc = 2e7;
58  ac = 0.07;
59  for ( int i=0; i<NCTFPARAM; i++ ) {
60  base.push_back(0);
61  env.push_back(0);
62  }
63  bt = 1;
64  base[0] = 1;
65  et = 4;
66  env[0] = 0.2;
67  env[1] = -1000;
68  wl = t1 = t2 = 0;
69  }
70  void update_terms() {
71  wl = lambda();
72  t1 = 0.5*M_PI*wl*wl*wl*cs;
73  t2 = M_PI*wl;
74  }
75 public:
76  CTFparam() { initialize(); update_terms(); }
77  CTFparam(double v, double sa, double ac) {
78  initialize();
79  volt(v); Cs(sa); amp_shift(ac);
80  }
81  int update(CTFparam* ctf) {
82  if ( !ctf ) return -1;
83  return update(*ctf);
84  }
85  int update(CTFparam& ctf);
86 
87  double volt() { return av; }
88  void volt(double v) { av = v; update_terms(); }
89  double focal_length() { return fl; }
90  void focal_length(double v) { fl = v; }
91  double Cs() { return cs; }
92  void Cs(double v) { cs = v; update_terms(); }
93  double Cc() { return cc; }
94  void Cc(double v) { cc = v; }
95  double alpha() { return a; }
96  void alpha(double v) { a = v; }
97  double dE() { return de; }
98  void dE(double v) { de = v; }
99  double amp_shift() { return ac; }
100  void amp_shift(double v) { ac = v; }
101 // double phi_fac() { if ( pf <= 0 ) pf = sqrt(1-af*af); return pf; }
102  double objective_aperture() { return oa; }
103  void objective_aperture(double v) { oa = v; }
104  double slit_width() { return sw; }
105  void slit_width(double v) { sw = v; }
106  double defocus_average() { return def_avg; }
107  void defocus_average(double v) { def_avg = v; }
108  double defocus_deviation() { return def_dev; }
109  void defocus_deviation(double v) { def_dev = v; }
110  double astigmatism_angle() { return ast_ang; }
111  void astigmatism_angle(double v) { ast_ang = v; }
112  long baseline_type() { return bt; }
113  void baseline_type(long t) { bt = t; }
114  vector<double>& baseline() { return base; }
115  double baseline(int i) { if ( i>=0 && i < NCTFPARAM ) return base[i]; else return 0; }
116  void baseline(int i, double d) { if ( i>=0 && i < NCTFPARAM ) base[i] = d; }
117  void baseline(double* b) { for ( int i=0; i<NCTFPARAM; i++ ) base[i] = b[i]; }
118  void baseline(vector<double>& b) { for ( int i=0; i<NCTFPARAM; i++ ) base[i] = b[i]; }
119  long envelope_type() { return et; }
120  void envelope_type(long t) { et = t; }
121  vector<double>& envelope() { return env; }
122  double envelope(int i) { if ( i>=0 && i < NCTFPARAM ) return env[i]; else return 0; }
123  void envelope(int i, double d) { if ( i>=0 && i < NCTFPARAM ) env[i] = d; }
124  void envelope(double* v) { for ( int i=0; i<NCTFPARAM; i++ ) env[i] = v[i]; }
125  void envelope(vector<double>& v) { for ( int i=0; i<NCTFPARAM; i++ ) env[i] = v[i]; }
126  double fom() { return f; }
127  void fom(double v) { f = v; }
128 
129  bool check_defocus() {
130  if ( def_avg < 1 || def_avg > 2e5 ) {
131  cerr << "Error: Defocus is out of range 0.0001 - 20 um! (" << def_avg*1e-4 << ")" << endl;
132  if ( def_avg < 1 ) def_avg = 1;
133  else if ( def_avg > 2e5 ) def_avg = 2e5;
134  return 1;
135  } else return 0;
136  }
137  bool check_Cs() {
138  if ( cs < 1e3 || cs > 1e8 ) {
139  cerr << "Error: Cs is out of range 0.0001 - 10 mm! (" << cs*1e-7 << ")" << endl;
140  if ( cs < 1e3 ) cs = 1e3;
141  else if ( cs > 1e8 ) cs = 1e8;
142  return 1;
143  } else return 0;
144  }
145  double lambda() { // Electron wavelength
146 // if ( av ) wl = 12.2643/sqrt(av*(1+av*0.97845e-6));
147  if ( av ) wl = 1e10*PLANCK/sqrt(2*EMASS*ECHARGE*av*(1+ECHARGE*av/(2*EMASS*LIGHTSPEED*LIGHTSPEED)));
148  return wl;
149  }
150  double frequency_cutoff() { // Aperture cutoff frequency
151  return oa/(2*fl*lambda());
152  }
153  double term1() { return t1; }
154  double term2() { return t2; }
155  double delta_phi(double s2, double angle) {
156  double defocus = def_avg + def_dev*cos(2*(angle - ast_ang));
157  return (t1*s2 - t2*defocus)*s2 - ac;
158  }
159  double calculate(double s2, double angle) {
160 // double dphi = delta_phi(s2, angle);
161 // return pf*sin(dphi) - af*cos(dphi);
162 // dphi -= asin(af);
163  return sin(delta_phi(s2, angle));
164  }
165  vector<double> calculate(int nrad, int npsi, double step_size);
166  Complex<double> calculate_complex(double s2, double angle) {
167  double dphi = delta_phi(s2, angle);
168 // return Complex<double>(af*cos(dphi),-pf*sin(dphi));
169  return Complex<double>(cos(dphi),sin(dphi));
170  }
171  double calc_baseline(double s) {
172  double s2(s*s), b(0), ds;
173  switch ( bt ) {
174  case 1:
175  case 4:
176  b = base[0] + base[1]*s + base[2]*s2 + base[3]*s*s2 + base[4]*s2*s2;
177  break;
178  case 2:
179  case 5:
180  b = base[0] + base[1]*exp(base[2]*s2) + base[3]*exp(base[4]*s2);
181  break;
182  case 3:
183  case 6:
184  b = base[0] + base[1]*exp(base[2]*sqrt(s) + base[3]*s2);
185  break;
186  default: ;
187  }
188  if ( bt > 3 ) {
189 // for ( int i=5; i<8; ++i ) cout << "\t" << base[i];
190 // cout << endl;
191  ds = base[6] - s;
192  b += base[5] * exp(base[7]*ds*ds);
193  }
194  if ( !isfinite(b) )
195  cerr << "Error in CTFparam::calc_baseline: baseline value not finite!" << endl;
196  else if ( b <= 0 ) b = 0;
197  return b;
198  }
199  double calc_envelope(double s) {
200  double s2(s*s), e(0);
201  switch ( et ) {
202  case 1:
203  e = env[0]*exp(env[1]*s2);
204  break;
205  case 2:
206  e = env[0] + env[1]*exp(env[2]*s2);
207  break;
208  case 3:
209  e = env[0]*exp(env[1]*s2) + env[2]*exp(env[3]*s2);
210  break;
211  case 4:
212  e = env[0] + env[1]*exp(env[2]*s2) + env[3]*exp(env[4]*s2);
213  break;
214  default: ;
215  }
216  if ( !isfinite(e) )
217  cerr << "Error in CTFparam::calc_envelope: baseline value not finite!" << endl;
218  else if ( e <= 0 ) e = 0;
219  return e;
220  }
221  vector<double> zeroes(double max_s);
224  int parse_baseline_equation(Bstring base_eq);
225  int parse_envelope_equation(Bstring env_eq);
226  vector<double> envelope_partial_coherence(long n, double freq_step);
227  vector<double> envelope_energy_spread(long n, double freq_step);
228  double zero(int i) {
229  double l = lambda();
230  double l3Cs = l*l*l*cs;
231  double ctf_z = def_avg/(l*l*cs);
232  double f = 1.0/sqrt(ctf_z - sqrt(ctf_z*ctf_z - i*2.0/l3Cs));
233  if ( !isfinite(f) ) f = -1;
234  if ( i == 1 ) firstzero = f;
235  return f;
236  }
237  double defocus_for_first_zero(double s) {
238  if ( s < 1e-6 ) return -1;
239  double l = lambda();
240  return 0.5*l*l*cs*s*s + 1/(l*s*s);
241  }
242  void show() {
243  cout << "Defocus average: " << def_avg*1e-4 << " um" << endl;
244  cout << "Defocus deviation: " << def_dev*1e-4 << " um" << endl;
245  cout << "Astigmatism angle: " << ast_ang*180/M_PI << " degrees" << endl;
246  cout << "Amplitude phase shift: " << ac << endl;
247  cout << "Voltage: " << av*1e-3 << " kV" << endl;
248  cout << "Wavelength: " << lambda() << " A" << endl;
249  cout << "Spherical aberration (Cs): " << cs*1e-7 << " mm" << endl;
250  cout << "Chromatic aberration (Cc): " << cc*1e-7 << " mm" << endl;
251  cout << "Illumination halfangle (alpha): " << a*1e3 << " mrad" << endl;
252  cout << "Energy spread: " << de << " eV" << endl;
253  cout << "Energy filter slit width: " << sw << " eV" << endl;
254  cout << "Objective aperture: " << oa*1e-4 << " µm" << endl;
255  }
256  void show_baseline() {
257  cout << "Baseline coefficients: " <<
258  baseline(0) << " " << baseline(1) << " " <<
259  baseline(2) << " " << baseline(3);
260  switch ( bt ) {
261  case 1: // Polynomial
262  case 4:
263  cout << " " << baseline(4) << " (polynomial";
264  break;
265  case 2: // Double gaussian
266  case 5:
267  cout << " " << baseline(4) << " (double gaussian";
268  break;
269  case 3: // EMAN style
270  case 6:
271  cout << " (EMAN";
272  break;
273  default:
274  break;
275  }
276  if ( baseline_type() > 3 ) cout << " with gaussian bump";
277  cout << ")" << endl;
278  }
279  void show_envelope() {
280  cout << "Envelope coefficients: ";
281  for ( int i=0; i<=envelope_type(); i++ )
282  cout << " " << envelope(i);
283  switch ( et ) {
284  case 1:
285  cout << " (single gaussian)";
286  break;
287  case 2:
288  cout << " (single gaussian with constant)";
289  break;
290  case 3:
291  cout << " (double gaussian)";
292  break;
293  case 4:
294  cout << " (double gaussian with constant)";
295  break;
296  default:
297  break;
298  }
299  cout << endl;
300  }
301 } ;
302 #define _CTFparam_
303 #endif
304 
305 // Function prototypes
308 double electron_wavelength(double volt);
309 double beta2(double volt);
310 double beta(double volt);
311 vector<double> C_curve(long n, double freq_step);
312 vector<double> defocus_range_profile(CTFparam& ctf, double freq_step, double def_min, double def_max, double def_step);
313 double defocus_factor(CTFparam& ctf, double def_min, double def_max, double def_step);
314 
ECHARGE
#define ECHARGE
Definition: utilities.h:52
CTFparam::dE
void dE(double v)
Definition: ctf.h:98
NCTFPARAM
#define NCTFPARAM
Definition: ctf.h:17
CTFparam::zero
double zero(int i)
Definition: ctf.h:228
CTFparam::Cc
double Cc()
Definition: ctf.h:93
CTFparam::dE
double dE()
Definition: ctf.h:97
CTFparam::calc_envelope
double calc_envelope(double s)
Definition: ctf.h:199
Complex
Definition: Complex.h:23
Bstring
Definition: Bstring.h:29
CTFparam::update
int update(CTFparam *ctf)
Definition: ctf.h:81
base
string base(string &filename)
Definition: string_util.cpp:28
CTFparam::baseline
void baseline(int i, double d)
Definition: ctf.h:116
CTFparam::objective_aperture
double objective_aperture()
Definition: ctf.h:102
LIGHTSPEED
#define LIGHTSPEED
Definition: utilities.h:54
CTFparam::defocus_deviation
void defocus_deviation(double v)
Definition: ctf.h:109
CTFparam::show_envelope
void show_envelope()
Definition: ctf.h:279
CTFparam::astigmatism_angle
double astigmatism_angle()
Definition: ctf.h:110
Complex.h
Class for complex numbers.
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
CTFparam::calc_baseline
double calc_baseline(double s)
Definition: ctf.h:171
CTFparam::defocus_for_first_zero
double defocus_for_first_zero(double s)
Definition: ctf.h:237
CTFparam::parse_baseline_equation
int parse_baseline_equation(Bstring base_eq)
Extracts the coefficients from the baseline string.
Definition: ctf.cpp:244
CTFparam::calculate
double calculate(double s2, double angle)
Definition: ctf.h:159
CTFparam::Cc
void Cc(double v)
Definition: ctf.h:94
CTFparam::slit_width
void slit_width(double v)
Definition: ctf.h:105
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
PLANCK
#define PLANCK
Definition: utilities.h:51
CTFparam::alpha
void alpha(double v)
Definition: ctf.h:96
CTFparam::envelope
double envelope(int i)
Definition: ctf.h:122
CTFparam::focal_length
void focal_length(double v)
Definition: ctf.h:90
CTFparam::term2
double term2()
Definition: ctf.h:154
CTFparam::show
void show()
Definition: ctf.h:242
CTFparam::CTFparam
CTFparam(double v, double sa, double ac)
Definition: ctf.h:77
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
CTFparam::volt
void volt(double v)
Definition: ctf.h:88
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
C_curve
vector< double > C_curve(long n, double freq_step)
Calculates the carbon scattering curve.
Definition: ctf.cpp:515
CTFparam::volt
double volt()
Definition: ctf.h:87
Bstring.h
Header file for the string class.
CTFparam::baseline_type
void baseline_type(long t)
Definition: ctf.h:113
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
CTFparam::delta_phi
double delta_phi(double s2, double angle)
Definition: ctf.h:155
CTFparam::envelope
void envelope(vector< double > &v)
Definition: ctf.h:125
CTFparam::Cs
void Cs(double v)
Definition: ctf.h:92
isfinite
bool isfinite(complex< T > &value)
CTFparam::fom
double fom()
Definition: ctf.h:126
CTFparam::baseline
vector< double > & baseline()
Definition: ctf.h:114
CTFparam
Definition: ctf.h:28
CTFparam::show_baseline
void show_baseline()
Definition: ctf.h:256
CTFparam::envelope_type
void envelope_type(long t)
Definition: ctf.h:120
CTFparam::envelope_type
long envelope_type()
Definition: ctf.h:119
CTFparam::frequency_cutoff
double frequency_cutoff()
Definition: ctf.h:150
CTFparam::fom
void fom(double v)
Definition: ctf.h:127
CTFparam::baseline
double baseline(int i)
Definition: ctf.h:115
beta
double beta(double volt)
Calculates the relative electron velocity.
Definition: ctf.cpp:500
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:106
electron_wavelength
double electron_wavelength(double volt)
Calculates the wavelength in angstrom from the acceleration voltage.
Definition: ctf.cpp:456
json.h
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
CTFparam::objective_aperture
void objective_aperture(double v)
Definition: ctf.h:103
CTFparam::baseline
void baseline(double *b)
Definition: ctf.h:117
CTFparam::term1
double term1()
Definition: ctf.h:153
CTFparam::envelope
void envelope(int i, double d)
Definition: ctf.h:123
CTFparam::CTFparam
CTFparam()
Definition: ctf.h:76
beta2
double beta2(double volt)
Calculates the square of the relative electron velocity.
Definition: ctf.cpp:479
CTFparam::defocus_average
void defocus_average(double v)
Definition: ctf.h:107
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::envelope
void envelope(double *v)
Definition: ctf.h:124
ctf_from_json
CTFparam ctf_from_json(JSvalue &js)
Converts a JSON object list to microscope parameters.
Definition: ctf.cpp:423
CTFparam::baseline_equation
Bstring baseline_equation()
Generates a baseline equation string from 4 to 10 coefficients.
Definition: ctf.cpp:167
CTFparam::baseline
void baseline(vector< double > &b)
Definition: ctf.h:118
CTFparam::envelope
vector< double > & envelope()
Definition: ctf.h:121
CTFparam::calculate_complex
Complex< double > calculate_complex(double s2, double angle)
Definition: ctf.h:166
ctf_to_json
JSvalue ctf_to_json(CTFparam &cp)
Converts microscope parameters to a JSON object list.
Definition: ctf.cpp:397
CTFparam::astigmatism_angle
void astigmatism_angle(double v)
Definition: ctf.h:111
CTFparam::Cs
double Cs()
Definition: ctf.h:91
CTFparam::amp_shift
void amp_shift(double v)
Definition: ctf.h:100
CTFparam::baseline_type
long baseline_type()
Definition: ctf.h:112
EMASS
#define EMASS
Definition: utilities.h:53