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

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