All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Member Functions | Protected Member Functions | Protected Attributes
GeopsyCore::SignalTemplate< sampleType > Class Template Reference

Template class to store signals. More...

#include <SignalTemplate.h>

Inheritance diagram for GeopsyCore::SignalTemplate< sampleType >:
QGpCoreTools::CacheItem

List of all members.

Public Member Functions

int add (const SignalTemplate< sampleType > *sig, int sigStart=0, int thisStart=0, int nToCopy=-1, bool checkInvalid=false, sampleType invalidValue=0)
sampleType average (int startAt, int nSamples) const
sampleType average2 (int startAt, int nSamples) const
const sampleType * constLockSamples (char *file, int line) const
virtual const sampleType * constLockSamples () const
void copySamplesFrom (const SignalTemplate< sampleType > *sig)
bool copySamplesFrom (const SignalTemplate< sampleType > *sig, int sigStart, int thisStart, int nToCopy)
double dataSizeMb () const
int findValue (sampleType value, int from, bool whileNotFound=false) const
void freeSamples ()
void initValues (sampleType value, int startAt, int nSamples)
sampleType * lockSamples (char *file, int line)
virtual sampleType * lockSamples ()
sampleType maximum () const
void multiply (const SignalTemplate< sampleType > *sig)
void multiply (sampleType value)
int nSamples () const
void range (sampleType &min, sampleType &max) const
virtual void setNSamples (int n)
 SignalTemplate ()
 SignalTemplate (int n)
 SignalTemplate (const SignalTemplate< sampleType > &o)
void subtractSignal (const SignalTemplate< sampleType > *sig)
void unlockSamples (char *file, int line) const
virtual void unlockSamples () const
sampleType variance (int startAt, int nSamples) const
virtual ~SignalTemplate ()

Protected Member Functions

void adjustLimits (const SignalTemplate< sampleType > *sig, int &sigStart, int &thisStart, int &nToCopy) const
virtual int dataSize () const
virtual bool isAllocated () const
sampleType * samples () const
void setSamples (sampleType *s) const

Protected Attributes

int _nSamples

Detailed Description

template<class sampleType>
class GeopsyCore::SignalTemplate< sampleType >

Template class to store signals.

Samples can be any number with arithmetic operators (int, float, double, or Complex). For double and complex numbers, the most appropriate object might be DoubleSignal and ComplexSignal, respectively.

All operations on signals assume that the signal is not locked. You can lock signal before but it is useless.


Constructor & Destructor Documentation

template<class sampleType >
GeopsyCore::SignalTemplate< sampleType >::SignalTemplate ( )

Set the number of samples as 0. Before allocating signals (first call to LOCK_SAMPLES), be sure to set the correct number of samples with setNSamples().

{
  _samples=0;
  _nSamples=0;
}
template<class sampleType >
GeopsyCore::SignalTemplate< sampleType >::SignalTemplate ( int  n)

Set the number of samples as n.

{
  _samples=0;
  _nSamples=n;
}
template<class sampleType>
GeopsyCore::SignalTemplate< sampleType >::SignalTemplate ( const SignalTemplate< sampleType > &  o)

Set the same number of samples as o. No sample values are copied.

References GeopsyCore::SignalTemplate< sampleType >::_nSamples.

                                                                              :
    CacheItem(o)
{
  _nSamples=o._nSamples;
  _samples=0;
}
template<class sampleType >
GeopsyCore::SignalTemplate< sampleType >::~SignalTemplate ( ) [virtual]

Frees internal sample vector.

References GeopsyCore::GeopsyCoreEngine::cache(), QGpCoreTools::Cache::free(), and GeopsyCore::geopsyCore.

{
  geopsyCore->cache()->free(this, false);
}

Member Function Documentation

template<class sampleType>
int GeopsyCore::SignalTemplate< sampleType >::add ( const SignalTemplate< sampleType > *  sig,
int  sigStart = 0,
int  thisStart = 0,
int  nToCopy = -1,
bool  checkInvalid = false,
sampleType  invalidValue = 0 
)

Add nToCopy samples of this signal with the sig samples (sample by sample) starting at sigStart index from beginning of sig. Samples are copied to this signal starting at index thisStart. if checkInvalid is true sample values are check for invalid value invalidValue. All matching samples are discarded from the sum.

The number of summed samples is returned. It is equal to nToCopy is no invalid value is found and if signals sufficiently overlap (from indexes sigStart and thisStart).

References CONST_LOCK_SAMPLES, LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

Referenced by HVStationSignals::horizontal().

{
  TRACE;
  if(nToCopy==-1) nToCopy=nSamples();
  adjustLimits(sig, sigStart, thisStart, nToCopy);
  if(nToCopy<=0) return 0;
  int nCommon=0;
  CacheProcess cp;
  cp << this << sig;
  LOCK_SAMPLES(sampleType, thisSamples, this)
    CONST_LOCK_SAMPLES(sampleType, sigSamples, sig)
      sampleType * thisPtr=thisSamples + thisStart;
      const sampleType * sigPtr=sigSamples + sigStart;
      sampleType * endPtr=thisPtr + nToCopy;
      if(checkInvalid) {
        for( ;thisPtr < endPtr;thisPtr++, sigPtr++ ) {
          if(( *thisPtr)==invalidValue) ( *thisPtr)=( *sigPtr);
          else {
            nCommon++;
            ( *thisPtr) += ( *sigPtr);
          }
        }
      } else {
        for( ;thisPtr < endPtr;thisPtr++, sigPtr++ )
          ( *thisPtr) += ( *sigPtr);
      }
    UNLOCK_SAMPLES(sig)
  UNLOCK_SAMPLES(this)
  return nCommon;
}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::adjustLimits ( const SignalTemplate< sampleType > *  sig,
int &  sigStart,
int &  thisStart,
int &  nToCopy 
) const [protected]

Internal function to adjust sigStart, thisStart and nToCopy to fit only the common part of these two signal.

References GeopsyCore::SignalTemplate< sampleType >::_nSamples, and TRACE.

{
  TRACE;
  // If signal have no common time range return
  if(sigStart >= sig->_nSamples ||
       thisStart >= _nSamples ||
       sigStart + nToCopy < 0 ||
       thisStart + nToCopy < 0) {
     nToCopy=0;
     return;
  }
  if(sigStart < 0) {
    nToCopy += sigStart;
    thisStart -= sigStart;
    sigStart=0;
  }
  if(thisStart < 0) {
    nToCopy += thisStart;
    sigStart -= thisStart;
    thisStart=0;
  }
  if(thisStart + nToCopy >= _nSamples) nToCopy=_nSamples - thisStart;
  if(sigStart + nToCopy >= sig->_nSamples) nToCopy=sig->_nSamples - sigStart;
}
template<class sampleType >
sampleType GeopsyCore::SignalTemplate< sampleType >::average ( int  startAt,
int  nSamples 
) const

Returns the average of the sample values starting at startAt and for only nSamples.

References CONST_LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

{
  TRACE;
  if(startAt >= _nSamples) return 0;
  int endAt=startAt + nSamples;
  if(endAt > _nSamples) endAt=_nSamples;
  if(startAt < 0) startAt=0;
  sampleType average=0;
  CONST_LOCK_SAMPLES(sampleType, thisSamples, this)
    for(int i=startAt;i < endAt;i++ ) average += thisSamples[ i ];
  UNLOCK_SAMPLES(this)
  return average/nSamples;
}
template<class sampleType >
sampleType GeopsyCore::SignalTemplate< sampleType >::average2 ( int  startAt,
int  nSamples 
) const

Returns the average of the squared sample values starting at startAt and for only nSamples. Note that the squared sum is divided by (nSamples -1). To compute the standard deviation, combine this result with average():

int n=s->nSamples(); stddev=s->average2(0,n) - n/(n-1)*s->average(0,n);

References CONST_LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

{
  TRACE;
  if(startAt >= _nSamples) return 0;
  int endAt=startAt + nSamples;
  if(endAt > _nSamples) endAt=_nSamples;
  if(startAt < 0) startAt=0;
  sampleType average2=0;
  CONST_LOCK_SAMPLES(sampleType, thisSamples, this)
    for(int i=startAt;i < endAt;i++ ) {
      average2 += thisSamples[ i ] * thisSamples[ i ];
    }
  UNLOCK_SAMPLES(this)
  return average2/(nSamples-1);
}
template<class sampleType>
const sampleType* GeopsyCore::SignalTemplate< sampleType >::constLockSamples ( char *  file,
int  line 
) const

Reimplemented in GeopsyCore::Signal.

template<class sampleType >
const sampleType * GeopsyCore::SignalTemplate< sampleType >::constLockSamples ( ) const [virtual]

General function to be called before using the samples

Reimplemented in GeopsyCore::Signal.

References GeopsyCore::GeopsyCoreEngine::cache(), GeopsyCore::geopsyCore, and QGpCoreTools::Cache::makeAvailable().

{
  lockData();
  if(isAllocated() || geopsyCore->cache()->makeAvailable(this)) {
    return _samples;
  } else {
    unlockData();
    return 0;
  }
}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::copySamplesFrom ( const SignalTemplate< sampleType > *  sig)

References GeopsyCore::SignalTemplate< sampleType >::_nSamples, CONST_LOCK_SAMPLES, LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

{
  TRACE;
  if(sig->_nSamples!=_nSamples) return ;
  CacheProcess cp;
  cp << this << sig;
  LOCK_SAMPLES(sampleType, thisSamples, this)
    CONST_LOCK_SAMPLES(sampleType, sigSamples, sig)
      memcpy(thisSamples, sigSamples, _nSamples * sizeof(sampleType) );
    UNLOCK_SAMPLES(sig)
  UNLOCK_SAMPLES(this)
}
template<class sampleType>
bool GeopsyCore::SignalTemplate< sampleType >::copySamplesFrom ( const SignalTemplate< sampleType > *  sig,
int  sigStart,
int  thisStart,
int  nToCopy 
)

References CONST_LOCK_SAMPLES, LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

{
  TRACE;
  adjustLimits(sig, sigStart, thisStart, nToCopy);
  if(nToCopy<=0) return false;
  CacheProcess cp;
  cp << this << sig;
  LOCK_SAMPLES(sampleType, thisSamples, this)
    CONST_LOCK_SAMPLES(sampleType, sigSamples, sig)
      memcpy(thisSamples + thisStart, sigSamples + sigStart, nToCopy * sizeof(sampleType) );
    UNLOCK_SAMPLES(sig)
  UNLOCK_SAMPLES(this)
  return true;
}
template<class sampleType>
int GeopsyCore::SignalTemplate< sampleType >::dataSize ( ) const [inline, protected, virtual]

Returns the memory space in bytes used by the internal sample vector. Used by Cache.

Implements QGpCoreTools::CacheItem.

Reimplemented in GeopsyCore::DynamicSignal.

{return _nSamples * sizeof(sampleType);}
template<class sampleType>
double GeopsyCore::SignalTemplate< sampleType >::dataSizeMb ( ) const [inline]

Returns the memory space in Nbytes used by the internal sample vector. Used by Cache.

Referenced by GeopsyCore::SignalHeaderObject::sampleSize().

{return (double) dataSize()/(1024.0 * 1024.0);}
template<class sampleType>
int GeopsyCore::SignalTemplate< sampleType >::findValue ( sampleType  value,
int  from,
bool  whileNotFound = false 
) const

Scans signal starting at from for value value. If whileNotFound is true, scan is runs until finding value. On the contrary, if it is false, the scan stops if the signal samples differ from value. In both cases, if the end of the signal is reached, the total number of samples is returned.

References CONST_LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

{
  TRACE;
  int i=-1;
  CONST_LOCK_SAMPLES(sampleType, thisSamples, this)
    if(whileNotFound) {
      for(i=from;i < _nSamples;i++ ) {
        if(thisSamples[ i ]!=value) {
          break;
        }
      }
    } else {
      for(i=from;i < _nSamples;i++ ) {
        if(thisSamples[ i ]==value) {
          break;
        }
      }
    }
  UNLOCK_SAMPLES(this)
  return i;
}
template<class sampleType >
void GeopsyCore::SignalTemplate< sampleType >::freeSamples ( ) [inline]

Frees the internal sample vector. This cannot be called between LOCK_SAMPLES and UNLOCK_SAMPLES.

References GeopsyCore::GeopsyCoreEngine::cache(), QGpCoreTools::Cache::free(), and GeopsyCore::geopsyCore.

{
  lockData();
  if(isAllocated()) {
    geopsyCore->cache()->free(this, false);
  }
  unlockData();
}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::initValues ( sampleType  value,
int  startAt,
int  nSamples 
)

Set sample values to value, starting at startAt and for nSamples only.

References LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

Referenced by DampingResults::compute(), GeopsyCore::SparseKeepSignal::initValues(), and StackWeights::stack().

{
  TRACE;
  if(startAt >= _nSamples) return ;
  int endAt=startAt + nSamples;
  if(endAt > _nSamples) endAt=_nSamples;
  if(startAt < 0) startAt=0;
  LOCK_SAMPLES(sampleType, thisSamples, this)
    for(int i=startAt;i < endAt;i++ ) thisSamples[ i ]=value;
  UNLOCK_SAMPLES(this)
}
template<class sampleType>
virtual bool GeopsyCore::SignalTemplate< sampleType >::isAllocated ( ) const [inline, protected, virtual]

Implement this function to return true is data is allocated.

Implements QGpCoreTools::CacheItem.

{return _samples;}
template<class sampleType>
sampleType* GeopsyCore::SignalTemplate< sampleType >::lockSamples ( char *  file,
int  line 
)

Reimplemented in GeopsyCore::Signal.

template<class sampleType >
sampleType * GeopsyCore::SignalTemplate< sampleType >::lockSamples ( ) [virtual]

General function to be called before using the samples

Reimplemented in GeopsyCore::Signal.

References GeopsyCore::GeopsyCoreEngine::cache(), GeopsyCore::geopsyCore, and QGpCoreTools::Cache::makeAvailable().

{
  lockData();
  if(isAllocated() || geopsyCore->cache()->makeAvailable(this)) {
    return _samples;
  } else {
    unlockData();
    return 0;
  }
}
template<class sampleType >
sampleType GeopsyCore::SignalTemplate< sampleType >::maximum ( ) const

Returns the maximum of the sample values.

References CONST_LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

{
  TRACE;
  sampleType max=0;
  CONST_LOCK_SAMPLES(sampleType, thisSamples, this)
    for(int i=0;i < _nSamples;i++ ) {
      if(thisSamples[ i ] > 0) {
        if(thisSamples[ i ] > max) max=thisSamples[ i ];
      } else {
        if(thisSamples[ i ] < -max) max=-thisSamples[ i ];
      }
    }
  UNLOCK_SAMPLES(this)
  return max;
}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::multiply ( const SignalTemplate< sampleType > *  sig)

References CONST_LOCK_SAMPLES, LOCK_SAMPLES, GeopsyCore::SignalTemplate< sampleType >::nSamples(), TRACE, and UNLOCK_SAMPLES.

Referenced by HVStationSignals::horizontal(), GeopsyCore::SubSignalPool::multiply(), LinearFKActiveStationSignals::normalize(), StructureStationSignals::organizeSubPool(), and Process::run().

{
  TRACE;
  if(nSamples()!=sig->nSamples()) return;
  CacheProcess cp;
  cp << this << sig;
  LOCK_SAMPLES(sampleType, thisSamples, this)
    CONST_LOCK_SAMPLES(sampleType, sigSamples, sig)
      for(int i=0; i < _nSamples; i++ ) thisSamples[ i ] *= sigSamples[ i ];
    UNLOCK_SAMPLES(sig)
  UNLOCK_SAMPLES(this)
}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::multiply ( sampleType  value)

Multiplies all samples by value.

References LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

{
  TRACE;
  LOCK_SAMPLES(sampleType, thisSamples, this)
    for(int i=0; i < _nSamples; i++ ) thisSamples[ i ] *= value;
  UNLOCK_SAMPLES(this)
}
template<class sampleType>
int GeopsyCore::SignalTemplate< sampleType >::nSamples ( ) const [inline]

Returns the number of samples.

Referenced by GeopsyCore::TimeRangeList::add(), GeopsyCore::SubSignalPool::associate3Components(), GeopsyCore::DoubleSignal::clipMaxToKeep(), GeopsyCore::DoubleSignal::convolution(), GeopsyCore::DoubleSignal::copyAmplitudeFrom(), GeopsyCore::DoubleSignal::decimateTime(), HVStationSignals::horizontal(), ArrayCore::StationCoupleProcess::horizontalAutocorr(), GeopsyCore::DoubleSignal::hv(), GeopsyCore::SparseKeepSignal::initValues(), MatFormat::load(), GeopsyCore::SignalFile::loadGeopsySignal(), GeopsyCore::SubSignalPool::merge(), GeopsyCore::DoubleSignal::morletWavelet(), GeopsyCore::SignalTemplate< sampleType >::multiply(), ShotRecord::nSamples(), GeopsyCore::SignalHeaderObject::nSamples(), StructureStationSignals::organizeSubPool(), ShotRecord::organizeSubPool(), GeopsyCore::SubSignalPool::rotateComponents(), Process::run(), GeopsyCore::SubSignalPool::samplesCount(), GeopsyCore::SignalFile::save(), GeopsyCore::SubSignalPool::saveGeopsySignal(), GeopsyCore::SignalHeaderObject::setDuration(), GeopsyCore::StationProcessSignals::setHighPassFilter(), GeopsyCore::GeopsyCoreEngine::showSignal(), GeopsyCore::DoubleSignal::staltaToKeep(), GeopsyCore::DoubleSignal::timeCorrelation(), GeopsyCore::SubSignalPool::timeRange(), GeopsyGui::SignalLayer::updateGrid(), PtMotionResults::updateSignals(), ArrayCore::StationCoupleProcess::verticalAutocorr(), GeopsyGui::SignalLayer::visibleSamples(), GeopsyCore::SubSignalPool::waveletTransform(), GeopsyCore::XMLSignal::xml_setBinaryData(), and GeopsyCore::XMLSignal::xml_writeBinaryData().

{return _nSamples;}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::range ( sampleType &  min,
sampleType &  max 
) const

Returns the min and max of the sample values.

References CONST_LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

{
  TRACE;
  CONST_LOCK_SAMPLES(sampleType, thisSamples, this)
    min=thisSamples[ 0 ];
    max=thisSamples[ 0 ];
    for(int i=0; i < _nSamples; i++ ) {
      if(thisSamples[ i ] < min) min=thisSamples[ i ];
      if(thisSamples[ i ] > max) max=thisSamples[ i ];
    }
  UNLOCK_SAMPLES(this)
}
template<class sampleType>
sampleType * GeopsyCore::SignalTemplate< sampleType >::samples ( ) const [inline, protected]

Returns a vector with the samples. its length is given by nSamples(). This vector must be used ONLY between LOCK_SAMPLES and UNLOCK_SAMPLES. Outside, there is not guaranties that the pointer points to the samples, especially in multi thred context.

{return _samples;}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::setNSamples ( int  n) [inline, virtual]

Set n as the number of samples. The signal cannot be allocated when calling this function. isAllocated() tells whether the samples are allocated. Samples are allocated after a first call to LOCK_SAMPLES. UNLOCK_SAMPLES does not necessarely deallocates the samples. To effectively deallocate the samples call freeSamples().

Reimplemented in GeopsyCore::Signal, GeopsyCore::DoubleSignal, and GeopsyCore::DynamicSignal.

{ASSERT(!_samples); _nSamples=n;}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::setSamples ( sampleType *  s) const [inline, protected]
{_samples=s;}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::subtractSignal ( const SignalTemplate< sampleType > *  sig)

References CONST_LOCK_SAMPLES, LOCK_SAMPLES, TRACE, and UNLOCK_SAMPLES.

Referenced by StructureStationSignals::organizeSubPool(), GeopsyCore::SubSignalPool::subtractSignal(), and GeopsyCore::SubSignalPool::subtractSignals().

{
  TRACE;
  CacheProcess cp;
  cp << this << sig;
  LOCK_SAMPLES(sampleType, thisSamples, this)
    CONST_LOCK_SAMPLES(sampleType, sigSamples, sig)
      int nSamples=_nSamples;
      if(sig->nSamples() < _nSamples) nSamples=sig->nSamples();
      for(int i=0; i < nSamples; i++ ) thisSamples[ i ] -= sigSamples[ i ];
    UNLOCK_SAMPLES(sig)
  UNLOCK_SAMPLES(this)
}
template<class sampleType>
void GeopsyCore::SignalTemplate< sampleType >::unlockSamples ( char *  file,
int  line 
) const
template<class sampleType >
void GeopsyCore::SignalTemplate< sampleType >::unlockSamples ( ) const [inline, virtual]

General function to be called before using the samples

{
  unlockData();
}
template<class sampleType >
sampleType GeopsyCore::SignalTemplate< sampleType >::variance ( int  startAt,
int  nSamples 
) const
{
  double aver=average(startAt, nSamples);
  return average2(startAt, nSamples) - nSamples*aver*aver/(nSamples-1);
}

Member Data Documentation

template<class sampleType>
int GeopsyCore::SignalTemplate< sampleType >::_nSamples [protected]

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines