59 cerr <<
"in img_ctf_calculate" << endl;
63 if ( lores < 0 ) lores = 0;
64 if ( hires <= 0 ) hires = sampling.
x();
65 if ( lores > 0 && lores < hires ) swap(lores, hires);
68 double slo = (lores > 0)? 1/lores: 0;
73 long i, n, x, y, z, sign;
74 double sx, sy, sz, s, s2, w, ctf_fac, base, env, ctf_env;
82 cout <<
"Calculating a CTF function:" << endl;
84 case 1: cout <<
"Flipping phases" << endl;
break;
85 case 2: cout <<
"Applying a CTF" << endl;
break;
86 case 3: cout <<
"Correcting for the CTF" << endl;
break;
87 case 4: cout <<
"Correcting for the CTF with an optimal Wiener filter" << endl;
break;
88 case 5: cout <<
"Correcting for the CTF with baseline enhancement" << endl;
break;
89 case 6: cout <<
"Correcting for the CTF with baseline2 enhancement" << endl;
break;
90 case 7: cout <<
"Flipping phases with baseline enhancement" << endl;
break;
91 case 8: cout <<
"Correcting for the CTF with baseline3 enhancement" << endl;
break;
99 cout <<
"Resolution range: " << hires <<
" - ";
100 if ( lores > 0 ) cout << lores <<
" A" << endl;
101 else cout <<
"inf A" << endl;
102 if ( action == 3 || action == 5 || action == 6 )
103 cout <<
"Wiener factor: " << wiener << endl;
105 cout <<
"Envelope: " << env_eq << endl;
106 cout <<
"Noise: " << base_eq << endl;
111 for ( i=n=0; n<p->
images(); n++ ) {
112 for ( z=0; z<p->
sizeZ(); z++ ) {
114 if ( z > h.
z() ) sz -= p->
sizeZ();
115 sz *= freq_scale.z();
116 for ( y=0; y<p->
sizeY(); y++ ) {
118 if ( y > h.
y() ) sy -= p->
sizeY();
119 sy *= freq_scale.y();
120 for ( x=0; x<p->
sizeX(); x++, i++ ) {
122 if ( x > h.
x() ) sx -= p->
sizeX();
123 sx *= freq_scale.x();
124 s2 = sx*sx + sy*sy + sz*sz;
126 if ( s >= slo && s <= shi ) {
127 ctf_fac = cp.
calculate(s2, atan2(sy,sx));
129 sign = ( ctf_fac < 0 )? -1: 1;
132 case 1: w = sign;
break;
133 case 2: w = ctf_fac;
break;
135 w = ctf_fac/(ctf_fac*ctf_fac + wiener);
140 ctf_env = ctf_fac*env;
142 w = ctf_fac*sqrt(env)/(ctf_fac*ctf_env + base);
147 w = ctf_fac/(ctf_fac*ctf_fac*base + wiener);
151 w = 1/(ctf_fac*base + sign*wiener);
154 if ( base > 0 ) w = sign/sqrt(base);
158 w = sign/sqrt(ctf_fac*ctf_fac*env + base);
198 unsigned long i, n, x, y, z;
199 double sx, sy, sz, s2;
204 cout <<
"Calculating a wave aberration function:" << endl;
207 cout <<
"Voltage: " << cp.
volt() <<
" V" << endl;
208 cout <<
"Wavelength: " << cp.
lambda() <<
" A" << endl;
209 cout <<
"Cs: " << cp.
Cs() <<
" A" << endl;
210 cout <<
"Amplitude contrast: " << cp.
amp_fac() << endl;
214 for ( i=n=0; n<p->
images(); n++ ) {
215 for ( z=0; z<p->
sizeZ(); z++ ) {
217 if ( z > h.
z() ) sz -= p->
sizeZ();
218 sz *= freq_scale.z();
219 for ( y=0; y<p->
sizeY(); y++ ) {
221 if ( y > h.
y() ) sy -= p->
sizeY();
222 sy *= freq_scale.y();
223 for ( x=0; x<p->
sizeX(); x++, i++ ) {
225 if ( x > h.
x() ) sx -= p->
sizeX();
226 sx *= freq_scale.x();
227 s2 = sx*sx + sy*sy + sz*sz;
269 double lores,
double hires)
271 if ( action < 1 || action > 10 )
return 0;
273 if ( wiener < 0.01 ) wiener = 0.01;
278 int back_transform(0);
286 for ( nn=j=0; nn<p->
images(); nn++ )
302 if ( action < 1 || action > 10 )
return 0;
304 if ( wiener < 0.01 ) wiener = 0.01;
309 int back_transform(0);
314 cout <<
"DEBUG img_ctf_apply: image transformed" << endl;
319 for ( nn=j=0; nn<p->
images(); nn++ )
325 if ( back_transform ) {
329 cout <<
"DEBUG img_ctf_apply: image back-transformed" << endl;
338 double wiener,
double def,
double res_lo,
double res_hi,
355 img_ctf_apply(pt, ctf_temp, action, wiener, res_lo, res_hi, planf, planb);
357 pt->
resize(oldsize, translate);
381 if ( !em_ctf )
return -1;
384 cout <<
"DEBUG img_ctf_apply_to_proj: def_avg=" << em_ctf->
defocus_average() << endl;
390 img_ctf_apply(proj, ctf_temp, 2, 0, res_lo, res_hi, planf_2D, planb_2D);
432 Vector3<int> tile_size,
double tilt,
double axis,
double res_lo,
double res_hi)
434 if ( res_lo > 0 && res_lo < res_hi ) swap(res_lo, res_hi);
443 tile_size = tile_size.
min(p->
size());
445 tile_size = tile_size.
max(min_size);
446 tile_size = tile_size.
min(max_size);
448 tile_size = tile_size.
min(p->
size());
455 double ca(cos(axis)), sa(sin(axis)), tt(tan(tilt));
457 double* d =
new double[pt->
images()];
462 for ( nn=0; nn<pt->
images(); nn++ ) {
476 cout <<
"Applying a tilted image transfer function:" << endl;
478 case 1: cout <<
"Flipping phases" << endl;
break;
479 case 2: cout <<
"Applying a CTF" << endl;
break;
480 case 3: cout <<
"Correcting for the CTF" << endl;
break;
481 case 4: cout <<
"Correcting for the CTF with an optimal Wiener filter" << endl;
break;
482 case 5: cout <<
"Correcting for the CTF with baseline enhancement" << endl;
break;
483 case 6: cout <<
"Correcting for the CTF with baseline2 enhancement" << endl;
break;
484 case 7: cout <<
"Flipping phases with baseline enhancement" << endl;
break;
485 case 8: cout <<
"Correcting for the CTF with baseline3 enhancement" << endl;
break;
490 cout <<
"Tile size: " << tile_size << endl;
491 cout <<
"Defocus range: " << d[0] <<
" - " << d[nn-1] <<
" (" << nn <<
")" << endl;
492 cout <<
"Tilt angle and axis: " << tilt*180.0/
M_PI <<
" " << axis*180.0/
M_PI << endl;
494 cout <<
"Resolution range: " << res_hi <<
" - ";
495 if ( res_lo > 0 ) cout << res_lo <<
" A" << endl;
496 else cout <<
"inf A" << endl;
497 if ( action == 3 || action == 5 || action == 6 )
498 cout <<
"Wiener factor: " << wiener << endl;
500 cout <<
"Envelope: " << env_eq << endl;
501 cout <<
"Noise: " << base_eq << endl;
514 dispatch_apply(pt->
images(), dispatch_get_global_queue(0, 0), ^(
size_t i){
515 img_ttf_apply_one(pt, i, ctf, action, wiener, d[i], res_lo, res_hi, psize, planf, planb);
518 #pragma omp parallel for 519 for (
long i=0; i<pt->
images(); i++ )
520 img_ttf_apply_one(pt, i, ctf, action, wiener, d[i], res_lo, res_hi, psize, planf, planb);
557 cerr <<
"in img_ctf_radial_average" << endl;
562 if ( em_ctf->
Cs() < 1e3 || em_ctf->
Cs() > 1e8 )
566 unsigned long nrad = p->
sizeX()/2;
572 double dx, dy, dz, radius, angle, fraction, cosang, sinang;
575 double* num =
new double[nrad];
579 cout <<
"Radial average for CTF fitting:" << endl;
580 cout <<
"Size: " << nrad << endl;
581 cout <<
"Power spectrum origin: " << p->
image->
origin() << endl;
582 cout <<
"Defocus, deviation, angle: " <<
586 if ( p->
sizeZ() < 2 ) {
588 cout <<
"DEBUG img_ctf_radial_average: doing image " << n+1 << endl;
589 for ( i=0; i<nrad; i++ ) num[i] = 0;
590 for ( y=0; y<p->
sizeY(); y++ ) {
592 for ( x=0; x<p->
sizeX(); x++ ) {
594 radius = sqrt(dx*dx + dy*dy);
595 iradius = (long) radius;
596 if ( iradius < nrad ) {
600 radius *= sqrt(smax2*cosang*cosang + smin2*sinang*sinang);
601 if ( !isfinite(radius) )
602 cerr <<
"Warning: radius for " << iradius <<
" not finite!" << endl;
603 iradius = (long) radius;
604 if ( iradius < nrad - 1 ) {
605 fraction = radius - iradius;
606 num[iradius] += 1.0 - fraction;
607 i = p->
index(0,x,y,0,n);
608 rdata[iradius] += (1.0 - fraction)*(*p)[i];
610 if ( iradius < nrad ) {
611 num[iradius] += fraction;
612 rdata[iradius] += fraction*(*p)[i];
618 for ( iradius=0; iradius<nrad; iradius++ )
619 if ( num[iradius] ) rdata[iradius] /= num[iradius];
622 cout <<
"DEBUG img_ctf_radial_average: calculating 3D RPS" << endl;
623 for ( i=0; i<nrad; i++ ) num[i] = 0;
624 for ( z=0; z<p->
sizeZ(); z++ ) {
626 for ( y=0; y<p->
sizeY(); y++ ) {
628 for ( x=0; x<p->
sizeX(); x++ ) {
630 radius = sqrt(dx*dx + dy*dy + dz*dz);
631 iradius = (long) radius;
632 if ( iradius < nrad - 1 ) {
633 fraction = radius - iradius;
634 num[iradius] += 1.0 - fraction;
635 i = p->
index(0,x,y,z,n);
636 rdata[n*nrad+iradius] += (1.0 - fraction)*(*p)[i];
638 if ( iradius < nrad ) {
639 num[iradius] += fraction;
640 rdata[iradius] += fraction*(*p)[i];
646 for ( iradius=0; iradius<nrad; iradius++ )
647 if ( num[iradius] ) rdata[iradius] /= num[iradius];
662 double s, recip_interval = 1.0/real_size;
665 for ( i=0; i<prmb->
sizeX(); i++ ) {
666 s = i*recip_interval;
707 if ( hires < 2*p->sampling().x() ) hires = 2*p->
sampling().x();
708 if ( lores < hires ) lores = p->
real_size().
x();
712 cout <<
"Determining CTF parameters:" << endl;
713 cout <<
"Sampling: " << p->
sampling().x() <<
" A" << endl;
714 cout <<
"Voltage: " << em_ctf->
volt() <<
" V" << endl;
715 cout <<
"Wavelength: " << em_ctf->
lambda() <<
" A" << endl;
716 cout <<
"Cs: " << em_ctf->
Cs() <<
" A" << endl;
717 cout <<
"Amplitude contrast: " << em_ctf->
amp_fac() << endl;
718 cout <<
"Resolution range: " << hires <<
" - " << lores <<
" A" << endl;
719 cout <<
"Baseline type: " << em_ctf->
baseline_type() << endl;
720 cout <<
"Envelope type: " << em_ctf->
envelope_type() << endl << endl;
729 double fom(0), fom_prev(-1), fom_max(0);
737 while ( ( fom_max > fom_prev ) && --iter ) {
751 if ( i<1 || fom_max <= fom ) {
759 cout <<
"Best defocus (" << i <<
"): " 763 <<
" (" << fom_max <<
")" << endl;
769 return em_ctf->
zero(1);
788 cerr <<
"Error in img_ctf_fit_astigmatism: No radial power spectrum calculated!" << endl;
792 double pixel_size = prad->
sampling().x();
793 if ( pixel_size <= 0 ) pixel_size = 1;
795 long i, ib1, ib2, im, j, nn(0), nz(0);
796 long k = prad->
sizeX()/100;
798 long ilo = (long) (real_size/lores + 0.5);
799 long ihi = (long) (real_size/hires + 0.5);
800 if ( ihi >= prad->
sizeX() ) ihi = prad->
sizeX() - 1;
801 double b1, b2, m, OM(0);
802 double max_s = 0.45/pixel_size;
806 double* zero = em_ctf->
zeroes(max_s, nz);
810 for ( i=1; i<nz; i++ ) {
811 ib1 = (long) (zero[i-1]*real_size + 0.5);
812 ib2 = (long) (zero[i]*real_size + 0.5);
814 if ( im >= ilo && im <= ihi ) {
815 for ( b1=(*prmb)[ib1], j=ib1-k; j<=ib1+k; j++ )
816 if ( b1 > (*prmb)[j] ) b1 = (*prmb)[j];
817 for ( b2=(*prmb)[ib2], j=ib2-k; j<=ib2+k; j++ )
818 if ( b2 > (*prmb)[j] ) b2 = (*prmb)[j];
819 for ( m=(*prmb)[im], j=im-k; j<=im+k; j++ )
820 if ( m < (*prmb)[j] ) m = (*prmb)[j];
821 OM += im*(m - (b1 + b2)/2);
833 cout <<
"Fitting measure: " << OM << endl;
857 cerr <<
"Error in img_ctf_fit_baseline: No radial power spectrum calculated!" << endl;
890 cerr <<
"Error in img_ctf_fit_envelope: No radial power spectrum calculated!" << endl;
905 double step_size,
double* r,
long rmin,
long rmax)
908 long nr(rmax-rmin+1);
916 for ( i=rmin; i<=rmax; i++ ) fom += (c[i]*c[i] - 0.5)*r[i];
943 double lores,
double hires,
944 double def_start,
double def_end,
double def_inc)
948 if ( hires < 2*p->sampling().x() ) hires = 2*p->
sampling().x();
949 if ( lores < hires ) lores = 100;
950 if ( lores > 100 ) lores = 100;
952 long i, nx(p->
sizeX()/2);
954 long rmin = (long) (p->
real_size().
x()/lores);
955 long rmax = (long) (p->
real_size().
x()/hires);
957 for ( i=0; i<rmin; i++ ) prad->
set(i, (*prad)[rmin]);
961 for ( i=0; i<nx; i++ )
962 if ( r[i] ) r[i] = (*prad)[i]/r[i] - 1;
965 long nr(rmax-rmin+1);
966 double def, def_best(0);
967 double fom, fom_max(0);
970 cout <<
"Defocus range: " << def_start*1e-4 <<
" - " << def_end*1e-4 << endl;
971 cout <<
"Accelerating voltage: " << em_ctf->
volt()*1e-3 <<
" kV" << endl;
972 cout <<
"Wavelength: " << em_ctf->
lambda() <<
" A" << endl;
973 cout <<
"Spherical abberation: " << em_ctf->
Cs()*1e-7 <<
" mm" << endl;
974 cout <<
"Amplitude contrast: " << em_ctf->
amp_fac() << endl;
975 cout <<
"Range: " << rmin <<
" - " << rmax << endl;
976 cout <<
"Frequency step size: " << step_size <<
" 1/A" << endl;
980 cout <<
"Defocus\tCC" << endl;
981 while ( def_inc >= 50 ) {
982 for ( def=def_start; def<=def_end; def+=def_inc ) {
984 if ( fom_max < fom ) {
989 cout << def*1e-4 <<
tab << fom << endl;
991 def_start = def_best - 2*def_inc;
992 def_end = def_best + 2*def_inc;
994 if ( def_start < 100 ) def_start = 100;
1002 em_ctf->
fom(fom_max);
1040 for ( i=0; i<20 && ( best_fom/prev_fom - 1 > 1e-6 ); i++ ) {
1041 for ( na=0, angle=angle_min; angle<=angle_max; angle+=angle_inc ) na++;
1043 fom =
new double[na];
1045 for ( angle=angle_min, j=0; angle<=angle_max; angle+=angle_inc, j++ ) {
1051 dispatch_apply(na, dispatch_get_global_queue(0, 0), ^(
size_t k){
1055 #pragma omp parallel for 1056 for ( j=0; j<na; j++ ) {
1061 for ( j=0; j<na; j++ ) {
1062 if ( best_fom < fom[j] ) {
1063 prev_fom = best_fom;
1072 ddiff = best_dev - def_dev;
1080 angle_min = best_ang - angle_range/2;
1081 angle_max = best_ang + angle_range/2;
1083 }
else def_dev += ddiff/2;
1093 cout <<
"Astigmatism refined: " << em_ctf->
defocus_average() <<
" +- " 1101 if ( hires < 2*p->sampling().x() ) hires = 2*p->
sampling().x();
1102 if ( lores < hires ) lores = p->
real_size().
x();
1106 size_t rmin = (size_t) (p->
real_size().
x()/lores);
1107 size_t rmax = (size_t) (p->
real_size().
x()/hires);
1109 double ell, ell_start(0.001), ell_mult(1.2), ell_max(0.1), ell_end(ell_max);
1110 double best_ell(0), best_angle(0);
1111 double angle, ang_start(0), ang_inc(
M_PI/36), ang_end(
M_PI);
1112 double best_fom(0), mell, mang, m, d;
1116 long nr = prad->
sizeX();
1117 double* ra =
new double[nr];
1118 for ( j=0; j<nr; j++ ) ra[j] = (*prad)[j];
1124 Bimage* pell = p->
radial(rmin, rmax, 1, best_ell, best_angle, NULL);
1125 for ( best_fom = 0, j=rmin; j<=rmax; j++ ) {
1126 d = (*pell)[j] / r[j] - 1;
1129 best_fom = sqrt(best_fom/(rmax - rmin + 1));
1134 cout << setprecision(4) <<
"Ell\tAng\tFOM" << endl;
1135 cout << best_ell <<
tab << best_angle*180.0/
M_PI <<
tab << best_fom << endl;
1138 while ( ell_mult > 1.001 ) {
1139 for ( ell=ell_start; ell<ell_end; ell*=ell_mult ) {
1140 mell = mang = m = 0;
1141 for ( n=0, angle=ang_start; angle<ang_end; angle+=ang_inc ) n++;
1143 fom =
new double[n];
1144 for ( i=0, angle=ang_start; angle<ang_end; angle+=ang_inc, i++ ) ar[i] = angle;
1147 dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(
size_t k){
1148 Bimage* pel = p->
radial(rmin, rmax, 1, ell, ar[k], NULL);
1151 for (
long j=rmin; j<=rmax; j++ ) {
1152 d = (*pel)[j] / r[j] - 1;
1155 fom[k] = sqrt(fom[k]/(rmax - rmin + 1));
1160 #pragma omp parallel for 1161 for ( i=0; i<n; i++ ) {
1162 Bimage* pel = p->
radial(rmin, rmax, 1, ell, ar[i], NULL);
1165 for (
long j=rmin; j<=rmax; j++ ) {
1166 d = (*pel)[j] / r[j] - 1;
1169 fom[i] = sqrt(fom[i]/(rmax - rmin + 1));
1175 for ( i=0; i<n; i++ ) {
1182 if ( best_fom < fom[i] ) {
1193 cout << mell <<
tab << mang*180.0/
M_PI <<
tab << m << endl;
1195 ell_start = best_ell/ell_mult;
1196 ell_end = best_ell*ell_mult;
1197 ell_mult = (ell_mult - 1)/2 + 1;
1198 ang_start = best_angle - 4*ang_inc;
1199 ang_end = best_angle + 4*ang_inc;
1204 cout <<
"Astigmatism: " << best_ell <<
" " 1205 << best_angle*180.0/
M_PI <<
" " << best_fom << endl;
1223 for ( i=0; i<5; i++ ) {
1228 double real_size = simp->
constant(4);
1232 long ilo = (long) (real_size/lores + 0.5);
1233 long ihi = (long) (real_size/hires + 0.5);
1235 double base, env, a,
b, d, s, R(0);
1236 double recip_interval = 1.0/real_size;
1240 for ( i=ilo; i<=ihi; i++ ) {
1244 a = env*ctf[i]*ctf[i];
1245 b = simp->
fx[i] - base;
1246 if ( a > 1e-3 && fabs(b) > 1e-3 ) {
1259 cout <<
"Fitting residual: " << R << endl;
1266 double pixel_size = prad->
sampling().x();
1267 if ( pixel_size <= 0 ) pixel_size = 1;
1269 long i, j, ib, n, nc(4);
1270 long istart = (long) (real_size/lores);
1271 long iend = (long) (real_size/hires);
1272 if ( istart < 1 ) istart = 1;
1273 if ( iend >= prad->
sizeX() ) iend = prad->
sizeX() - 1;
1274 long w = (iend - istart)/10;
1276 double recip_interval = 1.0/real_size;
1277 double max_s = 0.45/pixel_size;
1278 long k = prad->
sizeX()/100;
1284 double* s =
new double [prad->
sizeX()];
1285 double* d =
new double [prad->
sizeX()];
1286 for ( i=0; i<prad->
sizeX(); i++ ) s[i] = d[i] = 0;
1288 double* zero = em_ctf->
zeroes(max_s, n);
1293 cout <<
"DEBUG ctf_fit_baseline: zeroes=" << n << endl;
1296 double* mavg = NULL;
1299 for ( i=0; i<n; i++ ) {
1300 ib = (long) (zero[i]*real_size + 0.5);
1303 for ( j=(ib>k)?ib-k:0; j<=ib+k && j<prad->
sizeX(); j++ )
1304 if ( d[i] > (*prad)[j] ) d[i] = (*prad)[j];
1308 for ( i=istart, n=0; i<iend; i++ ) {
1309 if ( min > (*prad)[i] - mavg[i] ) {
1310 min = (*prad)[i] - mavg[i];
1311 s[n] = recip_interval*i;
1313 if ( d[n] > mavg[i] ) d[n] = mavg[i];
1327 cout <<
"DEBUG ctf_fit_baseline: fitting baseline type " << em_ctf->
baseline_type() << endl;
1331 if ( n < 5 ) nc = n - 1;
1341 cerr <<
"Error: Baseline type " << em_ctf->
baseline_type() <<
" not supported" << endl;
1356 double pixel_size = prad->
sampling().x();
1357 if ( pixel_size <= 0 ) pixel_size = 1;
1359 long i, j, nzero, ib;
1360 long k = prad->
sizeX()/80;
1363 double max_s = 0.45/pixel_size;
1366 double* zero = em_ctf->
zeroes(max_s, nzero);
1368 if ( nzero > prad->
sizeX() ) nzero = prad->
sizeX();
1371 double* s =
new double[nzero];
1372 double* d =
new double[nzero];
1373 for ( i=0; i<
NCTFPARAM; i++ ) coeff[i] = 0;
1374 for ( i=0; i<nzero; i++ ) s[i] = d[i] = 0;
1376 ib = (long) (zero[0]*real_size + 0.5);
1378 for ( i=ib/2; i<ib; i++ ) {
1387 for ( i=1; i<nzero; i++ ) {
1388 s[i] = (zero[i-1]+zero[i])*0.5;
1389 ib = (long) (s[i]*real_size + 0.5);
1391 d[i] = (*prad)[ib] -
b;
1392 for ( j=(ib>k)?ib-k:0; j<=ib+k && j<prad->
sizeX(); j++ )
1393 if ( d[i] < (*prad)[j] -
b ) d[i] = (*prad)[j] -
b;
1397 cout <<
"DEBUG ctf_fit_envelope: type " << em_ctf->
envelope_type() << endl;
1398 for ( i=0; i<5; i++ ) cout <<
tab << em_ctf->
envelope(i);
1404 for ( i=0; i<2; i++ ) coeff[i+1] = em_ctf->
envelope(i);
1406 for ( i=0; i<2; i++ ) coeff[i] = coeff[i+1];
1410 for ( i=0; i<3; i++ ) coeff[i] = em_ctf->
envelope(i);
1414 for ( i=0; i<4; i++ ) coeff[i+1] = em_ctf->
envelope(i);
1416 for ( i=0; i<4; i++ ) coeff[i] = coeff[i+1];
1420 for ( i=0; i<5; i++ ) coeff[i] = em_ctf->
envelope(i);
1427 cout <<
"DEBUG ctf_fit_envelope:";
1428 for ( i=0; i<5; i++ ) cout <<
tab << coeff[i];
1462 simp.
limits(2, -100, 100);
1467 for ( i=0; i<4; i++ ) coeff[i] = simp.
parameter(i);
1477 for ( i=0; i<simp->
npoint; i++ ) {
1479 exp(simp->
param[2]*sqrt(simp->
x[i]) + simp->
param[3]*simp->
x[i]*simp->
x[i]));
1508 if ( coeff[0] ) simp.
limits(0, 0, 1000);
1509 simp.
limits(1, 0.001, 100);
1510 simp.
limits(2, -1e4, -0.1);
1514 for ( i=0; i<3; i++ ) coeff[i] = simp.
parameter(i);
1522 double R(0), df, s2;
1524 for ( i=0; i<simp->
npoint; i++ ) {
1525 s2 = simp->
x[i]*simp->
x[i];
1556 simp.
limits(2, -1000, -0.1);
1560 simp.
limits(4, -10000, -1);
1565 for ( i=0; i<5; i++ ) coeff[i] = simp.
parameter(i);
1590 if ( coeff[0] ) simp.
limits(0, 0, 1000);
1591 simp.
limits(1, 0.001, 100);
1592 simp.
limits(2, -500, -0.1);
1595 simp.
limits(3, 0.01, 100);
1596 simp.
limits(4, -5000, -1);
1601 for ( i=0; i<5; i++ ) coeff[i] = simp.
parameter(i);
1609 double R(0), df, s2;
1611 for ( i=0; i<simp->
npoint; i++ ) {
1612 s2 = simp->
x[i]*simp->
x[i];
void change_type(char letter)
Get the data type indicated by a single letter code.
fft_plan fft_setup_plan(size_t x, size_t y, size_t z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
int img_ctf_apply_to_proj(Bimage *proj, CTFparam *em_ctf, double defocus, double res_lo, double res_hi, fft_plan planf_2D, fft_plan planb_2D)
Applies a CTF to a projection image.
int replace(Bimage *img)
Replaces the data with that from the given image.
int img_ttf_apply(Bimage *p, CTFparam *ctf, int action, double wiener, Vector3< int > tile_size, double tilt, double axis, double res_lo, double res_hi)
Applies or corrects for the tilted contrast transfer function (TTF).
double fit_polynomial(int n, double *x, double *y, int order, double *coeff)
Fits a data set to a polynomial function.
double double_gaussian_R(Bsimplex *simp)
Header file for image class.
void parameter(int i, double p)
Header file moving average calculations.
double * moving_average(int number, double *x, int window)
Calculates a moving average over an array of data.
Bimage * extract_tiles(size_t nn, size_t ntiles, Vector3< size_t > *coords, Vector3< size_t > tile_size)
Extracts a set of tiles at specified positions from an image into a new image.
Bimage * img_ctf_radial_subtract_baseline(Bimage *prad, double real_size, CTFparam *em_ctf)
int calculate_background(size_t nn)
Calculates the background for one sub-image.
Header file for general utilities.
double img_ctf_fit(Bimage *p, long n, CTFparam *em_ctf, double lores, double hires)
Determines the CTF parameters from a power spectrum.
double astigmatism_angle()
Structure used in the downhill simplex method.
double calc_envelope(double s)
double img_ctf_fit_astigmatism(Bimage *p, long n, CTFparam *em_ctf, double lores, double hires)
Fits the astigmatism with a given defocus, baseline and envelope.
int update(CTFparam *ctf)
Updates a CTF structure from another.
double ctf_test_defocus(CTFparam *em_ctf, double def, long nx, double step_size, double *r, long rmin, long rmax)
double run(int maxcycles, double tolerance, double(funk)(Bsimplex *))
Generalized matrix class.
size_t index(size_t nx, size_t ny) const
double gaussian_R(Bsimplex *simp)
int img_ttf_apply_one(Bimage *p, long nn, CTFparam *ctf, int action, double wiener, double def, double res_lo, double res_hi, Vector3< long > psize, fft_plan planf, fft_plan planb)
Vector3< size_t > size() const
size_t statistics()
Calculates the statistics for an image.
Bimage * radial(size_t minrad, size_t maxrad, double rad_step, int wrap=0)
Calculates the radial average of an image.
int assemble_tiles(Bimage *pt)
Assembles overlapping tiles into this image.
General image parameter class.
Vector3< double > real_size()
double calc_baseline(double s)
double img_ctf_fit_envelope(Bimage *p, long n, CTFparam *em_ctf, double lores, double hires)
Fits only the envelope for a given CTF.
int img_ctf_apply(Bimage *p, CTFparam *em_ctf, int action, double wiener, double lores, double hires)
Applies or corrects for the contrast transfer function (CTF).
Complex< double > complex(size_t j)
Returns a complex value at the given index.
double ctf_fit_eman_to_baseline(long n, double *x, double *y, double *coeff)
double delta_phi(double s2, double angle)
int resize(Vector3< int > nusize, Vector3< int > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.
double eman_baseline_R(Bsimplex *simp)
Bimage * img_wave_aberration(CTFparam cp, Vector3< int > size, Vector3< float > sampling)
Calculates a wave aberration function.
double img_ctf_astigmatism_measure(Bimage *p, long n, double real_size, CTFparam *em_ctf, double lores, double hires)
double defocus_deviation()
double calculate(double s2, double angle)
Bstring envelope_equation()
Generates an envelope equation string from 4 coefficients.
double ctf_fit_envelope(Bimage *prad, double real_size, CTFparam *em_ctf, double lores, double hires)
Header file for CTF (contrast transfer function) functions.
double ctf_fit_gaussian(long n, double *x, double *y, double *coeff)
Bimage * img_ctf_calculate(CTFparam cp, int action, double wiener, Vector3< long > size, Vector3< double > sampling, double lores, double hires)
Calculates a CTF complex image.
void origin(double x, double y, double z)
void set(size_t j, double v)
Sets a single value at the given index.
Bimage * extract(size_t nn)
Extracts one sub-image into new image structure.
double ctf_fit_baseline(Bimage *prad, double real_size, CTFparam *em_ctf, double lores, double hires)
void parameters(int n, double *p)
void complex_to_real()
The real part of a complex image is written to a simple image.
Header file for CTF (contrast transfer function) functions.
unsigned char * data_pointer()
double ctf_R(Bsimplex *simp)
Nelder and Mead downhill simplex method for generalized parameter fitting.
Bimage * copy()
Copies the header information and data of an image into a new image structure.
void constant(int i, double p)
Header file for matrix functions.
double img_ctf_find_defocus(Bimage *p, long n, CTFparam *em_ctf, double lores, double hires, double def_start, double def_end, double def_inc)
Searches for the defocus based on correlation.
double img_ctf_fit_astigmatism_new(Bimage *p, CTFparam *em_ctf, double lores, double hires)
Bstring baseline_equation()
Generates a baseline equation string from 4 to 10 coefficients.
Vector class for 3-value vectors used in 3D space. The internal variables are an array of 3 numbers...
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
int pad(size_t sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
void limits(int i, double vlo, double vhi)
double ctf_fit_double_gaussian_envelope(long n, double *x, double *y, double *coeff)
double img_ctf_fit_baseline(Bimage *p, long n, CTFparam *em_ctf, double lores, double hires)
Fits only the baseline for a given CTF.
double * zeroes(double max_s, long &nzero)
Calculates the zeroes of a CTF curve on the spatial frequency scale.
int findNextPowerOf(int startNumber, int powerOf)
Finds the next greatest number that is a power of a given number.
double * moving_polynomial(int order, int number, double *x, int window)
Calculates a moving polynomial fit over an array of data.
CTFparam * copy()
Copies the CTF structure to a new one.
FourierType fourier_type()
ostream & tab(ostream &out)
double ctf_fit_double_gaussian_baseline(long n, double *x, double *y, double *coeff)
Bimage * img_ctf_radial_average(Bimage *p, long n, CTFparam *em_ctf)
Calculates the power spectrum radial average corrected for astigmatism.