Bsoft  2.1.0
Bernard's software package
mg_ctf_sim.cpp
Go to the documentation of this file.
1 
9 #include "mg_ctf.h"
10 #include "utilities.h"
11 
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14 
15 
16 
17 int img_ttf_simulate_line(Bimage* p, Bimage* pn, long img_num, long i,
18  char ax, CTFparam& ctf, int action, double wiener,
19  double scale, fft_plan plan)
20 {
21  double ddef = ((double)i - p->image[img_num].origin()[0])*scale;
22  if ( ax == 'x' )
23  ddef = ((double)i - p->image[img_num].origin()[1])*scale;
24 
25  CTFparam ctf_copy = ctf;
26  ctf_copy.defocus_average(ctf.defocus_average()+ddef);
27 
28  Bimage* pctf = img_ctf_calculate(ctf_copy, action, wiener, pn->size(), pn->sampling(0), 1e6, 0);
29 
30  Bimage* pnc = pn->extract(img_num);
31 
32  pnc->complex_multiply(pctf);
33 
34  delete pctf;
35 
36  pnc->fft_back(plan);
37 
38  long xx, yy, j;
39  if ( ax == 'x' )
40  for ( xx=0, i*=p->sizeX(), j=i+img_num*p->image_size(); xx<p->sizeX(); xx++, i++, j++ )
41  p->set(j, (*pnc)[i]);
42  else
43  for ( yy=0, j=i+img_num*p->image_size(); yy<p->sizeY(); yy++, i+=p->sizeX(), j+=p->sizeX() )
44  p->set(j, (*pnc)[i]);
45 
46  delete pnc;
47 
48  return 0;
49 }
50 
51 long img_ttf_simulate_one(Bimage* p, Bimage* pn, long img_num,
52  CTFparam& ctf, int action, double wiener, double tilt,
53  double axis, fft_plan plan)
54 
55 {
56  char ax = (fabs(axis)<M_PI/4)? 'x': 'y';
57  long nn = (ax=='x')? pn->sizeY(): pn->sizeX();
58  double scale(pn->sampling(0)[1]*tan(tilt));
59 
60  if ( verbose )
61  cout << "Image " << img_num << ": " << tilt*180.0/M_PI << endl;
62 
63 #ifdef HAVE_GCD
64  __block long n(0);
65  dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
66  dispatch_apply(nn, dispatch_get_global_queue(0, 0), ^(size_t i){
67  img_ttf_simulate_line(p, pn, img_num, i, ax, ctf, action, wiener, scale, plan);
68  dispatch_sync(myq, ^{
69  n++;
70  if ( verbose )
71  cout << "Line " << n << "\r" << flush;
72  });
73  });
74 #else
75  long n(0);
76 #pragma omp parallel for
77  for ( long i=0; i<nn; i++ ) {
78  img_ttf_simulate_line(p, pn, img_num, i, ax, ctf, action, wiener, scale, plan);
79  #pragma omp critical
80  {
81  n++;
82  if ( verbose )
83  cout << "Line " << n << "\r" << flush;
84  }
85  }
86 #endif
87 
88  if ( verbose )
89  cout << "Lines calculated [" << img_num << "]: " << n << endl;
90 
91  return n;
92 }
93 
109 Bimage* img_ttf_simulate(Bimage* pn, CTFparam& ctf, int action,
110  double wiener, double tilt, double tilt_inc, double axis)
111 
112 {
113  if ( pn->sampling(0).volume() < 0.1 ) pn->sampling(Vector3<double>(1,1,1));
114 
115  pn->fft();
116 
117  Bimage* p = new Bimage(Float, TSimple, pn->size(), pn->images());
118  p->sampling(pn->sampling(0));
119  p->origin(p->size()/2);
120 
121  if ( verbose ) {
122  ctf.show();
123  cout << "Action: " << action << endl;
124  cout << "Size: " << p->size() << endl;
125  cout << "Images: " << p->images() << endl;
126  cout << "Sampling: " << p->sampling(0) << endl;
127  cout << "Tilt angle and increment: " << tilt*180/M_PI << " " << tilt_inc*180/M_PI << endl;
128  cout << "Tilt axis: " << axis*180/M_PI << endl << endl;
129  }
130 
131  fft_plan plan = p->fft_setup(FFTW_BACKWARD, 0);
132 
133  for ( long nn=0; nn<pn->images(); ++nn, tilt += tilt_inc )
134  img_ttf_simulate_one(p, pn, nn, ctf, action, wiener, tilt, axis, plan);
135 
136  fft_destroy_plan(plan);
137 
138  return p;
139 }
140 
Bimage::fft_setup
fft_plan fft_setup(fft_direction dir, int opt=0)
Sets up a plan for fast Fourier transforms.
Definition: Bimage_fft.cpp:29
Bimage::complex_multiply
int complex_multiply(Bimage *p)
Calculates the product of a complex image with a simple image.
Definition: Bimage_complex.cpp:400
img_ctf_calculate
Bimage * img_ctf_calculate(CTFparam cp, int action, double wiener, Vector3< long > size, Vector3< double > sam, double lores=0, double hires=0)
Calculates a CTF complex image.
Definition: mg_ctf.cpp:45
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
img_ttf_simulate
Bimage * img_ttf_simulate(Bimage *pn, CTFparam &ctf, int action, double wiener, double tilt, double tilt_inc, double axis)
Calculates an exact tilted CTF imposed image.
Definition: mg_ctf_sim.cpp:109
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bimage::fft_back
int fft_back()
Definition: Bimage.h:831
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
img_ttf_simulate_line
int img_ttf_simulate_line(Bimage *p, Bimage *pn, long img_num, long i, char ax, CTFparam &ctf, int action, double wiener, double scale, fft_plan plan)
Definition: mg_ctf_sim.cpp:17
Bimage::images
long images()
Definition: Bimage.h:504
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
img_ttf_simulate_one
long img_ttf_simulate_one(Bimage *p, Bimage *pn, long img_num, CTFparam &ctf, int action, double wiener, double tilt, double axis, fft_plan plan)
Definition: mg_ctf_sim.cpp:51
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Bimage::fft
int fft()
Definition: Bimage.h:830
Bimage::image_size
long image_size()
Definition: Bimage.h:290
mg_ctf.h
Functions for CTF (contrast transfer function) processing.
CTFparam::show
void show()
Definition: ctf.h:242
Bimage
General image parameter class.
Definition: Bimage.h:177
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
CTFparam
Definition: ctf.h:28
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:106
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Float
@ Float
Definition: utilities.h:161
verbose
int verbose