Openholo  v5.0
Open Source Digital Holographic Library
ophSig.h
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install, copy or use the software.
7 //
8 //
9 // License Agreement
10 // For Open Source Digital Holographic Library
11 //
12 // Openholo library is free software;
13 // you can redistribute it and/or modify it under the terms of the BSD 2-Clause license.
14 //
15 // Copyright (C) 2017-2024, Korea Electronics Technology Institute. All rights reserved.
16 // E-mail : contact.openholo@gmail.com
17 // Web : http://www.openholo.org
18 //
19 // Redistribution and use in source and binary forms, with or without modification,
20 // are permitted provided that the following conditions are met:
21 //
22 // 1. Redistribution's of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
24 //
25 // 2. Redistribution's in binary form must reproduce the above copyright notice,
26 // this list of conditions and the following disclaimer in the documentation
27 // and/or other materials provided with the distribution.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the copyright holder or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 // This software contains opensource software released under GNU Generic Public License,
41 // NVDIA Software License Agreement, or CUDA supplement to Software License Agreement.
42 // Check whether software you use contains licensed software.
43 //
44 //M*/
45 
46 
47 #ifndef __ophSig_h
48 #define __ophSig_h
49 
50 #include "tinyxml2.h"
51 #include "Openholo.h"
52 #include "sys.h"
53 
54 #ifdef _WIN64
55 #ifdef SIG_EXPORT
56 #define SIG_DLL __declspec(dllexport)
57 #else
58 #define SIG_DLL __declspec(dllimport)
59 #endif
60 #else
61 #ifdef SIG_EXPORT
62 #define SIG_DLL __attribute__((visibility("default")))
63 #else
64 #define SIG_DLL
65 #endif
66 #endif
67 
69  int cols;
70  int rows;
76  Real wavelength[3];
77 };
78 
82 
99 
104 
157 
162 
262 
267 
337 
342 
421 
426 
453 
455 
459 
471 
478 class SIG_DLL ophSig : public Openholo
479 {
480 protected:
481 
482  virtual ~ophSig(void) = default;
483 
484  virtual void ophFree(void);
485  bool is_CPU;
488  fftw_plan bwd_plan, fwd_plan;
489 
492  Real_t _foc[3];
493 
494 
495 
503  bool sigConvertOffaxis_CPU(Real angleX, Real angleY);
511  bool sigConvertOffaxis_GPU(Real angleX, Real angleY);
519  bool sigConvertHPO_CPU(Real depth, Real_t redRate);
527  bool sigConvertHPO_GPU(Real depth, Real_t redRate);
528 
538  bool sigConvertCAC_CPU(double red, double green, double blue);
547  bool sigConvertCAC_GPU(double red, double green, double blue);
553  double sigGetParamAT_CPU();
559  double sigGetParamAT_GPU();
569  double sigGetParamSF_CPU(float zMax, float zMin, int sampN, float th);
579  double sigGetParamSF_GPU(float zMax, float zMin, int sampN, float th);
580 
586  bool propagationHolo_CPU(float depth);
592  bool propagationHolo_GPU(float depth);
593 
594  bool Color_propagationHolo_GPU(float depth);
595 public:
599  explicit ophSig(void);
606  bool load(const char *real, const char *imag);
613  bool save(const char *real, const char *imag);
614  bool save(const char *real);
615 
621  bool loadAsOhc(const char *fname);
627  bool saveAsOhc(const char *fname);
635  template<typename T>
636  void linInterp(vector<T> &X, matrix<Complex<T>> &src, vector<T> &Xq, matrix<Complex<T>> &dst);
644  template<typename T>
645  vector<T> linspace(T first, T last, int len) {
646  vector<Real> result(len);
647  Real step = (last - first) / (len - 1);
648  for (int i = 0; i < len; i++) { result[i] = first + i * step; }
649  return result;
650  }
656  template<typename T>
657  void absMat(matrix<Complex<T>>& src, matrix<T>& dst) {
658  if (src.size != dst.size) {
659  dst.resize(src.size[_X], src.size[_Y]);
660  }
661  for (int i = 0; i < src.size[_X]; i++)
662  {
663  for (int j = 0; j < src.size[_Y]; j++)
664  {
665  dst.mat[i][j] = sqrt(src.mat[i][j][_RE] * src.mat[i][j][_RE] + src.mat[i][j][_IM] * src.mat[i][j][_IM]);
666  }
667  }
668  }
674  template<typename T>
675  void absMat(matrix<T>& src, matrix<T>& dst) {
676  if (src.size != dst.size) {
677  dst.resize(src.size[_X], src.size[_Y]);
678  }
679  for (int i = 0; i < src.size[_X]; i++)
680  {
681  for (int j = 0; j < src.size[_Y]; j++)
682  {
683  dst.mat[i][j] = abs(src.mat[i][j]);
684  }
685  }
686  }
692  template<typename T>
693  void angleMat(matrix<Complex<T>>& src, matrix<T>& dst) {
694  if (src.size != dst.size) {
695  dst.resize(src.size[_X], src.size[_Y]);
696  }
697  for (int i = 0; i < src.size[_X]; i++)
698  {
699  for (int j = 0; j < src.size[_Y]; j++)
700  {
701  angle(src(i, j), dst(i, j));
702  }
703  }
704  }
710  template<typename T>
712  if (src.size != dst.size) {
713  dst.resize(src.size[_X], src.size[_Y]);
714  }
715  for (int i = 0; i < src.size[_X]; i++)
716  {
717  for (int j = 0; j < src.size[_Y]; j++)
718  {
719  dst(i, j) = src(i, j).conj();
720 
721  }
722  }
723  }
729  template<typename T>
730  void expMat(matrix<Complex<T>>& src, matrix<Complex<T>>& dst) {
731  if (src.size != dst.size) {
732  dst.resize(src.size[_X], src.size[_Y]);
733  }
734  for (int i = 0; i < src.size[_X]; i++)
735  {
736  for (int j = 0; j < src.size[_Y]; j++)
737  {
738  dst.mat[i][j][_RE] = exp(src.mat[i][j][_RE]) * cos(src.mat[i][j][_IM]);
739  dst.mat[i][j][_IM] = exp(src.mat[i][j][_RE]) * sin(src.mat[i][j][_IM]);
740  }
741  }
742  }
748  template<typename T>
749  void expMat(matrix<T>& src, matrix<T>& dst) {
750  if (src.size != dst.size) {
751  dst.resize(src.size[_X], src.size[_Y]);
752  }
753  for (int i = 0; i < src.size[_X]; i++)
754  {
755  for (int j = 0; j < src.size[_Y]; j++)
756  {
757  dst.mat[i][j] = exp(src.mat[i][j]);
758  }
759  }
760  }
766  template<typename T>
767  void meanOfMat(matrix<T>& src, T &dst) {
768  dst = 0;
769  for (int i = 0; i < src.size[_X]; i++)
770  {
771  for (int j = 0; j < src.size[_Y]; j++)
772  {
773  dst += src(i, j);
774  }
775  }
776  dst = dst / (src.size[_X] * src.size[_Y]);
777  }
784  Real max = MIN_REAL;
785  for (int i = 0; i < src.size[_X]; i++)
786  {
787  for (int j = 0; j < src.size[_Y]; j++)
788  {
789  if (src(i, j) > max) max = src(i, j);
790  }
791  }
792  return max;
793  }
800  {
801  Real max = MIN_REAL;
802  for (int i = 0; i < src.size[_X]; i++)
803  {
804  for (int j = 0; j < src.size[_Y]; j++)
805  {
806  if (src(i, j)[_RE] > max) max = src(i, j)[_RE];
807  //if (src(i, j)[_IM] > max) max = src(i, j)[_IM];
808  }
809  }
810  return max;
811  }
816  //template<typename T>
818  Real min = MAX_REAL;
819  for (int i = 0; i < src.size[_X]; i++)
820  {
821  for (int j = 0; j < src.size[_Y]; j++)
822  {
823  if (src(i, j) < min) min = src(i, j);
824  }
825  }
826  return min;
827  }
833  Real min = MAX_REAL;
834  for (int i = 0; i < src.size[_X]; i++)
835  {
836  for (int j = 0; j < src.size[_Y]; j++)
837  {
838  if (src(i, j)[_RE] < min) min = src(i, j)[_RE];
839  }
840  }
841  return min;
842  }
843 
850  {
851  if (src.size != dst.size) {
852  dst.resize(src.size[_X], src.size[_Y]);
853  }
854  int xshift = src.size[_X] / 2;
855  int yshift = src.size[_Y] / 2;
856  for (int i = 0; i < src.size[_X]; i++)
857  {
858  int ii = (i + xshift) % src.size[_X];
859  for (int j = 0; j < src.size[_Y]; j++)
860  {
861  int jj = (j + yshift) % src.size[_Y];
862  dst.mat[ii][jj][_RE] = src.mat[i][j].real();
863  dst.mat[ii][jj][_IM] = src.mat[i][j].imag();
864  }
865  }
866  }
867 
875  template<typename T>
876  void fft1(matrix<Complex<T>> &src, matrix<Complex<T>> &dst, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
884  template<typename T>
885  void fft2(matrix<Complex<T>> &src, matrix<Complex<T>> &dst, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
891  bool readConfig(const char* fname);
892  void Parameter_Set(int nx, int ny, double width, double height , double NA );
893  void wavelength_Set(double wavelength);
894  void focal_length_Set(double red , double green, double blue, double rad);
895  void Data_output(uchar *data, int pos ,int bitpixel);
896  void Wavenumber_output(int &wavenumber);
897 
905  bool sigConvertOffaxis(Real angleX, Real angleY);
906  bool cvtOffaxis_CPU(Real angleX, Real angleY);
907  void cvtOffaxis_GPU(Real angleX, Real angleY);
908 
909 
917  bool sigConvertHPO(Real depth, Real_t redRate);
918 
919 
928  bool sigConvertCAC(double red, double green, double blue);
929 
935  bool propagationHolo(float depth);
942  OphComplexField propagationHolo(OphComplexField complexH, float depth);
943 
949  double sigGetParamAT();
950 
960  double sigGetParamSF(float zMax, float zMin, int sampN, float th);
961 
966  void setMode(bool is_CPU);
974  void cField2Buffer(matrix<Complex<Real>>& src, Complex<Real> **dst, int nx, int ny);
975 
983  void ColorField2Buffer(matrix<Complex<Real>>& src, Complex<Real> **dst, int nx, int ny);
984 
992  bool getComplexHFromPSDH(const char* fname0, const char* fname90, const char* fname180, const char* fname270);
993 
1003  bool getComplexHFrom3ArbStepPSDH(const char* f0, const char* f1, const char* f2, const char* fOI, const char* fRI, int nIter);
1004 
1005 };
1006 
1007 #endif // !__ophSig_h
1008 
Complex< Real > maxOfMat(matrix< Complex< Real >> &src)
Function for extracts maximum of matrix , where matrix is complex number.
Definition: ophSig.h:799
void abs(const oph::Complex< T > &src, oph::Complex< T > &dst)
Definition: function.h:113
Abstract class.
Definition: Openholo.h:145
class for the complex number and its arithmetic. type T == type cplx type only float || double T real...
Definition: complex.h:354
void angleMat(matrix< Complex< T >> &src, matrix< T > &dst)
Function for extracts Complex phase value.
Definition: ophSig.h:693
#define SIG_DLL
Definition: ophSig.h:64
Real_t _radius
Definition: ophSig.h:491
void meanOfMat(matrix< T > &src, T &dst)
Function for extracts mean of matrix.
Definition: ophSig.h:767
unsigned char uchar
Definition: typedef.h:64
float Real
Definition: typedef.h:55
fftw_plan fwd_plan
Definition: ophSig.h:488
void conjMat(matrix< Complex< T >> &src, matrix< Complex< T >> &dst)
Function for extracts Complex conjugate value.
Definition: ophSig.h:711
#define OPH_ESTIMATE
Definition: define.h:76
matrix< T > & resize(int x, int y)
Definition: mat.h:117
#define _Y
Definition: define.h:96
#define _IM
Definition: complex.h:58
ophSigConfig _cfgSig
Definition: ophSig.h:486
#define _X
Definition: define.h:92
int _wavelength_num
Definition: ophSig.h:490
Complex< Real > minOfMat(matrix< Complex< Real >> &src)
Function for extracts minimum of matrix , where matrix is complex number.
Definition: ophSig.h:832
int cols
Definition: ophSig.h:69
void expMat(matrix< Complex< T >> &src, matrix< Complex< T >> &dst)
Function for extracts exponent e(x), where x is complex number.
Definition: ophSig.h:730
int rows
Definition: ophSig.h:70
Real_t NA
Definition: ophSig.h:73
OphComplexField * ComplexH
Definition: ophSig.h:487
ivec2 size
Definition: mat.h:71
int wavelength_num
Definition: ophSig.h:75
bool is_CPU
Definition: ophSig.h:485
#define _RE
Definition: complex.h:55
Definition: ophSig.h:478
void fft2(ivec2 n, Complex< Real > *in, int sign=OPH_FORWARD, uint flag=OPH_ESTIMATE)
Functions for performing fftw 2-dimension operations inside Openholo.
Definition: Openholo.cpp:559
Real_t z
Definition: ophSig.h:74
Real_t height
Definition: ophSig.h:72
std::vector< T > * mat
Definition: mat.h:70
void absMat(matrix< T > &src, matrix< T > &dst)
Function for extracts real absolute value.
Definition: ophSig.h:675
double Real_t
Definition: typedef.h:56
void fftShift(matrix< Complex< Real >> &src, matrix< Complex< Real >> &dst)
Shift zero-frequency component to center of spectrum.
Definition: ophSig.h:849
void absMat(matrix< Complex< T >> &src, matrix< T > &dst)
Function for extracts Complex absolute value.
Definition: ophSig.h:657
virtual bool loadAsOhc(const char *fname)
Function to read OHC file.
Definition: Openholo.cpp:280
Real minOfMat(matrix< Real > &src)
Function for extracts minimum of matrix , where matrix is real number.
Definition: ophSig.h:817
void expMat(matrix< T > &src, matrix< T > &dst)
Function for extracts exponent e(x), where x is real number.
Definition: ophSig.h:749
void angle(const std::vector< Complex< T >> &src, std::vector< T > &dst)
Definition: function.h:159
#define MAX_REAL
Definition: define.h:152
virtual bool saveAsOhc(const char *fname)
Function to write OHC file
Definition: Openholo.cpp:252
Real maxOfMat(matrix< Real > &src)
Function for extracts maximum of matrix , where matrix is real number.
Definition: ophSig.h:783
#define MIN_REAL
Definition: define.h:151
Real_t width
Definition: ophSig.h:71
#define OPH_FORWARD
Definition: define.h:66
void fft1(int n, Complex< Real > *in, int sign=OPH_FORWARD, uint flag=OPH_ESTIMATE)
Functions for performing fftw 1-dimension operations inside Openholo.
Definition: Openholo.cpp:527
vector< T > linspace(T first, T last, int len)
Generate linearly spaced vector.
Definition: ophSig.h:645
unsigned int uint
Definition: typedef.h:62
virtual void ophFree(void)
Pure virtual function for override in child classes.
Definition: Openholo.cpp:811