OpenMS
SignalToNoiseEstimatorMedianRapid.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 
13 #include <cassert>
14 #include <vector>
15 
16 namespace OpenMS
17 {
18 
43  {
46 
47 public:
48 
61  struct OPENMS_DLLAPI NoiseEstimator
62  {
66  double mz_start;
68  double window_length;
70  std::vector<double> result_windows_even;
72  std::vector<double> result_windows_odd;
73 
76 
78  NoiseEstimator(double nr_windows_, double mz_start_, double win_len_) :
79  nr_windows(nr_windows_),
80  mz_start(mz_start_),
81  window_length(win_len_),
82  result_windows_even(nr_windows_),
83  result_windows_odd(nr_windows_+1)
84  {}
85 
93  double get_noise_value (double mz)
94  {
95  // Take the average of the two stored values
96  // Avoid division by 0 (since most clients will divide by the noise value)
97  return std::max(1.0, (get_noise_even(mz)+get_noise_odd(mz))/2.0 );
98  }
99 
100  double get_noise_even (double mz)
101  {
102  // PRECONDITION
103  int window_nr = (int)((mz - mz_start)/window_length);
104  assert(window_nr >= 0);
105  assert(window_nr < (int)result_windows_even.size());
106 
107  double noise = result_windows_even[window_nr];
108  return noise;
109  }
110 
111  double get_noise_odd (double mz)
112  {
113  // PRECONDITION
114  int window_nr = (int)((mz - mz_start + window_length/2.0)/window_length);
115  assert(window_nr >= 0);
116  assert(window_nr < (int)result_windows_odd.size());
117 
118  double noise = result_windows_odd[window_nr];
119  return noise;
120  }
121  };
122 
124  SignalToNoiseEstimatorMedianRapid(double window_length) :
125  window_length_(window_length)
126  {
127  }
128 
134  {
135  return estimateNoise(spectrum->getMZArray()->data, spectrum->getIntensityArray()->data);
136  }
137 
143  {
144  return estimateNoise(chrom->getTimeArray()->data, chrom->getIntensityArray()->data);
145  }
146 
151  NoiseEstimator estimateNoise(const std::vector<double>& mz_array, const std::vector<double>& int_array)
152  {
153  // PRECONDITION
154  assert(mz_array.size() == int_array.size());
155  assert(mz_array.size() > 2);
156 
157  int nr_windows = (int)((mz_array[mz_array.size()-1] - mz_array[0])/window_length_) + 1;
158  NoiseEstimator eval(nr_windows, mz_array[0], window_length_);
159 
160  // Compute even windows
161  computeNoiseInWindows_(mz_array, int_array, eval.result_windows_even, mz_array[0]);
162  // Compute odd windows
163  computeNoiseInWindows_(mz_array, int_array, eval.result_windows_odd, mz_array[0] - window_length_/2.0);
164 
165  return eval;
166  }
167 
168 private:
169 
175  void computeNoiseInWindows_(const std::vector<double>& mz_array, std::vector<double> int_array, std::vector<double> & result, double mz_start);
176 
183  double computeMedian_(std::vector<double>::iterator & first, std::vector<double>::iterator & last);
184 
185  };
186 
187 } // namespace OpenMS
188 
189 
Estimates the signal/noise (S/N) ratio of each data point in a scan by using the median (window based...
Definition: SignalToNoiseEstimatorMedianRapid.h:43
double window_length_
Window length parameter.
Definition: SignalToNoiseEstimatorMedianRapid.h:45
NoiseEstimator estimateNoise(OpenMS::Interfaces::SpectrumPtr spectrum)
Compute noise estimator for an m/z and intensity array using windows.
Definition: SignalToNoiseEstimatorMedianRapid.h:133
NoiseEstimator estimateNoise(const std::vector< double > &mz_array, const std::vector< double > &int_array)
Compute noise estimator for an m/z and intensity array using windows.
Definition: SignalToNoiseEstimatorMedianRapid.h:151
SignalToNoiseEstimatorMedianRapid(double window_length)
default constructor
Definition: SignalToNoiseEstimatorMedianRapid.h:124
void computeNoiseInWindows_(const std::vector< double > &mz_array, std::vector< double > int_array, std::vector< double > &result, double mz_start)
Computes the noise in windows for two input arrays and stores the median intensity in the result (int...
double computeMedian_(std::vector< double >::iterator &first, std::vector< double >::iterator &last)
Median computation on a part of an array [first,last)
NoiseEstimator estimateNoise(OpenMS::Interfaces::ChromatogramPtr chrom)
Compute noise estimator for an m/z and intensity array using windows.
Definition: SignalToNoiseEstimatorMedianRapid.h:142
boost::shared_ptr< Chromatogram > ChromatogramPtr
Definition: openms/include/OpenMS/INTERFACES/DataStructures.h:130
boost::shared_ptr< Spectrum > SpectrumPtr
Definition: openms/include/OpenMS/INTERFACES/DataStructures.h:210
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Class to compute the noise value at a given position.
Definition: SignalToNoiseEstimatorMedianRapid.h:62
NoiseEstimator(double nr_windows_, double mz_start_, double win_len_)
Constructor.
Definition: SignalToNoiseEstimatorMedianRapid.h:78
double window_length
Length of the window in m/z direction.
Definition: SignalToNoiseEstimatorMedianRapid.h:68
double get_noise_even(double mz)
Definition: SignalToNoiseEstimatorMedianRapid.h:100
double get_noise_value(double mz)
Return the noise value at a given m/z position.
Definition: SignalToNoiseEstimatorMedianRapid.h:93
NoiseEstimator()
Constructor.
Definition: SignalToNoiseEstimatorMedianRapid.h:75
double mz_start
Start of m/z domain.
Definition: SignalToNoiseEstimatorMedianRapid.h:66
std::vector< double > result_windows_odd
Noise values for window starting at mz_start - 0.5 * window_length (length = nr_windows + 1)
Definition: SignalToNoiseEstimatorMedianRapid.h:72
std::vector< double > result_windows_even
Noise values for window starting at mz_start (length = nr_windows)
Definition: SignalToNoiseEstimatorMedianRapid.h:70
double get_noise_odd(double mz)
Definition: SignalToNoiseEstimatorMedianRapid.h:111
int nr_windows
Number of windows in m/z direction for which noise values are stored.
Definition: SignalToNoiseEstimatorMedianRapid.h:64