OpendTect-6_4  6.4
agc.h
Go to the documentation of this file.
1 #ifndef agc_h
2 #define agc_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: K. Tingdahl
9  Date: March 2008
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "sorting.h"
16 #include "thread.h"
17 #include "paralleltask.h"
18 #include "valseries.h"
19 
24 template <class T>
25 mClass(Algo) AGC : public ParallelTask
26 {
27 public:
28  AGC();
29  ~AGC();
30  void setInput(const ValueSeries<T>&,int sz);
31  void setOutput(ValueSeries<T>&);
33 
34  void setSampleGate(const Interval<int>&);
35  const Interval<int>& getSampleGate() const;
36 
37  void setMuteFraction(float lvmf) { mutefraction_ = lvmf;}
39  float getMuteFraction() const { return mutefraction_;}
40 
41  bool doPrepare(int nrthreads);
42 
43 protected:
44 
45  void computeEnergyMute();
46  bool doWork(od_int64,od_int64,int);
47  int minThreadSize() const { return 200; }
48  od_int64 nrIterations() const { return size_; }
49 
57 
60 
61 };
62 
63 
64 template <class T> inline
66  : input_( 0 )
67  , output_( 0 )
68  , samplerg_(-5,5)
69  , lock_( 0 )
70  , size_( 0 )
71  , mutefraction_( 0 )
72 {}
73 
74 
75 template <class T> inline
77 { delete lock_; }
78 
79 
80 template <class T> inline
81 void AGC<T>::setInput( const ValueSeries<T>& nvs, int sz )
82 { input_ = &nvs; size_ = sz; }
83 
84 
85 template <class T> inline
87 { output_ = &nvs; }
88 
89 
90 template <class T> inline
92 { samplerg_ = nrg; }
93 
94 
95 template <class T> inline
97 { return samplerg_; }
98 
99 
100 template <class T> inline
101 bool AGC<T>::doPrepare( int nrthreads )
102 {
103  if ( !input_ || !output_ ||
104  (output_->reSizeable() && !output_->setSize(size_)) )
105  return false;
106 
107  energies_.setSize( mCast(int,size_), mUdf(T) );
108 
109  if ( nrthreads )
110  {
111  if ( !lock_ ) lock_ = new Threads::ConditionVar;
112  threadsinenergycalc_ = nrthreads;
113  }
114  else
115  { delete lock_; lock_ = 0; }
116 
117  return true;
118 }
119 
120 
121 template <class T> inline
123 {
124  energymute_ = 0;
125  if ( mIsUdf(mutefraction_) ||
126  mIsZero(mutefraction_,1e-5) )
127  return;
128 
129  const od_int64 sample = mNINT64(size_*mutefraction_);
130  if ( sample<0 || sample>=size_ )
131  return;
132 
133  mAllocLargeVarLenArr( T, energies, size_ );
134  OD::sysMemCopy( energies.ptr(), energies_.arr(), size_*sizeof(T) );
135  sortFor( energies.ptr(), size_, sample );
136  energymute_ = energies[sample];
137 }
138 
139 
140 template <class T> inline
141 bool AGC<T>::doWork( od_int64 start, od_int64 stop, int threadidx )
142 {
143  for ( int idx=0; idx<=stop; idx++ )
144  {
145  const T value = input_->value(idx);
146  energies_[idx] = mIsUdf( value ) ? mUdf(T) : value*value;
147  }
148 
149  if ( lock_ )
150  {
151  lock_->lock();
153  if ( !threadsinenergycalc_ )
154  lock_->signal(true);
155  else while ( threadsinenergycalc_ )
156  lock_->wait();
157 
158  if ( !threadidx )
159  {
161  lock_->signal(true);
163  }
164  else while ( threadsinenergycalc_!=-1 )
165  lock_->wait();
166 
167  lock_->unLock();
168  }
169  else
170  {
172  }
173 
174  for ( int idx=mCast(int,start); idx<=stop; idx++ )
175  {
176  int nrenergies = 0;
177  float energysum = 0;
178  for ( int energyidx=idx+samplerg_.start;
179  energyidx<=idx+samplerg_.stop;
180  energyidx++ )
181  {
182  if ( energyidx<0 || energyidx>=size_ )
183  continue;
184 
185  const T energy = energies_[energyidx];
186  if ( mIsUdf(energy) )
187  continue;
188 
189  energysum += energy;
190  nrenergies++;
191  }
192 
193  if ( nrenergies ) energysum /= nrenergies;
194 
195  float outputval = 0;
196  if ( energysum>=energymute_ && energysum>0 )
197  {
198  const T inpval = input_->value(idx);
199  outputval = mIsUdf( inpval )
200  ? inpval : inpval/Math::Sqrt( energysum );
201  }
202 
203  output_->setValue( idx, outputval );
204  }
205 
206  return true;
207 }
208 
209 #endif
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:287
Is an object that faciliates many threads to wait for something to happen.
Definition: thread.h:108
void computeEnergyMute()
Definition: agc.h:122
float getMuteFraction() const
Definition: agc.h:39
void sortFor(T *arr, I sz, I itarget)
Definition: sorting.h:238
od_int64 nrIterations() const
Definition: agc.h:48
#define mIsZero(x, eps)
Definition: commondefs.h:53
#define mCast(tp, v)
Definition: commondefs.h:124
#define od_int64
Definition: plftypes.h:36
void setMuteFraction(float lvmf)
The lowest fraction will be muted.
Definition: agc.h:37
int threadsinenergycalc_
Definition: agc.h:58
bool doWork(od_int64, od_int64, int)
Definition: agc.h:141
TypeSet< T > energies_
Definition: agc.h:55
T energymute_
Definition: agc.h:56
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:66
const T inpval
Definition: arrayndalgo.h:1661
float mutefraction_
Definition: agc.h:54
Threads::ConditionVar * lock_
Definition: agc.h:59
void setSampleGate(const Interval< int > &)
Definition: agc.h:91
AGC()
Definition: agc.h:65
Set of (small) copyable elements.
Definition: commontypes.h:30
ValueSeries< T > * output_
Definition: agc.h:52
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
void setInput(const ValueSeries< T > &, int sz)
Definition: agc.h:81
const Interval< int > & getSampleGate() const
Definition: agc.h:96
Interface to a series of values.
Definition: odmemory.h:17
#define mNINT64(x)
Definition: commondefs.h:46
const ValueSeries< T > * input_
Definition: agc.h:50
bool doPrepare(int nrthreads)
Definition: agc.h:101
void signal(bool all)
void sysMemCopy(void *, const void *, int64_t)
void setOutput(ValueSeries< T > &)
Output can be the same as input.
Definition: agc.h:86
T stop
Definition: ranges.h:93
Computes an AGC over a ValueSeries.
Definition: agc.h:25
#define mAllocLargeVarLenArr(type, varnm, __size)
Definition: varlenarray.h:31
T start
Definition: ranges.h:92
~AGC()
Definition: agc.h:76
int minThreadSize() const
Definition: agc.h:47
#define mClass(module)
Definition: commondefs.h:164
float Sqrt(float)
od_int64 size_
Definition: agc.h:51
Interval< int > samplerg_
Definition: agc.h:53

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