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

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