EMAN2
Public Member Functions | Public Attributes | Private Member Functions
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)
vector< float > compute_1d_fromimage (int size, float ds, EMData *image)
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 zero (int n) 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 122 of file ctf.h.


Constructor & Destructor Documentation

EMAN1Ctf::EMAN1Ctf ( )

Definition at line 43 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.

{
        defocus = 0;
        bfactor = 0;
        amplitude = 0;
        ampcont = 0;
        noise1 = 0;
        noise2 = 0;
        noise3 = 0;
        noise4 = 0;
        voltage = 0;
        cs = 0;
        apix = 0;
}
EMAN::EMAN1Ctf::EMAN1Ctf ( const vector< float > &  vf) [inline]

Definition at line 139 of file ctf.h.

References from_vector().

{from_vector(vf);}      //for unpickling
EMAN1Ctf::~EMAN1Ctf ( )

Definition at line 59 of file ctf.cpp.

{
}

Member Function Documentation

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

Definition at line 171 of file ctf.h.

References ampcont, and sqrt().

Referenced by compute_1d(), and compute_2d_complex().

                {
                        return (sqrt(1 - ampcont * ampcont/10000.0f));
                }
float EMAN::EMAN1Ctf::calc_amplitude ( float  gamma) [inline, private]

Definition at line 209 of file ctf.h.

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

Referenced by compute_2d_complex().

                {
                        float t1 = sqrt(1.0f - ampcont * ampcont/10000.0f) * sin(gamma);
                        float v = amplitude * (t1 + ampcont/100.0f * cos(gamma));
                        return v;
                }
float EMAN::EMAN1Ctf::calc_ctf1 ( float  g,
float  gamma,
float  s 
) [inline, private]

Definition at line 203 of file ctf.h.

References ampcont, amplitude, and EMAN::Ctf::bfactor.

Referenced by calc_snr(), compute_1d(), and compute_2d_complex().

                {
                        float r = amplitude * exp(-(bfactor/4.0f * s * s)) * (g * sin(gamma) + ampcont/100.0f * cos(gamma));
                        return r;
                }
float EMAN::EMAN1Ctf::calc_g1 ( ) [inline, private]

Definition at line 182 of file ctf.h.

References calc_lambda(), and EMAN::Ctf::cs.

Referenced by compute_1d(), and compute_2d_complex().

                {
                        float lambda = calc_lambda();
                        float g1 = 2.5e6f * cs * lambda * lambda * lambda;
                        return g1;
                }
float EMAN::EMAN1Ctf::calc_g2 ( ) [inline, private]

Definition at line 189 of file ctf.h.

References calc_lambda(), and EMAN::Ctf::defocus.

Referenced by compute_1d(), and compute_2d_complex().

                {
                        float lambda = calc_lambda();
                        float g2 = 5000.0f * -defocus * lambda;
                        return g2;
                }
float EMAN::EMAN1Ctf::calc_gamma ( float  g1,
float  g2,
float  s 
) [inline, private]

Definition at line 196 of file ctf.h.

Referenced by compute_1d(), and compute_2d_complex().

                {
                        float s2 = s * s;
                        float gamma = (float) (-2 * M_PI * (g1 * s2 * s2 + g2 * s2));
                        return gamma;
                }
float EMAN::EMAN1Ctf::calc_lambda ( ) [inline, private]

Definition at line 176 of file ctf.h.

References sqrt(), and EMAN::Ctf::voltage.

Referenced by calc_g1(), and calc_g2().

                {
                        float lambda = 12.2639f / sqrt(voltage * 1000.0f + 0.97845f * voltage * voltage);
                        return lambda;
                }
float EMAN::EMAN1Ctf::calc_noise ( float  s) [inline, private]

Definition at line 216 of file ctf.h.

References noise1, noise2, noise3, noise4, and sqrt().

Referenced by calc_snr(), compute_1d(), and compute_2d_complex().

                {
                        float ns = (float) M_PI / 2 * noise4 * s;
                        float ns2 = ns * ns;
                        float n = noise3 * exp(-ns2 - s * noise2 - sqrt(fabs(s)) * noise1);
                        return n;
                }
float EMAN::EMAN1Ctf::calc_snr ( float  g1,
float  gamma,
float  s 
) [inline, private]

Definition at line 224 of file ctf.h.

References calc_ctf1(), and calc_noise().

Referenced by compute_1d().

                {
                        float ctf1 = calc_ctf1(g1, gamma, s);
                        float ctf2 = ctf1 * ctf1 / calc_noise(s);
                        return ctf2;
                }
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::Ctf::CTF_AMP, EMAN::Ctf::CTF_BACKGROUND, EMAN::Ctf::CTF_SIGN, EMAN::Ctf::CTF_SNR, EMAN::Ctf::CTF_SNR_SMOOTH, EMAN::Ctf::CTF_TOTAL, EMAN::Ctf::CTF_WIENER_FILTER, EMAN::Ctf::CTFOS, EMAN::XYData::get_yatx(), LOGERR, and sqrt().

{
        Assert(size > 0);

        float tmp_f1 = CTFOS * sqrt((float) 2) * size / 2;
        int np = (int) ceil(tmp_f1) + 2;
        vector < float >r;

        r.resize(np);

//      float ds = 1 / (apix * size * CTFOS);
        float s = 0;
        float g1 = calc_g1();
        float g2 = calc_g2();
        float amp1 = calc_amp1();

        switch (type) {
        case CTF_AMP:
                for (int i = 0; i < np; i++) {
                        float gamma = calc_gamma(g1, g2, s);
                        r[i] = calc_ctf1(amp1, gamma, s);
                        s += ds;
                }
                break;

        case CTF_SIGN:
                for (int i = 0; i < np; i++) {
                        float gamma = calc_gamma(g1, g2, s);
                        r[i] = calc_ctf1(amp1, gamma, s)>0?1.0f:-1.0f;
                        s += ds;
                }
                break;

        case CTF_BACKGROUND:
                for (int i = 0; i < np; i++) {
                        r[i] = calc_noise(s);
                        s += ds;
                }
                break;

        case CTF_SNR:
        case CTF_SNR_SMOOTH:
//              if (!sf) {
//                      LOGERR("CTF computation error, no SF found\n");
//                      return r;
//              }

                for (int i = 0; i < np; i++) {
                        float gamma = calc_gamma(g1, g2, s);
                        r[i] = calc_snr(amp1, gamma, s);
                        if (s && sf) {
                                r[i] *= sf->get_yatx(s);
                        }
                        s += ds;
                }

                break;

        case CTF_WIENER_FILTER:
                if (!sf) {
                        LOGERR("CTF computation error, no SF found\n");
                        return r;
                }

                for (int i = 0; i < np; i++) {
                        float gamma = calc_gamma(g1, g2, s);
                        r[i] = calc_snr(amp1, gamma, s);
                        if (s && sf) {
                                r[i] *= sf->get_yatx(s);
                        }

                        r[i] = 1.0f / (1.0f + 1.0f / r[i]);
                        s += ds;
                }
                break;

        case CTF_TOTAL:
                if (!sf) {
                        LOGERR("CTF computation error, no SF found\n");
                        return r;
                }

                for (int i = 0; i < np; i++) {
                        float gamma = calc_gamma(g1, g2, s);
                        if (sf) {
                                r[i] = calc_ctf1(amp1, gamma, s);
                                r[i] = r[i] * r[i] * sf->get_yatx(s) + calc_noise(s);
                        }
                        else {
                                r[i] = calc_ctf1(amp1, gamma, s);
                                r[i] = r[i] * r[i] + calc_noise(s);
                        }
                        s += ds;
                }
                break;
        default:
                break;
        }

        return r;
}
vector< float > EMAN1Ctf::compute_1d_fromimage ( int  size,
float  ds,
EMData image 
) [virtual]

Implements EMAN::Ctf.

Definition at line 286 of file ctf.cpp.

{
        vector < float >r;

        printf("Sorry, this function hasn't been implemented for EMAN1Ctf\n");
        
        int np=size/2;
        r.resize(np);

        return r;
}
void EMAN1Ctf::compute_2d_complex ( EMData image,
CtfType  type,
XYData struct_factor = 0 
) [virtual]

Implements EMAN::Ctf.

Definition at line 299 of file ctf.cpp.

References EMAN::Ctf::apix, calc_amp1(), calc_amplitude(), calc_ctf1(), calc_g1(), calc_g2(), calc_gamma(), calc_noise(), EMAN::Ctf::CTF_AMP, EMAN::Ctf::CTF_BACKGROUND, EMAN::Ctf::CTF_SIGN, EMAN::Ctf::CTF_SNR, EMAN::Ctf::CTF_SNR_SMOOTH, EMAN::Ctf::CTF_TOTAL, EMAN::Ctf::CTF_WIENER_FILTER, 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.

{
        if (!image) {
                LOGERR("image is null. cannot computer 2D complex CTF");
                return;
        }

        if (image->is_complex() == false) {
                LOGERR("compute_2d_complex can only work on complex images");
                return;
        }

        int nx = image->get_xsize();
        int ny = image->get_ysize();

        if (nx != ny + 2) {
                LOGERR("compute_2d_complex only works on (nx, nx-2) images");
                return;
        }

        float ds = 1.0f / (apix * ny);
        image->to_one();

        float *d = image->get_data();
        float g1 = calc_g1();
        float g2 = calc_g2();

        if (type == CTF_BACKGROUND) {
                for (int y = 0; y < ny; y++) {
                        int ynx = y * nx;

                        for (int x = 0; x < nx / 2; x++) {
#ifdef  _WIN32
                                float s = (float) _hypot(x, y - ny / 2.0f) * ds;
#else
                                float s = (float) hypot(x, y - ny / 2.0f) * ds;
#endif
                                d[x * 2 + ynx] = calc_noise(s);
                                d[x * 2 + ynx + 1] = 0;                 // The phase is somewhat arbitrary
                        }
                }
        }
        else if (type == CTF_AMP) {
                for (int y = 0; y < ny; y++) {
                        int ynx = y * nx;

                        for (int x = 0; x < nx / 2; x++) {
#ifdef  _WIN32
                                float s = (float)_hypot((float) x, (float) y - ny / 2) * ds;
#else
                                float s = (float)hypot((float) x, (float) y - ny / 2) * ds;
#endif  //_WIN32
                                float gamma = calc_gamma(g1, g2, s);
                                float v = fabs(calc_amplitude(gamma));
                                d[x * 2 + ynx] = v;
                                d[x * 2 + ynx + 1] = 0;
                        }
                }
        }
        else if (type == CTF_SIGN) {
                for (int y = 0; y < ny; y++) {
                        int ynx = y * nx;
                        for (int x = 0; x < nx / 2; x++) {
#ifdef  _WIN32
                                float s = (float)_hypot(x, y - ny / 2.0f) * ds;
#else
                                float s = (float)hypot(x, y - ny / 2.0f) * ds;
#endif
                                float gamma = calc_gamma(g1, g2, s);
                                float v = calc_amplitude(gamma);
                                d[x * 2 + ynx] = v > 0 ? 1.0f : -1.0f;
                                d[x * 2 + ynx + 1] = 0;
                        }
                }

        }
        else if (type == CTF_SNR || type == CTF_SNR_SMOOTH) {
                float amp1 = calc_amp1();

                for (int y = 0; y < ny; y++) {
                        int ynx = y * nx;

                        for (int x = 0; x < nx / 2; x++) {

#ifdef  _WIN32
                                float s = (float)_hypot(x, y - ny / 2.0f) * ds;
#else
                                float s = (float)hypot(x, y - ny / 2.0f) * ds;
#endif
                                float gamma = calc_gamma(g1, g2, s);
                                float f = calc_ctf1(amp1, gamma, s);
                                float noise = calc_noise(s);
                                f = f * f / noise;

                                if (s && sf) {
                                        f *= sf->get_yatx(s);
                                }
                                d[x * 2 + ynx] *= f;
                                d[x * 2 + ynx + 1] = 0;
                        }
                }
        }
        else if (type == CTF_WIENER_FILTER) {
                float amp1 = calc_amp1();

                for (int y = 0; y < ny; y++) {
                        int ynx = y * nx;

                        for (int x = 0; x < nx / 2; x++) {

#ifdef  _WIN32
                                float s = (float)_hypot(x, y - ny / 2.0f) * ds;
#else
                                float s = (float)hypot(x, y - ny / 2.0f) * ds;
#endif
                                float gamma = calc_gamma(g1, g2, s);
                                float f = calc_ctf1(amp1, gamma, s);
                                float noise = calc_noise(s);
                                f = f * f / noise;

                                if (s) {
                                        f *= sf->get_yatx(s);
                                }
                                f = 1.0f / (1.0f + 1.0f / f);
                                d[x * 2 + ynx] *= f;
                                d[x * 2 + ynx + 1] = 0;
                        }
                }
        }
        else if (type == CTF_TOTAL) {
                float amp1 = calc_amp1();

                for (int y = 0; y < ny; y++) {
                        int ynx = y * nx;

                        for (int x = 0; x < nx / 2; x++) {

#ifdef  _WIN32
                                float s = (float)_hypot(x, y - ny / 2.0f) * ds;
#else
                                float s = (float)hypot(x, y - ny / 2.0f) * ds;
#endif
                                float gamma = calc_gamma(g1, g2, s);
                                float f = calc_ctf1(amp1, gamma, s);
                                float noise = calc_noise(s);
                                f = f * f;

                                if (sf && s) {
                                        f *= sf->get_yatx(s);
                                }
                                f+=noise;

                                d[x * 2 + ynx] *= f;
                                d[x * 2 + ynx + 1] = 0;
                        }
                }
        }

        image->update();
}
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.

{


}
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.

{
        if (new_ctf) {
                const EMAN1Ctf *c = static_cast<const EMAN1Ctf *>(new_ctf);
                defocus = c->defocus;
                bfactor = c->bfactor;
                amplitude = c->amplitude;
                ampcont = c->ampcont;
                noise1 = c->noise1;
                noise2 = c->noise2;
                noise3 = c->noise3;
                noise4 = c->noise4;
                voltage = c->voltage;
                cs = c->cs;
                apix = c->apix;
        }
}
bool EMAN1Ctf::equal ( const Ctf ctf1) const [virtual]

Implements EMAN::Ctf.

Definition at line 462 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.

{
        if (ctf1) {
                const EMAN1Ctf *c = static_cast<const EMAN1Ctf *>(ctf1);
                if (defocus == c->defocus &&
                        bfactor == c->bfactor &&
                        amplitude == c->amplitude &&
                        ampcont == c->ampcont &&
                        noise1 == c->noise1 &&
                        noise2 == c->noise2 &&
                        noise3 == c->noise3 &&
                        noise4 == c->noise4 && voltage == c->voltage && cs == c->cs && apix == c->apix) {
                        return true;
                }
        }
        return false;
}
void EMAN1Ctf::from_dict ( const Dict dict) [virtual]

Implements EMAN::Ctf.

Definition at line 80 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.

{
        defocus = dict["defocus"];
        bfactor = dict["bfactor"];
        amplitude = dict["amplitude"];
        ampcont = dict["ampcont"];
        noise1 = dict["noise1"];
        noise2 = dict["noise2"];
        noise3 = dict["noise3"];
        noise4 = dict["noise4"];
        voltage = dict["voltage"];
        cs = dict["cs"];
        apix = dict["apix"];
}
int EMAN1Ctf::from_string ( const string &  ctf) [virtual]

Implements EMAN::Ctf.

Definition at line 64 of file ctf.cpp.

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

{
        Assert(ctf != "");
        char type;
        int pos;
        int i = sscanf(ctf.c_str(), "%c%f %f %f %f %f %f %f %f %f %f %f%n",
                                   &type,&defocus, &bfactor, &amplitude, &ampcont, &noise1,
                                   &noise2, &noise3, &noise4, &voltage, &cs, &apix, &pos);
        if (pos==-1) {
                const char *s=ctf.c_str();
                throw InvalidValueException(s," Invalid CTF string");
        }
                
        return 0;
}
void EMAN1Ctf::from_vector ( const vector< float > &  vctf) [virtual]

Implements EMAN::Ctf.

Definition at line 113 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.

Referenced by EMAN1Ctf(), and EMAN::EMData::get_ctf().

{
        defocus = vctf[0];
        bfactor = vctf[1];
        amplitude = vctf[2];
        ampcont = vctf[3];
        noise1 = vctf[4];
        noise2 = vctf[5];
        noise3 = vctf[6];
        noise4 = vctf[7];
        voltage = vctf[8];
        cs = vctf[9];
        apix = vctf[10];
}
float EMAN::EMAN1Ctf::get_bfactor ( ) const [inline]

Definition at line 165 of file ctf.h.

References EMAN::Ctf::bfactor.

                {
                        return bfactor;
                }
float EMAN::EMAN1Ctf::get_defocus ( ) const [inline]

Definition at line 161 of file ctf.h.

References EMAN::Ctf::defocus.

                {
                        return defocus;
                }
Dict EMAN1Ctf::to_dict ( ) const [virtual]

Implements EMAN::Ctf.

Definition at line 95 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.

{
        Dict dict;
        dict["defocus"] = defocus;
        dict["bfactor"] = bfactor;
        dict["amplitude"] = amplitude;
        dict["ampcont"] = ampcont;
        dict["noise1"] = noise1;
        dict["noise2"] = noise2;
        dict["noise3"] = noise3;
        dict["noise4"] = noise4;
        dict["voltage"] = voltage;
        dict["cs"] = cs;
        dict["apix"] = apix;

        return dict;
}
string EMAN1Ctf::to_string ( ) const [virtual]

Implements EMAN::Ctf.

Definition at line 148 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.

{
        char ctf[1024];
        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",
                        defocus, bfactor, amplitude, ampcont, noise1, noise2, noise3, noise4, voltage, cs,
                        apix);

        return string(ctf);
}
vector< float > EMAN1Ctf::to_vector ( ) const [virtual]

Implements EMAN::Ctf.

Definition at line 128 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.

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

{
        vector<float> vctf;

        vctf.push_back(defocus);
        vctf.push_back(bfactor);
        vctf.push_back(amplitude);
        vctf.push_back(ampcont);
        vctf.push_back(noise1);
        vctf.push_back(noise2);
        vctf.push_back(noise3);
        vctf.push_back(noise4);
        vctf.push_back(voltage);
        vctf.push_back(cs);
        vctf.push_back(apix);

        return vctf;
}
float EMAN1Ctf::zero ( int  n) const [virtual]

Implements EMAN::Ctf.

Definition at line 480 of file ctf.cpp.

{ return 0; }

Member Data Documentation


The documentation for this class was generated from the following files: