PSFEstimationwithCPSO
CPSO Class Reference

This class represents the Search method Cooperative Particle Swarm Optimization (CPSO). More...

#include <cpso.hpp>

List of all members.

Public Member Functions

 CPSO (double *_zernikes, int *_phase_mask, int *_diffraction_mask, int _phase_size, int _image_size, int _n_zernikes, int _psf_range, WORD _w, WORD c1, WORD c2, WORD _reset_at, int _n_particles, int _n_swarms)
 The Contructor.
 ~CPSO ()
 The destructor.
void set_images (double *object, double *image)
 This method makes the final preparation for the search.
void finalize_cl ()
 Finalize all internal objects related to this CPSO instance.
WORD getMinCost ()
 Get the minimum cost obtained from all the swarms available.
int getBestPsfPos ()
 Get the position between all the particles of the best PSF found.
void getBestCoefs (WORD *_coefs)
 Get the best coefficients.
void getBestPsf (WORD *psf)
 Get the best PSF.
void getBestPsfe (WORD *psfe)
 Get the best extracted (final) PSF.
void getBestPhase (WORD *phase)
 Get the best phase.
void getBestConvolvedObject (WORD *conobj)
 Get the best convolved object.
void getBestConvolvedObjectFFT (FFT_TYPE *conobj_fft)
 Get the FFT of the best convolved object.
void getBestPsfeFFT (FFT_TYPE *psfe_fft)
 Get the FFT of the best extracted PSF.
void getObjectFFT (FFT_TYPE *obj_fft)
 Get the FFT of the object.
void getImageFFT (FFT_TYPE *img_fft)
 Get the FFT of the image.
void replicateValue (WORD value, int sz, cl_mem cl_ref)
 Replicate one value 'sz' times into a OpenCl pointer.
void setW (WORD _w)
 Set the search parameter 'w' for all the particles.
void setC1 (WORD _c1)
 Set the search parameter 'c1' for all the particles.
void setC2 (WORD _c2)
 Set the search parameter 'c2' for all the particles.
void setOriginalPsf (WORD *original_psf)
 Set the exact original PSF that caused the corruption over the image.
double calcPsfDifferences ()
 Calculate the difference from the Original PSF to the currently calculated PSF.
void run (TimeTracker **trackers, int n_cycles)
 This method runs the search of the object over the image.
void runPsf (TimeTracker **trackers)
 Runs 'number of particles * number of swarms' PSFs, that were configured previously for this CPSO instance.
void runPsf (TimeTracker **trackers, int n_psfs)
 Runs 'n_psfs' PSFs.
void generatePhase (TimeTracker **tracker, int n_psfs)
 This method calculates the phase and the pupil.
void makePsf (TimeTracker **tracker, int n_psfs)
 Calculate the basic PSF information.
void convolveObj (TimeTracker **tracker, int n_psfs)
 Convolve the object with the PSFs calculated, generating several corrupted images.
void calcCost (TimeTracker **tracker, int n_psfs)
 Calculate the cost of the convolved objects.
void runCPSO (TimeTracker **tracker, int n_cycles)
 Run the CPSO search.
void reduce_squares (TimeTracker **tracker, int n_reductions, int reduction_width, cl_mem square, cl_mem sum, WORD *result)
 This method makes the reductions for 'n_reductions' matrixes.
FFT_TYPE validatePsf (double *psf)
 Validate an external PSF.
void commitBestValues ()
void copyToDeviceAsFloat (cl_command_queue command_queue, cl_mem dest, double *values, int size)
void copyToHostAsDouble (cl_command_queue command_queue, cl_mem src, double *values, int size)
void lock ()
 Lock this CPSO instance.
void release ()
 Release this CPSO instance.
bool isInUse ()
 Inform if this CPSO instance is in use.
WORD getGBestCost ()
 Returns the best cost found by the search. Should be called Only after the run method has finished.
void setStartupCoefs (WORD *coefs)
 Set the Zernike coefficients for the startup.
void saveFirstResult ()
 Save the results of the first Particle / PSF as the best value found so far.

Static Public Member Functions

static void store_static_data (cl_context _context, int _n_zernikes, int _phase_size, int _image_size, double *zernikes, int *phase_mask, int *_diffraction_mask)
 This method is responsible for storing the static data.
static void clear_static_data ()
 Clear all the static data.
static WORD getRandCoef (double range)
 Get a random coefficient within a specified range.
static double calc_mean (double *image, int img_area)
 Calculate the mean of a set of values.
static double calc_variance (double *image, int img_area)
 Calculate the variance of a set of values.
static double calc_stddev (double *image, int img_area)
 Calculate the standard deviation of a set of values.
static void generateRandomCoefs (WORD *coefs, int n_zernikes, double range)
 Generate random coefficients in a specific range.
static void generateNormalDistrRandomCoefs (WORD *coefs, int n_zernikes, double range)
 Generate random coefficients in a normal distribution.
static double randNormalDistribution (double mean, double std_dev)
 Generate a double value in a normal distribution.
template<class T >
static void invertPsf (T *psf, T *psf_inv, int psf_size)
 Invert the PSF to put the bright spot in the center of the image.

Detailed Description

This class represents the Search method Cooperative Particle Swarm Optimization (CPSO).

All processing related to this search method is included into this class.


Constructor & Destructor Documentation

CPSO::CPSO ( double *  _zernikes,
int *  _phase_mask,
int *  _diffraction_mask,
int  _phase_size,
int  _image_size,
int  _n_zernikes,
int  _psf_range,
WORD  _w,
WORD  c1,
WORD  c2,
WORD  _reset_at,
int  _n_particles,
int  _n_swarms 
)

The Contructor.

It is responsible for requesting a queue for the OpenCl factory and for allocating all the static and non-static data.

Parameters:
_zernikesThe complete Zernike matrix.
_phase_maskThe phase mask.
_diffraction_maskThe diffraction mask.
_phase_sizeThe phase mask.
_image_sizeThe image width.
_n_zernikesThe number of Zernike terms used for the calculations.
_psf_rangeThe Zernike coefficient range.
_wThe parameter w for the CPSO search.
c1The parameter c1 for the CPSO search.
c2The parameter c2 for the CPSO search.
_reset_atThe particle reset factor for the CPSO search.
_n_particlesThe number of particles for the CPSO search.
_n_swarmsThe number of swarms for the CPSO search.

Member Function Documentation

double CPSO::calc_mean ( double *  values,
int  size 
) [static]

Calculate the mean of a set of values.

Parameters:
valuesThe values to calculate the mean.
sizeThe array size.
Returns:
The calculated mean.
double CPSO::calc_stddev ( double *  values,
int  size 
) [static]

Calculate the standard deviation of a set of values.

Parameters:
valuesThe values to calculate the standard deviation.
sizeThe array size.
Returns:
The calculated standard deviation.
double CPSO::calc_variance ( double *  values,
int  size 
) [static]

Calculate the variance of a set of values.

Parameters:
valuesThe values to calculate the variance.
sizeThe array size.
Returns:
The calculated variance.
void CPSO::calcCost ( TimeTracker **  tracker,
int  n_psfs 
)

Calculate the cost of the convolved objects.

Parameters:
trackerThe tracker.
n_psfsThe number of PSFs that were used on this search.
double CPSO::calcPsfDifferences ( )

Calculate the difference from the Original PSF to the currently calculated PSF.

Returns:
the difference between the PSFs.
void CPSO::clear_static_data ( ) [static]

Clear all the static data.

This method Must be called only Once during the application life time!

void CPSO::commitBestValues ( )

Not in use.

void CPSO::convolveObj ( TimeTracker **  tracker,
int  n_psfs 
)

Convolve the object with the PSFs calculated, generating several corrupted images.

Parameters:
trackerThe tracker.
n_psfsThe number of PSFs that will generate the same number of convolved objects.
void CPSO::copyToDeviceAsFloat ( cl_command_queue  command_queue,
cl_mem  dest,
double *  values,
int  size 
)

Force the values to be copied from host to device as floats (not in use).

void CPSO::copyToHostAsDouble ( cl_command_queue  command_queue,
cl_mem  src,
double *  values,
int  size 
)

Force the values to be copied from device to host as doubles (not in use).

void CPSO::generateNormalDistrRandomCoefs ( WORD *  coefs,
int  n_zernikes,
double  range 
) [static]

Generate random coefficients in a normal distribution.

Parameters:
coefsA pointer where the the coefficients will be generated.
n_zernikesThe number of coefficients to be generated.
rangeThe coefficients range.
void CPSO::generatePhase ( TimeTracker **  tracker,
int  n_psfs 
)

This method calculates the phase and the pupil.

Parameters:
trackerThe tracker.
n_psfsThe number of PSFs to be calculated.
void CPSO::generateRandomCoefs ( WORD *  coefs,
int  n_zernikes,
double  range 
) [static]

Generate random coefficients in a specific range.

Parameters:
coefsA pointer where the the coefficients will be generated.
n_zernikesThe number of coefficients to be generated.
rangeThe coefficients range.
void CPSO::getBestCoefs ( WORD *  _coefs)

Get the best coefficients.

Parameters:
_coefsThe WORD pointer where to copy the coefficients.
void CPSO::getBestConvolvedObject ( WORD *  conobj)

Get the best convolved object.

Parameters:
conobjThe Host pointer where to copy the best convolved object.
void CPSO::getBestConvolvedObjectFFT ( FFT_TYPE *  conobj_fft)

Get the FFT of the best convolved object.

Parameters:
conobj_fftThe Host pointer where to copy the FFT of the best convolved object.
void CPSO::getBestPhase ( WORD *  phase)

Get the best phase.

Parameters:
phaseThe Host pointer where to copy the best phase.
void CPSO::getBestPsf ( WORD *  psf)

Get the best PSF.

Parameters:
psfThe Host pointer where to copy the best PSF.
void CPSO::getBestPsfe ( WORD *  psfe)

Get the best extracted (final) PSF.

Parameters:
psfeThe Host pointer where to copy the best final PSF.
void CPSO::getBestPsfeFFT ( FFT_TYPE *  psfe_fft)

Get the FFT of the best extracted PSF.

Parameters:
psfe_fftThe Host pointer where to copy the FFT of the best extracted PSF.
int CPSO::getBestPsfPos ( )

Get the position between all the particles of the best PSF found.

Returns:
The particle position of the best PSF found.
void CPSO::getImageFFT ( FFT_TYPE *  img_fft)

Get the FFT of the image.

Parameters:
img_fftThe Host pointer where to copy the FFT of the image.
WORD CPSO::getMinCost ( )

Get the minimum cost obtained from all the swarms available.

Returns:
The minimum cost obtained during the search.
void CPSO::getObjectFFT ( FFT_TYPE *  obj_fft)

Get the FFT of the object.

Parameters:
obj_fftThe Host pointer where to copy the FFT of the object.
WORD CPSO::getRandCoef ( double  range) [static]

Get a random coefficient within a specified range.

Parameters:
rangeThe range to generate the random value.
Returns:
The random coefficient.
template<class T >
static void CPSO::invertPsf ( T *  psf,
T *  psf_inv,
int  psf_size 
) [inline, static]

Invert the PSF to put the bright spot in the center of the image.

The inverted PSF is stored in the psf_inv pointer, which must be previously allocated.

Parameters:
psfThe PSF to be inverted.
psf_invThe PSF inverted.
psf_sizeThe width of the PSF.
void CPSO::makePsf ( TimeTracker **  tracker,
int  n_psfs 
)

Calculate the basic PSF information.

Parameters:
trackerThe tracker.
n_psfsThe number of PSFs to be calculated.
double CPSO::randNormalDistribution ( double  mean,
double  std_dev 
) [static]

Generate a double value in a normal distribution.

Parameters:
meanThe mean of the normal distribution.
std_devThe standard deviation of the normal distribution.
Returns:
The normal distribution number generated.
void CPSO::reduce_squares ( TimeTracker **  tracker,
int  n_reductions,
int  reduction_width,
cl_mem  square,
cl_mem  sum,
WORD *  result 
)

This method makes the reductions for 'n_reductions' matrixes.

Every summed matrix will return a WORD value inside the diff parameter, thus this parameter must have allocated enough space for the results.

Parameters:
trackerThe tracker
n_reductionsThe number of matrixes to be reduced.
reduction_widthThe square matrix width.
squareThe matrix data.
sumThe OpenCl pointer where the intermediate sum will be stored.
resultThe pointer where every final resulting will be stored.
void CPSO::replicateValue ( WORD  value,
int  sz,
cl_mem  cl_ref 
)

Replicate one value 'sz' times into a OpenCl pointer.

Parameters:
valueThe value to be replicated
szThe number of times the values will be replicated.
cl_refThe OpenCl pointer where to replicate the value.
void CPSO::run ( TimeTracker **  trackers,
int  n_cycles 
)

This method runs the search of the object over the image.

It must be called to start the CPSO search.

Parameters:
trackersThe tracker.
n_cyclesThe number of search cycles to be executed.
void CPSO::runCPSO ( TimeTracker **  tracker,
int  n_cycles 
)

Run the CPSO search.

Parameters:
trackerThe tracker.
n_cyclesThe number of cycles that this search was initially configured to run.
void CPSO::runPsf ( TimeTracker **  trackers,
int  n_psfs 
)

Runs 'n_psfs' PSFs.

Parameters:
trackersThe trackers.
n_psfsThe number of PSFs to be calculated.
void CPSO::runPsf ( TimeTracker **  trackers)

Runs 'number of particles * number of swarms' PSFs, that were configured previously for this CPSO instance.

Parameters:
trackersThe trackers.
void CPSO::set_images ( double *  object,
double *  image 
)

This method makes the final preparation for the search.

It must be called to set the coefficients, the object and the image.

Parameters:
objectThe object that will be used as starting point to obtain the image.
imageThe image that must be obtained by the search.
void CPSO::setC1 ( WORD  _c1)

Set the search parameter 'c1' for all the particles.

Parameters:
_c1The search parameter c1.
void CPSO::setC2 ( WORD  _c2)

Set the search parameter 'c2' for all the particles.

Parameters:
_c2The search parameter c2.
void CPSO::setOriginalPsf ( WORD *  original_psf)

Set the exact original PSF that caused the corruption over the image.

Parameters:
original_psfthe original PSF.
void CPSO::setStartupCoefs ( WORD *  coefs)

Set the Zernike coefficients for the startup.

It must be called before the search has begun.

Parameters:
coefsThe Zernike coefficients for the startup.
void CPSO::setW ( WORD  _w)

Set the search parameter 'w' for all the particles.

Parameters:
_wThe search parameter w.
void CPSO::store_static_data ( cl_context  _context,
int  _n_zernikes,
int  _phase_size,
int  _image_size,
double *  zernikes,
int *  phase_mask,
int *  _diffraction_mask 
) [static]

This method is responsible for storing the static data.

It stores the static data Only Once for every available context. It's the developer's responsibility to call the static method clear_static_data after all the processing in all available CPSOs is done. This method can be called several times during the application life time.

Parameters:
_contextThe OpenCl context.
_n_zernikesThe number of Zernike terms used for the calculations.
_phase_sizeThe phase width.
_image_sizeThe image width.
zernikesThe complete Zernike matrix.
phase_maskThe phase mask.
_diffraction_maskThe diffraction mask.
FFT_TYPE CPSO::validatePsf ( double *  psf_data)

Validate an external PSF.

The validation is made by:

  • Convolutioning the external PSF with the internal object;
  • Calculating the mismatch between the convolutioned object and the internal image.
Parameters:
psf_dataThe external PSF.
Returns:
The mismatch calculated.

The documentation for this class was generated from the following files:
 All Classes Functions