Openholo  v1.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 
55 
56 #ifdef SIG_EXPORT
57 #define SIG_DLL __declspec(dllexport)
58 #else
59 #define SIG_DLL __declspec(dllimport)
60 #endif
61 
63  int cols;
64  int rows;
70  Real wavelength[3];
71 };
72 
76 
93 
100 class SIG_DLL ophSig : public Openholo
101 {
102 protected:
103 
104  virtual ~ophSig(void) = default;
105 
106  virtual void ophFree(void);
107  bool is_CPU;
110  fftw_plan bwd_plan, fwd_plan;
111 
112  //float _width;
113  //float _height;
114 
115  //float _NA;
116  //float _z;
117 
119 
120  /*float _angleX;
121  float _angleY;
122  float _redRate;*/
125 
126 public:
130  explicit ophSig(void);
138  bool load(const char *real, const char *imag);
146  bool save(const char *real, const char *imag);
147  bool save(const char *real);
148  bool loadAsOhc(const char *fname);
149  bool saveAsOhc(const char *fname);
150  template<typename T>
151  void linInterp(vector<T> &X, matrix<Complex<T>> &src, vector<T> &Xq, matrix<Complex<T>> &dst);
159  template<typename T>
160  vector<T> linspace(T first, T last, int len) {
161  vector<Real> result(len);
162 
163  Real step = (last - first) / (len - 1);
164  for (int i = 0; i < len; i++) { result[i] = first + i * step; }
165  return result;
166  }
180  template<typename T>
181  void absMat(matrix<Complex<T>>& src, matrix<T>& dst) {
182  if (src.size != dst.size) {
183  dst.resize(src.size[_X], src.size[_Y]);
184  }
185  for (int i = 0; i < src.size[_X]; i++)
186  {
187  for (int j = 0; j < src.size[_Y]; j++)
188  {
189  dst.mat[i][j] = sqrt(src.mat[i][j]._Val[_RE] * src.mat[i][j]._Val[_RE] + src.mat[i][j]._Val[_IM] * src.mat[i][j]._Val[_IM]);
190  }
191  }
192  }
198  template<typename T>
199  void absMat(matrix<T>& src, matrix<T>& dst) {
200  if (src.size != dst.size) {
201  dst.resize(src.size[_X], src.size[_Y]);
202  }
203  for (int i = 0; i < src.size[_X]; i++)
204  {
205  for (int j = 0; j < src.size[_Y]; j++)
206  {
207  dst.mat[i][j] = abs(src.mat[i][j]);
208  }
209  }
210  }
216  template<typename T>
217  void angleMat(matrix<Complex<T>>& src, matrix<T>& dst) {
218  if (src.size != dst.size) {
219  dst.resize(src.size[_X], src.size[_Y]);
220  }
221  for (int i = 0; i < src.size[_X]; i++)
222  {
223  for (int j = 0; j < src.size[_Y]; j++)
224  {
225  angle(src(i, j), dst(i, j));
226  }
227  }
228  }
234  template<typename T>
235  void conjMat(matrix<Complex<T>>& src, matrix<Complex<T>>& dst) {
236  if (src.size != dst.size) {
237  dst.resize(src.size[_X], src.size[_Y]);
238  }
239  for (int i = 0; i < src.size[_X]; i++)
240  {
241  for (int j = 0; j < src.size[_Y]; j++)
242  {
243  dst(i, j) = src(i, j).conj();
244 
245  }
246  }
247  }
253  template<typename T>
254  void expMat(matrix<Complex<T>>& src, matrix<Complex<T>>& dst) {
255  if (src.size != dst.size) {
256  dst.resize(src.size[_X], src.size[_Y]);
257  }
258  for (int i = 0; i < src.size[_X]; i++)
259  {
260  for (int j = 0; j < src.size[_Y]; j++)
261  {
262  dst.mat[i][j]._Val[_RE] = exp(src.mat[i][j]._Val[_RE]) * cos(src.mat[i][j]._Val[_IM]);
263  dst.mat[i][j]._Val[_IM] = exp(src.mat[i][j]._Val[_RE]) * sin(src.mat[i][j]._Val[_IM]);
264  }
265  }
266  }
272  template<typename T>
273  void expMat(matrix<T>& src, matrix<T>& dst) {
274  if (src.size != dst.size) {
275  dst.resize(src.size[_X], src.size[_Y]);
276  }
277  for (int i = 0; i < src.size[_X]; i++)
278  {
279  for (int j = 0; j < src.size[_Y]; j++)
280  {
281  dst.mat[i][j] = exp(src.mat[i][j]);
282  }
283  }
284  }
290  template<typename T>
291  void meanOfMat(matrix<T>& src, T &dst) {
292  dst = 0;
293  for (int i = 0; i < src.size[_X]; i++)
294  {
295  for (int j = 0; j < src.size[_Y]; j++)
296  {
297  dst += src(i, j);
298  }
299  }
300  dst = dst / (src.size[_X] * src.size[_Y]);
301  }
306  //template<typename T>
307  Real maxOfMat(matrix<Real>& src) {
308  Real max = MIN_REAL;
309  for (int i = 0; i < src.size[_X]; i++)
310  {
311  for (int j = 0; j < src.size[_Y]; j++)
312  {
313  if (src(i, j) > max) max = src(i, j);
314  }
315  }
316  return max;
317  }
318 
319  //template<typename T>
320  Complex<Real> maxOfMat(matrix<Complex<Real>>& src) {
321  Real max = MIN_REAL;
322  for (int i = 0; i < src.size[_X]; i++)
323  {
324  for (int j = 0; j < src.size[_Y]; j++)
325  {
326  if (src(i, j)._Val[_RE] > max) max = src(i, j)._Val[_RE];
327  }
328  }
329  return max;
330  }
335  //template<typename T>
336  Real minOfMat(matrix<Real>& src) {
337  Real min = MAX_REAL;
338  for (int i = 0; i < src.size[_X]; i++)
339  {
340  for (int j = 0; j < src.size[_Y]; j++)
341  {
342  if (src(i, j) < min) min = src(i, j);
343  }
344  }
345  return min;
346  }
347  //template<typename T>
348  Complex<Real> minOfMat(matrix<Complex<Real>>& src) {
349  Real min = MAX_REAL;
350  for (int i = 0; i < src.size[_X]; i++)
351  {
352  for (int j = 0; j < src.size[_Y]; j++)
353  {
354  if (src(i, j)._Val[_RE] < min) min = src(i, j)._Val[_RE];
355  }
356  }
357  return min;
358  }
359 
360  //template<typename T>
361  //void ophSig::meshgrid(vector<T>& src1, vector<T>& src2, matrix<T>& dst1, matrix<T>& dst2)
362  //{
363  // int src1_total = static_cast<int>(src1.size());
364  // int src2_total = static_cast<int>(src2.size());
365 
366  // dst1.resize(src2_total, src1_total);
367  // dst2.resize(src2_total, src1_total);
368  // for (int i = 0; i < src1_total; i++)
369  // {
370  // for (int j = 0; j < src2_total; j++)
371  // {
372  // dst1(j, i) = src1.at(i);
373  // dst2(j, i) = src2.at(j);
374  // }
375  // }
376  //}
377  template<typename T>
378  void normalizeMat(matrix<T>& src, matrix<T>& dst) {
379  if (src.size != dst.size) {
380  dst.resize(src.size[_X], src.size[_Y]);
381  }
382  //여기문제임
383  Real min = minOfMat(src);
384  Real max = maxOfMat(src);
385 
386  for (size_t i = 0; i < src.size[_X]; i++)
387  {
388  for (size_t j = 0; j < src.size[_Y]; j++)
389  {
390  dst(i, j) = (src(i, j) - min) / max;
391  }
392  }
393  }
394 
395  //template<typename T>
396  void ophSig::fftShift(matrix<Complex<Real>> &src, matrix<Complex<Real>> &dst)
397  {
398  if (src.size != dst.size) {
399  dst.resize(src.size[_X], src.size[_Y]);
400  }
401  int xshift = src.size[_X] / 2;
402  int yshift = src.size[_Y] / 2;
403  for (int i = 0; i < src.size[_X]; i++)
404  {
405  int ii = (i + xshift) % src.size[_X];
406  for (int j = 0; j < src.size[_Y]; j++)
407  {
408  int jj = (j + yshift) % src.size[_Y];
409  dst.mat[ii][jj]._Val[_RE] = src.mat[i][j].real();
410  dst.mat[ii][jj]._Val[_IM] = src.mat[i][j].imag();
411  }
412  }
413  }
414 
415 
416  template<typename T>
417  void fft1(matrix<Complex<T>> &src, matrix<Complex<T>> &dst, int sign = OPH_FORWARD , uint flag = OPH_ESTIMATE );
425  template<typename T>
426  void fft2(matrix<Complex<T>> &src, matrix<Complex<T>> &dst, int sign = OPH_FORWARD, uint flag = OPH_ESTIMATE);
427  /*
428  * @brief Function for Shift zero-frequency component to center of spectrum
429  * @param src input signal
430  * @param dst output signal
431  */
432  //template<typename T>
433  //void fftShift(matrix<Complex<T>> &src, matrix<Complex<T>> &dst);
434 
440  //virtual bool readConfig(const char* fname);
441  bool readConfig(const char* fname);
445 
498 
506  bool sigConvertOffaxis(Real angleX, Real angleY);
507  bool cvtOffaxis_CPU(Real angleX, Real angleY);
508  void cvtOffaxis_GPU(Real angleX, Real angleY);
509 
513 
613 
620  bool sigConvertHPO(Real depth, Real_t redRate);
621  bool sigConvertHPO_CPU(Real depth, Real_t redRate);
622  bool sigConvertHPO_GPU(Real depth, Real_t redRate);
626 
696 
703  bool sigConvertCAC(double red, double green, double blue);
704  bool sigConvertCAC_CPU(double red, double green, double blue);
705  bool sigConvertCAC_GPU(double red, double green, double blue);
710  bool propagationHolo(float depth);
711  bool propagationHolo_CPU(float depth);
712  bool propagationHolo_GPU(float depth);
718  OphComplexField propagationHolo(OphComplexField complexH, float depth);
719 
723 
802 
810  double sigGetParamAT();
811  double sigGetParamAT_CPU();
812  double sigGetParamAT_GPU();
813 
817 
844 
856  double sigGetParamSF(float zMax, float zMin, int sampN, float th);
857  double sigGetParamSF_CPU(float zMax, float zMin, int sampN, float th);
858  double sigGetParamSF_GPU(float zMax, float zMin, int sampN, float th);
862 
868 
877  bool getComplexHFromPSDH(const char* fname0, const char* fname90, const char* fname180, const char* fname270);
878 
879  void setMode(bool is_CPU);
880 
881 
882 
883 
884 
885 
886  void cField2Buffer(matrix<Complex<Real>>& src, Complex<Real> **dst, int nx, int ny);
887  void ColorField2Buffer(matrix<Complex<Real>>& src, Complex<Real> **dst, int nx, int ny);
888 };
889 
890 #endif // !__ophSig_h
891 
Real_t * _foc
Definition: ophSig.h:124
Complex< Real > maxOfMat(matrix< Complex< Real >> &src)
Definition: ophSig.h:320
void abs(const oph::Complex< T > &src, oph::Complex< T > &dst)
Definition: function.h:112
Abstract class.
Definition: Openholo.h:83
void angleMat(matrix< Complex< T >> &src, matrix< T > &dst)
Function for extracts Complex phase value.
Definition: ophSig.h:217
#define SIG_DLL
Definition: ophSig.h:59
Real_t _radius
Definition: ophSig.h:123
void meanOfMat(matrix< T > &src, T &dst)
Function for returns exponent e(x), where x is real number.
Definition: ophSig.h:291
float Real
Definition: typedef.h:55
fftw_plan fwd_plan
Definition: ophSig.h:110
void conjMat(matrix< Complex< T >> &src, matrix< Complex< T >> &dst)
Function for extracts Complex conjugate value.
Definition: ophSig.h:235
#define OPH_ESTIMATE
Definition: define.h:76
#define _Y
Definition: define.h:84
#define _IM
Definition: complex.h:57
ophSigConfig _cfgSig
Definition: ophSig.h:108
#define _X
Definition: define.h:80
int _wavelength_num
Definition: ophSig.h:118
Complex< Real > minOfMat(matrix< Complex< Real >> &src)
Definition: ophSig.h:348
int cols
Definition: ophSig.h:63
void expMat(matrix< Complex< T >> &src, matrix< Complex< T >> &dst)
Function for returns exponent ex.
Definition: ophSig.h:254
int rows
Definition: ophSig.h:64
Real_t NA
Definition: ophSig.h:67
OphComplexField * ComplexH
Definition: ophSig.h:109
int wavelength_num
Definition: ophSig.h:69
bool is_CPU
Definition: ophSig.h:107
#define _RE
Definition: complex.h:54
Definition: ophSig.h:100
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:416
Real_t z
Definition: ophSig.h:68
Real_t height
Definition: ophSig.h:66
oph::matrix< Complex< Real > > OphComplexField
Definition: mat.h:421
void normalizeMat(matrix< T > &src, matrix< T > &dst)
Definition: ophSig.h:378
void absMat(matrix< T > &src, matrix< T > &dst)
Function for extracts real absolute value.
Definition: ophSig.h:199
double Real_t
Definition: typedef.h:56
void fftShift(matrix< Complex< Real >> &src, matrix< Complex< Real >> &dst)
Definition: ophSig.h:396
void absMat(matrix< Complex< T >> &src, matrix< T > &dst)
Function for Linear interpolation 1D.
Definition: ophSig.h:181
virtual bool loadAsOhc(const char *fname)
Function to read OHC file.
Definition: Openholo.cpp:249
Real minOfMat(matrix< Real > &src)
Function for extracts min of matrix.
Definition: ophSig.h:336
void expMat(matrix< T > &src, matrix< T > &dst)
Function for returns exponent e(x), where x is complex number.
Definition: ophSig.h:273
void angle(const std::vector< Complex< T >> &src, std::vector< T > &dst)
Definition: function.h:153
#define MAX_REAL
Definition: define.h:136
virtual bool saveAsOhc(const char *fname)
Function to write OHC file
Definition: Openholo.cpp:228
Real maxOfMat(matrix< Real > &src)
Function for extracts max of matrix.
Definition: ophSig.h:307
#define MIN_REAL
Definition: define.h:135
Real_t width
Definition: ophSig.h:65
#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:381
vector< T > linspace(T first, T last, int len)
Generate linearly spaced vector.
Definition: ophSig.h:160
unsigned int uint
Definition: typedef.h:62
virtual void ophFree(void)
Pure virtual function for override in child classes.
Definition: Openholo.cpp:589