OpendTect-6_4  6.4
genericnumer.h
Go to the documentation of this file.
1 #ifndef genericnumer_h
2 #define genericnumer_h
3 
4 /*@+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Kristofer Tingdahl
9  Date: 07-10-1999
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 
14 */
15 
16 #include "algomod.h"
17 #include "mathfunc.h"
18 #include <math.h>
19 
28 template <class A, class B, class C>
29 inline void GenericConvolve( int lx, int ifx, const A& x,
30  int ly, int ify, const B& y,
31  int lz, int ifz, C& z)
32 {
33  int ilx=ifx+lx-1,ily=ify+ly-1,ilz=ifz+lz-1,i,j,jlow,jhigh;
34  float sum;
35 
36  for ( i=ifz; i<=ilz; ++i )
37  {
38  jlow = i-ily;
39  if ( jlow < ifx ) jlow = ifx;
40 
41  jhigh = i-ify;
42  if ( jhigh > ilx ) jhigh = ilx;
43 
44  for ( j=jlow,sum=0.0; j<=jhigh; ++j )
45  {
46  if( !mIsUdf(x[j-ifx]) && !mIsUdf(y[i-j-ify]) )
47  sum += x[j-ifx]*y[i-j-ify];
48  }
49 
50  z[i-ifz] = sum;
51  }
52 }
53 
54 
55 template <class A, class B, class C>
56 inline void GenericConvolveNoUdf( int lx, int ifx, const A& x,
57  int ly, int ify, const B& y,
58  int lz, int ifz, C& z)
59 {
60  int ilx=ifx+lx-1,ily=ify+ly-1,ilz=ifz+lz-1,i,j,jlow,jhigh;
61  float sum;
62 
63  for ( i=ifz; i<=ilz; ++i )
64  {
65  jlow = i-ily;
66  if ( jlow < ifx ) jlow = ifx;
67 
68  jhigh = i-ify;
69  if ( jhigh > ilx ) jhigh = ilx;
70 
71  for ( j=jlow,sum=0.0; j<=jhigh; ++j )
72  sum += x[j-ifx]*y[i-j-ify];
73 
74  z[i-ifz] = sum;
75  }
76 }
77 
78 
79 
83 template <class A, class B>
84 inline float similarity( const A& a, const B& b, int sz, bool normalize=false,
85  int firstposa=0, int firstposb=0 )
86 {
87  float val1, val2;
88  double sqdist = 0, sq1 = 0, sq2 = 0;
89 
90  double meana = mUdf(double), stddeva = mUdf(double);
91  double meanb = mUdf(double), stddevb = mUdf(double);
92 
93  if ( normalize )
94  {
95  if ( sz==1 ) normalize = false;
96  else
97  {
98  double asum=0,bsum=0;
99  for ( int idx=0; idx<sz; idx++ )
100  {
101  asum += a[firstposa+idx];
102  bsum += b[firstposb+idx];
103  }
104 
105  meana = asum/sz;
106  meanb = bsum/sz;
107 
108  asum = 0;
109  bsum = 0;
110  for ( int idx=0; idx<sz; idx++ )
111  {
112  const double adiff = a[firstposa+idx]-meana;
113  const double bdiff = b[firstposb+idx]-meanb;
114  asum += adiff*adiff;
115  bsum += bdiff*bdiff;
116  }
117 
118  stddeva = Math::Sqrt(asum/(sz-1));
119  stddevb = Math::Sqrt(bsum/(sz-1));
120 
121  if ( mIsZero(stddeva,mDefEps) || mIsZero(stddevb,mDefEps) )
122  normalize=false;
123  }
124  }
125 
126  int curposa = firstposa;
127  int curposb = firstposb;
128 
129  for ( int idx=0; idx<sz; idx++ )
130  {
131  val1 = normalize ? (float) ( (a[curposa]-meana)/stddeva ) : a[curposa];
132  val2 = normalize ? (float) ( (b[curposb]-meanb)/stddevb ) : b[curposb];
133  if ( mIsUdf(val1) || mIsUdf(val2) )
134  return mUdf(float);
135 
136  sq1 += val1 * val1;
137  sq2 += val2 * val2;
138  sqdist += (val1-val2) * (val1-val2);
139 
140  curposa ++;
141  curposb ++;
142  }
143 
144  if ( mIsZero(sq1,mDefEps) && mIsZero(sq2,mDefEps) )
145  return 1;
146 
147  if ( mIsZero(sq1,mDefEps) || mIsZero(sq2,mDefEps) )
148  return 0;
149 
150  const float rt =
151  (float) ( Math::Sqrt(sqdist) / (Math::Sqrt(sq1) + Math::Sqrt(sq2)) );
152  return 1 - rt;
153 }
154 
155 
156 mGlobal(Algo) float similarity(const FloatMathFunction&,
157  const FloatMathFunction&,
158  float x1, float x2, float dist, int sz, bool normalize );
159 
160 
161 mGlobal(Algo) float semblance( const ObjectSet<float>& signals,
162  const Interval<int>& );
163 
164 mGlobal(Algo) float semblance( const ObjectSet<float>& signals,int signalsize,
165  const TypeSet<float>& signalstarts,
166  const Interval<int>& gate );
167 
168 mGlobal(Algo) double LanczosKernel( int size, double x );
169 
176 mGlobal(Algo) bool findValue(const FloatMathFunction&,float x1,float x2,
177  float& res,float targetval = 0,float tol=1e-5);
178 
179 
187 mGlobal(Algo) float findValueInAperture(const FloatMathFunction&,float startx,
188  const Interval<float>& aperture,float dx,float target=0,
189  float tol=1e-5);
190 
198 mGlobal(Algo) float findExtreme(const FloatMathFunction&,bool minima,float x1,
199  float x2,float tol = 1e-5);
200 
201 
202 template <class A, class B> inline
203 void reSample( const FloatMathFunction& input, const A& samplevals,
204  B& output, int nrsamples )
205 {
206  for ( int idx=0; idx<nrsamples; idx++ )
207  {
208  const float sampleval = samplevals[idx];
209  output[idx] = Values::isUdf(sampleval) ? mUdf(float) :
210  input.getValue(sampleval);
211  }
212 }
213 
214 
229 template <class A, class B, class C>
230 inline void genericCrossCorrelation( int lx, int ifx, const A& x,
231  int ly, int ify, const B& y,
232  int lz, int ifz, C& z)
233 {
234  mAllocLargeVarLenArr( float, xreversed, lx );
235 
236  for ( int i=0,j=lx-1; i<lx; ++i,--j)
237  xreversed[i] = x[j];
238  GenericConvolve( lx, 1-ifx-lx, xreversed, ly, ify, y, lz, ifz, z );
239 }
240 
241 
242 template <class A>
243 inline void reverseArray( A* in, int sz, A* out=0 )
244 {
245  if ( out )
246  for ( int idx=0; idx<sz; idx++ )
247  out[idx] = in[sz-1-idx];
248  else
249  {
250  mAllocVarLenArr( A, tmparr, sz/2 );
251  for ( int idx=0; idx<sz/2; idx++ )
252  {
253  tmparr[idx] = in[idx];
254  int opsamp = sz-1-idx;
255  in[idx] = in[opsamp];
256  in[opsamp] = tmparr[idx];
257  }
258  }
259 }
260 
261 
262 #endif
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:287
double LanczosKernel(int size, double x)
#define mGlobal(module)
Definition: commondefs.h:163
#define mIsZero(x, eps)
Definition: commondefs.h:53
void GenericConvolve(int lx, int ifx, const A &x, int ly, int ify, const B &y, int lz, int ifz, C &z)
Definition: genericnumer.h:29
Mathematical function.
Definition: mathfunc.h:61
void genericCrossCorrelation(int lx, int ifx, const A &x, int ly, int ify, const B &y, int lz, int ifz, C &z)
Definition: genericnumer.h:230
float findExtreme(const FloatMathFunction &, bool minima, float x1, float x2, float tol=1e-5)
const T val2
Definition: arrayndalgo.h:1699
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
float similarity(const A &a, const B &b, int sz, bool normalize=false, int firstposa=0, int firstposb=0)
Definition: genericnumer.h:84
float findValueInAperture(const FloatMathFunction &, float startx, const Interval< float > &aperture, float dx, float target=0, float tol=1e-5)
#define mAllocVarLenArr(type, varnm, __size)
Definition: varlenarray.h:54
#define mDefEps
Definition: commondefs.h:58
void reverseArray(A *in, int sz, A *out=0)
Definition: genericnumer.h:243
bool findValue(const FloatMathFunction &, float x1, float x2, float &res, float targetval=0, float tol=1e-5)
void GenericConvolveNoUdf(int lx, int ifx, const A &x, int ly, int ify, const B &y, int lz, int ifz, C &z)
Definition: genericnumer.h:56
#define mAllocLargeVarLenArr(type, varnm, __size)
Definition: varlenarray.h:31
const T val1
Definition: arrayndalgo.h:1699
bool isUdf(const T &t)
Definition: undefval.h:243
virtual RT getValue(PT) const =0
float Sqrt(float)
void reSample(const FloatMathFunction &input, const A &samplevals, B &output, int nrsamples)
Definition: genericnumer.h:203
float semblance(const ObjectSet< float > &signals, const Interval< int > &)

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019