| Bsoft 2.0.5 Bernard's software package

ps_ctf_plot.cpp
Go to the documentation of this file.
1
9 #include "ps_plot.h"
10 #include "ps_ctf_plot.h"
11 #include "moving_average.h"
12 #include "utilities.h"
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
17 int ps_show_ctf_param(Bplot* plot, CTFparam& em_ctf)
18 {
19 Bstring txt;
20 txt = Bstring(1e-3*em_ctf.volt(), "Accelerating voltage: %g kV");
21 plot->page(0).add_text(txt);
22 txt = Bstring(1e-4*em_ctf.defocus_average(), "Defocus: %g um");
23 plot->page(0).add_text(txt);
24 txt = Bstring(1e-7*em_ctf.Cs(), "Spherical aberration: %g mm");
25 plot->page(0).add_text(txt);
26 txt = Bstring(1e-7*em_ctf.Cc(), "Chromatic aberration: %g mm");
27 plot->page(0).add_text(txt);
28 txt = Bstring(1e3*em_ctf.alpha(), "Beam spread/source size: %g mrad");
29 plot->page(0).add_text(txt);
30 txt = Bstring(em_ctf.dE(), "Energy spread: %g eV");
31 plot->page(0).add_text(txt);
32
33 return 0;
34 }
35
36 int ps_show_ctf_fit(Bplot* plot, CTFparam& em_ctf)
37 {
38 Bstring txt;
39 txt = Bstring(1e-3*em_ctf.volt(), "Accelerating voltage: %g kV");
40 plot->page(0).add_text(txt);
41 txt = Bstring(1e-7*em_ctf.Cs(), "Spherical aberration: %g mm");
42 plot->page(0).add_text(txt);
43 txt = Bstring(em_ctf.amp_shift(), "Amplitude phase shift: %g");
44 plot->page(0).add_text(txt);
45 txt = Bstring(1e-4*em_ctf.defocus_average(), "Defocus average: %g um");
46 plot->page(0).add_text(txt);
47 txt = Bstring(1e-4*em_ctf.defocus_deviation(), "Defocus deviation: %g um");
48 plot->page(0).add_text(txt);
49 txt = Bstring(em_ctf.astigmatism_angle()*180.0/M_PI, "Astigmatism angle: %g °");
50 plot->page(0).add_text(txt);
51 txt = "Baseline: " + em_ctf.baseline_equation();
52 plot->page(0).add_text(txt);
53 txt = "Envelope: " + em_ctf.envelope_equation();
54 plot->page(0).add_text(txt);
55
56 return 0;
57 }
58
71 int ps_ctf_plot(Bstring& filename, CTFparam& em_ctf, size_t n, double freq_step)
72 {
73 size_t i, j, k, l, m, h, ncol(6);
74
75 Bstring title("Envelope curve");
76 title = filename + ": " + title;
77
78 Bplot* plot = new Bplot(1, n, ncol);
79 plot->title(title);
80
81 plot->page(0).title(title);
82 plot->page(0).columns(ncol);
83 for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
84 plot->page(0).column(0).label("s");
85 plot->page(0).column(1).label("Carbon");
86 plot->page(0).column(2).label("PartialCoherence");
87 plot->page(0).column(3).label("EnergySpread");
88 plot->page(0).column(4).label("Envelope");
89 plot->page(0).column(5).label("CTF");
90 plot->page(0).column(0).axis(1);
91 for ( i=1; i<ncol; i++ ) {
92 plot->page(0).column(i).type(2);
93 plot->page(0).column(i).axis(3);
94 plot->page(0).column(i).element_size(0.5);
95 }
96 plot->page(0).column(1).color(0,0,0);
97 plot->page(0).column(2).color(0,0,1);
98 plot->page(0).column(3).color(0,1,0);
99 plot->page(0).column(4).color(1,0.5,0);
100 plot->page(0).column(5).color(1,0,0);
101
102 double env;
103
104 vector<double> ec = C_curve(n, freq_step);
105 vector<double> epc = em_ctf.envelope_partial_coherence(n, freq_step);
106 vector<double> ees = em_ctf.envelope_energy_spread(n, freq_step);
107 vector<double> ctf = em_ctf.calculate(n, 1, freq_step);
108
109 for ( i=0, j=n, k=2*n, l=3*n, m=4*n, h=5*n; i<n; i++, j++, k++, l++, m++, h++ ) {
110 env = (ec[i]/ec[0]) * epc[i] * ees[i];
111 (*plot)[i] = i*freq_step;
112 (*plot)[j] = ec[i]/ec[0];
113 (*plot)[k] = epc[i];
114 (*plot)[l] = ees[i];
115 (*plot)[m] = env;
116 (*plot)[h] = env * ctf[i] * ctf[i];
117 // cout << s << tab << env << endl;
118 }
119
120 plot->page(0).axis(1).min(0);
121 plot->page(0).axis(1).max(freq_step*n);
122 plot->page(0).axis(1).label("Resolution (A)");
123 plot->page(0).axis(1).flags(1);
124 plot->page(0).axis(3).min(0);
125 plot->page(0).axis(3).max(1);
126 plot->page(0).axis(3).label("Power");
127
128 ps_show_ctf_param(plot, em_ctf);
129
130 ps_plot(filename, plot);
131
132 delete plot;
133
134 return 0;
135 }
136
150 int ps_ctf_plot(long n, double* rps, double interval,
151 CTFparam* em_ctf, Bstring& filename)
152 {
153 long i, j, k, l, m, ncol(5);
154 double s, base, env;
155
156 // double* ctf = em_ctf->calculate(n, 1, interval);
157 vector<double> ctf = em_ctf->calculate(n, 1, interval);
158
159 Bstring title("Contrast transfer function");
160 title = filename + ": " + title;
161
162 Bplot* plot = new Bplot(1, n, ncol);
163 plot->title(title);
164
165 plot->page(0).title(title);
166 plot->page(0).columns(ncol);
167 for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
168 plot->page(0).column(0).label("s");
169 plot->page(0).column(1).label("RPS");
170 plot->page(0).column(2).label("Baseline");
171 plot->page(0).column(3).label("Envelope");
172 plot->page(0).column(4).label("CTF");
173 plot->page(0).column(0).axis(1);
174 for ( i=1; i<ncol; i++ ) {
175 plot->page(0).column(i).type(2);
176 plot->page(0).column(i).axis(3);
177 plot->page(0).column(i).element_size(0.5);
178 }
179 plot->page(0).column(1).color(0,0,1);
180 plot->page(0).column(2).color(0,1,0);
181 plot->page(0).column(3).color(1,0.5,0);
182 plot->page(0).column(4).color(1,0,0);
183
184 double min(0), max(0);
185
186 for ( i=0, j=n, k=2*n, l=3*n, m=4*n; i<n; i++, j++, k++, l++, m++ ) {
187 s = i*interval;
188 base = em_ctf->calc_baseline(s);
189 env = em_ctf->calc_envelope(s);
190 (*plot)[i] = s;
191 (*plot)[j] = rps[i] - base;
192 (*plot)[k] = base - base;
193 (*plot)[l] = env;
194 (*plot)[m] = env * ctf[i] * ctf[i];
195 if ( min > (*plot)[m] ) min = (*plot)[m];
196 if ( max < (*plot)[m] ) max = (*plot)[m];
197 }
198
199 // max = em_ctf->calc_envelope(0);
200
201 // delete[] ctf;
202
203 plot->page(0).axis(1).min(0);
204 plot->page(0).axis(1).max(interval*n);
205 plot->page(0).axis(1).label("Resolution (A)");
206 plot->page(0).axis(1).flags(1);
207 plot->page(0).axis(3).min(min);
208 plot->page(0).axis(3).max(max);
209 plot->page(0).axis(3).label("Power");
210
211 ps_show_ctf_fit(plot, *em_ctf);
212
213 ps_plot(filename, plot);
214
215 delete plot;
216
217 return 0;
218 }
219
233 int ps_ctf_defocus_zeroes(Bstring& filename, double volts, double Cs, double amp_shift)
234 {
235 CTFparam cp(volts, Cs, amp_shift);
236 int i, j, nzero(20);
237 double defocus, zero, x_max(20), x_inc(0.01), y_max(0);
238 Bstring title("Zeroes for different defocus values");
239 Bstring label;
240
241 if ( verbose ) {
242 cout << "Acceleration voltage: " << volts/1000 << " kV" << endl;
243 cout << "Electron wavelength: " << cp.lambda() << " A" << endl;
244 cout << "Spherical aberration: " << Cs/1e7 << " mm" << endl;
245 cout << "Amplitude phase shift: " << amp_shift << endl << endl;
246 }
247
248 long nx = (long) (x_max/x_inc + 1);
249
250 Bplot* plot = new Bplot(1, nx, nzero+1);
251 plot->title(title);
252
253 plot->page(0).title(title);
254 plot->page(0).columns(nzero+1);
255 for ( i=0; i<=nzero; i++ ) plot->page(0).column(i).number(i);
256 plot->page(0).column(0).label("Defocus");
257 plot->page(0).column(0).axis(1);
258 for ( i=1; i<=nzero; i++ ) {
259 label = Bstring(i, "Zero%d");
260 plot->page(0).column(i).type(2);
261 plot->page(0).column(i).label(label);
262 plot->page(0).column(i).axis(3);
263 plot->page(0).column(i).element_size(0.5);
264 }
265 plot->page(0).column(1).color(0,0,0);
266 if ( nzero > 1 ) plot->page(0).column(2).color(1,0,0);
267 if ( nzero > 2 ) plot->page(0).column(3).color(0,1,0);
268 if ( nzero > 3 ) plot->page(0).column(4).color(0,0,1);
269 if ( nzero > 4 ) plot->page(0).column(5).color(1,1,0);
270 plot->page(0).axis(1).min(0);
271 plot->page(0).axis(1).max(x_max);
272 plot->page(0).axis(1).label("Defocus (um)");
273 plot->page(0).axis(3).label("Zero (A)");
274
275 for ( j=0, defocus=0; j<nx; defocus+=100, j++ ) {
276 cp.defocus_average(defocus);
277 (*plot)[j] = defocus/10000.0;
278 for ( i=1; i<=nzero; i++ ) {
279 zero = cp.zero(i);
280 if ( zero > y_max ) y_max = zero;
281 (*plot)[i*nx+j] = zero;
282 }
283 }
284
285 y_max = 10*ceil(y_max/10);
286 plot->page(0).axis(3).max(y_max);
287
288 label = "Acceleration voltage: " + Bstring(volts/1000, "%.0f") + " kV";
289 plot->page(0).add_text(label);
290 label = "Electron wavelength: " + Bstring(cp.lambda(), "%.4g") + " A";
291 plot->page(0).add_text(label);
292 label = "Spherical aberration coefficient: " + Bstring(Cs/1e7, "%.2g") + " mm";
293 plot->page(0).add_text(label);
294 label = "Amplitude phase shift: " + Bstring(amp_shift, "%.3g");
295 plot->page(0).add_text(label);
296
297 ps_plot(filename, plot);
298
299 delete plot;
300
301 return 0;
302 }
303
304 int point_spread(size_t n, vector<double>& a, double step)
305 {
306 size_t i, j;
307 double fac = M_PI*1.0L/n, v;
308 vector<double> t(n);
309
310 // a[0] = 0;
311
312 for ( i=0; i<n; i++ ) {
313 t[i] = a[0]/2;
314 v = fac*i;
315 for ( j=1; j<n; j++ )
316 t[i] += a[j]*cos(v*j);
317 t[i] *= 2;
318 }
319
320 for ( i=0; i<n; i++ ) a[i] = t[i]*step;
321
322 return 0;
323 }
324
337 int ps_point_spread(Bstring& filename, CTFparam& em_ctf, size_t n, double freq_step)
338 {
339 size_t i, j, k, l, m, ncol(5);
340
341 Bstring title("Envelope curve");
342 title = filename + ": " + title;
343
344 Bplot* plot = new Bplot(1, n, ncol);
345 plot->title(title);
346
347 plot->page(0).title(title);
348 plot->page(0).columns(ncol);
349 for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
350 plot->page(0).column(0).label("Distance(A)");
351 plot->page(0).column(1).label("Carbon");
352 plot->page(0).column(2).label("PartialCoherence");
353 plot->page(0).column(3).label("EnergySpread");
354 plot->page(0).column(4).label("Envelope");
355 plot->page(0).column(0).axis(1);
356 for ( i=1; i<ncol; i++ ) {
357 plot->page(0).column(i).type(2);
358 plot->page(0).column(i).axis(3);
359 plot->page(0).column(i).element_size(0.5);
360 }
361 plot->page(0).column(1).color(0,0,0);
362 plot->page(0).column(2).color(0,0,1);
363 plot->page(0).column(3).color(0,1,0);
364 plot->page(0).column(4).color(1,0.5,0);
365 // plot->page(0).column(4).color(1,0,0);
366
367 double step(0.5/(n*freq_step));
368
369 vector<double> ec = C_curve(n, freq_step);
370 vector<double> epc = em_ctf.envelope_partial_coherence(n, freq_step);
371 vector<double> ees = em_ctf.envelope_energy_spread(n, freq_step);
372
373 vector<double> env(n);
374
375 for ( i=0; i<n; i++ ) env[i] = ec[i] * epc[i] * ees[i];
376
377 point_spread(n, ec, freq_step);
378 point_spread(n, epc, freq_step);
379 point_spread(n, ees, freq_step);
380 point_spread(n, env, freq_step);
381
382 for ( i=0, j=n, k=2*n, l=3*n, m=4*n; i<n; i++, j++, k++, l++, m++ ) {
383 (*plot)[i] = i*step;
384 (*plot)[j] = ec[i];
385 (*plot)[k] = epc[i];
386 (*plot)[l] = ees[i];
387 (*plot)[m] = env[i];
388 // cout << s << tab << env << endl;
389 }
390
391 plot->page(0).axis(1).min(0);
392 plot->page(0).axis(1).max(10);
393 plot->page(0).axis(1).label("Distance (A)");
394 plot->page(0).axis(3).label("Amplitude");
395
396 ps_show_ctf_param(plot, em_ctf);
397
398 ps_plot(filename, plot);
399
400 delete plot;
401
402 return 0;
403 }
404
CTFparam::zero
double zero(int i)
Definition: ctf.h:218
CTFparam::Cs
double Cs()
Definition: ctf.h:87
Baxis::label
Bstring & label()
Definition: Bplot.h:28
CTFparam::dE
double dE()
Definition: ctf.h:93
moving_average.h
Header file moving average calculations.
Baxis::min
double min()
Definition: Bplot.h:30
Bplot::title
Bstring & title()
Definition: Bplot.h:189
Bcolumn::element_size
double element_size()
Definition: Bplot.h:74
utilities.h
Header file for general utilities.
CTFparam::astigmatism_angle
double astigmatism_angle()
Definition: ctf.h:104
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
CTFparam::calc_envelope
double calc_envelope(double s)
Definition: ctf.h:189
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
Bcolumn::color
double * color()
Definition: Bplot.h:76
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
ps_point_spread
int ps_point_spread(Bstring &filename, CTFparam &em_ctf, size_t n, double freq_step)
Generates a point spread plot from CTF envelope parameters.
Definition: ps_ctf_plot.cpp:337
Baxis::max
double max()
Definition: Bplot.h:32
ps_show_ctf_fit
int ps_show_ctf_fit(Bplot *plot, CTFparam &em_ctf)
Definition: ps_ctf_plot.cpp:36
CTFparam::calc_baseline
double calc_baseline(double s)
Definition: ctf.h:161
M_PI
#define M_PI
Definition: utilities.h:59
base
string base(string &filename)
Definition: string_util.cpp:28
ps_ctf_plot
int ps_ctf_plot(Bstring &filename, CTFparam &em_ctf, size_t n, double freq_step)
Generates a postscript plot from CTF parameters for defocus and the envelope.
Definition: ps_ctf_plot.cpp:71
Bcolumn::number
long number()
Definition: Bplot.h:61
Bcolumn::type
long type()
Definition: Bplot.h:66
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
CTFparam::defocus_deviation
double defocus_deviation()
Definition: ctf.h:102
CTFparam::calculate
double calculate(double s2, double angle)
Definition: ctf.h:149
Bpage::title
Bstring & title()
Definition: Bplot.h:100
CTFparam::lambda
double lambda()
Definition: ctf.h:139
ps_ctf_defocus_zeroes
int ps_ctf_defocus_zeroes(Bstring &filename, double volts, double Cs, double amp_shift)
Generates a postscript plot of defocus versus zeroes.
Definition: ps_ctf_plot.cpp:233
CTFparam::envelope_equation
Bstring envelope_equation()
Generates an envelope equation string from 4 coefficients.
Definition: ctf.cpp:209
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
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:128
C_curve
vector< double > C_curve(long n, double freq_step)
Calculates the carbon scattering curve.
Definition: ctf.cpp:462
ps_ctf_plot.h
Header file for postscript output functions.
Bpage::add_text
void add_text(Bstring &s)
Definition: Bplot.h:122
Baxis::flags
long flags()
Definition: Bplot.h:38
Bstring
Definition: Bstring.h:29
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:211
CTFparam::volt
double volt()
Definition: ctf.h:83
ps_plot.h
Header file for postscript output functions.
ps_show_ctf_param
int ps_show_ctf_param(Bplot *plot, CTFparam &em_ctf)
Definition: ps_ctf_plot.cpp:17
verbose
int verbose
CTFparam::baseline_equation
Bstring baseline_equation()
Generates a baseline equation string from 4 to 10 coefficients.
Definition: ctf.cpp:165
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:100
CTFparam::amp_shift
double amp_shift()
Definition: ctf.h:95
point_spread
int point_spread(size_t n, vector< double > &a, double step)
Definition: ps_ctf_plot.cpp:304
CTFparam::Cc
double Cc()
Definition: ctf.h:89
CTFparam::alpha
double alpha()
Definition: ctf.h:91
Bcolumn::axis
long axis()
Definition: Bplot.h:68
CTFparam
Definition: ctf.h:26
Bplot
Definition: Bplot.h:146