version  0.0.1
Defines the C++ API for MsPASS
Public Member Functions | Public Attributes | List of all members
mspass::seismic::PowerSpectrum Class Reference

#include <PowerSpectrum.h>

Inheritance diagram for mspass::seismic::PowerSpectrum:
mspass::seismic::BasicSpectrum mspass::utility::Metadata mspass::utility::BasicMetadata

Public Member Functions

 PowerSpectrum ()
 
template<class T >
 PowerSpectrum (const mspass::utility::Metadata &md, const std::vector< T > &d, const double dfin, const std::string nm, const double f0in, const double dtin, const int npts_in)
 
 PowerSpectrum (const PowerSpectrum &parent)
 
PowerSpectrumoperator= (const PowerSpectrum &parent)
 
PowerSpectrumoperator+= (const PowerSpectrum &other)
 Standard accumulation operator. More...
 
std::vector< double > amplitude () const
 Compute amplitude spectrum from power spectrum. More...
 
double power (const double f) const
 power at a given frequency. More...
 
double frequency (const int sample_number) const
 
std::vector< double > frequencies () const
 
size_t nf () const
 
double Nyquist () const
 
- Public Member Functions inherited from mspass::seismic::BasicSpectrum
 BasicSpectrum ()
 
 BasicSpectrum (const double dfin, const double f0in, const double dtin, const int npts_in)
 
 BasicSpectrum (const BasicSpectrum &parent)
 
BasicSpectrumoperator= (const BasicSpectrum &parent)
 
bool live () const
 
bool dead () const
 
void kill ()
 
void set_live ()
 
double df () const
 
double f0 () const
 
double dt () const
 Return the original sample interval of data used to generate spectrum. More...
 
double rayleigh () const
 Return the Rayleigh bin size for this spectrum. More...
 
int timeseries_npts () const
 
int sample_number (const double f) const
 
void set_df (const double dfin)
 
void set_f0 (const double f0in)
 
void set_dt (const double dtin)
 
void set_npts (const int npts_in)
 

Public Attributes

std::string spectrum_type
 
std::vector< double > spectrum
 
mspass::utility::ErrorLogger elog
 

Additional Inherited Members

- Protected Attributes inherited from mspass::seismic::BasicSpectrum
double dfval
 
double f0val
 
double parent_dt
 
double parent_npts
 
bool is_live
 

Detailed Description

Class defining the concept of a power psectrum.

Constructor & Destructor Documentation

◆ PowerSpectrum() [1/2]

mspass::seismic::PowerSpectrum::PowerSpectrum ( )

Default constructor. Makes am empty, dead datum.

11  : BasicSpectrum(),Metadata(), elog()
12 {
13  spectrum_type=string("UNDEFINED");
14 }
BasicSpectrum()
Definition: BasicSpectrum.h:23
mspass::utility::ErrorLogger elog
Definition: PowerSpectrum.h:28
std::string spectrum_type
Definition: PowerSpectrum.h:20
Metadata()
Definition: Metadata.h:80

References spectrum_type.

◆ PowerSpectrum() [2/2]

template<class T >
mspass::seismic::PowerSpectrum::PowerSpectrum ( const mspass::utility::Metadata md,
const std::vector< T > &  d,
const double  dfin,
const std::string  nm,
const double  f0in,
const double  dtin,
const int  npts_in 
)

Standard constructor template.

This constructor is a template largely to allow vectors of data other than double as inputs. If the input is not double it is converted on input to an std::vector<double> container stored in the spectrum attribute.

Parameters
mdcontents of this Metadata container are copied to the result.
dvector of sample data to load into spectrum array.
dfinfrequency bin size (sample interval)
nmname defining algorithm used to constuct this spectral estimate.
f0infrequency of component 0 of input (default is 0.0).
dtinparent sampell interval of data used to make this estimate
npts_innumber of points in parent time series used to compute this estimate.

Fully parameterized constructor template.

See class definition of PowerSpectrum for usage. Template to allow data vector of multiple types.

112  : BasicSpectrum(dfin,f0in,dtin,npts_in),
114  elog()
115 {
116  spectrum_type=nm;
117  spectrum.reserve(d.size());
118  for(size_t k=0;k<d.size();++k)
119  spectrum.push_back(static_cast<double>(d[k]));
120  this->set_live();
121 };
void set_live()
Definition: BasicSpectrum.h:94
std::vector< double > spectrum
Definition: PowerSpectrum.h:22
Definition: Metadata.h:76

References mspass::seismic::BasicSpectrum::set_live(), spectrum, and spectrum_type.

Member Function Documentation

◆ amplitude()

vector< double > mspass::seismic::PowerSpectrum::amplitude ( ) const

Compute amplitude spectrum from power spectrum.

The amplitude spectrum is sqrt of the power values. This is a convenience class to return the values in that form.

83 {
84  vector<double> result;
85  result.reserve(spectrum.size());
86  for(int k=0;k<spectrum.size();++k)
87  result.push_back(sqrt(spectrum[k]));
88  return result;
89 }

References spectrum.

◆ frequencies()

std::vector< double > mspass::seismic::PowerSpectrum::frequencies ( ) const
virtual

Return an std::vector container containing the frequency of each sample in the spectrum vector. Commonly necessary for plotting. Made virtual because nf method needs to be virtual.

Implements mspass::seismic::BasicSpectrum.

107 {
108  vector<double> f;
109  f.reserve(this->nf());
110  for(auto i=0;i<this->nf();++i) f.push_back(this->f0val+this->dfval*static_cast<double>(i));
111  return f;
112 }
size_t nf() const
Definition: PowerSpectrum.h:97

References nf().

◆ frequency()

double mspass::seismic::PowerSpectrum::frequency ( const int  sample_number) const
inlinevirtual

Return frequency at a specified sample number. Virtual to allow subclasses to throw an error for illegal value.

Implements mspass::seismic::BasicSpectrum.

84  {
85  const std::string base_error("PowerSpectrum::frequency: ");
86  if(sample_number<0) throw mspass::utility::MsPASSError(base_error
87  + "Sample number parameter passed cannot be negative");
88  if(sample_number>=(this->nf())) throw mspass::utility::MsPASSError(base_error
89  + "Sample number parameter passed xceeds range of spectrum array");
90  return this->f0()+sample_number*this->df();
91  };
int sample_number(const double f) const
Definition: BasicSpectrum.h:128
double df() const
Definition: BasicSpectrum.h:96
double f0() const
Definition: BasicSpectrum.h:101
Base class for error object thrown by MsPASS library routines.
Definition: MsPASSError.h:40

References mspass::seismic::BasicSpectrum::df(), mspass::seismic::BasicSpectrum::f0(), nf(), and mspass::seismic::BasicSpectrum::sample_number().

◆ nf()

size_t mspass::seismic::PowerSpectrum::nf ( ) const
inlinevirtual

Return the number of frequency bin.

Implements mspass::seismic::BasicSpectrum.

97 {return spectrum.size();};

References spectrum.

◆ Nyquist()

double mspass::seismic::PowerSpectrum::Nyquist ( ) const
inlinevirtual

Return the nyquist frequency for this estimate.

Implements mspass::seismic::BasicSpectrum.

99  {
100  return 1.0/(2.0*this->parent_dt);
101  };

◆ operator+=()

PowerSpectrum & mspass::seismic::PowerSpectrum::operator+= ( const PowerSpectrum other)

Standard accumulation operator.

Sometimes we need to sum power spectra. Type examplel would be total noise amplitude on a 3C seismogram or average noise amplitude in an array of instruments. This can be used to build such sum in the usual way. Add spectral elements sample by sample.

Exceptions
willthrow a MsPaSSError if the left and right side are not equal length of have different f0 of df values.
36 {
37  /* Allow self accumulation - double values */
38  if(this==(&other))
39  {
40  for(int k=0;k<this->nf();++k) spectrum[k]*=2.0;
41  }
42  else
43  {
44  if(this->nf() != other.nf())
45  {
46  stringstream ss;
47  ss << "operator+=(accumulation) size mismatch of spectrum arrays"<<endl
48  << "right hade side spectrum size="<<other.nf()<<endl
49  << "left had side spectrum size="<<this->nf()<<endl;
50  this->elog.log_error("PowerSpectrum::operator+-",
51  ss.str(),ErrorSeverity::Invalid);
52  this->kill();
53  }
54  else if(this->f0() != other.f0())
55  {
56  stringstream ss;
57  ss << "operator+=(accumulation) f0 mismatch of spectrum estimates"<<endl
58  << "right hade side spectrum f0="<<other.f0()<<endl
59  << "left had side spectrum f0="<<this->f0()<<endl;
60  this->elog.log_error("PowerSpectrum::operator+-",
61  ss.str(),ErrorSeverity::Invalid);
62  this->kill();
63  }
64  else if(this->df() != other.df())
65  {
66  stringstream ss;
67  ss << "operator+=(accumulation) df mismatch of spectrum estimates"<<endl
68  << "right hade side spectrum df="<<other.df()<<endl
69  << "left had side spectrum df="<<this->df()<<endl;
70  this->elog.log_error("PowerSpectrum::operator+-",
71  ss.str(),ErrorSeverity::Invalid);
72  this->kill();
73  }
74  else
75  {
76  for(int k=0;k<this->nf();++k)
77  spectrum[k] += other.spectrum[k];
78  }
79  }
80  return *this;
81 }
void kill()
Definition: BasicSpectrum.h:87
int log_error(const mspass::utility::MsPASSError &merr)
Definition: ErrorLogger.cc:81

References mspass::seismic::BasicSpectrum::df(), elog, mspass::seismic::BasicSpectrum::f0(), mspass::seismic::BasicSpectrum::kill(), mspass::utility::ErrorLogger::log_error(), nf(), and spectrum.

◆ power()

double mspass::seismic::PowerSpectrum::power ( const double  f) const

power at a given frequency.

Returns the power estimate at a specified frequency. Uses a linear interpolation between nearest neighbors. Returns the frequency exceeds the Nyquist silently returns 0.

Parameters
fis the frequency for which amplitude is desired.
Exceptions
MsPASSErorrobject will be throw f f is less than 0.
91 {
92  if(f<0.0) throw MsPASSError("PowerSpectrum::amplitude: requested amplitude for a negative frequency which is assumed to be an erorr",
93  ErrorSeverity::Invalid);
94  int filow;
95  filow=static_cast<int>((f-this->f0val)/this->dfval);
96  /* Force 0 at Nyquist and above - this allows simple interpolation in else */
97  if(filow>=(spectrum.size()-1))
98  return 0.0;
99  else
100  {
101  double slope=(spectrum[filow+1]-spectrum[filow])/this->dfval;
102  double flow=this->f0val+((double)filow)*this->dfval;
103  return spectrum[filow]+slope*(f-flow);
104  }
105 }

References spectrum.

Member Data Documentation

◆ elog

mspass::utility::ErrorLogger mspass::seismic::PowerSpectrum::elog

MsPASS Error logging class.

MsPASS uses an error logger to allow posting of error messages that go with the data. That approach is needed in map reduce to allow the error messages to be cleanly preserved.

◆ spectrum

std::vector<double> mspass::seismic::PowerSpectrum::spectrum

Vector of spectral estimates. spectrum[0] == estimate at f0.

◆ spectrum_type

std::string mspass::seismic::PowerSpectrum::spectrum_type

Descriptive name assigned by creator of algorithm used to generate it.

There are a number of different algorithms to generate power spectra. this name field should be used to set a unique name for a given algorithm.


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