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