OpenMS
SplineBisection.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Hannes Roest $
6 // $Authors: Hannes Roest $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/config.h>
12 
13 #include <limits>
14 #include <cmath>
15 
16 namespace OpenMS
17 {
24  namespace Math
25  {
26 
27  template <class T>
28  void spline_bisection(const T & peak_spline,
29  double const left_neighbor_mz,
30  double const right_neighbor_mz,
31  double & max_peak_mz,
32  double & max_peak_int,
33  double const threshold = 1e-6)
34  {
35  // calculate maximum by evaluating the spline's 1st derivative
36  // (bisection method)
37  double lefthand = left_neighbor_mz;
38  double righthand = right_neighbor_mz;
39 
40  bool lefthand_sign = true;
41  double eps = std::numeric_limits<double>::epsilon();
42 
43  // bisection
44  do
45  {
46  double mid = (lefthand + righthand) / 2.0;
47  double midpoint_deriv_val = peak_spline.derivative(mid);
48 
49  // if deriv nearly zero then maximum already found
50  if (!(std::fabs(midpoint_deriv_val) > eps))
51  {
52  break;
53  }
54 
55  bool midpoint_sign = (midpoint_deriv_val < 0.0) ? false : true;
56 
57  if (lefthand_sign ^ midpoint_sign)
58  {
59  righthand = mid;
60  }
61  else
62  {
63  lefthand = mid;
64  }
65  }
66  while (righthand - lefthand > threshold);
67 
68  max_peak_mz = (lefthand + righthand) / 2;
69  max_peak_int = peak_spline.eval(max_peak_mz);
70  }
71 
72  }
73 }
void spline_bisection(const T &peak_spline, double const left_neighbor_mz, double const right_neighbor_mz, double &max_peak_mz, double &max_peak_int, double const threshold=1e-6)
Definition: SplineBisection.h:28
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19