GCpp general purpose C++ library  version 1.0
GBaseSampleFFT Class Referenceabstract

#include <GBaseSampleFFT.hh>

Inheritance diagram for GBaseSampleFFT:
GRealSampleFFT

Public Member Functions

 GBaseSampleFFT (u_int n=0, double dt=1.L)
 
 GBaseSampleFFT (const GBaseSampleFFT &original)
 
GBaseSampleFFToperator= (const GBaseSampleFFT &original)
 
virtual ~GBaseSampleFFT ()
 
virtual void ResetData ()
 
virtual void ClearData ()
 
virtual u_int InitData (u_int n, double dt=1.L, bool clear=true)
 
virtual int ResizeData (u_int n)
 
virtual void InitFunctionData (u_int n, const double data[])=0
 
virtual void InitTransformData (u_int n, const double data[])=0
 
virtual void InitTransformData (u_int n, const double data_re[], const double data_im[])=0
 
virtual void SetFunctionValue (u_int i, double val)=0
 
virtual void SetTransformValue (u_int i, double re, double im=0.L)=0
 
virtual void SetFFTValue (u_int i, double re, double im=0.L)=0
 
virtual int ComputeTransformData ()=0
 
virtual int ComputeFunctionData ()=0
 
virtual void Normalise (double s=1.L)=0
 
virtual void Scale (double s)
 
virtual double GetSumSquared (bool f=false)
 
bool IsDefined () const
 
bool IsFunctionSet () const
 
bool IsTransformSet () const
 
void FunctionSet (bool b=true)
 
void TransformSet (bool b=true)
 
int GetDataType () const
 
u_int GetSamplesNumber () const
 
u_int GetDimension () const
 
bool HasSameSampling (const GBaseSampleFFT &sample, double prec=0.0001) const
 
virtual int SetTimeRange (double t_lo, double t_up, bool ctr=false)
 
virtual double GetTimeMin () const
 
virtual double GetTimeMax () const
 
virtual double GetTimeEnd () const
 
virtual double GetTimeStep () const
 
virtual double GetCriticalFrequency () const
 
virtual double GetFrequencyMax () const
 
virtual double GetFrequencyStep () const
 
double * GetFunctionData (bool check=true)
 
double * GetTransformData (bool check=true)
 
double * CopyFunctionData () const
 
double * CopyTransformData () const
 
double * CreatePowerSpectralDensity ()
 
double * CreatePSD ()
 
virtual int Write (const string &fname)
 
virtual int Write (FILE *fp)
 
virtual int Read (const string &fname)
 
virtual int Read (FILE *fp)
 

Protected Member Functions

virtual int WriteHeader (FILE *fp)
 
virtual int ReadHeader (FILE *fp)
 
void CopyData (const GBaseSampleFFT &original)
 

Protected Attributes

int data_type
 0=undefined; 1=real; 2=complex
 
u_int samples_number
 Number of data samples.
 
u_int samples_dim
 Effective arrays dimension (power of 2)
 
u_int alloc_dim
 Effective allocated dim.
 
double * data_array
 Array of function samples.
 
double * fft_array
 Array of Fourier transform data.
 
bool data_set
 Indicate if the data samples are set.
 
bool fft_set
 Indicate if the FFT data are set.
 
double time_min
 Time of the first sample.
 
double time_max
 Time of the last sample (after padding with zeros)
 
double time_end
 Time of the last (defined) sample (before padding)
 
double time_step
 Time step.
 
double freq_step
 Frequency step.
 
double freq_crit
 Frequency maximum value (Nyquist critical frequency)
 

Static Protected Attributes

static double epsilon = 1.e-30L
 Limit for 0 checking.
 
static string data_type_name [3] = { "undefined", "real", "complex" }
 Names of the data types.
 

Private Member Functions

 GObjectV (GBaseSampleFFT)
 

Detailed Description

This class defines a base class to manage data samples and the corresponding Fourier transform.

The data are processed as real numbers with the FFT functions using the Danielson-Lanczos algorithm (see Danielson-Lanczos algorithm for FFT). Thus, the arrays (sample and Fourier transform) sizes depend whether the function is a real or a complex function.

The FFT algorithm works with dimensions N=2^k (power of two), so if the data dimension is not a power of 2, it is padded with zeros. It is anyway suggested to use samples with a dimension that is a power of 2.

In order to reduce computation time, the Fourier transform and/or the function samples are only computed when required. The status for the 2 data arrays (function samples and Fourier transform data) is defined by 2 boolean variables: data_set and fft_set.

GFFT_time_scale_mode

Constructor & Destructor Documentation

GBaseSampleFFT::GBaseSampleFFT ( u_int  n = 0,
double  dt = 1.L 
)

Base constructor. If the number of samples data is defined, the arrays are initialised. The time step is used to initialise the effective time range, starting at t = 0 for the first sample.

Parameters
nnumber of function samples
dtsampling time step

References alloc_dim, data_array, data_set, data_type, fft_array, fft_set, freq_crit, freq_step, GDebugConst, GGetString(), InitData(), samples_dim, samples_number, time_end, time_max, time_min, and time_step.

GBaseSampleFFT::GBaseSampleFFT ( const GBaseSampleFFT original)

Copy constructor.

Parameters
originalobject to be copied

References alloc_dim, CopyData(), data_array, data_set, fft_array, fft_set, GDebugConst, samples_dim, and samples_number.

GBaseSampleFFT::~GBaseSampleFFT ( )
virtual

Destructor: cleans-up the data.

References GDebugDest, and ResetData().

Member Function Documentation

void GBaseSampleFFT::ClearData ( )
virtual

Fonctions that fills the function and the FFT samples with zeros.

References data_array, data_set, fft_array, fft_set, GDebugClass, and samples_dim.

Referenced by InitData().

virtual int GBaseSampleFFT::ComputeFunctionData ( )
pure virtual

Compute the inverse Fourier transform (function samples) from the Fourier transform data that are currently stored in the object. The function returns 0 if no error occured (dimensions and arrays are defined, ...).

Implemented in GRealSampleFFT.

Referenced by GetFunctionData(), GetSumSquared(), ResizeData(), and Write().

virtual int GBaseSampleFFT::ComputeTransformData ( )
pure virtual

Compute the Fourier transform from the function samples that are currently stored in the object. The function returns 0 if no error occured (dimensions and arrays are defined, ...).

Implemented in GRealSampleFFT.

Referenced by CreatePowerSpectralDensity(), GetSumSquared(), and GetTransformData().

void GBaseSampleFFT::CopyData ( const GBaseSampleFFT original)
protected

This function copies the data from original to the current object. The current data should have been resetted before (this is not checked here). It is not a virtual function, and it copies only the part corresponding to this stage of the heritance tree.

Parameters
originalobject to be copied

References alloc_dim, data_array, data_set, fft_array, fft_set, freq_crit, freq_step, GDebugClass, GetSamplesNumber(), GGetString(), samples_dim, samples_number, time_end, time_max, time_min, and time_step.

Referenced by GBaseSampleFFT(), GRealSampleFFT::GRealSampleFFT(), GRealSampleFFT::operator=(), and operator=().

double * GBaseSampleFFT::CopyFunctionData ( ) const

Allocate and create a copy of the function samples data. If the data are not defined, the function returns a NULL pointer. The returned array must be destroyed by the calling program.

References data_array, IsDefined(), and samples_dim.

double * GBaseSampleFFT::CopyTransformData ( ) const

Allocate and create a copy of the Forier transform data. If the data are not defined, the function returns a NULL pointer. The returned array must be destroyed by the calling program.

References fft_array, IsDefined(), and samples_dim.

double * GBaseSampleFFT::CreatePowerSpectralDensity ( )

Create an array of N/2+1 data with the power spectral density. The array must be deleted by the calling function.

References ComputeTransformData(), fft_array, fft_set, GDebugClass, and GetDimension().

Referenced by CreatePSD().

double * GBaseSampleFFT::CreatePSD ( )
inline

Create an array of N/2+1 data with the power spectral density. The array must be deleted by the calling function.

References CreatePowerSpectralDensity().

void GBaseSampleFFT::FunctionSet ( bool  b = true)
inline
double GBaseSampleFFT::GetCriticalFrequency ( ) const
inlinevirtual

Return the Nyquist critical frequency of the Fourier discrete transform (maximum frequency).

References freq_crit.

Referenced by GetFrequencyMax().

int GBaseSampleFFT::GetDataType ( ) const
inline

Return the identifier of the type of data (real / complex) of the sample.

References data_type.

Referenced by HasSameSampling().

u_int GBaseSampleFFT::GetDimension ( ) const
inline

Function returning the effective dimension of data arrays in terms of double real data.

References samples_dim.

Referenced by CreatePowerSpectralDensity(), GetSumSquared(), HasSameSampling(), GRealSampleFFT::SetConvolution(), and GRealSampleFFT::SetProduct().

double GBaseSampleFFT::GetFrequencyMax ( ) const
inlinevirtual

Return the maximum frequency (critical) of the Fourier discrete transform.

References GetCriticalFrequency().

double GBaseSampleFFT::GetFrequencyStep ( ) const
inlinevirtual

Return the frequency step of the Fourier discrete transform.

References freq_step.

double * GBaseSampleFFT::GetFunctionData ( bool  check = true)
inline

Returns pointer to the function samples. If check argument is true (default value), the functions checks if data must be recomputed before returning the result. The argument should be set to false to reduce computation time if the data does not need to contain valid data.

If the function is filled this way, the flag indicating that data is set must be updated (see FunctionSet).

Parameters
checkindicate to check for recomputing of data

References ComputeFunctionData(), data_array, and data_set.

Referenced by GExtremaFinder::Analyse(), GExtremaFinder::AnalyseMaxima(), GRealSampleFFT::Derivate(), GRealSampleFFT::Integrate(), GRealSampleFFT::operator*(), GRealSampleFFT::operator*=(), GRealSampleFFT::operator+(), GRealSampleFFT::operator+=(), GRealSampleFFT::operator-(), GRealSampleFFT::operator-=(), GRealSampleFFT::SetProduct(), and GRealSampleFFT::SetTimeResponse().

double GBaseSampleFFT::GetSumSquared ( bool  f = false)
virtual

Return the total power of the sample (sum squared amplitude).

Parameters
findicates if calculation is done in frequency space (default is time), but the result is the same...

References ComputeFunctionData(), ComputeTransformData(), data_array, data_type, fft_array, GetDimension(), GetSamplesNumber(), IsFunctionSet(), and IsTransformSet().

Referenced by GRealSampleFFT::GetPower().

double GBaseSampleFFT::GetTimeEnd ( ) const
inlinevirtual

Return the upper time limit of the function samples.

References time_end.

Referenced by GRealSampleFFT::InterpolateFrom(), and GRealSampleFFT::SetTimeResponse().

double GBaseSampleFFT::GetTimeMax ( ) const
inlinevirtual

Return the max time limit of the function samples (after padding with zeros for a dimension as a power of 2).

References time_max.

double GBaseSampleFFT::GetTimeMin ( ) const
inlinevirtual
double * GBaseSampleFFT::GetTransformData ( bool  check = true)
inline

Returns pointer to the function samples. If check argument is true (default value), the functions checks if data must be recomputed before returning the result. The argument should be set to false to reduce computation time if the data does not need to contain valid data.

If the transform is filled this way, the flag indicating that fft is set must be updated (see TransformSet).

Parameters
checkindicate to check for recomputing of data

References ComputeTransformData(), fft_array, and fft_set.

Referenced by GRealSampleFFT::SetConvolution().

GBaseSampleFFT::GObjectV ( GBaseSampleFFT  )
private

Macro from GCpp library that defines the following functions:

  • ClassName(): return the real class name of the object
  • StaticClassName(): return the used class name of the object (that may be a base class of the real object).
  • IsInstanceOf<T>(): return true if the current object is an instance of the template class name argument
  • Clone(): return an allocated copy of the object.
bool GBaseSampleFFT::HasSameSampling ( const GBaseSampleFFT sample,
double  prec = 0.0001 
) const

Check if the sample has sampling sampling parameters than the argument: number of data and time step.

Parameters
samplesample to be compared
precprecision for time step comparison

References GetDataType(), GetDimension(), and GetTimeStep().

Referenced by GRealSampleFFT::SetProduct().

u_int GBaseSampleFFT::InitData ( u_int  n,
double  dt = 1.L,
bool  clear = true 
)
virtual

This function (re)allocates the arrays for the sample and the FFT if the sample dimension has changed.

The given size n corresponds to the number of data samples. For real data it is the effective data array size, while for complex data, the effective allocated array size is
2*n.

If n is not a power of 2, the real allocated size (that is returned by the function) is the smallest power of 2 bigger than n.

The arrays are initialised with zeros if the clear argument is true. Both the function data and the Fourier transform are considered as defined, since they correspond to each other !

Parameters
nrequested size of arrays
dtsampling time step
clearindicate if the data have to be cleared

References alloc_dim, ClearData(), data_array, data_type, fft_array, GDebugClass, GFFTCheckDim(), GFFTGetPower2Dim(), GGetString(), GLogWarning(), ResetData(), samples_dim, samples_number, and SetTimeRange().

Referenced by GRealSampleFFT::Derivate(), GRealSampleFFT::DoubleSymmetric(), GBaseSampleFFT(), GRealSampleFFT::HalfDivide(), GRealSampleFFT::InitFunctionData(), GRealSampleFFT::InitTransformData(), GRealSampleFFT::Integrate(), GRealSampleFFT::InterpolateFrom(), GRealSampleFFT::operator*(), GRealSampleFFT::operator+(), GRealSampleFFT::operator-(), ReadHeader(), GRealSampleFFT::SetConvolution(), GRealSampleFFT::SetProduct(), and GRealSampleFFT::SetTimeResponse().

virtual void GBaseSampleFFT::InitFunctionData ( u_int  n,
const double  data[] 
)
pure virtual

Pure virtual function that must be defined in derived classes to fill the function samples data from an array.

Parameters
nnumber of data (expressed in real)
datadata array

Implemented in GRealSampleFFT.

virtual void GBaseSampleFFT::InitTransformData ( u_int  n,
const double  data[] 
)
pure virtual

Pure virtual function that must be defined in derived classes to fill the Fourier transform data from an array of real numbers. The array must be sorted in the following order: Re[0], Im[0], Re[1], Im[1], ... Re[n/2-1], Imsee GFFTComplex, GFFTReal, ... "n/2-1".

Parameters
nnumber of data (expressed in real)
datadata array

Implemented in GRealSampleFFT.

virtual void GBaseSampleFFT::InitTransformData ( u_int  n,
const double  data_re[],
const double  data_im[] 
)
pure virtual

Pure virtual function that must be defined in derived classes to fill the Fourier transform data from 2 arrays of real numbers: 1 for the real terms of the Fourier transform, 1 for the imaginary terms.

Parameters
nnumber of complex numbers data
data_rearray for real part of data
data_imarray for imaginary part of data

Implemented in GRealSampleFFT.

virtual void GBaseSampleFFT::Normalise ( double  s = 1.L)
pure virtual

Normalisation of the samples sum. This function is virtual because the normalisation procedure is different for real or complex samples).

Parameters
srequested normalisation

Implemented in GRealSampleFFT.

GBaseSampleFFT & GBaseSampleFFT::operator= ( const GBaseSampleFFT original)

Affectation operator.

Parameters
originalobject to be copied

References CopyData(), and GDebugClass.

int GBaseSampleFFT::Read ( const string &  fname)
virtual

Write the sample from a binary file. The file is opened and close. The function returns 0 if no error occured.

Parameters
fnamefile name

References GFFT_IOERR_FOPEN, and GLogWarning().

int GBaseSampleFFT::Read ( FILE *  fp)
virtual

Effective function to read the sample from a binary file. The file must have been opened previously in binary mode. It is not closed by the function. The function returns 0 if no error occured.

Parameters
fpfile pointer

References data_array, data_set, data_type, fft_set, GDebugClass, GFFT_IOERR_DATA, GFFT_IOERR_FOPEN, GGetStringHexa(), GLogWarning(), ReadHeader(), and samples_number.

int GBaseSampleFFT::ReadHeader ( FILE *  fp)
protectedvirtual

Read the file header (no check of file pointer).

Parameters
fpfile pointer

References data_set, data_type, data_type_name, fft_set, GDebugClass, GFFT_HEADER_ID, GFFT_IOERR_DATA_TYPE, GFFT_IOERR_HEADER, GFFT_IOERR_HEADER_DATA, GLogWarning(), InitData(), and SetTimeRange().

Referenced by Read().

void GBaseSampleFFT::ResetData ( )
virtual

Fonctions that deletes the data arrays and resets the number of samples.

References alloc_dim, data_array, data_set, fft_array, fft_set, freq_crit, freq_step, GDebugClass, GGetString(), samples_dim, samples_number, time_end, time_max, time_min, and time_step.

Referenced by InitData(), ResizeData(), and ~GBaseSampleFFT().

int GBaseSampleFFT::ResizeData ( u_int  n)
virtual

Function to resize the data sample.

The given size n corresponds to the number of data samples. For real data it is the effective data array size, while for complex data, the effective allocated array size is
2*n.

If n is not a power of 2, the effective sample dimension (that is returned by the function) is the smallest power of 2 bigger than n.

If the new number of samples is bigger than the old one, the sample is padded with zeros; If the new number of samples is smaller than the old one, the sample is is truncated. In this latter case, there is no memory reallocation (the previous allocation is enough), unlike the InitData function.

If the sample has been defined from its FFT, the function sample must be defined before its size is changed.

If the function samples are defined, they are copied in the new array and the Fourier transform is not valid anymore (it will be recomputed by functions that needs it).

In any case, the Fourier transform is not kept valid after resizing !

Parameters
nnew number of samples

References alloc_dim, ComputeFunctionData(), data_array, data_set, data_type, fft_array, fft_set, GDebugClass, GetTimeMin(), GetTimeStep(), GFFTGetPower2Dim(), GGetString(), GLogWarning(), IsDefined(), IsFunctionSet(), IsTransformSet(), ResetData(), samples_dim, samples_number, and SetTimeRange().

Referenced by GRealSampleFFT::SetConvolution().

void GBaseSampleFFT::Scale ( double  s)
virtual

Scaling of the sample (both function and FFT are scaled).

Parameters
srequested normalisation

References data_array, and samples_dim.

virtual void GBaseSampleFFT::SetFFTValue ( u_int  i,
double  re,
double  im = 0.L 
)
pure virtual

Set a single (complex) value for the FFT (see derived class descriptions for SetFFTValue and SetTransformValue).

Parameters
iindex of the FFT value
rereal part of the data
imimaginary part of the data

Implemented in GRealSampleFFT.

virtual void GBaseSampleFFT::SetFunctionValue ( u_int  i,
double  val 
)
pure virtual

Pure virtual function to set a single value in the sample. The index meaning is different for a real or a complex data sample.

The use of this function implies that the full sample is set using this function, and that the FFT data have to be recomputed.

Parameters
idata index
valdata (real) value

Implemented in GRealSampleFFT.

int GBaseSampleFFT::SetTimeRange ( double  t_lo,
double  t_hi,
bool  ctr = false 
)
virtual

Set the time range for the function (that defines the frequency range). This function does not scale the data if a time range was previously defined

If center mode is used, the time argument correspond to points times of the first and the last data (with a difference of n - 1 intervals). Otherwise, the limits are considered as bin limits, with the lower time of the first bin and upper time of the last bin (a difference of
intervals).

The function returns 0 if no error occured, and 1 if the time limits are invalid, 2 if the number of samples is not defined.

Parameters
t_lolower time limit
t_hiupper time limit
ctrindicates how the time limits are defined (see time scale)

References data_type, freq_crit, freq_step, samples_dim, samples_number, time_end, time_max, time_min, and time_step.

Referenced by GRealSampleFFT::Derivate(), GRealSampleFFT::GRealSampleFFT(), InitData(), GRealSampleFFT::InitFunctionData(), GRealSampleFFT::Integrate(), GRealSampleFFT::InterpolateFrom(), GRealSampleFFT::operator*(), GRealSampleFFT::operator+(), GRealSampleFFT::operator-(), ReadHeader(), ResizeData(), GRealSampleFFT::SetConvolution(), and GRealSampleFFT::SetTimeResponse().

virtual void GBaseSampleFFT::SetTransformValue ( u_int  i,
double  re,
double  im = 0.L 
)
pure virtual

Set a single (complex) value for the FFT (see derived class descriptions for SetFFTValue and SetTransformValue).

Parameters
iindex of the FFT value
rereal part of the data
imimaginary part of the data

Implemented in GRealSampleFFT.

void GBaseSampleFFT::TransformSet ( bool  b = true)
inline
int GBaseSampleFFT::Write ( const string &  fname)
virtual

Write the sample in a binary file. The file is opened and close. The function returns 0 if no error occured.

Parameters
fnamefile name

References GFFT_IOERR_FOPEN, and GLogWarning().

int GBaseSampleFFT::Write ( FILE *  fp)
virtual

Effective function to write the sample in a binary file. The file must have been opened previously in binary mode. It is not closed by the function. The function returns 0 if no error occured.

Parameters
fpfile pointer

References ComputeFunctionData(), data_array, data_type, GFFT_IOERR_FOPEN, GLogWarning(), IsFunctionSet(), samples_number, and WriteHeader().

int GBaseSampleFFT::WriteHeader ( FILE *  fp)
protectedvirtual

Write the file header (no check of file pointer).

Parameters
fpfile pointer

References data_type, GFFT_HEADER_ID, GFFT_IOERR_HEADER, GLogWarning(), samples_dim, samples_number, time_min, and time_step.

Referenced by Write().


The documentation for this class was generated from the following files: