@@ -132,96 +132,6 @@ class EIGEN_ALIGN_MAX WaveSpectrumEstimator {
132132 return (fp > 0.0 ) ? (1.0 / fp) : 0.0 ;
133133 }
134134
135- PMFitResult fitPiersonMoskowitz () const {
136- Vec S_obs = lastSpectrum_;
137- for (int i = 0 ; i < Nfreq; ++i) {
138- if (S_obs[i] <= 0.0 ) S_obs[i] = 1e-12 ;
139- }
140-
141- auto cost_fn = [&](double a, double fp) {
142- const double omega_p = 2.0 * M_PI * fp;
143- double cost = 0.0 ;
144- constexpr double beta = 0.74 ;
145-
146- for (int i = 0 ; i < Nfreq - 1 ; ++i) {
147- const double df = df_[i];
148- const double f = freqs_[i];
149- double model = a * g * g * std::pow (2.0 * M_PI * f, -5.0 )
150- * std::exp (-beta * std::pow (omega_p / (2.0 * M_PI * f), 4.0 ));
151- if (model <= 0.0 ) model = 1e-12 ;
152- const double d = WaveSpectrumShared::safe_log (S_obs[i]) -
153- WaveSpectrumShared::safe_log (model);
154- cost += df * d * d;
155- }
156- return cost;
157- };
158-
159- constexpr int N_fp_search = 32 ;
160- constexpr double fp_min = 0.05 ;
161- constexpr double fp_transition = 0.1 ;
162- constexpr double fp_max = 1.0 ;
163-
164- std::array<double , N_fp_search> fp_grid{};
165- const int n_log = static_cast <int >(N_fp_search * 0.4 );
166- const int n_lin = N_fp_search - n_log;
167-
168- for (int i = 0 ; i < n_log; ++i) {
169- const double t = double (i) / double (n_log - 1 );
170- fp_grid[i] = fp_min * std::pow (fp_transition / fp_min, t);
171- }
172- for (int i = 0 ; i < n_lin; ++i) {
173- const double t = double (i) / double (n_lin - 1 );
174- fp_grid[n_log + i] = fp_transition + t * (fp_max - fp_transition);
175- }
176-
177- double bestA = 1e-5 ;
178- double bestFp = fp_grid[0 ];
179- double bestC = std::numeric_limits<double >::infinity ();
180-
181- for (int ia = 0 ; ia < 8 ; ++ia) {
182- const double a = 1e-5 + ia * (1.0 - 1e-5 ) / 7.0 ;
183- for (int ifp = 0 ; ifp < N_fp_search; ++ifp) {
184- const double fp = fp_grid[ifp];
185- const double c = cost_fn (a, fp);
186- if (c < bestC) {
187- bestC = c;
188- bestA = a;
189- bestFp = fp;
190- }
191- }
192- }
193-
194- double alpha = bestA;
195- double fp = bestFp;
196- double stepA = 0.1 ;
197- double stepFp = 0.1 ;
198-
199- for (int iter = 0 ; iter < 40 ; ++iter) {
200- bool improved = false ;
201- double c = 0.0 ;
202-
203- c = cost_fn (alpha + stepA, fp);
204- if (c < bestC) { bestC = c; alpha += stepA; improved = true ; }
205-
206- c = cost_fn (alpha - stepA, fp);
207- if (c < bestC) { bestC = c; alpha -= stepA; improved = true ; }
208-
209- c = cost_fn (alpha, fp + stepFp);
210- if (c < bestC) { bestC = c; fp += stepFp; improved = true ; }
211-
212- c = cost_fn (alpha, fp - stepFp);
213- if (c < bestC) { bestC = c; fp -= stepFp; improved = true ; }
214-
215- if (!improved) {
216- stepA *= 0.5 ;
217- stepFp *= 0.5 ;
218- if (stepA < 1e-12 && stepFp < 1e-12 ) break ;
219- }
220- }
221-
222- return {alpha, fp, bestC};
223- }
224-
225135 void set_regularization_f0 (double f0_hz) {
226136 reg_f0_hz = std::max (1e-6 , f0_hz);
227137 }
0 commit comments