4 #include "mspass/utility/MsPASSError.h"
5 #include "mspass/seismic/TimeSeries.h"
6 #include "mspass/seismic/Seismogram.h"
7 #include "mspass/seismic/Ensemble.h"
8 #include "mspass/seismic/PowerSpectrum.h"
9 #include "mspass/utility/VectorStatistics.h"
10 #include "mspass/utility/Metadata.h"
11 #include "mspass/algorithms/TimeWindow.h"
12 #include "mspass/algorithms/algorithms.h"
13 namespace mspass::algorithms::amplitudes{
22 enum class ScalingMethod
29 const std::string scale_factor_key(
"calib");
55 template <
typename Tdata>
double scale(Tdata& d,
const ScalingMethod method,
58 if((method==ScalingMethod::ClipPerc) && (level<=0.0 || level>1.0))
59 throw mspass::utility::MsPASSError(
"scale function: illegal perf level specified for clip percentage scale - must be between 0 and 1\nData unaltered - may cause downstream problems",
60 mspass::utility::ErrorSeverity::Suspect);
66 if(d.is_defined(scale_factor_key))
68 newcalib=d.get_double(scale_factor_key);
76 ampwindow.
start=d.t0();
77 ampwindow.
end=d.endtime();
79 else if( (fabs(win.
start-d.t0())/d.dt()>0.5)
80 || (fabs(win.
end-d.endtime())/d.dt() > 0.5) )
83 ss <<
"Window time range is inconsistent with input data range"<<std::endl
84 <<
"Input data starttime="<<d.t0()<<
" and window start time="
85 << win.
start <<
" Difference="<<d.t0()-win.
start<<std::endl
86 <<
"Input data endtime="<<d.endtime()<<
" and window end time="
87 << win.
end <<
" Difference="<<d.endtime()-win.
end<<std::endl
88 <<
"One or the other exceeds 1/sample interval="<<d.dt()<<std::endl
89 <<
"Window for amplitude calculation changed to data range";
90 d.elog.log_error(
"scale",ss.str(),mspass::utility::ErrorSeverity::Complaint);
91 ampwindow.
start=d.t0();
92 ampwindow.
end=d.endtime();
99 windowed_data=mspass::algorithms::WindowData(d,ampwindow);
100 double amplitude,dscale;
103 case ScalingMethod::Peak:
104 amplitude=PeakAmplitude(windowed_data);
105 dscale = level/amplitude;
108 case ScalingMethod::ClipPerc:
109 amplitude=PercAmplitude(windowed_data,level);
112 dscale = 1.0/amplitude;
115 case ScalingMethod::MAD:
116 amplitude=MADAmplitude(windowed_data);
117 dscale = level/amplitude;
120 case ScalingMethod::RMS:
122 amplitude=RMSAmplitude(windowed_data);
123 dscale = level/amplitude;
127 d.put(scale_factor_key,newcalib);
158 if((method==ScalingMethod::ClipPerc) && (level<=0.0 || level>1.0))
159 throw mspass::utility::MsPASSError(
"scale_ensemble_members function: illegal perf level specified for clip percentage scale - must be between 0 and 1\nData unaltered - may cause downstream problems",
160 mspass::utility::ErrorSeverity::Suspect);
162 typename std::vector<Tdata>::iterator dptr;
163 std::vector<double> amps;
164 amps.reserve(d.
member.size());
168 thisamp=scale(*dptr,method,level,win);
169 amps.push_back(thisamp);
195 const ScalingMethod& method,
const double level,
const bool use_mean)
197 if((method==ScalingMethod::ClipPerc) && (level<=0.0 || level>1.0))
198 throw mspass::utility::MsPASSError(
"scale_ensemble function: illegal perf level specified for clip percentage scale - must be between 0 and 1\nData unaltered - may cause downstream problems",
199 mspass::utility::ErrorSeverity::Suspect);
202 typename std::vector<Tdata>::iterator dptr;
203 std::vector<double> amps;
204 amps.reserve(d.
member.size());
209 if(dptr->dead())
continue;
212 case ScalingMethod::Peak:
213 amplitude=PeakAmplitude(*dptr);
215 case ScalingMethod::ClipPerc:
216 amplitude=PercAmplitude(*dptr,level);
218 case ScalingMethod::MAD:
219 amplitude=MADAmplitude(*dptr);
221 case ScalingMethod::RMS:
223 amplitude=RMSAmplitude(*dptr);
226 amps.push_back(log(amplitude));
229 if(nlive==0)
return 0.0;
233 avgamp=ampstats.mean();
237 avgamp=ampstats.median();
242 double dscale=level/avgamp;
250 if(dptr->is_defined(scale_factor_key))
252 calib=dptr->get_double(scale_factor_key);
259 dptr->put(scale_factor_key,calib);
311 return 20.0*log10(ratio);
371 BandwidthData EstimateBandwidth(
const double signal_df,
373 const double snr_threshold,
const double tbp,
const double fhs=-1.0,
374 const bool fix_high_edge_to_fhs=
false);
414 const BandwidthData& bwd);
Defines a time window.
Definition: TimeWindow.h:14
double start
Definition: TimeWindow.h:19
double end
Definition: TimeWindow.h:23
Holds parameters defining a passband computed from snr.
Definition: amplitudes.h:275
double low_edge_f
Definition: amplitudes.h:278
double bandwidth() const
Definition: amplitudes.h:304
double bandwidth_fraction() const
Definition: amplitudes.h:296
double high_edge_f
Definition: amplitudes.h:280
double low_edge_snr
Definition: amplitudes.h:282
double high_edge_snr
Definition: amplitudes.h:284
Vector (three-component) seismogram data object.
Definition: CoreSeismogram.h:40
Scalar time series data object.
Definition: CoreTimeSeries.h:18
Definition: Ensemble.h:10
std::vector< Tdata > member
Container holding data objects.
Definition: Ensemble.h:21
Definition: PowerSpectrum.h:13
Base class for error object thrown by MsPASS library routines.
Definition: MsPASSError.h:40
Generic object to compute common robust statistics from a vector container of data.
Definition: VectorStatistics.h:16