65 int i = sscanf(ctf.c_str(),
"%c%f %f %f %f %f %f %f %f %f %f %f%n",
72 const char *s=ctf.c_str();
141 vctf.push_back(
apix);
150 sprintf(ctf,
"O%1.10g %1.10g %1.10g %1.10g %1.10g %1.10g %1.10g %1.10g %1.10g %1.10g %1.10g",
151 -
defocus,
bfactor/4.0,
amplitude,
ampcont/100.0,
noise1,
noise2,
noise3,
noise4,
voltage,
cs,
180 float tmp_f1 =
sqrt((
float) 2) * size / 2;
181 int np = (int) ceil(tmp_f1) + 2;
194 for (
int i = 0; i < np; i++) {
202 for (
int i = 0; i < np; i++) {
210 for (
int i = 0; i < np; i++) {
212 r[i] =
calc_ctf1(amp1, gamma, s)>0?1.0f:-1.0f;
218 for (
int i = 0; i < np; i++) {
231 for (
int i = 0; i < np; i++) {
244 LOGERR(
"CTF computation error, no SF found\n");
248 for (
int i = 0; i < np; i++) {
255 r[i] = 1.0f / (1.0f + 1.0f / r[i]);
262 LOGERR(
"CTF computation error, no SF found\n");
266 for (
int i = 0; i < np; i++) {
297 printf(
"Sorry, this function hasn't been implemented for EMAN1Ctf\n");
313 LOGERR(
"image is null. cannot computer 2D complex CTF");
317 if (image->is_complex() ==
false) {
318 LOGERR(
"compute_2d_complex can only work on complex images");
322 int nx = image->get_xsize();
323 int ny = image->get_ysize();
326 LOGERR(
"compute_2d_complex only works on (nx, nx-2) images");
330 float ds = 1.0f / (
apix * ny);
333 float *d = image->get_data();
338 for (
int y = 0;
y < ny;
y++) {
341 for (
int x = 0;
x < nx / 2;
x++) {
342 float s = (float) hypot(
x,
y - ny / 2.0f) * ds;
344 d[
x * 2 + ynx + 1] = 0;
349 for (
int y = 0;
y < ny;
y++) {
352 for (
int x = 0;
x < nx / 2;
x++) {
353 float s = (float)hypot((
float)
x, (float)
y - ny / 2) * ds;
358 d[
x * 2 + ynx + 1] = 0;
363 for (
int y = 0;
y < ny;
y++) {
366 for (
int x = 0;
x < nx / 2;
x++) {
367 float s = (float)hypot((
float)
x, (float)
y - ny / 2) * ds;
371 d[
x * 2 + ynx] = fabs(v);
372 d[
x * 2 + ynx + 1] = 0;
377 for (
int y = 0;
y < ny;
y++) {
379 for (
int x = 0;
x < nx / 2;
x++) {
380 float s = (float)hypot(
x,
y - ny / 2.0f) * ds;
383 d[
x * 2 + ynx] = v > 0 ? 1.0f : -1.0f;
384 d[
x * 2 + ynx + 1] = 0;
392 for (
int y = 0;
y < ny;
y++) {
395 for (
int x = 0;
x < nx / 2;
x++) {
397 float s = (float)hypot(
x,
y - ny / 2.0f) * ds;
407 d[
x * 2 + ynx + 1] = 0;
414 for (
int y = 0;
y < ny;
y++) {
417 for (
int x = 0;
x < nx / 2;
x++) {
419 float s = (float)hypot(
x,
y - ny / 2.0f) * ds;
428 f = 1.0f / (1.0f + 1.0f / f);
430 d[
x * 2 + ynx + 1] = 0;
437 for (
int y = 0;
y < ny;
y++) {
440 for (
int x = 0;
x < nx / 2;
x++) {
442 float s = (float)hypot(
x,
y - ny / 2.0f) * ds;
454 d[
x * 2 + ynx + 1] = 0;
514 int bglen=0,snrlen=0;
516 const char *s=ctf.c_str();
518 sscanf(s,
"%c%f %f %f %f %f %f %f %f %f %d%n",
519 &type,&
defocus, &
dfdiff,&
dfang,&
bfactor,&
ampcont,&
voltage, &
cs, &
apix,&
dsbg,&bglen,&pos);
525 for (i=0; i<bglen; i++) {
526 if (sscanf(s+pos,
",%f%n",&v,&j)<1)
return(1);
531 sscanf(s+pos,
" %d%n",&snrlen,&j);
534 for (i=0; i<snrlen; i++) {
535 if (sscanf(s+pos,
",%f%n",&v,&j)<1)
return(1);
547 sprintf(ctf,
"E%1.10g %1.10g %1.10g %1.10g %1.10g %1.10g %1.10g %1.10g %1.10g %d",
551 for (
int i=0; i<(int)
background.size(); i++) {
556 sprintf(ctf,
" %d",(
int)
snr.size());
558 for (
int i=0; i<(int)
snr.size(); i++) {
559 sprintf(ctf,
",%1.3g",
snr[i]);
570 defocus = (float)dict[
"defocus"];
572 dfdiff = (float)dict[
"dfdiff"];
574 dfang = (float)dict[
"dfang"];
576 bfactor = (float)dict[
"bfactor"];
578 ampcont = (float)dict[
"ampcont"];
580 voltage = (float)dict[
"voltage"];
582 cs = (float)dict[
"cs"];
584 apix = (float)dict[
"apix"];
586 dsbg = (float)dict[
"dsbg"];
599 dict[
"dfang"] =
dfang;
625 for (i=0; i<(int)vctf[9]; i++)
background[i]=vctf[i+10];
626 snr.resize((
int)vctf[i+10]);
627 for (
int j=0; j<(int)vctf[i+10]; j++)
snr[j]=vctf[i+j+11];
636 vctf.push_back(
dfang);
641 vctf.push_back(
apix);
642 vctf.push_back(
dsbg);
645 vctf.push_back((
float)
snr.size());
646 for (
unsigned int j=0; j<
snr.size(); j++) vctf.push_back(
snr[j]);
674 return -M_PI-asin(-2.0-
ampcont/100.0);
681 else printf(
"Phase = %0.4f deg out of range\n",
phase*180.0/M_PI);
688 vector <float> ret(size);
689 vector <float> norm(size);
691 if (!image->is_complex() || !image->is_ri())
ImageFormatException(
"EMAN2Ctf::compute_1d_fromimage requires a complex, ri image");
692 int isinten=image->get_attr_default(
"is_intensity",0);
693 if (isinten)
ImageFormatException(
"EMAN2Ctf::compute_1d_fromimage does not take intensity images");
695 int nx=image->get_attr(
"nx");
696 int ny=image->get_attr(
"ny");
697 float *data=image->get_data();
700 for (
y=i=0;
y<ny;
y++) {
701 for (
x=0;
x<nx;
x+=2,i+=2) {
702 if (
x==0 &&
y>ny/2)
continue;
704 float a=(float)atan2((
float)(
y<ny/2?
y:ny-
y),(
float)(
x/2));
710 if (f>=0 && f<size) {
721 for (i=0; i<size; i++) ret[i]/=norm[i]?norm[i]:1.0f;
728inline int max_int(
int a,
int b) {
return a>b?a:b; }
729inline int min_int(
int a,
int b) {
return a<b?a:b; }
743 float g1=M_PI/2.0*
cs*1.0e7*pow(
lambda(),3.0f);
749 for (
int i = 0; i < np; i++) {
750 float gam=-g1*s*s*s*s+g2*s*s;
751 r[i] = cos(gam-acac)*exp(-(
bfactor/4.0f * s * s));
757 for (
int i = 0; i < np; i++) {
758 float gam=-g1*s*s*s*s+g2*s*s;
759 r[i] = fabs(cos(gam-acac)*exp(-(
bfactor/4.0f * s * s)));
765 for (
int i = 0; i < np; i++) {
766 float gam=-g1*s*s*s*s+g2*s*s;
767 r[i] = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
774 for (
int i = 0; i < np; i++) {
775 float gam=-g1*s*s*s*s+g2*s*s;
776 r[i] = cos(gam-acac)<0?-1.0:1.0;
782 for (
int i = 0; i < np; i++) {
784 int j = (int)floor(f);
797 for (
int i = 0; i < np; i++) {
799 int j = (int)floor(f);
801 if (j>(
int)
snr.size()-2) r[i]=
snr.back();
802 else r[i]=
snr[j]*(1.0f-f)+
snr[j+1]*f;
813 vector < float >tsnr;
815 vector < float >dsnr;
820 for (
int i = 0; i < np; i++) {
821 float gam=-g1*s*s*s*s+g2*s*s;
822 tsnr[i] = cos(gam-acac)*exp(-(
bfactor/4.0f * s * s));
826 int j = (int)floor(f);
831 if (bg <=0) bg=.001f;
833 tsnr[i] = tsnr[i]*tsnr[i]/bg;
840 if (j>(
int)
snr.size()-2) dsnr[i]=
snr.back();
841 else dsnr[i]=
snr[j]*(1.0f-f)+
snr[j+1]*f;
850 for (
int i = 1; i < np; i++) {
869 double div = sum * sum_xx - sum_x * sum_x;
877 if (
div!=0.0) r[i]=(float) ((sum * sum_xy - sum_x * sum_y) /
div)*tsnr[i];
893 for (
int i = 0; i < np; i++) {
895 int j = (int)floor(f);
898 if (j>(
int)
snr.size()-2) {
904 r[i]=
snr[j]*(1.0f-f)+
snr[j+1]*f;
908 r[i]=r[i]/(r[i]+1.0f);
917 for (
int i = 0; i < np; i++) {
918 float gam=-g1*s*s*s*s+g2*s*s;
920 r[i] = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
924 r[i] = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
931 for (
int i = 0; i < np; i++) {
932 float gam=-g1*s*s*s*s+g2*s*s;
934 r[i] = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
935 r[i] = r[i] * r[i] * sf->
get_yatx(s);
939 r[i] = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
967 LOGERR(
"image is null. cannot computer 2D complex CTF");
971 if (image->is_complex() ==
false) {
972 LOGERR(
"compute_2d_complex can only work on complex images");
976 int nx = image->get_xsize();
977 int ny = image->get_ysize();
979 if ((ny%2==1 && nx!=ny+1) || (ny%2==0 && nx != ny + 2)) {
980 printf(
"nx=%d ny=%d\n",nx,ny);
981 LOGERR(
"compute_2d_complex only works on (nx, nx-2) images");
985 float ds = 1.0f / (
apix * ny);
988 float *d = image->get_data();
989 float g1=M_PI/2.0*
cs*1.0e7*pow(
lambda(),3.0f);
990 float g2=M_PI*
lambda()*10000.0;
995 for (
int y = -ny/2;
y < ny/2;
y++) {
999 for (
int x = 0;
x < nx / 2;
x++) {
1002 d[
x * 2 + ynx + 1] = 0;
1007 for (
int y = -ny/2;
y < ny/2;
y++) {
1011 for (
int x = 0;
x < nx / 2;
x++) {
1015 else gam=-g1*s*s*s*s+g2*
df(atan2((
float)
y,(
float)
x))*s*s;
1016 float v = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
1018 d[
x * 2 + ynx + 1] = 0;
1023 for (
int y = -ny/2;
y < ny/2;
y++) {
1027 for (
int x = 0;
x < nx / 2;
x++) {
1031 else gam=-g1*s*s*s*s+g2*
df(atan2((
float)
y,(
float)
x))*s*s;
1032 float v = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
1033 d[
x * 2 + ynx] = fabs(v);
1034 d[
x * 2 + ynx + 1] = 0;
1039 for (
int y = -ny/2;
y < ny/2;
y++) {
1043 for (
int x = 0;
x < nx / 2;
x++) {
1047 else gam=-g1*s*s*s*s+g2*
df(atan2((
float)
y,(
float)
x))*s*s;
1048 float v = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
1049 d[
x * 2 + ynx] = v*v;
1050 d[
x * 2 + ynx + 1] = 0;
1055 for (
int y = -ny/2;
y < ny/2;
y++) {
1059 for (
int x = 0;
x < nx / 2;
x++) {
1065 else if (s<.05) mf=1.0-exp(-pow((s-.04f)*300.0f,2.0f));
1067 else gam=-g1*s*s*s*s+g2*
df(atan2((
float)
y,(
float)
x))*s*s;
1068 float v = cos(gam-acac);
1069 if (v>0.9) v=exp(-(50.0f/4.0f * s*s));
1071 d[
x * 2 + ynx] = mf*v*v;
1072 d[
x * 2 + ynx + 1] = 0;
1077 for (
int y = -ny/2;
y < ny/2;
y++) {
1080 for (
int x = 0;
x < nx / 2;
x++) {
1084 else gam=-g1*s*s*s*s+g2*
df(atan2((
float)
y,(
float)
x))*s*s;
1085 float v = cos(gam-acac);
1086 d[
x * 2 + ynx] = v<0?-1.0:1.0;
1087 d[
x * 2 + ynx + 1] = 0;
1092 for (
int y = -ny/2;
y < ny/2;
y++) {
1096 for (
int x = 0;
x < nx / 2;
x++) {
1101 d[
x * 2 + ynx + 1] = 0;
1107 mf=1.0-exp(-pow((s-.04f)*300.0f,2.0f));
1111 else gam=-g1*s*s*s*s+g2*
df(atan2((
float)
y,(
float)
x))*s*s;
1112 float v = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
1113 d[
x * 2 + ynx] = mf*v*v;
1114 d[
x * 2 + ynx + 1] = 0;
1120 for (
int y = -ny/2;
y < ny/2;
y++) {
1124 for (
int x = 0;
x < nx / 2;
x++) {
1128 int j = (int)floor(f);
1130 if (j>(
int)
snr.size()-2) d[
x*2+ynx]=
snr.back();
1131 else d[
x*2+ynx]=
snr[j]*(1.0f-f)+
snr[j+1]*f;
1132 d[
x * 2 + ynx + 1] = 0;
1138 for (
int y = -ny/2;
y < ny/2;
y++) {
1142 for (
int x = 0;
x < nx / 2;
x++) {
1146 int j = (int)floor(f);
1148 if (j>(
int)
snr.size()-2) d[
x*2+ynx]=
snr.back();
1149 else d[
x*2+ynx]=
snr[j]*(1.0f-f)+
snr[j+1]*f;
1150 d[
x * 2 + ynx + 1] = 0;
1156 if (
dsbg==0) printf(
"Warning, DSBG set to 0\n");
1157 for (
int y = -ny/2;
y < ny/2;
y++) {
1161 for (
int x = 0;
x < nx / 2;
x++) {
1165 int j = (int)floor(f);
1168 if (j>(
int)
snr.size()-2) {
1175 snrf=
snr[j]*(1.0f-f)+
snr[j+1]*f;
1178 if (snrf<0) snrf=0.0;
1180 d[
x*2+ynx]=snrf/(snrf+1);
1183 d[
x * 2 + ynx + 1] = 0;
1190 for (
int y = -ny/2;
y < ny/2;
y++) {
1194 for (
int x = 0;
x < nx / 2;
x++) {
1198 else gam=-g1*s*s*s*s+g2*
df(atan2((
float)
y,(
float)
x))*s*s;
1199 float v = cos(gam-acac)*exp(-(
bfactor/4.0f * s*s));
1201 d[
x * 2 + ynx + 1] = 0;
1208 for (
int y = -ny/2;
y < ny/2;
y++) {
1212 for (
int x = 0;
x < nx / 2;
x++) {
1216 else gam=-g1*s*s*s*s+g2*
df(atan2((
float)
y,(
float)
x))*s*s;
1220 float v = (pow((
float)cos(gam-acac),2.0f))*exp(-(
bfactor/4.0f * s*s));
1224 d[
x * 2 + ynx + 1] = 0;
1232 else printf(
"Unknown CTF image mode\n");
1253 snr.size() != c->
snr.size()
1256 for (
unsigned int i=0; i<
background.size(); i++) {
1259 for (
unsigned int i=0; i<
snr.size(); i++) {
1260 if (
snr[i]!=c->
snr[i])
return false;
1268vector <float>zeroes;
1274for (
int i=-m; i<m; i++) {
1279 if (r2>0) zeroes.push_back(
sqrt(r2/(
cs*lam*lam*1.0e7)));
1281 if (r2a>0) zeroes.push_back(
sqrt(r2a/(
cs*lam*lam*1.0e7)));
1283if (zeroes.size()==0)
return 0.0f;
1284std::sort(zeroes.begin(),zeroes.end());
Ctf is the base class for all CTF model.
Dict is a dictionary to store <string, EMObject> pair.
EMAN1Ctf is the CTF model used in EMAN1.
vector< float > compute_1d_fromimage(int size, float ds, EMData *image)
float calc_snr(float g1, float gamma, float s)
void from_vector(const vector< float > &vctf)
int from_string(const string &ctf)
float calc_noise(float s)
void from_dict(const Dict &dict)
float calc_ctf1(float g, float gamma, float s)
void compute_2d_real(EMData *image, CtfType type, XYData *struct_factor=0)
void copy_from(const Ctf *new_ctf)
vector< float > to_vector() const
void compute_2d_complex(EMData *image, CtfType type, XYData *struct_factor=0)
float calc_gamma(float g1, float g2, float s)
vector< float > compute_1d(int size, float ds, CtfType type, XYData *struct_factor=0)
bool equal(const Ctf *ctf1) const
float calc_amplitude(float gamma)
EMAN2Ctf is the default CTF model used in EMAN2.
float df(float ang) const
bool equal(const Ctf *ctf1) const
float calc_noise(float s) const
void compute_2d_real(EMData *image, CtfType type, XYData *struct_factor=0)
float stos2(float s, float dZ)
vector< float > background
void from_dict(const Dict &dict)
vector< float > compute_1d_fromimage(int size, float ds, EMData *image)
vector< float > compute_1d(int size, float ds, CtfType type, XYData *struct_factor=0)
void from_vector(const vector< float > &vctf)
vector< float > to_vector() const
void copy_from(const Ctf *new_ctf)
void compute_2d_complex(EMData *image, CtfType type, XYData *struct_factor=0)
int from_string(const string &ctf)
void set_phase(float phase)
EMData stores an image's data and defines core image processing routines.
static float square_sum(float x, float y)
Calcuate (x*x + y*y).
static float hypot_fast(int x, int y)
Euclidean distance in 2D for integers computed fast using a cached lookup table.
XYData defines a 1D (x,y) data set.
float get_yatx(float x, bool outzero=true)
int max_int(int a, int b)
int min_int(int a, int b)
#define Assert(s)
Define Assert() function that is effective only when -DDEBUG is used.
EMData * sqrt() const
return square root of current image
EMData * phase() const
return phase part of a complex image as a real image format
void div(float f)
make each pixel value divided by a float number.
#define InvalidParameterException(desc)
#define InvalidValueException(val, desc)
#define ImageFormatException(desc)