Bsoft 2.1.4
Bernard's software package
mg_ctf_fit.cpp File Reference

Functions for CTF (contrast transfer function) processing. More...

#include "rwimg.h"
#include "mg_ctf.h"
#include "mg_ctf_fit.h"
#include "mg_processing.h"
#include "simplex.h"
#include "moving_average.h"
#include "utilities.h"
#include "timer.h"
#include <sys/stat.h>

Functions

double ctf_fit_baseline (Bimage *prad, double real_size, CTFparam &em_ctf, double lores, double hires)
 
double ctf_fit_envelope (Bimage *prad, double real_size, CTFparam &em_ctf, double lores, double hires)
 
double img_aberration_phase_fit_iter (Bimage *p, long nn, double lores, double hires, int flag, long iter, map< pair< long, long >, double > &wa)
 
int complete_weights (map< pair< long, long >, double > &w, int flag)
 
Bimageimg_ctf_radial_average (Bimage *p, long n, CTFparam &em_ctf)
 Calculates the power spectrum radial average corrected for astigmatism. More...
 
double img_ctf_fit_residual (Bimage *p, long n, CTFparam &em_ctf, double lores, double hires)
 
double img_ctf_find_defocus2 (Bimage *p, long n, CTFparam &em_ctf, double lores, double hires, double def_start, double def_end, double def_inc)
 
int img_manage_power_spectrum_peak (Bimage *p, Bimage *pb, long radius)
 
Bimageimg_ctf_fit_prepare (Bimage *p, double sigma)
 
Bimageimg_ctf_fit_prepare (Bimage *p, long n, double sigma)
 
double img_ctf_fit_old (Bimage *p, long n, CTFparam &em_ctf, double lores, double hires, double def_start, double def_end, double def_inc, int flag)
 Determines the CTF parameters from a power spectrum. More...
 
double img_ctf_fit (Bimage *p, long n, CTFparam &em_ctf, double lores, double hires, double def_start, double def_end, double def_inc, int flag)
 
double img_ctf_fit_baseline (Bimage *p, long n, CTFparam &em_ctf, double lores, double hires)
 Fits only the baseline for a given CTF. More...
 
double img_ctf_fit_envelope (Bimage *p, long n, CTFparam &em_ctf, double lores, double hires)
 Fits only the envelope for a given CTF. More...
 
double ctf_test_defocus (CTFparam &em_ctf, double def, double step_size, vector< double > &r, long rmin, long rmax)
 
double ctf_find_defocus (vector< double > &v, CTFparam &em_ctf, long rmin, long rmax, double step_size, double def_start, double def_end, double def_inc)
 
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. More...
 
Bimageimg_ctf_radial_subtract_baseline (Bimage *prad, double real_size, CTFparam &em_ctf)
 
double img_ctf_astigmatism_measure (Bimage *p, long n, double real_size, CTFparam &em_ctf, double lores, double hires)
 
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. More...
 
map< pair< long, long >, double > img_aberration_phase_fit (Bimage *p, long nn, double lores, double hires, int flag)
 Fits a phase image with aberration functions. More...
 
double aberration_fit_R (Bsimplex &simp)
 
double sine_aberration_fit_R (Bsimplex &simp)
 
double cosine_aberration_fit_R (Bsimplex &simp)
 
double img_aberration_phase_fit_iter_old (Bimage *p, long nn, double lores, double hires, int flag, long iter, map< pair< long, long >, double > &wa)
 
vector< map< pair< long, long >, double > > img_aberration_phase_fit (Bimage *p, double lores, double hires, int flag, long iter)
 
vector< map< pair< long, long >, double > > img_aberration_phase_fit (Bimage *p, double lores, double hires, vector< map< pair< long, long >, double > > &win, int flag, long iter)
 Fits a phase image with aberration terms. More...
 
double img_ctf_fit_aberration (Bimage *p, long n, CTFparam &em_ctf, double lores, double hires)
 
double img_water_ring_index (Bimage *p, long img_num, CTFparam &em_ctf)
 Calculates the water ring index from a power spectrum. More...
 
double img_water_ring_index (Bimage *prad)
 Calculates the water ring index from a power spectrum. More...
 
double water_ring_R (Bsimplex &simp)
 
vector< double > img_fit_water_ring (Bimage *p)
 
double gaussian_R (Bsimplex &simp)
 
double gaussian_WR (Bsimplex &simp)
 
double gaussian1_R (Bsimplex &simp)
 
double double_gaussian_R (Bsimplex &simp)
 
double double_gaussian_WR (Bsimplex &simp)
 
double eman_baseline_R (Bsimplex &simp)
 
double ctf_fit_double_gaussian_baseline (vector< double > &x, vector< double > &y, vector< double > &coeff)
 
double ctf_fit_eman_to_baseline (vector< double > &x, vector< double > &y, vector< double > &coeff)
 
double ctf_fit_gaussian (vector< double > &x, vector< double > &y, vector< double > &coeff)
 
double ctf_fit_baseline_bump (vector< double > &x, vector< double > &y, vector< double > &coeff)
 
double ctf_fit_double_gaussian_envelope (vector< double > &x, vector< double > &y, vector< double > &coeff)
 

Variables

int verbose
 

Detailed Description

Functions for CTF (contrast transfer function) processing.

Author
Bernard Heymann
Date
Created: 19970715
Modified: 20221213

Function Documentation

◆ aberration_fit_R()

double aberration_fit_R ( Bsimplex simp)

◆ complete_weights()

int complete_weights ( map< pair< long, long >, double > &  w,
int  flag 
)

◆ cosine_aberration_fit_R()

double cosine_aberration_fit_R ( Bsimplex simp)

◆ ctf_find_defocus()

double ctf_find_defocus ( vector< double > &  v,
CTFparam em_ctf,
long  rmin,
long  rmax,
double  step_size,
double  def_start,
double  def_end,
double  def_inc 
)

◆ ctf_fit_baseline()

double ctf_fit_baseline ( Bimage prad,
double  real_size,
CTFparam em_ctf,
double  lores,
double  hires 
)

◆ ctf_fit_baseline_bump()

double ctf_fit_baseline_bump ( vector< double > &  x,
vector< double > &  y,
vector< double > &  coeff 
)

◆ ctf_fit_double_gaussian_baseline()

double ctf_fit_double_gaussian_baseline ( vector< double > &  x,
vector< double > &  y,
vector< double > &  coeff 
)

◆ ctf_fit_double_gaussian_envelope()

double ctf_fit_double_gaussian_envelope ( vector< double > &  x,
vector< double > &  y,
vector< double > &  coeff 
)

◆ ctf_fit_eman_to_baseline()

double ctf_fit_eman_to_baseline ( vector< double > &  x,
vector< double > &  y,
vector< double > &  coeff 
)

◆ ctf_fit_envelope()

double ctf_fit_envelope ( Bimage prad,
double  real_size,
CTFparam em_ctf,
double  lores,
double  hires 
)

◆ ctf_fit_gaussian()

double ctf_fit_gaussian ( vector< double > &  x,
vector< double > &  y,
vector< double > &  coeff 
)

◆ ctf_test_defocus()

double ctf_test_defocus ( CTFparam em_ctf,
double  def,
double  step_size,
vector< double > &  r,
long  rmin,
long  rmax 
)

◆ double_gaussian_R()

double double_gaussian_R ( Bsimplex simp)

◆ double_gaussian_WR()

double double_gaussian_WR ( Bsimplex simp)

◆ eman_baseline_R()

double eman_baseline_R ( Bsimplex simp)

◆ gaussian1_R()

double gaussian1_R ( Bsimplex simp)

◆ gaussian_R()

double gaussian_R ( Bsimplex simp)

◆ gaussian_WR()

double gaussian_WR ( Bsimplex simp)

◆ img_aberration_phase_fit() [1/3]

vector< map< pair< long, long >, double > > img_aberration_phase_fit ( Bimage p,
double  lores,
double  hires,
int  flag,
long  iter 
)

◆ img_aberration_phase_fit() [2/3]

vector< map< pair< long, long >, double > > img_aberration_phase_fit ( Bimage p,
double  lores,
double  hires,
vector< map< pair< long, long >, double > > &  win,
int  flag,
long  iter 
)

Fits a phase image with aberration terms.

Parameters
*pphase image.
loreslow resolution limit.
hireshigh resolution limit.
wininitial aberration parameters.
flag0=full, 1=odd, 2=even.
itermaximum number of iterations.
Returns
vector<map<pair<long,long>,double>> vector of aberration weights.

◆ img_aberration_phase_fit() [3/3]

map< pair< long, long >, double > img_aberration_phase_fit ( Bimage p,
long  nn,
double  lores,
double  hires,
int  flag 
)

Fits a phase image with aberration functions.

Parameters
*pimage structure.
nnsub-image number.
loreslow resolution limit.
hireshigh resolution limit.
flag0=full, 1=odd, 2=even.
Returns
map<pair<long,long>,double> aberration weights.
The aberration terms are calculated for each pixel and added to a symmetric matrix.
The matrix is inverted and the aberration weights calculated.
The origin of the image must be at {0,0}.

◆ img_aberration_phase_fit_iter()

double img_aberration_phase_fit_iter ( Bimage p,
long  nn,
double  lores,
double  hires,
int  flag,
long  iter,
map< pair< long, long >, double > &  wa 
)

◆ img_aberration_phase_fit_iter_old()

double img_aberration_phase_fit_iter_old ( Bimage p,
long  nn,
double  lores,
double  hires,
int  flag,
long  iter,
map< pair< long, long >, double > &  wa 
)

◆ img_ctf_astigmatism_measure()

double img_ctf_astigmatism_measure ( Bimage p,
long  n,
double  real_size,
CTFparam em_ctf,
double  lores,
double  hires 
)

◆ img_ctf_find_defocus()

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.

Parameters
*pimage structure.
nsub-image number.
&em_ctfCTF parameter structure.
loreslow resolution limit.
hireshigh resolution limit.
def_startdefocus search start (default 1e3).
def_enddefocus search end (default 2e5).
def_incdefocus search increment (default 1e3).
Returns
double R factor.
A radial power spectrum is calculated.
A range of defocus values is tested (1000-200000 angstrom, 0.1-20 um), 
and the best fitting value accepted.
The new parameters are written into the CTFparam structure.

◆ img_ctf_find_defocus2()

double img_ctf_find_defocus2 ( Bimage p,
long  n,
CTFparam em_ctf,
double  lores,
double  hires,
double  def_start,
double  def_end,
double  def_inc 
)

◆ img_ctf_fit()

double img_ctf_fit ( Bimage p,
long  n,
CTFparam em_ctf,
double  lores,
double  hires,
double  def_start,
double  def_end,
double  def_inc,
int  flag 
)

◆ img_ctf_fit_aberration()

double img_ctf_fit_aberration ( Bimage p,
long  n,
CTFparam em_ctf,
double  lores,
double  hires 
)

◆ img_ctf_fit_astigmatism()

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.

Parameters
*pimage structure.
nsub-image number.
&em_ctfCTF parameter structure.
loreslow resolution limit.
hireshigh resolution limit
Returns
double objective measure (larger is better).
A radial power spectrum is calculated and the baseline subtracted.
The defocus deviation starts from a low value to get an estimate of
the astigmatism angle. The defocus deviation is modified nased on
the direction of improvements in the fit, at each iteration 
narrowing the angular search for the astigmatism angle.
The new parameters are written into the CTFparam structure.

◆ img_ctf_fit_baseline()

double img_ctf_fit_baseline ( Bimage p,
long  n,
CTFparam em_ctf,
double  lores,
double  hires 
)

Fits only the baseline for a given CTF.

Parameters
*pimage structure.
nsub-image number.
&em_ctfCTF parameter structure.
loreslow resolution limit.
hireshigh resolution limit
Returns
double R factor.
A radial power spectrum is calculated incorporating astigmatism.
The baseline of the required type is fit.
The new parameters are written into the CTFparam structure.

◆ img_ctf_fit_envelope()

double img_ctf_fit_envelope ( Bimage p,
long  n,
CTFparam em_ctf,
double  lores,
double  hires 
)

Fits only the envelope for a given CTF.

Parameters
*pimage structure.
nsub-image number.
*em_ctfCTF parameter structure.
loreslow resolution limit.
hireshigh resolution limit
Returns
double R factor.
A radial power spectrum is calculated incorporating astigmatism.
The envelope is fit.
The new parameters are written into the CTFparam structure.

◆ img_ctf_fit_old()

double img_ctf_fit_old ( Bimage p,
long  n,
CTFparam em_ctf,
double  lores,
double  hires,
double  def_start,
double  def_end,
double  def_inc,
int  flag 
)

Determines the CTF parameters from a power spectrum.

Parameters
*pimage structure.
nsub-image number.
&em_ctfCTF parameter structure.
loreslow resolution limit.
hireshigh resolution limit
def_startdefocus search start (default 1e3).
def_enddefocus search end (default 2e5).
def_incdefocus search increment (default 1e3).
flagflag to determine astigmatism.
Returns
double water ring index.
Input: Power spectrum or its logarithm.
A radial power spectrum is calculated.
A range of defocus values is tested (100-200000 angstrom, 0.01-20 um), 
defining the baseline as passing through the zeroes for each defocus 
and fitting it to a 4th order polynomial.
The envelope function is a simple gaussian on top of the baseline and
fitted to minimize the RMSD between the calculated curve and the
radial power spectrum logarithm.
The fitting is limited to the spatial frequency range between the 
first and last zeroes.
Defocus values are positive for underfocus.
Functions:
    angle = atan(y/x)
    s2 = reciprocal space distance squared
    defocus_average = (defocus_max + defocus_min)/2
    defocus_deviation = (defocus_max - defocus_min)/2
    defocus = defocus_average + defocus_deviation*cos(2*(angle - astigmatism_angle))
    phase = 0.5*PI*lambda*lambda*lambda*Cs*s2*s2 - PI*lambda*defocus*s2 - amp_shift;
    CTF = sin(phase)
The new parameters are written into the CTPparam structure.

◆ img_ctf_fit_prepare() [1/2]

Bimage * img_ctf_fit_prepare ( Bimage p,
double  sigma 
)

◆ img_ctf_fit_prepare() [2/2]

Bimage * img_ctf_fit_prepare ( Bimage p,
long  n,
double  sigma 
)

◆ img_ctf_fit_residual()

double img_ctf_fit_residual ( Bimage p,
long  n,
CTFparam em_ctf,
double  lores,
double  hires 
)

◆ img_ctf_radial_average()

Bimage * img_ctf_radial_average ( Bimage p,
long  n,
CTFparam em_ctf 
)

Calculates the power spectrum radial average corrected for astigmatism.

Parameters
*pimage structure.
nsub-image number.
&em_ctfCTF parameter structure.
Returns
Bimage* radial average, NULL on error.
A power spectrum with its origin at the center.
Functions:
    angle = atan(y/x) - astigmatism_angle
    s2 = reciprocal space distance squared
    defocus_min = defocus_avg - defocus_dev
    defocus_max = defocus_avg + defocus_dev
    smin2 = 1 - defocus_dev/defocus_avg
    smax2 = 1 + defocus_dev/defocus_avg
    radius = sqrt(2*s2*(smax2*cos(angle)*cos(angle)+
                smin2*sin(angle)*sin(angle))/(smax2+smin2))
The radial average is returned as a new 1D image.

◆ img_ctf_radial_subtract_baseline()

Bimage * img_ctf_radial_subtract_baseline ( Bimage prad,
double  real_size,
CTFparam em_ctf 
)

◆ img_fit_water_ring()

vector< double > img_fit_water_ring ( Bimage p)

◆ img_manage_power_spectrum_peak()

int img_manage_power_spectrum_peak ( Bimage p,
Bimage pb,
long  radius 
)

◆ img_water_ring_index() [1/2]

double img_water_ring_index ( Bimage p,
long  img_num,
CTFparam em_ctf 
)

Calculates the water ring index from a power spectrum.

Parameters
*ppower spectrum.
img_numsub-image number.
&em_ctfCTF parameter structure.
Returns
double water ing index.
A radial power spectrum is calculated.
The the water ring index is defined as:
    wri = wp/b - 1
where wp is the intensity at s=0.26 (3.8 Å) and b is the background.
The background is estimated as the average of sections before and after 
the water peak:
    b1 between s=0.1 and s=0.2
    b2 between s=0.3 and s=0.4

◆ img_water_ring_index() [2/2]

double img_water_ring_index ( Bimage prad)

Calculates the water ring index from a power spectrum.

Parameters
*pradradial power spectrum.
Returns
double water ing index.
The the water ring index is defined as:
    wri = wp/b - 1
where wp is the average intensity between 0.2 and 0.3, covering the 
peak at s=0.26 (3.8 Å).
The background, b, is estimated as the average of sections before and after 
the water peak:
    b1 between s=0.1 and s=0.2
    b2 between s=0.3 and s=0.4

◆ sine_aberration_fit_R()

double sine_aberration_fit_R ( Bsimplex simp)

◆ water_ring_R()

double water_ring_R ( Bsimplex simp)

Variable Documentation

◆ verbose

int verbose
extern