Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

EMAN::EMAN1Ctf Class Reference

EMAN1Ctf is the CTF model used in EMAN1. More...

#include <ctf.h>

Inheritance diagram for EMAN::EMAN1Ctf:

Inheritance graph
[legend]
Collaboration diagram for EMAN::EMAN1Ctf:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 EMAN1Ctf ()
 EMAN1Ctf (const vector< float > &vf)
 ~EMAN1Ctf ()
vector< float > compute_1d (int size, float ds, CtfType type, XYData *struct_factor=0)
void compute_2d_real (EMData *image, CtfType type, XYData *struct_factor=0)
void compute_2d_complex (EMData *image, CtfType type, XYData *struct_factor=0)
int from_string (const string &ctf)
string to_string () const
void from_dict (const Dict &dict)
Dict to_dict () const
void from_vector (const vector< float > &vctf)
vector< float > to_vector () const
void copy_from (const Ctf *new_ctf)
bool equal (const Ctf *ctf1) const
float get_defocus () const
float get_bfactor () const

Public Attributes

float amplitude
float ampcont
float noise1
float noise2
float noise3
float noise4

Private Member Functions

float calc_amp1 ()
float calc_lambda ()
float calc_g1 ()
float calc_g2 ()
float calc_gamma (float g1, float g2, float s)
float calc_ctf1 (float g, float gamma, float s)
float calc_amplitude (float gamma)
float calc_noise (float s)
float calc_snr (float g1, float gamma, float s)

Detailed Description

EMAN1Ctf is the CTF model used in EMAN1.

Definition at line 112 of file ctf.h.


Constructor & Destructor Documentation

EMAN1Ctf::EMAN1Ctf  ) 
 

Definition at line 43 of file ctf.cpp.

References ampcont, amplitude, noise1, noise2, noise3, and noise4.

00044 {
00045         defocus = 0;
00046         bfactor = 0;
00047         amplitude = 0;
00048         ampcont = 0;
00049         noise1 = 0;
00050         noise2 = 0;
00051         noise3 = 0;
00052         noise4 = 0;
00053         voltage = 0;
00054         cs = 0;
00055         apix = 0;
00056 }

EMAN::EMAN1Ctf::EMAN1Ctf const vector< float > &  vf  )  [inline]
 

Definition at line 129 of file ctf.h.

00129 {from_vector(vf);}      //for unpickling

EMAN1Ctf::~EMAN1Ctf  ) 
 

Definition at line 59 of file ctf.cpp.

00060 {
00061 }


Member Function Documentation

float EMAN::EMAN1Ctf::calc_amp1  )  [inline, private]
 

Definition at line 158 of file ctf.h.

References sqrt().

Referenced by compute_1d(), and compute_2d_complex().

00159                 {
00160                         return (sqrt(1 - ampcont * ampcont/10000.0f));
00161                 }

float EMAN::EMAN1Ctf::calc_amplitude float  gamma  )  [inline, private]
 

Definition at line 196 of file ctf.h.

References amplitude(), sqrt(), and v.

Referenced by compute_2d_complex().

00197                 {
00198                         float t1 = sqrt(1.0f - ampcont * ampcont/10000.0f) * sin(gamma);
00199                         float v = amplitude * (t1 + ampcont/100.0f * cos(gamma));
00200                         return v;
00201                 }

float EMAN::EMAN1Ctf::calc_ctf1 float  g,
float  gamma,
float  s
[inline, private]
 

Definition at line 190 of file ctf.h.

References amplitude().

Referenced by compute_1d(), and compute_2d_complex().

00191                 {
00192                         float r = amplitude * exp(-(bfactor/4.0f * s * s)) * (g * sin(gamma) + ampcont/100.0f * cos(gamma));
00193                         return r;
00194                 }

float EMAN::EMAN1Ctf::calc_g1  )  [inline, private]
 

Definition at line 169 of file ctf.h.

Referenced by compute_1d(), and compute_2d_complex().

00170                 {
00171                         float lambda = calc_lambda();
00172                         float g1 = 2.5e6f * cs * lambda * lambda * lambda;
00173                         return g1;
00174                 }

float EMAN::EMAN1Ctf::calc_g2  )  [inline, private]
 

Definition at line 176 of file ctf.h.

Referenced by compute_1d(), and compute_2d_complex().

00177                 {
00178                         float lambda = calc_lambda();
00179                         float g2 = 5000.0f * -defocus * lambda;
00180                         return g2;
00181                 }

float EMAN::EMAN1Ctf::calc_gamma float  g1,
float  g2,
float  s
[inline, private]
 

Definition at line 183 of file ctf.h.

Referenced by compute_1d(), and compute_2d_complex().

00184                 {
00185                         float s2 = s * s;
00186                         float gamma = (float) (-2 * M_PI * (g1 * s2 * s2 + g2 * s2));
00187                         return gamma;
00188                 }

float EMAN::EMAN1Ctf::calc_lambda  )  [inline, private]
 

Definition at line 163 of file ctf.h.

References sqrt().

00164                 {
00165                         float lambda = 12.2639f / sqrt(voltage * 1000.0f + 0.97845f * voltage * voltage);
00166                         return lambda;
00167                 }

float EMAN::EMAN1Ctf::calc_noise float  s  )  [inline, private]
 

Definition at line 203 of file ctf.h.

References sqrt().

Referenced by compute_1d(), and compute_2d_complex().

00204                 {
00205                         float ns = (float) M_PI / 2 * noise4 * s;
00206                         float ns2 = ns * ns;
00207                         float n = noise3 * exp(-ns2 - s * noise2 - sqrt(fabs(s)) * noise1);
00208                         return n;
00209                 }

float EMAN::EMAN1Ctf::calc_snr float  g1,
float  gamma,
float  s
[inline, private]
 

Definition at line 211 of file ctf.h.

Referenced by compute_1d().

00212                 {
00213                         float ctf1 = calc_ctf1(g1, gamma, s);
00214                         float ctf2 = ctf1 * ctf1 / calc_noise(s);
00215                         return ctf2;
00216                 }

vector< float > EMAN1Ctf::compute_1d int  size,
float  ds,
CtfType  type,
XYData struct_factor = 0
[virtual]
 

Implements EMAN::Ctf.

Definition at line 177 of file ctf.cpp.

References Assert, calc_amp1(), calc_ctf1(), calc_g1(), calc_g2(), calc_gamma(), calc_noise(), calc_snr(), EMAN::XYData::get_yatx(), LOGERR, and sqrt().

00178 {
00179         Assert(size > 0);
00180 
00181         float tmp_f1 = CTFOS * sqrt((float) 2) * size / 2;
00182         int np = (int) ceil(tmp_f1) + 2;
00183         vector < float >r;
00184 
00185         r.resize(np);
00186 
00187 //      float ds = 1 / (apix * size * CTFOS);
00188         float s = 0;
00189         float g1 = calc_g1();
00190         float g2 = calc_g2();
00191         float amp1 = calc_amp1();
00192 
00193         switch (type) {
00194         case CTF_AMP:
00195                 for (int i = 0; i < np; i++) {
00196                         float gamma = calc_gamma(g1, g2, s);
00197                         r[i] = calc_ctf1(amp1, gamma, s);
00198                         s += ds;
00199                 }
00200                 break;
00201 
00202         case CTF_SIGN:
00203                 for (int i = 0; i < np; i++) {
00204                         float gamma = calc_gamma(g1, g2, s);
00205                         r[i] = calc_ctf1(amp1, gamma, s)>0?1.0f:-1.0f;
00206                         s += ds;
00207                 }
00208                 break;
00209 
00210         case CTF_BACKGROUND:
00211                 for (int i = 0; i < np; i++) {
00212                         r[i] = calc_noise(s);
00213                         s += ds;
00214                 }
00215                 break;
00216 
00217         case CTF_SNR:
00218         case CTF_SNR_SMOOTH:
00219 //              if (!sf) {
00220 //                      LOGERR("CTF computation error, no SF found\n");
00221 //                      return r;
00222 //              }
00223 
00224                 for (int i = 0; i < np; i++) {
00225                         float gamma = calc_gamma(g1, g2, s);
00226                         r[i] = calc_snr(amp1, gamma, s);
00227                         if (s && sf) {
00228                                 r[i] *= sf->get_yatx(s);
00229                         }
00230                         s += ds;
00231                 }
00232 
00233                 break;
00234 
00235         case CTF_WIENER_FILTER:
00236                 if (!sf) {
00237                         LOGERR("CTF computation error, no SF found\n");
00238                         return r;
00239                 }
00240 
00241                 for (int i = 0; i < np; i++) {
00242                         float gamma = calc_gamma(g1, g2, s);
00243                         r[i] = calc_snr(amp1, gamma, s);
00244                         if (s && sf) {
00245                                 r[i] *= sf->get_yatx(s);
00246                         }
00247 
00248                         r[i] = 1.0f / (1.0f + 1.0f / r[i]);
00249                         s += ds;
00250                 }
00251                 break;
00252 
00253         case CTF_TOTAL:
00254                 if (!sf) {
00255                         LOGERR("CTF computation error, no SF found\n");
00256                         return r;
00257                 }
00258 
00259                 for (int i = 0; i < np; i++) {
00260                         float gamma = calc_gamma(g1, g2, s);
00261                         if (sf) {
00262                                 r[i] = calc_ctf1(amp1, gamma, s);
00263                                 r[i] = r[i] * r[i] * sf->get_yatx(s) + calc_noise(s);
00264                         }
00265                         else {
00266                                 r[i] = calc_ctf1(amp1, gamma, s);
00267                                 r[i] = r[i] * r[i] + calc_noise(s);
00268                         }
00269                         s += ds;
00270                 }
00271                 break;
00272         default:
00273                 break;
00274         }
00275 
00276         return r;
00277 }

void EMAN1Ctf::compute_2d_complex EMData image,
CtfType  type,
XYData struct_factor = 0
[virtual]
 

Implements EMAN::Ctf.

Definition at line 288 of file ctf.cpp.

References calc_amp1(), calc_amplitude(), calc_ctf1(), calc_g1(), calc_g2(), calc_gamma(), calc_noise(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::XYData::get_yatx(), EMAN::EMData::get_ysize(), EMAN::EMData::is_complex(), LOGERR, nx, ny, EMAN::EMData::to_one(), EMAN::EMData::update(), v, x, and y.

00289 {
00290         if (!image) {
00291                 LOGERR("image is null. cannot computer 2D complex CTF");
00292                 return;
00293         }
00294 
00295         if (image->is_complex() == false) {
00296                 LOGERR("compute_2d_complex can only work on complex images");
00297                 return;
00298         }
00299 
00300         int nx = image->get_xsize();
00301         int ny = image->get_ysize();
00302 
00303         if (nx != ny + 2) {
00304                 LOGERR("compute_2d_complex only works on (nx, nx-2) images");
00305                 return;
00306         }
00307 
00308         float ds = 1.0f / (apix * ny);
00309         image->to_one();
00310 
00311         float *d = image->get_data();
00312         float g1 = calc_g1();
00313         float g2 = calc_g2();
00314 
00315         if (type == CTF_BACKGROUND) {
00316                 for (int y = 0; y < ny; y++) {
00317                         int ynx = y * nx;
00318 
00319                         for (int x = 0; x < nx / 2; x++) {
00320 #ifdef  _WIN32
00321                                 float s = (float) _hypot(x, y - ny / 2.0f) * ds;
00322 #else
00323                                 float s = (float) hypot(x, y - ny / 2.0f) * ds;
00324 #endif
00325                                 d[x * 2 + ynx] = calc_noise(s);
00326                                 d[x * 2 + ynx + 1] = 0;                 // The phase is somewhat arbitrary
00327                         }
00328                 }
00329         }
00330         else if (type == CTF_AMP) {
00331                 for (int y = 0; y < ny; y++) {
00332                         int ynx = y * nx;
00333 
00334                         for (int x = 0; x < nx / 2; x++) {
00335 #ifdef  _WIN32
00336                                 float s = (float)_hypot((float) x, (float) y - ny / 2) * ds;
00337 #else
00338                                 float s = (float)hypot((float) x, (float) y - ny / 2) * ds;
00339 #endif  //_WIN32
00340                                 float gamma = calc_gamma(g1, g2, s);
00341                                 float v = fabs(calc_amplitude(gamma));
00342                                 d[x * 2 + ynx] = v;
00343                                 d[x * 2 + ynx + 1] = 0;
00344                         }
00345                 }
00346         }
00347         else if (type == CTF_SIGN) {
00348                 for (int y = 0; y < ny; y++) {
00349                         int ynx = y * nx;
00350                         for (int x = 0; x < nx / 2; x++) {
00351 #ifdef  _WIN32
00352                                 float s = (float)_hypot(x, y - ny / 2.0f) * ds;
00353 #else
00354                                 float s = (float)hypot(x, y - ny / 2.0f) * ds;
00355 #endif
00356                                 float gamma = calc_gamma(g1, g2, s);
00357                                 float v = calc_amplitude(gamma);
00358                                 d[x * 2 + ynx] = v > 0 ? 1.0f : -1.0f;
00359                                 d[x * 2 + ynx + 1] = 0;
00360                         }
00361                 }
00362 
00363         }
00364         else if (type == CTF_SNR || type == CTF_SNR_SMOOTH) {
00365                 float amp1 = calc_amp1();
00366 
00367                 for (int y = 0; y < ny; y++) {
00368                         int ynx = y * nx;
00369 
00370                         for (int x = 0; x < nx / 2; x++) {
00371 
00372 #ifdef  _WIN32
00373                                 float s = (float)_hypot(x, y - ny / 2.0f) * ds;
00374 #else
00375                                 float s = (float)hypot(x, y - ny / 2.0f) * ds;
00376 #endif
00377                                 float gamma = calc_gamma(g1, g2, s);
00378                                 float f = calc_ctf1(amp1, gamma, s);
00379                                 float noise = calc_noise(s);
00380                                 f = f * f / noise;
00381 
00382                                 if (s && sf) {
00383                                         f *= sf->get_yatx(s);
00384                                 }
00385                                 d[x * 2 + ynx] *= f;
00386                                 d[x * 2 + ynx + 1] = 0;
00387                         }
00388                 }
00389         }
00390         else if (type == CTF_WIENER_FILTER) {
00391                 float amp1 = calc_amp1();
00392 
00393                 for (int y = 0; y < ny; y++) {
00394                         int ynx = y * nx;
00395 
00396                         for (int x = 0; x < nx / 2; x++) {
00397 
00398 #ifdef  _WIN32
00399                                 float s = (float)_hypot(x, y - ny / 2.0f) * ds;
00400 #else
00401                                 float s = (float)hypot(x, y - ny / 2.0f) * ds;
00402 #endif
00403                                 float gamma = calc_gamma(g1, g2, s);
00404                                 float f = calc_ctf1(amp1, gamma, s);
00405                                 float noise = calc_noise(s);
00406                                 f = f * f / noise;
00407 
00408                                 if (s) {
00409                                         f *= sf->get_yatx(s);
00410                                 }
00411                                 f = 1.0f / (1.0f + 1.0f / f);
00412                                 d[x * 2 + ynx] *= f;
00413                                 d[x * 2 + ynx + 1] = 0;
00414                         }
00415                 }
00416         }
00417         else if (type == CTF_TOTAL) {
00418                 float amp1 = calc_amp1();
00419 
00420                 for (int y = 0; y < ny; y++) {
00421                         int ynx = y * nx;
00422 
00423                         for (int x = 0; x < nx / 2; x++) {
00424 
00425 #ifdef  _WIN32
00426                                 float s = (float)_hypot(x, y - ny / 2.0f) * ds;
00427 #else
00428                                 float s = (float)hypot(x, y - ny / 2.0f) * ds;
00429 #endif
00430                                 float gamma = calc_gamma(g1, g2, s);
00431                                 float f = calc_ctf1(amp1, gamma, s);
00432                                 float noise = calc_noise(s);
00433                                 f = f * f;
00434 
00435                                 if (sf && s) {
00436                                         f *= sf->get_yatx(s);
00437                                 }
00438                                 f+=noise;
00439 
00440                                 d[x * 2 + ynx] *= f;
00441                                 d[x * 2 + ynx + 1] = 0;
00442                         }
00443                 }
00444         }
00445 
00446         image->update();
00447 }

void EMAN1Ctf::compute_2d_real EMData image,
CtfType  type,
XYData struct_factor = 0
[virtual]
 

Implements EMAN::Ctf.

Definition at line 280 of file ctf.cpp.

00281 {
00282 
00283 
00284 }

void EMAN1Ctf::copy_from const Ctf new_ctf  )  [virtual]
 

Implements EMAN::Ctf.

Definition at line 158 of file ctf.cpp.

References ampcont, amplitude, EMAN::Ctf::apix, EMAN::Ctf::bfactor, EMAN::Ctf::cs, EMAN::Ctf::defocus, noise1, noise2, noise3, noise4, and EMAN::Ctf::voltage.

00159 {
00160         if (new_ctf) {
00161                 const EMAN1Ctf *c = static_cast<const EMAN1Ctf *>(new_ctf);
00162                 defocus = c->defocus;
00163                 bfactor = c->bfactor;
00164                 amplitude = c->amplitude;
00165                 ampcont = c->ampcont;
00166                 noise1 = c->noise1;
00167                 noise2 = c->noise2;
00168                 noise3 = c->noise3;
00169                 noise4 = c->noise4;
00170                 voltage = c->voltage;
00171                 cs = c->cs;
00172                 apix = c->apix;
00173         }
00174 }

bool EMAN1Ctf::equal const Ctf ctf1  )  const [virtual]
 

Implements EMAN::Ctf.

Definition at line 451 of file ctf.cpp.

References ampcont, amplitude, EMAN::Ctf::apix, EMAN::Ctf::bfactor, EMAN::Ctf::cs, EMAN::Ctf::defocus, noise1, noise2, noise3, noise4, and EMAN::Ctf::voltage.

00452 {
00453         if (ctf1) {
00454                 const EMAN1Ctf *c = static_cast<const EMAN1Ctf *>(ctf1);
00455                 if (defocus == c->defocus &&
00456                         bfactor == c->bfactor &&
00457                         amplitude == c->amplitude &&
00458                         ampcont == c->ampcont &&
00459                         noise1 == c->noise1 &&
00460                         noise2 == c->noise2 &&
00461                         noise3 == c->noise3 &&
00462                         noise4 == c->noise4 && voltage == c->voltage && cs == c->cs && apix == c->apix) {
00463                         return true;
00464                 }
00465         }
00466         return false;
00467 }

void EMAN1Ctf::from_dict const Dict dict  )  [virtual]
 

Implements EMAN::Ctf.

Definition at line 80 of file ctf.cpp.

References ampcont, amplitude, noise1, noise2, noise3, and noise4.

00081 {
00082         defocus = dict["defocus"];
00083         bfactor = dict["bfactor"];
00084         amplitude = dict["amplitude"];
00085         ampcont = dict["ampcont"];
00086         noise1 = dict["noise1"];
00087         noise2 = dict["noise2"];
00088         noise3 = dict["noise3"];
00089         noise4 = dict["noise4"];
00090         voltage = dict["voltage"];
00091         cs = dict["cs"];
00092         apix = dict["apix"];
00093 }

int EMAN1Ctf::from_string const string &  ctf  )  [virtual]
 

Implements EMAN::Ctf.

Definition at line 64 of file ctf.cpp.

References Assert, and InvalidValueException.

00065 {
00066         Assert(ctf != "");
00067         char type;
00068         int pos;
00069         int i = sscanf(ctf.c_str(), "%c%f %f %f %f %f %f %f %f %f %f %f%n",
00070                                    &type,&defocus, &bfactor, &amplitude, &ampcont, &noise1,
00071                                    &noise2, &noise3, &noise4, &voltage, &cs, &apix, &pos);
00072         if (pos==-1) {
00073                 const char *s=ctf.c_str();
00074                 throw InvalidValueException(s," Invalid CTF string");
00075         }
00076                 
00077         return 0;
00078 }

void EMAN1Ctf::from_vector const vector< float > &  vctf  )  [virtual]
 

Implements EMAN::Ctf.

Definition at line 113 of file ctf.cpp.

References ampcont, amplitude, noise1, noise2, noise3, and noise4.

Referenced by EMAN::EMData::get_ctf().

00114 {
00115         defocus = vctf[0];
00116         bfactor = vctf[1];
00117         amplitude = vctf[2];
00118         ampcont = vctf[3];
00119         noise1 = vctf[4];
00120         noise2 = vctf[5];
00121         noise3 = vctf[6];
00122         noise4 = vctf[7];
00123         voltage = vctf[8];
00124         cs = vctf[9];
00125         apix = vctf[10];
00126 }

float EMAN::EMAN1Ctf::get_bfactor  )  const [inline]
 

Definition at line 152 of file ctf.h.

00153                 {
00154                         return bfactor;
00155                 }

float EMAN::EMAN1Ctf::get_defocus  )  const [inline]
 

Definition at line 148 of file ctf.h.

00149                 {
00150                         return defocus;
00151                 }

Dict EMAN1Ctf::to_dict  )  const [virtual]
 

Implements EMAN::Ctf.

Definition at line 95 of file ctf.cpp.

00096 {
00097         Dict dict;
00098         dict["defocus"] = defocus;
00099         dict["bfactor"] = bfactor;
00100         dict["amplitude"] = amplitude;
00101         dict["ampcont"] = ampcont;
00102         dict["noise1"] = noise1;
00103         dict["noise2"] = noise2;
00104         dict["noise3"] = noise3;
00105         dict["noise4"] = noise4;
00106         dict["voltage"] = voltage;
00107         dict["cs"] = cs;
00108         dict["apix"] = apix;
00109 
00110         return dict;
00111 }

string EMAN1Ctf::to_string  )  const [virtual]
 

Implements EMAN::Ctf.

Definition at line 148 of file ctf.cpp.

References ampcont, amplitude, noise1, noise2, noise3, and noise4.

00149 {
00150         char ctf[1024];
00151         sprintf(ctf, "O%1.3g %1.3g %1.3g %1.3g %1.3g %1.3g %1.3g %1.3g %1.3g %1.3g %1.3g",
00152                         defocus, bfactor, amplitude, ampcont, noise1, noise2, noise3, noise4, voltage, cs,
00153                         apix);
00154 
00155         return string(ctf);
00156 }

vector< float > EMAN1Ctf::to_vector  )  const [virtual]
 

Implements EMAN::Ctf.

Definition at line 128 of file ctf.cpp.

References ampcont, amplitude, noise1, noise2, noise3, and noise4.

Referenced by EMAN::MrcIO::read_mrc_header().

00129 {
00130         vector<float> vctf;
00131 
00132         vctf.push_back(defocus);
00133         vctf.push_back(bfactor);
00134         vctf.push_back(amplitude);
00135         vctf.push_back(ampcont);
00136         vctf.push_back(noise1);
00137         vctf.push_back(noise2);
00138         vctf.push_back(noise3);
00139         vctf.push_back(noise4);
00140         vctf.push_back(voltage);
00141         vctf.push_back(cs);
00142         vctf.push_back(apix);
00143 
00144         return vctf;
00145 }


Member Data Documentation

float EMAN::EMAN1Ctf::ampcont
 

Definition at line 118 of file ctf.h.

Referenced by copy_from(), EMAN1Ctf(), equal(), from_dict(), from_vector(), to_string(), and to_vector().

float EMAN::EMAN1Ctf::amplitude
 

Definition at line 117 of file ctf.h.

Referenced by copy_from(), EMAN1Ctf(), equal(), from_dict(), from_vector(), to_string(), and to_vector().

float EMAN::EMAN1Ctf::noise1
 

Definition at line 119 of file ctf.h.

Referenced by copy_from(), EMAN1Ctf(), equal(), from_dict(), from_vector(), to_string(), and to_vector().

float EMAN::EMAN1Ctf::noise2
 

Definition at line 120 of file ctf.h.

Referenced by copy_from(), EMAN1Ctf(), equal(), from_dict(), from_vector(), to_string(), and to_vector().

float EMAN::EMAN1Ctf::noise3
 

Definition at line 121 of file ctf.h.

Referenced by copy_from(), EMAN1Ctf(), equal(), from_dict(), from_vector(), to_string(), and to_vector().

float EMAN::EMAN1Ctf::noise4
 

Definition at line 122 of file ctf.h.

Referenced by copy_from(), EMAN1Ctf(), equal(), from_dict(), from_vector(), to_string(), and to_vector().


The documentation for this class was generated from the following files:
Generated on Tue Jun 11 13:41:57 2013 for EMAN2 by  doxygen 1.3.9.1