Bsoft  2.1.0
Bernard's software package
Bimage_complex.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "timer.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
23 {
24  if ( compoundtype == TComplex ) return;
25 
26  if ( compoundtype == TRGB || compoundtype == TRGBA || compoundtype == TCMYK ) color_to_simple();
27 
28  if ( compoundtype != TSimple ) {
29  cerr << "Error: Conversion from compound types to complex type not supported!" << endl;
30  exit(-1);
31  }
32 
33  long ds(x*y*z*n);
34  Complex<float>* cdata = new Complex<float>[ds];
35 
36  for ( long j=0; j<ds; j++ ) cdata[j] = (*this)[j];
37 
39  compoundtype = TComplex;
40  c = 2;
41 
42  data_assign((unsigned char *) cdata);
43 
44  statistics();
45 }
46 
51 {
52  if ( compoundtype != TComplex ) return;
53 
54  long j, k, ds(x*y*z*n);
55  float* fdata = new float[ds];
56 
57  for ( j=k=0; j<ds; j++, k+=2 ) fdata[j] = (*this)[k];
58 
60  compoundtype = TSimple;
61  c = 1;
62 
63  data_assign((unsigned char *) fdata);
64 
65  statistics();
66 }
67 
72 {
73  if ( compoundtype != TComplex ) return;
74 
75  long j, k, ds(x*y*z*n);
76  float* fdata = new float[ds];
77 
78  for ( j=0, k=1; j<ds; j++, k+=2 ) fdata[j] = (*this)[k];
79 
81  compoundtype = TSimple;
82  c = 1;
83 
84  data_assign((unsigned char *) fdata);
85 
86  statistics();
87 }
88 
93 {
94  if ( compoundtype != TComplex ) return;
95 
96  long j, ds(x*y*z*n);
97  float* fdata = new float[ds];
98 
99  for ( j=0; j<ds; j++ ) fdata[j] = (complex(j)).power();
100 
101  data_type(Float);
102  compoundtype = TSimple;
103  c = 1;
104 
105  data_assign((unsigned char *) fdata);
106 
107  statistics();
108 }
109 
115 {
116  if ( compoundtype != TComplex ) return;
117 
118  long j, ds(x*y*z*n);
119  float* fdata = new float[ds];
120 
121  for ( j=0; j<ds; j++ ) fdata[j] = (complex(j)).amp();
122 
123  data_type(Float);
124  compoundtype = TSimple;
125  c = 1;
126 
127  data_assign((unsigned char *) fdata);
128 
129  statistics();
130 }
131 
137 {
138  if ( compoundtype != TComplex ) return;
139 
140  long j, ds(x*y*z*n);
141  float* fdata = new float[ds];
142 
143  for ( j=0; j<ds; j++ ) {
144  fdata[j] = (complex(j)).amp();
145  if ( (complex(j)).phi() < 0 ) fdata[j] = -fdata[j];
146  }
147 
148  data_type(Float);
149  compoundtype = TSimple;
150  c = 1;
151 
152  data_assign((unsigned char *) fdata);
153 
154  statistics();
155 }
156 
162 {
163  if ( compoundtype != TComplex ) return;
164 
165  long j, ds(x*y*z*n);
166  float* fdata = new float[ds];
167 
168  for ( j=0; j<ds; j++ ) fdata[j] = (complex(j)).phi();
169 
170  data_type(Float);
171  compoundtype = TSimple;
172  c = 1;
173 
174  data_assign((unsigned char *) fdata);
175 
176  statistics();
177 }
178 
184 {
185  if ( compoundtype != TComplex ) return;
186 
187  long j;
188 
189  for ( j=1; j<datasize; j+=2 ) set(j, -(*this)[j]);
190 }
191 
201 {
202  if ( compoundtype != TComplex ) return 0;
203 
204  long nn, i, j, ds(x*y*z);
205  double pwr(0), pwrn;
206 
207  for ( nn=i=0; nn<n; nn++ ) {
208  pwrn = 0;
209  for ( j=0; j<ds; i++, j++ ) pwrn += (complex(i)).power();
210  pwr += pwrn;
211  image[nn].FOM(pwrn);
212  }
213 
214  return pwr;
215 }
216 
226 {
227  if ( compoundtype != TComplex ) return 0;
228 
229  set(0, 0); set(1, 0);
230 
231  long nn, i, j, ds(x*y*z);
232  double pwr = complex_power(), pwrn, f;
233 
234  for ( nn=0; nn<n; nn++ ) {
235  pwrn = image[nn].FOM();
236  if ( pwrn ) {
237  f = 1/sqrt(pwrn);
238  for ( j=0, i=nn*ds; j<ds; i++, j++ ) set(i, complex(i) * f);
239  }
240  }
241 
242  statistics();
243 
244  return pwr;
245 }
246 
257 {
258  if ( compoundtype != TComplex ) return -1;
259 
260  if ( shift.length() == 0 ) return 0;
261 
262  long nn;
263 
264  for ( nn=0; nn<n; nn++ )
265  phase_shift(nn, shift);
266 
267  return 0;
268 }
269 
281 {
282  if ( compoundtype != TComplex ) return -1;
283 
284  if ( shift.length() == 0 ) return 0;
285 
286  if ( verbose & VERB_FULL )
287  cout << "Translate within unit cell by: " << shift << endl;
288 
289  long j(nn*x*y*z), xx, yy, zz;
290  long h, k, l;
291  double phi(0);
292  Vector3<double> half((x - 1)/2, (y - 1)/2, (z - 1)/2);
294  Complex<double> cv;
295 
296  for ( zz=0; zz<z; zz++ ) {
297  l = zz;
298  if ( l > half[2] ) l -= (long)z;
299  for ( yy=0; yy<y; yy++ ) {
300  k = yy;
301  if ( k > half[1] ) k -= (long)y;
302  for ( xx=0; xx<x; xx++, j++ ) {
303  h = xx;
304  if ( h > half[0] ) h -= (long)x;
305  phi = h*t[0] + k*t[1] + l*t[2];
306  cv = complex(j);
307  cv.shift_phi(phi);
308  set(j, cv);
309  }
310  }
311  }
312 
313  image[nn].origin(image[nn].origin() + shift);
314 
315  return 0;
316 }
317 
328 {
329  if ( compoundtype != TComplex ) return -1;
330 
331  if ( verbose & VERB_PROCESS )
332  cout << "Translate within unit cell to phase origin:" << endl;
333 
334  long j, nn, xx, yy, zz;
335  long h, k, l;
336  double skl, sl, phi;
337  Vector3<double> shift, half((x - 1)/2, (y - 1)/2, (z - 1)/2);
338  Complex<double> cv;
339 
340  if ( verbose & VERB_PROCESS )
341  cout << "Image\tox\toy\toz" << endl;
342  for ( j=nn=0; nn<n; nn++ ) {
343  if ( verbose & VERB_PROCESS )
344  cout << nn+1 << tab << image[nn].origin()[0] << tab << image[nn].origin()[1] << tab << image[nn].origin()[2] << endl;
345  shift = (image[nn].origin()/size()) * TWOPI;
346  for ( zz=0; zz<z; zz++ ) {
347  l = zz;
348  if ( l > half[2] ) l -= (long)z;
349  sl = l*shift[2];
350  for ( yy=0; yy<y; yy++ ) {
351  k = yy;
352  if ( k > half[1] ) k -= (long)y;
353  skl = sl + k*shift[1];
354  for ( xx=0; xx<x; xx++, j++ ) {
355  h = xx;
356  if ( h > half[0] ) h -= (long)x;
357  phi = h*shift[0] + skl;
358  cv = complex(j);
359  cv.shift_phi(phi);
360  set(j, cv);
361  }
362  }
363  }
364 // image[nn].origin(image[nn].origin() + shift*size());
365  image[nn].origin(0.0,0.0,0.0);
366  }
367  if ( verbose & VERB_PROCESS )
368  cout << endl;
369 
370  return 0;
371 }
372 
383 {
384  Vector3<double> shift(x/2, y/2, z/2);
385 
387 
388  return 0;
389 }
390 
401 {
402  if ( !d.uc ) return -1;
403 
404  if ( compoundtype != TComplex || p->compound_type() != TSimple ) return -1;
405 
407  p->change_type(Float);
408 
409  if ( verbose & VERB_FULL )
410  cout << "Multiplying the complex image" << endl << endl;
411 
412  long i, ds(x*y*z*n);
413 
414  for ( i=0; i<ds; i++ ) set(i, complex(i) * (*p)[i]);
415 
416  return 0;
417 }
418 
431 {
432  if ( !d.uc ) return -1;
433 
434  if ( compoundtype != TComplex || p->compound_type() != TComplex ) return -1;
435 
437  p->change_type(Float);
438 
439  if ( verbose & VERB_FULL )
440  cout << "Calculating the complex product" << endl << endl;
441 
442  long i, ds(x*y*z*n);
443 
444  for ( i=0; i<ds; i++ ) set(i, complex(i) * p->complex(i));
445 
446  return 0;
447 }
448 
461 {
462  if ( !d.uc ) return -1;
463 
464  if ( compound_type() != TComplex || p->compound_type() != TComplex ) return -1;
465 
467  p->change_type(Float);
468 
469  if ( verbose & VERB_FULL )
470  cout << "Calculating the complex conjugate product" << endl << endl;
471 
472  long i, ds(x*y*z*n);
473 
474  for ( i=0; i<ds; i++ ) set(i, complex(i) * (p->complex(i)).conj());
475 
476  return 0;
477 }
478 
491 {
492  if ( !d.uc ) return NULL;
493 
494  if ( compound_type() != TComplex || p->compound_type() != TComplex ) return NULL;
495 
496  if ( !check_if_same_image_size(p) ) return NULL;
497 
498  Bimage* pc = p->copy();
499 
500  if ( verbose & VERB_FULL )
501  cout << "Calculating the complex conjugate product" << endl << endl;
502 
503  long i, j, nn, ds(x*y*z);
504 
505  for ( nn=j=0; nn<p->n; nn++ )
506  for ( i=0; i<ds; i++, j++ ) pc->set(j, complex(i) * (p->complex(j)).conj());
507 
508  return pc;
509 }
510 
522 {
523  if ( !d.uc ) return -1;
524  if ( !pmask ) return -1;
525 
526  pmask->change_type(Float);
527 
528  if ( verbose & VERB_FULL )
529  cout << "Masking a complex image" << endl;
530 
531  long i, ds(x*y*z*n);
532  Complex<double> cv;
533 
534  for ( i=0; i<ds; i++ )
535  if ( (*pmask)[i] <= 0 ) set(i, cv);
536 
537  return 0;
538 }
539 
551 {
552  if ( !d.uc ) return -1;
553  if ( !pmask ) return -1;
554 
555  if ( verbose & VERB_FULL )
556  cout << "Masking a complex image" << endl;
557 
558  long i, ds(x*y*z*n);
559  Complex<double> cv;
560 
561  for ( i=0; i<ds; i++ )
562  if ( (*pmask)[i] >= 0 ) set(i, cv);
563 
564  return 0;
565 }
566 
582 {
583  if ( !d.uc ) return -1;
584  if ( !pmask ) return -1;
585 
586  pmask->change_type(Float);
587 
588  if ( verbose & VERB_FULL )
589  cout << "Masking a complex image with a dual mask" << endl;
590 
591  Bimage* p = copy();
592  next = p;
593 
594  long i, ds(x*y*z*n);
595  Complex<double> cv;
596 
597  for ( i=0; i<ds; i++ ) {
598  if ( (*pmask)[i] <= 0 ) set(i, cv);
599  if ( (*pmask)[i] >= 0 ) p->set(i, cv);
600  }
601 
602  return 0;
603 }
604 
615 int Bimage::complex_bandpass(double hires, double lores)
616 {
617  if ( !d.uc ) return -1;
618 
619  if ( compoundtype != TComplex ) return -1;
620 
622 
623  long i, nn, xx, yy, zz;
624  long ix, iy, iz;
625  double sx2, sy2, sz2, s2;
626 
627  check_resolution(hires);
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);
632  Vector3<double> iscale(1.0/real_size());
633  Vector3<long> h((x - 1)/2, (y - 1)/2, (z - 1)/2);
634  Complex<double> cv;
635 
636  if ( verbose & VERB_FULL )
637  cout << "Bandpass limit to: " << 1/sqrt(s2hi) << " - " << 1/sqrt(s2lo) << " Å" << endl;
638 
639  if ( verbose & VERB_DEBUG )
640  cout << "DEBUG Bimage::complex_bandpass: hires=" << hires << " lores" << lores << endl;
641 
642  for ( nn=i=0; nn<n; nn++ ) {
643  for ( zz=0; zz<z; zz++ ) {
644  iz = zz;
645  if ( zz > h[2] ) iz -= (long)z;
646  sz2 = iz*iscale[2];
647  sz2 *= sz2;
648  for ( yy=0; yy<y; yy++ ) {
649  iy = yy;
650  if ( yy > h[1] ) iy -= (long)y;
651  sy2 = iy*iscale[1];
652  sy2 *= sy2;
653  for ( xx=0; xx<x; xx++, i++ ) {
654  ix = xx;
655  if ( xx > h[0] ) ix -= (long)x;
656  sx2 = ix*iscale[0];
657  sx2 *= sx2;
658  s2 = sx2 + sy2 + sz2;
659  if ( s2 > s2hi || s2 < s2lo ) set(i, cv);
660  }
661  }
662  }
663  }
664 
665  return 0;
666 }
667 
680 {
681  if ( !d.uc ) {
682  error_show("Error in Bimage::pack_two_in_complex", __FILE__, __LINE__);
683  cerr << "No data for " << file_name() << endl;
684  return NULL;
685  }
686 
687  if ( !p->data_pointer() ) {
688  error_show("Error in Bimage::pack_two_in_complex", __FILE__, __LINE__);
689  cerr << "No data for " << p->file_name() << endl;
690  return NULL;
691  }
692 
693  if ( compound_type() > TSimple ) {
694  error_show("Error in Bimage::pack_two_in_complex", __FILE__, __LINE__);
695  cerr << "Data for " << file_name() << " may not be compound!" << endl;
696  return NULL;
697  }
698 
699  if ( p->compound_type() > TSimple ) {
700  error_show("Error in Bimage::pack_two_in_complex", __FILE__, __LINE__);
701  cerr << "Data for " << p->file_name() << " may not be compound!" << endl;
702  return NULL;
703  }
704 
706 
707  long i, j;
708 
709  Bimage* pc = new Bimage(Float, TComplex, size(), n);
710 
711  if ( !pc ) {
712  error_show("Bimage::pack_two_in_complex", __FILE__, __LINE__);
713  return NULL;
714  }
715 
717  for ( i=0; i<n; i++ ) pc->image[i] = image[i];
718 
719  if ( verbose & VERB_FULL )
720  cout << "Packing two real images into a complex image" << endl;
721 
722  for ( i=j=0; i<datasize; i++ ) {
723  pc->set(j++, (*this)[i]);
724  pc->set(j++, (*p)[i]);
725  }
726 
727  pc->check();
728 
729  if ( verbose & VERB_DEBUG )
730  cout << "DEBUG Bimage::pack_two_in_complex: std=" << pc->standard_deviation() << endl;
731 
732  return pc;
733 }
734 
750 {
751  if ( compoundtype != TComplex ) return NULL;
752 
754 
755  int use;
756  long i, j, xx, yy, zz, nn, xh(x/2 + 1);
757  long ix, iy, iz;
758  Vector3<long> h(size()/2);
759  Complex<float> temp1, temp2, cv;
760 
761  Bimage* p = copy();
762 
763  if ( verbose & VERB_FULL )
764  cout << "Unpacking a combined Fourier transform" << endl << endl;
765 
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;
773  use = 1;
774  if ( xx == ix ) {
775  if ( yy > h[1] ) use = 0;
776  if ( yy == iy && zz > h[2] ) use = 0;
777  }
778  if ( use ) {
779  i = index(xx, yy, zz, nn);
780  j = index(ix, iy, iz, nn);
781  temp1 = complex(i).unpack_first(complex(j));
782  temp2 = complex(i).unpack_second(complex(j));
783  set(i, temp1);
784  set(j, temp1.conj());
785  p->set(i, temp2);
786  p->set(j, temp2.conj());
787  }
788  }
789  }
790  }
791  }
792 
793  return p;
794 }
795 
814 {
815  if ( !d.uc ) return -1;
816 
817  if ( compound_type() != TComplex ) return -1;
818 
820 
821  int use;
822  long i, j, nn, xx, yy, zz, xh(x/2 + 1);
823  long ix, iy, iz;
824  Vector3<long> h(size()/2);
825 
826  double scale(1), sum1, sum2;
827  Complex<double> temp1, temp2;
828 
829  if ( verbose & VERB_FULL )
830  cout << "Calculating the complex conjugate product" << endl;
831 
832  if ( verbose & VERB_DEBUG )
833  cout << "DEBUG Bimage::combined_complex_product: F0=" << complex(0).real() << " " << complex(0).imag() << endl;
834 
835  double tvs = getwalltime(), tvf;
836 
837  for ( nn=0; nn<n; nn++ ) {
838  sum1 = sum2 = 0;
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;
845  use = 1;
846  if ( xx == ix ) {
847  if ( yy > h[1] ) use = 0;
848  if ( yy == iy && zz > h[2] ) use = 0;
849  }
850  if ( use ) {
851  i = index(xx, yy, zz, nn);
852  j = index(ix, iy, iz, nn);
853  temp1 = complex(i).unpack_first(complex(j));
854  temp2 = complex(i).unpack_second(complex(j));
855  set(i, temp1 * temp2.conj());
856  set(j, complex(i).conj());
857  sum1 += temp1.power();
858  sum2 += temp2.power();
859  }
860  }
861  }
862  }
863  if ( sum1*sum2 ) {
864  scale = 0.5/sqrt(sum1*sum2);
865  multiply(nn, scale);
866  }
867  }
868 
869  if ( !isfinite(scale) ) {
870  error_show("Error in Bimage::combined_complex_product", __FILE__, __LINE__);
871  cerr << "Scale not finite = " << scale << endl;
872  return -9;
873  }
874 
875  if ( verbose & VERB_TIME ) {
876  tvf = getwalltime();
877  cout << "CC time: " << tvf - tvs << endl;
878  }
879 
880  return 0;
881 }
882 
902 {
903  return combined_complex_product(0, 0, pmask);
904 }
905 
906 
926 int Bimage::combined_complex_product(double hires, double lores)
927 {
928  return combined_complex_product(hires, lores, NULL);
929 }
930 
951 int Bimage::combined_complex_product(double hires, double lores, Bimage* pmask)
952 {
953  if ( !d.uc ) return -1;
954 
955  if ( compound_type() != TComplex ) return -1;
956 
958 
959  int use;
960  long i, j, nn, xx, yy, zz, xh(x/2 + 1);
961  long ix, iy, iz;
962  double sx2(0), sy2(0), sz2(0), s2;
963 
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;
967 
968  Vector3<double> iscale(1.0/real_size());
969  Vector3<long> h(size()/2);
970 
971  if ( verbose & VERB_FULL ) {
972  cout << "Calculating the complex conjugate product:" << endl;
973  cout << "Resolution range: ";
974  if ( hires > 0 ) cout << 1/sqrt(s2hi) << " - ";
975  else cout << "0 - ";
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;
979  }
980 
981  double scale(1), sum1, sum2;
982  Complex<double> temp1, temp2, cv(0,0);
983 
984  if ( verbose & VERB_DEBUG )
985  cout << "DEBUG Bimage::combined_complex_product: F0 = " << complex(0).real() << " " << complex(0).imag() << endl;
986 
987  int err(0);
988  double tvs = getwalltime(), tvf;
989 
990  for ( nn=0; nn<n; nn++ ) {
991  sum1 = sum2 = 0;
992  for ( zz=0; zz<z; zz++ ) {
993  iz = (zz>0)? z - zz: 0;
994  if ( s2hi > 0 ) {
995  sz2 = zz;
996  if ( zz > h[2] ) sz2 -= (double)z;
997  sz2 *= iscale[2];
998  sz2 *= sz2;
999  }
1000  for ( yy=0; yy<y; yy++ ) {
1001  iy = (yy>0)? y - yy: 0;
1002  if ( s2hi > 0 ) {
1003  sy2 = yy;
1004  if ( yy > h[1] ) sy2 -= (double)y;
1005  sy2 *= iscale[1];
1006  sy2 *= sy2;
1007  }
1008  for ( xx=0; xx<xh; xx++ ) {
1009  ix = (xx>0)? x - xx: 0;
1010  use = 1;
1011  if ( xx == ix ) {
1012  if ( yy > h[1] ) use = 0;
1013  if ( yy == iy && zz > h[2] ) use = 0;
1014  }
1015  i = index(xx, yy, zz, nn);
1016  j = index(ix, iy, iz, nn);
1017  if ( use ) {
1018  if ( s2hi > 0 ) {
1019  sx2 = xx;
1020  if ( xx > h[0] ) sx2 -= (double)x;
1021  sx2 *= iscale[0];
1022  sx2 *= sx2;
1023  s2 = sx2 + sy2 + sz2;
1024  if ( s2 < s2lo || s2 > s2hi ) use = 0;
1025  }
1026  if ( pmask && (*pmask)[i] <= 0 ) use = 0;
1027  if ( use ) {
1028  temp1 = complex(i).unpack_first(complex(j));
1029  temp2 = complex(i).unpack_second(complex(j));
1030  set(i, temp1 * temp2.conj());
1031  set(j, complex(i).conj());
1032  sum1 += temp1.power();
1033  sum2 += temp2.power();
1034  } else {
1035  set(i, cv);
1036  set(j, cv);
1037  }
1038  }
1039  }
1040  }
1041  }
1042  scale = sum1*sum2;
1043  if ( scale > 0 ) {
1044  scale = 0.5/sqrt(scale);
1045  multiply(nn, scale);
1046 /* statistics();
1047  if ( image->maximum() > 1 ) {
1048  cerr << "Error in Bimage::combined_complex_product: maximum too large" << image->maximum() << endl;
1049  bexit(-1);
1050  }*/
1051  } else {
1052  cerr << "Error in Bimage::combined_complex_product: scaling failed!" << endl;
1053  err--;
1054  }
1055  }
1056 
1057  if ( verbose & VERB_TIME ) {
1058  tvf = getwalltime();
1059  cout << "CC(BP) time: " << tvf - tvs << endl;
1060  }
1061 
1062  if ( err ) {
1063  error_show("Error in Bimage::combined_complex_product", __FILE__, __LINE__);
1064  cerr << "Number of scaling errors: " << err << " (" << n << ")" << endl;
1065  }
1066 
1067  return err;
1068 }
1069 
1090 int Bimage::combined_complex_product_implicit_mask(double hires, double lores)
1091 {
1092  if ( !d.uc ) return -1;
1093 
1094  if ( compound_type() != TComplex ) return -1;
1095 
1096  change_type(Float);
1097 
1098  int use;
1099  long i, j, nn, xx, yy, zz, xh(x/2 + 1);
1100  long ix, iy, iz;
1101  double sx2(0), sy2(0), sz2(0), s2, tp1, tp2;
1102 
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;
1106 
1107  Vector3<double> iscale(1.0/real_size());
1108  Vector3<long> h(size()/2);
1109 
1110  if ( verbose & VERB_FULL ) {
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;
1118  }
1119 
1120  double scale(1), sum1, sum2;
1121  Complex<double> temp1, temp2, cv;
1122 
1123  if ( verbose & VERB_DEBUG )
1124  cout << "DEBUG Bimage::combined_complex_product: F0 = " << complex(0).real() << " " << complex(0).imag() << endl;
1125 
1126  int err(0);
1127  double tvs = getwalltime(), tvf;
1128 
1129  for ( nn=0; nn<n; nn++ ) {
1130  sum1 = sum2 = 0;
1131  for ( zz=0; zz<z; zz++ ) {
1132  iz = (zz>0)? z - zz: 0;
1133  if ( s2hi > 0 ) {
1134  sz2 = zz;
1135  if ( zz > h[2] ) sz2 -= (double)z;
1136  sz2 *= iscale[2];
1137  sz2 *= sz2;
1138  }
1139  for ( yy=0; yy<y; yy++ ) {
1140  iy = (yy>0)? y - yy: 0;
1141  if ( s2hi > 0 ) {
1142  sy2 = yy;
1143  if ( yy > h[1] ) sy2 -= (double)y;
1144  sy2 *= iscale[1];
1145  sy2 *= sy2;
1146  }
1147  for ( xx=0; xx<xh; xx++ ) {
1148  ix = (xx>0)? x - xx: 0;
1149  use = 1;
1150  if ( xx == ix ) {
1151  if ( yy > h[1] ) use = 0;
1152  if ( yy == iy && zz > h[2] ) use = 0;
1153  }
1154  i = index(xx, yy, zz, nn);
1155  j = index(ix, iy, iz, nn);
1156  if ( use ) {
1157  if ( s2hi > 0 ) {
1158  sx2 = xx;
1159  if ( xx > h[0] ) sx2 -= (double)x;
1160  sx2 *= iscale[0];
1161  sx2 *= sx2;
1162  s2 = sx2 + sy2 + sz2;
1163  if ( s2 < s2lo || s2 > s2hi ) use = 0;
1164  }
1165  if ( use ) {
1166  temp1 = complex(i).unpack_first(complex(j));
1167  tp1 = temp1.power();
1168  if ( tp1 < SMALLFLOAT ) {
1169  use = 0;
1170  } else {
1171  temp2 = complex(i).unpack_second(complex(j));
1172  tp2 = temp2.power();
1173  if ( tp2 < SMALLFLOAT ) use = 0;
1174  }
1175  }
1176  if ( use ) {
1177  set(i, temp1 * temp2.conj());
1178  set(j, complex(i).conj());
1179  sum1 += temp1.power();
1180  sum2 += temp2.power();
1181  } else {
1182  set(i, cv);
1183  set(j, cv);
1184  }
1185  }
1186  }
1187  }
1188  }
1189  scale = sum1*sum2;
1190  if ( scale > 0 ) {
1191  scale = 0.5/sqrt(scale);
1192  multiply(nn, scale);
1193  } else {
1194  cerr << "Error in Bimage::combined_complex_product: scaling failed!" << endl;
1195  err--;
1196  }
1197  }
1198 
1199  if ( verbose & VERB_TIME ) {
1200  tvf = getwalltime();
1201  cout << "CC(BP) time: " << tvf - tvs << endl;
1202  }
1203 
1204  if ( err ) {
1205  error_show("Error in Bimage::combined_complex_product", __FILE__, __LINE__);
1206  cerr << "Number of scaling errors: " << err << " (" << n << ")" << endl;
1207  }
1208 
1209  return err;
1210 }
1211 
1224 {
1225  if ( !d.uc ) return -1;
1226  if ( !pamp ) return -1;
1227 
1228  if ( compound_type() != TComplex ) return -1;
1229 
1230  change_type(Float);
1231 
1232  pamp->change_type(Float);
1233 
1234  if ( verbose & VERB_FULL )
1235  cout << "Merging amplitudes and phases" << endl << endl;
1236 
1237  long i, ds(x*y*z*n);
1238  double a, ar, d, R(0);
1239  Complex<double> cv;
1240 
1241  for ( i=0; i<ds; i++ ) {
1242  cv = complex(i);
1243  a = cv.amp();
1244  if ( pamp->compound_type() == TSimple ) ar = (*pamp)[i];
1245  else ar = (pamp->complex(i)).amp();
1246  d = a - ar;
1247  R += d*d;
1248  cv.amp(ar);
1249  set(i, cv);
1250  }
1251 
1252  R = sqrt(R/ds);
1253 
1254  return R;
1255 }
1256 
1271 double Bimage::merge_amplitudes_and_phases(Bimage* pref, double res_hi, double res_lo)
1272 {
1273  if ( !d.uc ) return -1;
1274  if ( !pref ) return -1;
1275 
1276  if ( pref->compound_type() != TComplex ) {
1277  cerr << "Error: The reference map must be a Fourier transform!" << endl << endl;
1278  return -1;
1279  }
1280 
1281  change_type(Float);
1282 
1283  pref->change_type(Float);
1284 
1285  check_resolution(res_hi);
1286  if ( res_lo < res_hi ) res_lo = res_hi;
1287 
1288  if ( verbose & VERB_FULL )
1289  cout << "Merging amplitudes and phases in the range " << res_hi << " - " << res_lo << " Å" << endl << endl;
1290 
1291  double minrad = real_size()[0]/res_lo;
1292  double maxrad = real_size()[0]/res_hi;
1293  if ( maxrad < minrad + 1 ) maxrad = minrad + 1;
1294  double minrad2 = minrad*minrad, maxrad2 = maxrad*maxrad;
1295 
1296  long i, nn, xx, yy, zz, na(0);
1297  double x2, y2, z2, r2;
1298  Vector3<long> h((x - 1)/2, (y - 1)/2, (z - 1)/2);
1299 
1300  double a, ar, d, R(0);
1301  Complex<double> cv;
1302 
1303  for ( i=nn=0; nn<n; nn++ ) {
1304  for ( zz=0; zz<z; zz++ ) {
1305  z2 = zz;
1306  if ( zz > h[2] ) z2 -= (long)z;
1307  z2 *= z2;
1308  for ( yy=0; yy<y; yy++ ) {
1309  y2 = yy;
1310  if ( yy > h[1] ) y2 -= (long)y;
1311  y2 *= y2;
1312  for ( xx=0; xx<x; xx++, i++ ) {
1313  x2 = xx;
1314  if ( xx > h[0] ) x2 -= (long)x;
1315  x2 *= x2;
1316  r2 = x2 + y2 + z2;
1317  cv = (*this)[i];
1318  if ( r2 > maxrad2 ) { // Low-pass filter
1319  cv = 0;
1320  } else if ( r2 < minrad2 ) { // Keep original low-frequency data
1321  cv = pref->complex(i);
1322  } else { // Reset to the original amplitudes
1323  a = cv.amp();
1324  ar = (pref->complex(i)).amp();
1325  d = a - ar;
1326  R += d*d;
1327  na++;
1328  cv.amp(ar);
1329  }
1330  set(i, cv);
1331  }
1332  }
1333  }
1334  }
1335 
1336  if ( na ) R = sqrt(R/na);
1337 
1338  if ( verbose & VERB_DEBUG )
1339  cout << "DEBUG Bimage::merge_amplitudes_and_phases: minrad=" << minrad
1340  << " maxrad=" << maxrad << " n=" << n << " R" << R << endl;
1341 
1342  return R;
1343 }
1344 
1364 {
1365  if ( compound_type() != TComplex ) return NULL;
1366 
1367  change_type(Float);
1368 
1369  statistics();
1370 
1371  if ( verbose & VERB_PROCESS ) {
1372  cout << "Generating a phase-colored power spectrum" << endl;
1373  } else if ( verbose & VERB_LABEL )
1374  cout << "Generating a phase-colored power spectrum" << endl;
1375 
1376  long i, ds(x*y*z*n);
1377  double amp_ratio;
1378 
1379  if ( scale <= 0 ) scale = 1/(avg + std);
1380  else scale /= avg + std;
1381 
1382  scale *= 255;
1383 
1384  // Shift the phases
1386 
1387  Bimage* ps = new Bimage(UCharacter, TRGB, size(), n);
1388 
1390 
1391  for ( i=0; i<ds; i++ ) {
1392  amp_ratio = (complex(i)).amp()*scale; // Weigh with amplitudes
1393  if ( amp_ratio > 0 ) {
1394  if ( amp_ratio > 255 ) amp_ratio = 255;
1395  rgb.phase((complex(i)).phi(), amp_ratio);
1396  ps->set(i, rgb);
1397  }
1398  }
1399 
1400  ps->center_wrap();
1401 
1402  return ps;
1403 }
1404 
1405 
Bimage::complex_multiply
int complex_multiply(Bimage *p)
Calculates the product of a complex image with a simple image.
Definition: Bimage_complex.cpp:400
Bimage::complex_to_intensities
void complex_to_intensities()
The intensities from a complex image is written to a simple image.
Definition: Bimage_complex.cpp:92
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::shift
int shift(Vector3< double > vec, int fill_type=0, double fill=0)
Shifts an image.
Definition: Bimage_transform.cpp:589
Bimage::merge_amplitudes_and_phases
double merge_amplitudes_and_phases(Bimage *pamp)
Merges the amplitudes from one map with the phases of another.
Definition: Bimage_complex.cpp:1223
UCharacter
@ UCharacter
Definition: utilities.h:153
Complex
Definition: Complex.h:23
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:43
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Bimage::simple_to_complex
void simple_to_complex()
A simple image is converted to a complex image.
Definition: Bimage_complex.cpp:22
Vector3< double >
Bimage::pack_two_in_complex
Bimage * pack_two_in_complex(Bimage *p)
Packs two real images into one complex image.
Definition: Bimage_complex.cpp:679
use
const char * use[]
Definition: b3v.cpp:19
Bimage::complex_to_imaginary
void complex_to_imaginary()
The imaginary part of a complex image is written to a simple image.
Definition: Bimage_complex.cpp:71
TypePointer::uc
unsigned char * uc
Definition: Bimage.h:70
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
TComplex
@ TComplex
Definition: Bimage.h:60
Bimage::complex_apply_negative_mask
int complex_apply_negative_mask(Bimage *pmask)
Applies a negative mask to a complex image.
Definition: Bimage_complex.cpp:550
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
RGB
Definition: Color.h:24
Bsub_image::FOM
double FOM()
Definition: Bimage.h:165
Complex::shift_phi
void shift_phi(const double d)
Definition: Complex.h:112
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
Bimage::complex_apply_dual_mask
int complex_apply_dual_mask(Bimage *pmask)
Applies a dual mask to a complex image.
Definition: Bimage_complex.cpp:581
Bimage::phase_shift
int phase_shift(Vector3< double > shift)
Phase shifts a complex image.
Definition: Bimage_complex.cpp:256
Bimage::complex_conjugate_product_one2many
Bimage * complex_conjugate_product_one2many(Bimage *p)
Calculates the product of a complex image with the conjugate of a second.
Definition: Bimage_complex.cpp:490
Bimage::unpack_combined_transform
Bimage * unpack_combined_transform()
Unpacks a complex transform obtained from two real images.
Definition: Bimage_complex.cpp:749
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::combined_complex_product_implicit_mask
int combined_complex_product_implicit_mask(double hires, double lores)
Calculates the complex conjugate product of a complex image resulting from combining and Fourier tran...
Definition: Bimage_complex.cpp:1090
Bimage::file_name
string & file_name()
Definition: Bimage.h:246
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:46
TRGBA
@ TRGBA
Definition: Bimage.h:64
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
Bimage::complex_conjugate
void complex_conjugate()
Calculates the complex conjugate image.
Definition: Bimage_complex.cpp:183
TRGB
@ TRGB
Definition: Bimage.h:63
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
timer.h
Utilities for timing functions.
MIN2PI
#define MIN2PI
Definition: utilities.h:75
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1027
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage.h
Header file for image class.
Bimage::center_wrap
int center_wrap()
Centers an image with wrapping.
Definition: Bimage_transform.cpp:708
Bimage::complex_power
double complex_power()
Calculates the power in a complex image.
Definition: Bimage_complex.cpp:200
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Complex::power
double power()
Definition: Complex.h:103
Bimage::check_resolution
void check_resolution(double &resolution)
Checks that the resolution falls within reasonable limits.
Definition: Bimage.cpp:635
Bimage::complex_normalize
double complex_normalize()
Normalizes the power in a complex image.
Definition: Bimage_complex.cpp:225
verbose
int verbose
isfinite
bool isfinite(complex< T > &value)
Bimage::complex_to_amplitudes
void complex_to_amplitudes()
The amplitudes from a complex image is written to a simple image.
Definition: Bimage_complex.cpp:114
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
TCMYK
@ TCMYK
Definition: Bimage.h:65
Bimage::complex_to_phases
void complex_to_phases()
The phases from a complex image is written to a simple image.
Definition: Bimage_complex.cpp:161
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Complex::conj
Complex conj()
Definition: Complex.h:106
Bimage::complex_conjugate_product
int complex_conjugate_product(Bimage *p)
Calculates the product of a complex image with the conjugate of a second.
Definition: Bimage_complex.cpp:460
Bimage::check
void check()
Checks an image for reasonable properties.
Definition: Bimage.cpp:419
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bimage::check_if_same_image_size
bool check_if_same_image_size(Bimage *p)
Check if two images are the same size.
Definition: Bimage.cpp:592
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:875
Bimage::intensities_phase_colored
Bimage * intensities_phase_colored(double scale)
Generates a power spectrum with phases colored according to a color wheel.
Definition: Bimage_complex.cpp:1363
Bimage::complex_product
int complex_product(Bimage *p)
Calculates the complex product of two complex images.
Definition: Bimage_complex.cpp:430
getwalltime
double getwalltime()
Returns the current time.
Definition: timer.cpp:26
Bimage::phase_shift_to_origin
int phase_shift_to_origin()
Phase shifts a set of reflections to the image origin.
Definition: Bimage_complex.cpp:327
Bimage::complex_apply_mask
int complex_apply_mask(Bimage *pmask)
Applies a mask to a complex image.
Definition: Bimage_complex.cpp:521
Complex::amp
double amp()
Definition: Complex.h:104
Float
@ Float
Definition: utilities.h:161
Bimage::rgb
RGB< double > rgb(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1055
Bimage::complex_bandpass
int complex_bandpass(double hires, double lores)
Bandpasses a Fourier transform.
Definition: Bimage_complex.cpp:615
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bimage::complex_to_signed_amplitudes
void complex_to_signed_amplitudes()
The signed amplitudes from a complex image is written to a simple image.
Definition: Bimage_complex.cpp:136
Bimage::phase_shift_to_center
int phase_shift_to_center()
Phase shifts a set of reflections to the nominal center of the image origin.
Definition: Bimage_complex.cpp:382
Bimage::combined_complex_product
int combined_complex_product()
Calculates the complex conjugate product of a complex image resulting from combining and Fourier tran...
Definition: Bimage_complex.cpp:813
NoTransform
@ NoTransform
Definition: Bimage.h:41
Bimage::complex_to_real
void complex_to_real()
The real part of a complex image is written to a simple image.
Definition: Bimage_complex.cpp:50
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::check_if_same_size
bool check_if_same_size(Bimage *p)
Check if two images are the same size.
Definition: Bimage.cpp:566
TWOPI
#define TWOPI
Definition: utilities.h:72