24 if ( compoundtype ==
TComplex )
return;
28 if ( compoundtype !=
TSimple ) {
29 cerr <<
"Error: Conversion from compound types to complex type not supported!" << endl;
36 for (
long j=0; j<ds; j++ ) cdata[j] = (*
this)[j];
52 if ( compoundtype !=
TComplex )
return;
54 long j, k, ds(x*y*z*n);
55 float* fdata =
new float[ds];
57 for ( j=k=0; j<ds; j++, k+=2 ) fdata[j] = (*
this)[k];
73 if ( compoundtype !=
TComplex )
return;
75 long j, k, ds(x*y*z*n);
76 float* fdata =
new float[ds];
78 for ( j=0, k=1; j<ds; j++, k+=2 ) fdata[j] = (*
this)[k];
94 if ( compoundtype !=
TComplex )
return;
97 float* fdata =
new float[ds];
99 for ( j=0; j<ds; j++ ) fdata[j] = (
complex(j)).power();
116 if ( compoundtype !=
TComplex )
return;
119 float* fdata =
new float[ds];
121 for ( j=0; j<ds; j++ ) fdata[j] = (
complex(j)).amp();
138 if ( compoundtype !=
TComplex )
return;
141 float* fdata =
new float[ds];
143 for ( j=0; j<ds; j++ ) {
145 if ( (
complex(j)).phi() < 0 ) fdata[j] = -fdata[j];
163 if ( compoundtype !=
TComplex )
return;
166 float* fdata =
new float[ds];
168 for ( j=0; j<ds; j++ ) fdata[j] = (
complex(j)).phi();
185 if ( compoundtype !=
TComplex )
return;
189 for ( j=1; j<datasize; j+=2 )
set(j, -(*
this)[j]);
202 if ( compoundtype !=
TComplex )
return 0;
204 long nn, i, j, ds(x*y*z);
207 for ( nn=i=0; nn<n; nn++ ) {
209 for ( j=0; j<ds; i++, j++ ) pwrn += (
complex(i)).power();
227 if ( compoundtype !=
TComplex )
return 0;
231 long nn, i, j, ds(x*y*z);
234 for ( nn=0; nn<n; nn++ ) {
238 for ( j=0, i=nn*ds; j<ds; i++, j++ )
set(i,
complex(i) * f);
258 if ( compoundtype !=
TComplex )
return -1;
260 if (
shift.length() == 0 )
return 0;
264 for ( nn=0; nn<n; nn++ )
282 if ( compoundtype !=
TComplex )
return -1;
284 if (
shift.length() == 0 )
return 0;
287 cout <<
"Translate within unit cell by: " <<
shift << endl;
289 long j(nn*x*y*z), xx, yy, zz;
296 for ( zz=0; zz<z; zz++ ) {
298 if ( l > half[2] ) l -= (long)z;
299 for ( yy=0; yy<y; yy++ ) {
301 if ( k > half[1] ) k -= (long)y;
302 for ( xx=0; xx<x; xx++, j++ ) {
304 if ( h > half[0] ) h -= (long)x;
305 phi = h*t[0] + k*t[1] + l*t[2];
329 if ( compoundtype !=
TComplex )
return -1;
332 cout <<
"Translate within unit cell to phase origin:" << endl;
334 long j, nn, xx, yy, zz;
341 cout <<
"Image\tox\toy\toz" << endl;
342 for ( j=nn=0; nn<n; nn++ ) {
346 for ( zz=0; zz<z; zz++ ) {
348 if ( l > half[2] ) l -= (long)z;
350 for ( yy=0; yy<y; yy++ ) {
352 if ( k > half[1] ) k -= (long)y;
353 skl = sl + k*
shift[1];
354 for ( xx=0; xx<x; xx++, j++ ) {
356 if ( h > half[0] ) h -= (long)x;
357 phi = h*
shift[0] + skl;
402 if ( !d.
uc )
return -1;
410 cout <<
"Multiplying the complex image" << endl << endl;
414 for ( i=0; i<ds; i++ )
set(i,
complex(i) * (*p)[i]);
432 if ( !d.
uc )
return -1;
440 cout <<
"Calculating the complex product" << endl << endl;
462 if ( !d.
uc )
return -1;
470 cout <<
"Calculating the complex conjugate product" << endl << endl;
492 if ( !d.
uc )
return NULL;
501 cout <<
"Calculating the complex conjugate product" << endl << endl;
503 long i, j, nn, ds(x*y*z);
505 for ( nn=j=0; nn<p->n; nn++ )
523 if ( !d.
uc )
return -1;
524 if ( !pmask )
return -1;
529 cout <<
"Masking a complex image" << endl;
534 for ( i=0; i<ds; i++ )
535 if ( (*pmask)[i] <= 0 )
set(i, cv);
552 if ( !d.
uc )
return -1;
553 if ( !pmask )
return -1;
556 cout <<
"Masking a complex image" << endl;
561 for ( i=0; i<ds; i++ )
562 if ( (*pmask)[i] >= 0 )
set(i, cv);
583 if ( !d.
uc )
return -1;
584 if ( !pmask )
return -1;
589 cout <<
"Masking a complex image with a dual mask" << endl;
597 for ( i=0; i<ds; i++ ) {
598 if ( (*pmask)[i] <= 0 )
set(i, cv);
599 if ( (*pmask)[i] >= 0 ) p->
set(i, cv);
617 if ( !d.
uc )
return -1;
619 if ( compoundtype !=
TComplex )
return -1;
623 long i, nn, xx, yy, zz;
625 double sx2, sy2, sz2, s2;
628 if ( lores <= 0 ) lores = 1e30;
629 if ( lores < hires ) swap(lores, hires);
630 double s2hi = 1/(hires*hires);
631 double s2lo = 1/(lores*lores);
637 cout <<
"Bandpass limit to: " << 1/sqrt(s2hi) <<
" - " << 1/sqrt(s2lo) <<
" Å" << endl;
640 cout <<
"DEBUG Bimage::complex_bandpass: hires=" << hires <<
" lores" << lores << endl;
642 for ( nn=i=0; nn<n; nn++ ) {
643 for ( zz=0; zz<z; zz++ ) {
645 if ( zz > h[2] ) iz -= (long)z;
648 for ( yy=0; yy<y; yy++ ) {
650 if ( yy > h[1] ) iy -= (long)y;
653 for ( xx=0; xx<x; xx++, i++ ) {
655 if ( xx > h[0] ) ix -= (long)x;
658 s2 = sx2 + sy2 + sz2;
659 if ( s2 > s2hi || s2 < s2lo )
set(i, cv);
682 error_show(
"Error in Bimage::pack_two_in_complex", __FILE__, __LINE__);
683 cerr <<
"No data for " <<
file_name() << endl;
688 error_show(
"Error in Bimage::pack_two_in_complex", __FILE__, __LINE__);
689 cerr <<
"No data for " << p->
file_name() << endl;
694 error_show(
"Error in Bimage::pack_two_in_complex", __FILE__, __LINE__);
695 cerr <<
"Data for " <<
file_name() <<
" may not be compound!" << endl;
700 error_show(
"Error in Bimage::pack_two_in_complex", __FILE__, __LINE__);
701 cerr <<
"Data for " << p->
file_name() <<
" may not be compound!" << endl;
712 error_show(
"Bimage::pack_two_in_complex", __FILE__, __LINE__);
717 for ( i=0; i<n; i++ ) pc->
image[i] =
image[i];
720 cout <<
"Packing two real images into a complex image" << endl;
722 for ( i=j=0; i<datasize; i++ ) {
723 pc->
set(j++, (*
this)[i]);
724 pc->
set(j++, (*p)[i]);
730 cout <<
"DEBUG Bimage::pack_two_in_complex: std=" << pc->
standard_deviation() << endl;
751 if ( compoundtype !=
TComplex )
return NULL;
756 long i, j, xx, yy, zz, nn, xh(x/2 + 1);
764 cout <<
"Unpacking a combined Fourier transform" << endl << endl;
766 for ( nn=0; nn<n; nn++ ) {
767 for ( zz=0; zz<z; zz++ ) {
768 iz = (zz>0)? z - zz: 0;
769 for ( yy=0; yy<y; yy++ ) {
770 iy = (yy>0)? y - yy: 0;
771 for ( xx=0; xx<xh; xx++ ) {
772 ix = (xx>0)? x - xx: 0;
775 if ( yy > h[1] )
use = 0;
776 if ( yy == iy && zz > h[2] )
use = 0;
779 i =
index(xx, yy, zz, nn);
780 j =
index(ix, iy, iz, nn);
815 if ( !d.
uc )
return -1;
822 long i, j, nn, xx, yy, zz, xh(x/2 + 1);
826 double scale(1), sum1, sum2;
830 cout <<
"Calculating the complex conjugate product" << endl;
833 cout <<
"DEBUG Bimage::combined_complex_product: F0=" <<
complex(0).real() <<
" " <<
complex(0).imag() << endl;
837 for ( nn=0; nn<n; nn++ ) {
839 for ( zz=0; zz<z; zz++ ) {
840 iz = (zz>0)? z - zz: 0;
841 for ( yy=0; yy<y; yy++ ) {
842 iy = (yy>0)? y - yy: 0;
843 for ( xx=0; xx<xh; xx++ ) {
844 ix = (xx>0)? x - xx: 0;
847 if ( yy > h[1] )
use = 0;
848 if ( yy == iy && zz > h[2] )
use = 0;
851 i =
index(xx, yy, zz, nn);
852 j =
index(ix, iy, iz, nn);
857 sum1 += temp1.
power();
858 sum2 += temp2.
power();
864 scale = 0.5/sqrt(sum1*sum2);
870 error_show(
"Error in Bimage::combined_complex_product", __FILE__, __LINE__);
871 cerr <<
"Scale not finite = " << scale << endl;
877 cout <<
"CC time: " << tvf - tvs << endl;
953 if ( !d.
uc )
return -1;
960 long i, j, nn, xx, yy, zz, xh(x/2 + 1);
962 double sx2(0), sy2(0), sz2(0), s2;
964 if ( lores > 0 && lores < hires ) swap(lores, hires);
965 double s2hi = (hires > 0)? 1/(hires*hires): 0;
966 double s2lo = (lores > 0)? 1/(lores*lores): 0;
972 cout <<
"Calculating the complex conjugate product:" << endl;
973 cout <<
"Resolution range: ";
974 if ( hires > 0 ) cout << 1/sqrt(s2hi) <<
" - ";
976 if ( lores > 0 ) cout << 1/sqrt(s2lo) <<
" A" << endl;
977 else cout <<
"inf A" << endl;
978 if ( pmask ) cout <<
"Mask: " << pmask->
file_name() << endl;
981 double scale(1), sum1, sum2;
985 cout <<
"DEBUG Bimage::combined_complex_product: F0 = " <<
complex(0).real() <<
" " <<
complex(0).imag() << endl;
990 for ( nn=0; nn<n; nn++ ) {
992 for ( zz=0; zz<z; zz++ ) {
993 iz = (zz>0)? z - zz: 0;
996 if ( zz > h[2] ) sz2 -= (double)z;
1000 for ( yy=0; yy<y; yy++ ) {
1001 iy = (yy>0)? y - yy: 0;
1004 if ( yy > h[1] ) sy2 -= (double)y;
1008 for ( xx=0; xx<xh; xx++ ) {
1009 ix = (xx>0)? x - xx: 0;
1012 if ( yy > h[1] )
use = 0;
1013 if ( yy == iy && zz > h[2] )
use = 0;
1015 i =
index(xx, yy, zz, nn);
1016 j =
index(ix, iy, iz, nn);
1020 if ( xx > h[0] ) sx2 -= (double)x;
1023 s2 = sx2 + sy2 + sz2;
1024 if ( s2 < s2lo || s2 > s2hi )
use = 0;
1026 if ( pmask && (*pmask)[i] <= 0 )
use = 0;
1032 sum1 += temp1.
power();
1033 sum2 += temp2.
power();
1044 scale = 0.5/sqrt(scale);
1052 cerr <<
"Error in Bimage::combined_complex_product: scaling failed!" << endl;
1059 cout <<
"CC(BP) time: " << tvf - tvs << endl;
1063 error_show(
"Error in Bimage::combined_complex_product", __FILE__, __LINE__);
1064 cerr <<
"Number of scaling errors: " << err <<
" (" << n <<
")" << endl;
1092 if ( !d.
uc )
return -1;
1099 long i, j, nn, xx, yy, zz, xh(x/2 + 1);
1101 double sx2(0), sy2(0), sz2(0), s2, tp1, tp2;
1103 if ( lores > 0 && lores < hires ) swap(lores, hires);
1104 double s2hi = (hires > 0)? 1/(hires*hires): 0;
1105 double s2lo = (lores > 0)? 1/(lores*lores): 0;
1111 cout <<
"Calculating the complex conjugate product:" << endl;
1112 cout <<
"Resolution range: ";
1113 if ( hires > 0 ) cout << 1/sqrt(s2hi) <<
" - ";
1114 else cout <<
"0 - ";
1115 if ( lores > 0 ) cout << 1/sqrt(s2lo) <<
" A" << endl;
1116 else cout <<
"inf A" << endl;
1117 cout <<
"Implicit mask" << endl;
1120 double scale(1), sum1, sum2;
1124 cout <<
"DEBUG Bimage::combined_complex_product: F0 = " <<
complex(0).real() <<
" " <<
complex(0).imag() << endl;
1129 for ( nn=0; nn<n; nn++ ) {
1131 for ( zz=0; zz<z; zz++ ) {
1132 iz = (zz>0)? z - zz: 0;
1135 if ( zz > h[2] ) sz2 -= (double)z;
1139 for ( yy=0; yy<y; yy++ ) {
1140 iy = (yy>0)? y - yy: 0;
1143 if ( yy > h[1] ) sy2 -= (double)y;
1147 for ( xx=0; xx<xh; xx++ ) {
1148 ix = (xx>0)? x - xx: 0;
1151 if ( yy > h[1] )
use = 0;
1152 if ( yy == iy && zz > h[2] )
use = 0;
1154 i =
index(xx, yy, zz, nn);
1155 j =
index(ix, iy, iz, nn);
1159 if ( xx > h[0] ) sx2 -= (double)x;
1162 s2 = sx2 + sy2 + sz2;
1163 if ( s2 < s2lo || s2 > s2hi )
use = 0;
1167 tp1 = temp1.
power();
1172 tp2 = temp2.
power();
1179 sum1 += temp1.
power();
1180 sum2 += temp2.
power();
1191 scale = 0.5/sqrt(scale);
1194 cerr <<
"Error in Bimage::combined_complex_product: scaling failed!" << endl;
1201 cout <<
"CC(BP) time: " << tvf - tvs << endl;
1205 error_show(
"Error in Bimage::combined_complex_product", __FILE__, __LINE__);
1206 cerr <<
"Number of scaling errors: " << err <<
" (" << n <<
")" << endl;
1225 if ( !d.
uc )
return -1;
1226 if ( !pamp )
return -1;
1235 cout <<
"Merging amplitudes and phases" << endl << endl;
1237 long i, ds(x*y*z*n);
1238 double a, ar, d, R(0);
1241 for ( i=0; i<ds; i++ ) {
1245 else ar = (pamp->
complex(i)).amp();
1273 if ( !d.
uc )
return -1;
1274 if ( !pref )
return -1;
1277 cerr <<
"Error: The reference map must be a Fourier transform!" << endl << endl;
1286 if ( res_lo < res_hi ) res_lo = res_hi;
1289 cout <<
"Merging amplitudes and phases in the range " << res_hi <<
" - " << res_lo <<
" Å" << endl << endl;
1293 if ( maxrad < minrad + 1 ) maxrad = minrad + 1;
1294 double minrad2 = minrad*minrad, maxrad2 = maxrad*maxrad;
1296 long i, nn, xx, yy, zz, na(0);
1297 double x2, y2, z2, r2;
1300 double a, ar, d, R(0);
1303 for ( i=nn=0; nn<n; nn++ ) {
1304 for ( zz=0; zz<z; zz++ ) {
1306 if ( zz > h[2] ) z2 -= (long)z;
1308 for ( yy=0; yy<y; yy++ ) {
1310 if ( yy > h[1] ) y2 -= (long)y;
1312 for ( xx=0; xx<x; xx++, i++ ) {
1314 if ( xx > h[0] ) x2 -= (long)x;
1318 if ( r2 > maxrad2 ) {
1320 }
else if ( r2 < minrad2 ) {
1324 ar = (pref->
complex(i)).amp();
1336 if ( na ) R = sqrt(R/na);
1339 cout <<
"DEBUG Bimage::merge_amplitudes_and_phases: minrad=" << minrad
1340 <<
" maxrad=" << maxrad <<
" n=" << n <<
" R" << R << endl;
1372 cout <<
"Generating a phase-colored power spectrum" << endl;
1374 cout <<
"Generating a phase-colored power spectrum" << endl;
1376 long i, ds(x*y*z*n);
1379 if ( scale <= 0 ) scale = 1/(avg + std);
1380 else scale /= avg + std;
1391 for ( i=0; i<ds; i++ ) {
1392 amp_ratio = (
complex(i)).amp()*scale;
1393 if ( amp_ratio > 0 ) {
1394 if ( amp_ratio > 255 ) amp_ratio = 255;