MsPASS C++ API  2.4.4.dev114+gf4c3cfaca
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
MTPowerSpectrumEngine.h
1#ifndef __MTPOWERSPECTRUM_ENGINE_H__
2#define __MTPOWERSPECTRUM_ENGINE_H__
3
4#include "mspass/algorithms/deconvolution/GSLFFTResources.h"
5#include "mspass/seismic/PowerSpectrum.h"
6#include "mspass/seismic/TimeSeries.h"
7#include "mspass/utility/dmatrix.h"
8#include <boost/archive/text_iarchive.hpp>
9#include <boost/archive/text_oarchive.hpp>
10#include <gsl/gsl_errno.h>
11#include <gsl/gsl_fft_complex.h>
12#include <cmath>
13#include <memory>
14#include <string>
15#include <vector>
16
17namespace mspass::algorithms::deconvolution {
33public:
53 MTPowerSpectrumEngine(const int winsize, const double tbp, const int ntapers,
54 const int nfftin = -1, const double dtin = 1.0);
93 std::vector<double> apply(const std::vector<double> &d);
95 double df() const { return deltaf; };
98 std::vector<double> frequencies();
100 int taper_length() const { return taperlen; };
102 double time_bandwidth_product() const { return tbp; };
104 int number_tapers() const { return ntapers; };
107 int fftsize() const { return nfft; };
109 double dt() const { return operator_dt; };
124 double set_df(double dt) {
125 const std::string caller("MTPowerSpectrumEngine::set_df");
126 if (!std::isfinite(dt) || dt <= 0.0)
128 caller + ": sample interval must be finite and positive",
129 mspass::utility::ErrorSeverity::Invalid);
130 const int this_nf = this->nf();
131 if (this_nf <= 1)
133 caller + ": engine fft length is not configured",
134 mspass::utility::ErrorSeverity::Invalid);
135 const double new_deltaf = 1.0 / (static_cast<double>(this->nfft) * dt);
136 if (!std::isfinite(new_deltaf) || new_deltaf <= 0.0)
138 caller + ": sample interval produces an invalid frequency spacing",
139 mspass::utility::ErrorSeverity::Invalid);
140 this->operator_dt = dt;
141 this->deltaf = new_deltaf;
142 return deltaf;
143 };
146 int nf() {
147 /* this simple formula depends upon integer truncation when used with
148 nfft as an odd number. For reference, this is what prieto uses in
149 the python multitaper package:
150 if (nfft%2 == 0):
151 nf = int(nfft/2 + 1)
152 else:
153 nf = int((nfft+1)/2)
154 they will yield the same result but this is simpler and faster
155 */
156 return (this->nfft) / 2 + 1;
157 };
158
159private:
160 int taperlen;
161 int ntapers;
162 int nfft;
163 double tbp;
164 double operator_dt;
166 /* Frequency bin interval of last data processed.*/
167 double deltaf;
168 gsl_fft_complex_wavetable *wavetable;
169 gsl_fft_complex_workspace *workspace;
170 friend boost::serialization::access;
171 template <class Archive>
172 void save(Archive &ar, const unsigned int version) const {
173 ar & taperlen;
174 ar & ntapers;
175 ar & nfft;
176 ar & tbp;
177 ar & operator_dt;
178 ar & tapers;
179 ar & deltaf;
180 }
181 template <class Archive> void load(Archive &ar, const unsigned int version) {
182 int loaded_taperlen;
183 int loaded_ntapers;
184 int loaded_nfft;
185 double loaded_tbp;
186 double loaded_operator_dt;
187 mspass::utility::dmatrix loaded_tapers;
188 double loaded_deltaf;
189 ar & loaded_taperlen;
190 ar & loaded_ntapers;
191 ar & loaded_nfft;
192 ar & loaded_tbp;
193 ar & loaded_operator_dt;
194 ar & loaded_tapers;
195 ar & loaded_deltaf;
196
197 const std::string caller("MTPowerSpectrumEngine serialization load");
198 if (loaded_nfft <= 0 || loaded_taperlen <= 0 || loaded_ntapers <= 0)
200 caller + ": archived fft, taper, and taper-count lengths must be "
201 "positive",
202 mspass::utility::ErrorSeverity::Invalid);
203 if (loaded_nfft < loaded_taperlen)
205 caller + ": archived fft length is shorter than taper length",
206 mspass::utility::ErrorSeverity::Invalid);
207 if (!std::isfinite(loaded_tbp) || loaded_tbp <= 0.0 ||
208 !std::isfinite(loaded_operator_dt) || loaded_operator_dt <= 0.0 ||
209 !std::isfinite(loaded_deltaf) || loaded_deltaf <= 0.0)
211 caller + ": archived spectral parameters must be finite and "
212 "positive",
213 mspass::utility::ErrorSeverity::Invalid);
214 if (!loaded_tapers.storage_is_consistent() ||
215 loaded_tapers.rows() != static_cast<size_t>(loaded_ntapers) ||
216 loaded_tapers.columns() != static_cast<size_t>(loaded_taperlen))
218 caller + ": archived taper matrix dimensions are inconsistent",
219 mspass::utility::ErrorSeverity::Invalid);
220 auto resources = detail::AllocateGSLFFTResources(loaded_nfft, caller);
221
222 loaded_tapers.swap(tapers);
223 if (wavetable != nullptr)
224 gsl_fft_complex_wavetable_free(wavetable);
225 if (workspace != nullptr)
226 gsl_fft_complex_workspace_free(workspace);
227 taperlen = loaded_taperlen;
228 ntapers = loaded_ntapers;
229 nfft = loaded_nfft;
230 tbp = loaded_tbp;
231 operator_dt = loaded_operator_dt;
232 deltaf = loaded_deltaf;
233 wavetable = resources.wavetable.release();
234 workspace = resources.workspace.release();
235 }
236 BOOST_SERIALIZATION_SPLIT_MEMBER()
237};
238} // namespace mspass::algorithms::deconvolution
239#endif
Multittaper power spectral estimator.
Definition MTPowerSpectrumEngine.h:32
int number_tapers() const
Definition MTPowerSpectrumEngine.h:104
int taper_length() const
Definition MTPowerSpectrumEngine.h:100
double set_df(double dt)
Putter equivalent of df.
Definition MTPowerSpectrumEngine.h:124
MTPowerSpectrumEngine()
Definition MTPowerSpectrumEngine.cc:21
MTPowerSpectrumEngine & operator=(const MTPowerSpectrumEngine &parent)
Definition MTPowerSpectrumEngine.cc:147
std::vector< double > frequencies()
Definition MTPowerSpectrumEngine.cc:329
double dt() const
Definition MTPowerSpectrumEngine.h:109
~MTPowerSpectrumEngine()
Definition MTPowerSpectrumEngine.cc:140
int nf()
Definition MTPowerSpectrumEngine.h:146
mspass::seismic::PowerSpectrum apply(const mspass::seismic::TimeSeries &d)
Process a TimeSeries.
Definition MTPowerSpectrumEngine.cc:178
double time_bandwidth_product() const
Definition MTPowerSpectrumEngine.h:102
double df() const
Definition MTPowerSpectrumEngine.h:95
int fftsize() const
Definition MTPowerSpectrumEngine.h:107
Definition PowerSpectrum.h:11
Implemntation of TimeSeries for MsPASS.
Definition TimeSeries.h:14
Base class for error object thrown by MsPASS library routines.
Definition MsPASSError.h:38
Lightweight, simple matrix object.
Definition dmatrix.h:105
bool storage_is_consistent() const noexcept
Definition dmatrix.h:148
size_t rows() const
Definition dmatrix.cc:215
size_t columns() const
Definition dmatrix.cc:216
void swap(dmatrix &other) noexcept
Definition dmatrix.h:141