MsPASS C++ API  2.4.4.dev122+gb075c5a7b
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
mspass::algorithms::amplitudes::PowerSpectrum Class Reference

#include <PowerSpectrum.h>

Inheritance diagram for mspass::algorithms::amplitudes::PowerSpectrum:
Collaboration diagram for mspass::algorithms::amplitudes::PowerSpectrum:

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)
PowerSpectrum & operator= (const PowerSpectrum &parent)
PowerSpectrum & operator+= (const PowerSpectrum &other)
 Standard accumulation operator.
std::vector< double > amplitude () const
 Compute amplitude spectrum from power spectrum.
double power (const double f) const
 power at a given frequency.
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)
BasicSpectrum & operator= (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.
double rayleigh () const
 Return the Rayleigh bin size for this spectrum.
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/3]

Default constructor. Makes am empty, dead datum.

11 : BasicSpectrum(1.0, 0.0, 0.0, 0), Metadata(), elog() {
12 spectrum_type = string("UNDEFINED");
13}
std::string spectrum_type
Definition PowerSpectrum.h:18
BasicSpectrum()
Definition BasicSpectrum.h:22
mspass::utility::ErrorLogger elog
Definition PowerSpectrum.h:26
Metadata()
Definition Metadata.h:105

◆ PowerSpectrum() [2/3]

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.

111 : BasicSpectrum(dfin, f0in, dtin, npts_in), mspass::utility::Metadata(md),
112 elog() {
113 spectrum_type = nm;
114 spectrum.reserve(d.size());
115 for (size_t k = 0; k < d.size(); ++k)
116 spectrum.push_back(static_cast<double>(d[k]));
117 this->set_live();
118};
std::vector< double > spectrum
Definition PowerSpectrum.h:20
void set_live()
Definition BasicSpectrum.h:92
std::map< std::string, boost::any > md
Definition Metadata.h:473

◆ PowerSpectrum() [3/3]

mspass::seismic::PowerSpectrum::PowerSpectrum ( const PowerSpectrum & parent)

Standard copy constructor.

15 : BasicSpectrum(parent), Metadata(parent), elog(parent.elog) {
16 spectrum_type = parent.spectrum_type;
17 spectrum_type = parent.spectrum_type;
18 spectrum = parent.spectrum;
19 elog = parent.elog;
20}
mspass::utility::ErrorLogger elog
Definition PowerSpectrum.h:26

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.

68 {
69 vector<double> result;
70 result.reserve(spectrum.size());
71 for (int k = 0; k < spectrum.size(); ++k)
72 result.push_back(sqrt(spectrum[k]));
73 return result;
74}

◆ 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.

99 {
100 vector<double> f;
101 f.reserve(this->nf());
102 for (auto i = 0; i < this->nf(); ++i)
103 f.push_back(this->f0val + this->dfval * static_cast<double>(i));
104 return f;
105}
size_t nf() const
Definition PowerSpectrum.h:96

◆ 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.

80 {
81 const std::string base_error("PowerSpectrum::frequency: ");
82 if (sample_number < 0)
83 throw mspass::utility::MsPASSError(
84 base_error + "Sample number parameter passed cannot be negative");
85 if (sample_number >= (this->nf()))
86 throw mspass::utility::MsPASSError(
87 base_error +
88 "Sample number parameter passed xceeds range of spectrum array");
89 return this->f0() + sample_number * this->df();
90 };
int sample_number(const double f) const
Definition BasicSpectrum.h:125
double df() const
Definition BasicSpectrum.h:94
double f0() const
Definition BasicSpectrum.h:99

◆ nf()

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

Return the number of frequency bin.

Implements mspass::seismic::BasicSpectrum.

96{ return spectrum.size(); };

◆ Nyquist()

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

Return the nyquist frequency for this estimate.

Implements mspass::seismic::BasicSpectrum.

98{ return 1.0 / (2.0 * this->parent_dt); };
double parent_dt
Definition BasicSpectrum.h:171

◆ 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.
31 {
32 /* Allow self accumulation - double values */
33 if (this == (&other)) {
34 for (int k = 0; k < this->nf(); ++k)
35 spectrum[k] *= 2.0;
36 } else {
37 if (this->nf() != other.nf()) {
38 stringstream ss;
39 ss << "operator+=(accumulation) size mismatch of spectrum arrays" << endl
40 << "right hand side spectrum size=" << other.nf() << endl
41 << "left hand side spectrum size=" << this->nf() << endl;
42 this->elog.log_error("PowerSpectrum::operator+=", ss.str(),
43 ErrorSeverity::Invalid);
44 this->kill();
45 } else if (this->f0() != other.f0()) {
46 stringstream ss;
47 ss << "operator+=(accumulation) f0 mismatch of spectrum estimates" << endl
48 << "right hand side spectrum f0=" << other.f0() << endl
49 << "left hand side spectrum f0=" << this->f0() << endl;
50 this->elog.log_error("PowerSpectrum::operator+=", ss.str(),
51 ErrorSeverity::Invalid);
52 this->kill();
53 } else if (this->df() != other.df()) {
54 stringstream ss;
55 ss << "operator+=(accumulation) df mismatch of spectrum estimates" << endl
56 << "right hand side spectrum df=" << other.df() << endl
57 << "left hand side spectrum df=" << this->df() << endl;
58 this->elog.log_error("PowerSpectrum::operator+=", ss.str(),
59 ErrorSeverity::Invalid);
60 this->kill();
61 } else {
62 for (int k = 0; k < this->nf(); ++k)
63 spectrum[k] += other.spectrum[k];
64 }
65 }
66 return *this;
67}
void kill()
Definition BasicSpectrum.h:85

◆ operator=()

PowerSpectrum & mspass::seismic::PowerSpectrum::operator= ( const PowerSpectrum & parent)

Standard assignment operator.

21 {
22 if (this != (&parent)) {
23 this->Metadata::operator=(parent);
24 this->BasicSpectrum::operator=(parent);
25 spectrum_type = parent.spectrum_type;
26 spectrum = parent.spectrum;
27 elog = parent.elog;
28 }
29 return *this;
30}
BasicSpectrum & operator=(const BasicSpectrum &parent)
Definition BasicSpectrum.h:52
Metadata & operator=(const Metadata &mdold)
Definition Metadata.cc:460

◆ 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. Frequencies outside the stored frequency grid silently return 0.

Parameters
fis the frequency for which amplitude is desired.
Exceptions
MsPASSErorrobject will be throw f f is less than 0.
75 {
76 if (f < 0.0)
77 throw MsPASSError("PowerSpectrum::power: requested power for a "
78 "negative frequency",
79 ErrorSeverity::Invalid);
80 if (f < this->f0val)
81 return 0.0;
82 if (spectrum.empty())
83 return 0.0;
84 const size_t last_index = spectrum.size() - 1;
85 const double last_frequency =
86 this->f0val + static_cast<double>(last_index) * this->dfval;
87 if (f > last_frequency)
88 return 0.0;
89 if (f == last_frequency || last_index == 0)
90 return spectrum.back();
91
92 size_t filow = static_cast<size_t>((f - this->f0val) / this->dfval);
93 if (filow >= last_index)
94 filow = last_index - 1;
95 const double slope = (spectrum[filow + 1] - spectrum[filow]) / this->dfval;
96 const double flow = this->f0val + static_cast<double>(filow) * this->dfval;
97 return spectrum[filow] + slope * (f - flow);
98}
double f0val
Definition BasicSpectrum.h:169
double dfval
Definition BasicSpectrum.h:167

Member Data Documentation

◆ 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

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

◆ 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: