version  0.0.1
Defines the C++ API for MsPASS
PowerSpectrum.h
1 #ifndef _POWER_SPECTRUM_H_
2 #define _POWER_SPECTRUM_H_
3 #include <vector>
4 #include "mspass/utility/Metadata.h"
5 #include "mspass/utility/MsPASSError.h"
6 #include "mspass/utility/ErrorLogger.h"
7 #include "mspass/seismic/BasicSpectrum.h"
8 namespace mspass::seismic
9 {
13 {
14 public:
20  std::string spectrum_type;
22  std::vector<double> spectrum;
30  PowerSpectrum();
47  template <class T> PowerSpectrum(const mspass::utility::Metadata& md,
48  const std::vector<T>& d,
49  const double dfin,
50  const std::string nm,
51  const double f0in,
52  const double dtin,
53  const int npts_in);
54  PowerSpectrum(const PowerSpectrum& parent);
55  PowerSpectrum& operator=(const PowerSpectrum& parent);
65  PowerSpectrum& operator+=(const PowerSpectrum& other);
70  std::vector<double> amplitude() const;
81  double power(const double f) const;
82 
83  double frequency(const int sample_number) const
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  };
92  std::vector<double> frequencies() const;
93  /* \brief Return number of frequencies in estimate (size of spectrum vector).
94 
95  Users should be aware that when zero padding is used the size of the
96  spectrum vector will be larger than half the number of time series samples.*/
97  size_t nf()const{return spectrum.size();};
99  double Nyquist() const {
100  return 1.0/(2.0*this->parent_dt);
101  };
102 };
110  const std::vector<T>& d,const double dfin,const std::string nm,
111  const double f0in, const double dtin, const int npts_in)
112  : BasicSpectrum(dfin,f0in,dtin,npts_in),
113  mspass::utility::Metadata(md),
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 };
122 } //end namespace
123 #endif
Definition: BasicSpectrum.h:20
int sample_number(const double f) const
Definition: BasicSpectrum.h:128
void set_live()
Definition: BasicSpectrum.h:94
double df() const
Definition: BasicSpectrum.h:96
double f0() const
Definition: BasicSpectrum.h:101
Definition: PowerSpectrum.h:13
std::vector< double > frequencies() const
Definition: PowerSpectrum.cc:106
double power(const double f) const
power at a given frequency.
Definition: PowerSpectrum.cc:90
size_t nf() const
Definition: PowerSpectrum.h:97
PowerSpectrum()
Definition: PowerSpectrum.cc:11
PowerSpectrum & operator+=(const PowerSpectrum &other)
Standard accumulation operator.
Definition: PowerSpectrum.cc:35
mspass::utility::ErrorLogger elog
Definition: PowerSpectrum.h:28
std::vector< double > spectrum
Definition: PowerSpectrum.h:22
std::vector< double > amplitude() const
Compute amplitude spectrum from power spectrum.
Definition: PowerSpectrum.cc:82
double frequency(const int sample_number) const
Definition: PowerSpectrum.h:83
double Nyquist() const
Definition: PowerSpectrum.h:99
std::string spectrum_type
Definition: PowerSpectrum.h:20
Container to hold error logs for a data object.
Definition: ErrorLogger.h:61
Definition: Metadata.h:76
Base class for error object thrown by MsPASS library routines.
Definition: MsPASSError.h:40
Define metadata keys.
Definition: BasicSpectrum.h:6