EMAN2
ctf.h
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef eman__ctf__h__
33#define eman__ctf__h__ 1
34
35#include <cmath>
36
37#include "emobject.h"
38
39#ifdef WIN32
40 #ifndef M_PI
41 #define M_PI 3.14159265358979323846f
42 #endif //M_PI
43#endif //WIN32
44
45using std::string;
46using std::map;
47
48namespace EMAN
49{
50 class EMData;
51 class XYData;
52
59 class Ctf
60 {
61 public:
62 // NOTE: ctf is positive for the first peak, instead of negative
64 {
65 CTF_AMP, // ctf ampltidue only
66 CTF_SIGN, // ctf sign (+-1)
67 CTF_BACKGROUND, // Background, no ctf oscillation
68 CTF_SNR, // Signal to noise ratio
69 CTF_SNR_SMOOTH, // Signal to noise ratio, smoothed, algorithm may vary, but this should be more suitable for weighting
70 CTF_WIENER_FILTER, // Weiner Filter = 1/(1+1/snr)
71 CTF_TOTAL, // AMP*AMP+NOISE
72 CTF_FITREF, // CTF amplitude squared without B-factor and low resolution zeroed
73 CTF_NOISERATIO, // 1-Noise/Total, when a particle is filtered with this it will still have noise, but the structure factor will look as if it's noise-free
74 CTF_INTEN, // ctf intensity only (no background or envelope)
75 CTF_POWEVAL, // ctf intensity, no B-factor and filtered below 20 A
76 CTF_ALIFILT, // ctf intensity -> mean subtracted to cause phase-flipping near zeroes, used to focus alignment on Thon rings
77 CTF_ABS // ctf ampltidue only (abs value)
78 };
79 public:
80 virtual ~ Ctf()
81 {
82 }
83
84 float defocus; // Defocus in microns, note that postitive is now underfocus, whereas values in EMAN1 are negative overfocus
85 float bfactor; // B-factor expressed using the x-ray convention (e^-B/4 s^2 in amplitude space) EMAN1 used E^-B s^2
86 float voltage; // in kV
87 float cs; // in mm
88 float apix; //
89
90 virtual int from_string(const string & ctf) = 0; // The first letter of the string indicates the subclass type
91 virtual string to_string() const = 0;
92
93 virtual void from_dict(const Dict & dict) = 0;
94 virtual Dict to_dict() const = 0;
95
96 virtual void from_vector(const vector<float>& vctf) = 0;
97 virtual vector<float> to_vector() const = 0;
98
99 virtual float zero(int n) const = 0;
100
101 virtual vector < float >compute_1d(int size,float ds, CtfType t, XYData * struct_factor = 0) = 0;
102 // This computes a 1D power spectrum from an image with astigmatism compensation. This is not entirely valid.
103 // It will stretch/shrink the power spectrum as a function of angle to make a single coherent 1D CTF curve.
104 // The cost is that the shrinking means that the structure factor is being distorted to make this work.
105 // Nonetheless, this is a useful tool in fitting astigmatic data
106 virtual vector <float>compute_1d_fromimage(int size, float ds, EMData *image) = 0;
107 virtual void compute_2d_real(EMData * img, CtfType t, XYData * struct_factor = 0) = 0;
108 virtual void compute_2d_complex(EMData * img, CtfType t, XYData * struct_factor = 0) = 0;
109
110 virtual void copy_from(const Ctf * new_ctf) = 0;
111 virtual bool equal(const Ctf * ctf1) const = 0;
112
113 virtual float get_phase() const = 0;
114 virtual void set_phase(float phase) = 0;
115 };
116
119 class EMAN1Ctf:public Ctf
120 {
121 public:
122// float defocus; // 0 Defocus in microns, note that postitive is now underfocus, whereas values in EMAN1 are negative overfocus
123// float bfactor; // 1 B-factor expressed using the x-ray convention (e^-B/4 s^2 in amplitude space) EMAN1 used E^-B s^2
124 float amplitude; // 2
125 float ampcont; // 3
126 float noise1; // 4
127 float noise2; // 5
128 float noise3; // 6
129 float noise4; // 7
130// float voltage; // 8
131// float cs; // 9
132// float apix; // 10
133
134 public:
135 EMAN1Ctf();
136 EMAN1Ctf(const vector<float>& vf) {from_vector(vf);} //for unpickling
137 ~EMAN1Ctf();
138
139 vector < float >compute_1d(int size,float ds, CtfType type, XYData * struct_factor = 0);
140 vector <float>compute_1d_fromimage(int size, float ds, EMData *image);
141 void compute_2d_real(EMData * image, CtfType type, XYData * struct_factor = 0);
142 void compute_2d_complex(EMData * image, CtfType type, XYData * struct_factor = 0);
143
144 int from_string(const string & ctf);
145 string to_string() const;
146
147 void from_dict(const Dict & dict);
148 Dict to_dict() const;
149
150 void from_vector(const vector<float>& vctf);
151 vector<float> to_vector() const;
152
153 void copy_from(const Ctf * new_ctf);
154 bool equal(const Ctf * ctf1) const;
155
156 float zero(int n) const;
157
158 float get_defocus() const
159 {
160 return defocus;
161 }
162 float get_bfactor() const
163 {
164 return bfactor;
165 }
166
167 float get_phase() const { return 0.0; }
168
169 void set_phase(float phase) { }
170
171 private:
172 inline float calc_amp1()
173 {
174 return (sqrt(1 - ampcont * ampcont/10000.0f));
175 }
176
177 inline float calc_lambda()
178 {
179 float lambda = 12.2639f / sqrt(voltage * 1000.0f + 0.97845f * voltage * voltage);
180 return lambda;
181 }
182
183 inline float calc_g1()
184 {
185 float lambda = calc_lambda();
186 float g1 = 2.5e6f * cs * lambda * lambda * lambda;
187 return g1;
188 }
189
190 inline float calc_g2()
191 {
192 float lambda = calc_lambda();
193 float g2 = 5000.0f * -defocus * lambda;
194 return g2;
195 }
196
197 inline float calc_gamma(float g1, float g2, float s)
198 {
199 float s2 = s * s;
200 float gamma = (float) (-2 * M_PI * (g1 * s2 * s2 + g2 * s2));
201 return gamma;
202 }
203
204 inline float calc_ctf1(float g, float gamma, float s)
205 {
206 float r = amplitude * exp(-(bfactor/4.0f * s * s)) * (g * sin(gamma) + ampcont/100.0f * cos(gamma));
207 return r;
208 }
209
210 inline float calc_amplitude(float gamma)
211 {
212 float t1 = sqrt(1.0f - ampcont * ampcont/10000.0f) * sin(gamma);
213 float v = amplitude * (t1 + ampcont/100.0f * cos(gamma));
214 return v;
215 }
216
217 inline float calc_noise(float s)
218 {
219 float ns = (float) M_PI / 2 * noise4 * s;
220 float ns2 = ns * ns;
221 float n = noise3 * exp(-ns2 - s * noise2 - sqrt(fabs(s)) * noise1);
222 return n;
223 }
224
225 inline float calc_snr(float g1, float gamma, float s)
226 {
227 float ctf1 = calc_ctf1(g1, gamma, s);
228 float ctf2 = ctf1 * ctf1 / calc_noise(s);
229 return ctf2;
230 }
231
232 };
233
236 class EMAN2Ctf:public Ctf
237 {
238 public:
239// float defocus; // defocus in microns, positive underfocus
240 float dfdiff; // defocus difference for astigmatism, defocus is the major elliptical axis
241 float dfang; // angle of the major elliptical axis in degrees measured counterclockwise from x
242// float bfactor; // B-factor in 1/A^2 expressed using the x-ray convention (e^-B/4 s^2 in amplitude space) EMAN1 used E^-B s^2
243 float ampcont; // amplitude contrast as a percentage ie- this should be 10 for 10% amp contrast
244// float voltage; // microscope voltage in kV
245// float cs; // Cs in mm
246// float apix; // A/pix value used when generating 2D results
247 float dsbg; // ds value for background and SNR
248 vector<float> background; // background intensity, 1 value per radial pixel (NX/2, corners ignored)
249 vector<float> snr; // SNR, 1 value per radial pixel (NX/2, corners assumed 0)
250
251 vector<float> get_snr(){ return snr;}
252 void set_snr(const vector<float>& vf) {snr = vf;}
253 vector<float> get_background(){ return background;}
254 void set_background(const vector<float>& vf) {background = vf;}
255
256 public:
257 EMAN2Ctf();
258 EMAN2Ctf(const vector<float>& vf) {from_vector(vf);} //for unpickling
259 ~EMAN2Ctf();
260
261 vector <float> compute_1d(int size,float ds, CtfType type, XYData * struct_factor = 0);
262 vector <float> compute_1d_fromimage(int size, float ds, EMData *image);
263 void compute_2d_real(EMData * image, CtfType type, XYData * struct_factor = 0);
264 void compute_2d_complex(EMData * image, CtfType type, XYData * struct_factor = 0);
265
266 int from_string(const string & ctf);
267 string to_string() const;
268
269 void from_dict(const Dict & dict);
270 Dict to_dict() const;
271
272 void from_vector(const vector<float>& vctf);
273 vector<float> to_vector() const;
274
275 void copy_from(const Ctf * new_ctf);
276 bool equal(const Ctf * ctf1) const;
277
278 float zero(int n) const;
279
280 inline float stos2(float s,float dZ) {
281 float lmb=lambda();
282 return sqrt((defocus*1.0e4f-sqrt(defocus*defocus*1.0e8f -2.0e11f*cs*s*s*(defocus-dZ)*lmb*lmb+1.0e14f*cs*cs*pow(s*lmb,4.0f)))/(1.0e7f*cs*lmb*lmb));
283 }
284
285 // phase is defined here as being zero for pure phase contrast for user convenience.
286 // The phase shift used in the cos(gamma-phase) expression is shifted -pi/2 from this
287 float get_phase() const;
288
289 void set_phase(float phase);
290 private:
291
292 // Electron wavelength in A
293 inline float lambda() const
294 {
295 float lambda = 12.2639f / sqrt(voltage * 1000.0f + 0.97845f * voltage * voltage);
296 return lambda;
297 }
298
299 // returns defocus as a function of angle. ang in radians !
300 inline float df(float ang) const {
301 if (dfdiff==0.0) return defocus;
302 return defocus+dfdiff/2.0f*cos(2.0f*ang-2.0f*M_PI/180.0f*dfang);
303 }
304
305 inline float calc_noise(float s) const
306 {
307 int si=(int)(s/dsbg);
308 if (si>(int)background.size()||si<0) return background.back();
309 return background[si];
310 }
311
312 };
313
314}
315
316
317
318#endif //eman__ctf__h__
Ctf is the base class for all CTF model.
Definition: ctf.h:60
virtual void compute_2d_complex(EMData *img, CtfType t, XYData *struct_factor=0)=0
virtual float get_phase() const =0
virtual void from_dict(const Dict &dict)=0
virtual vector< float > compute_1d_fromimage(int size, float ds, EMData *image)=0
float cs
Definition: ctf.h:87
virtual vector< float > to_vector() const =0
virtual void from_vector(const vector< float > &vctf)=0
virtual int from_string(const string &ctf)=0
float bfactor
Definition: ctf.h:85
virtual float zero(int n) const =0
virtual void set_phase(float phase)=0
virtual string to_string() const =0
virtual bool equal(const Ctf *ctf1) const =0
float voltage
Definition: ctf.h:86
float apix
Definition: ctf.h:88
virtual void compute_2d_real(EMData *img, CtfType t, XYData *struct_factor=0)=0
virtual vector< float > compute_1d(int size, float ds, CtfType t, XYData *struct_factor=0)=0
float defocus
Definition: ctf.h:84
virtual Dict to_dict() const =0
CtfType
Definition: ctf.h:64
@ CTF_TOTAL
Definition: ctf.h:71
@ CTF_FITREF
Definition: ctf.h:72
@ CTF_NOISERATIO
Definition: ctf.h:73
@ CTF_SNR_SMOOTH
Definition: ctf.h:69
@ CTF_WIENER_FILTER
Definition: ctf.h:70
@ CTF_AMP
Definition: ctf.h:65
@ CTF_ALIFILT
Definition: ctf.h:76
@ CTF_INTEN
Definition: ctf.h:74
@ CTF_SIGN
Definition: ctf.h:66
@ CTF_BACKGROUND
Definition: ctf.h:67
@ CTF_POWEVAL
Definition: ctf.h:75
@ CTF_SNR
Definition: ctf.h:68
@ CTF_ABS
Definition: ctf.h:77
virtual void copy_from(const Ctf *new_ctf)=0
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
EMAN1Ctf is the CTF model used in EMAN1.
Definition: ctf.h:120
vector< float > compute_1d_fromimage(int size, float ds, EMData *image)
Definition: ctf.cpp:293
float amplitude
Definition: ctf.h:124
float calc_snr(float g1, float gamma, float s)
Definition: ctf.h:225
void from_vector(const vector< float > &vctf)
Definition: ctf.cpp:112
int from_string(const string &ctf)
Definition: ctf.cpp:60
float noise2
Definition: ctf.h:127
float calc_noise(float s)
Definition: ctf.h:217
void set_phase(float phase)
Definition: ctf.h:169
void from_dict(const Dict &dict)
Definition: ctf.cpp:79
float noise4
Definition: ctf.h:129
float calc_g2()
Definition: ctf.h:190
float zero(int n) const
Definition: ctf.cpp:482
float get_defocus() const
Definition: ctf.h:158
float calc_ctf1(float g, float gamma, float s)
Definition: ctf.h:204
void compute_2d_real(EMData *image, CtfType type, XYData *struct_factor=0)
Definition: ctf.cpp:287
float noise1
Definition: ctf.h:126
void copy_from(const Ctf *new_ctf)
Definition: ctf.cpp:157
float ampcont
Definition: ctf.h:125
EMAN1Ctf(const vector< float > &vf)
Definition: ctf.h:136
vector< float > to_vector() const
Definition: ctf.cpp:127
float noise3
Definition: ctf.h:128
float get_phase() const
Definition: ctf.h:167
float calc_amp1()
Definition: ctf.h:172
float get_bfactor() const
Definition: ctf.h:162
string to_string() const
Definition: ctf.cpp:147
void compute_2d_complex(EMData *image, CtfType type, XYData *struct_factor=0)
Definition: ctf.cpp:306
Dict to_dict() const
Definition: ctf.cpp:94
float calc_g1()
Definition: ctf.h:183
float calc_gamma(float g1, float g2, float s)
Definition: ctf.h:197
vector< float > compute_1d(int size, float ds, CtfType type, XYData *struct_factor=0)
Definition: ctf.cpp:176
bool equal(const Ctf *ctf1) const
Definition: ctf.cpp:464
float calc_lambda()
Definition: ctf.h:177
float calc_amplitude(float gamma)
Definition: ctf.h:210
EMAN2Ctf is the default CTF model used in EMAN2.
Definition: ctf.h:237
vector< float > get_snr()
Definition: ctf.h:251
float df(float ang) const
Definition: ctf.h:300
float zero(int n) const
Definition: ctf.cpp:1267
void set_snr(const vector< float > &vf)
Definition: ctf.h:252
string to_string() const
Definition: ctf.cpp:544
bool equal(const Ctf *ctf1) const
Definition: ctf.cpp:1239
float calc_noise(float s) const
Definition: ctf.h:305
void compute_2d_real(EMData *image, CtfType type, XYData *struct_factor=0)
Definition: ctf.cpp:956
float stos2(float s, float dZ)
Definition: ctf.h:280
vector< float > background
Definition: ctf.h:248
EMAN2Ctf(const vector< float > &vf)
Definition: ctf.h:258
vector< float > snr
Definition: ctf.h:249
float dfdiff
Definition: ctf.h:240
void from_dict(const Dict &dict)
Definition: ctf.cpp:567
vector< float > compute_1d_fromimage(int size, float ds, EMData *image)
Definition: ctf.cpp:685
vector< float > compute_1d(int size, float ds, CtfType type, XYData *struct_factor=0)
Definition: ctf.cpp:731
void from_vector(const vector< float > &vctf)
Definition: ctf.cpp:612
vector< float > to_vector() const
Definition: ctf.cpp:630
float dfang
Definition: ctf.h:241
void copy_from(const Ctf *new_ctf)
Definition: ctf.cpp:653
vector< float > get_background()
Definition: ctf.h:253
void compute_2d_complex(EMData *image, CtfType type, XYData *struct_factor=0)
Definition: ctf.cpp:964
Dict to_dict() const
Definition: ctf.cpp:594
float get_phase() const
Definition: ctf.cpp:671
void set_background(const vector< float > &vf)
Definition: ctf.h:254
int from_string(const string &ctf)
Definition: ctf.cpp:509
float ampcont
Definition: ctf.h:243
float dsbg
Definition: ctf.h:247
float lambda() const
Definition: ctf.h:293
void set_phase(float phase)
Definition: ctf.cpp:677
EMData stores an image's data and defines core image processing routines.
Definition: emdata.h:82
XYData defines a 1D (x,y) data set.
Definition: xydata.h:47
EMData * sqrt() const
return square root of current image
EMData * phase() const
return phase part of a complex image as a real image format
E2Exception class.
Definition: aligner.h:40