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

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