OpenMS
SignalToNoiseEstimatorMedianRapid.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- 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 
45  {
48 
49 public:
50 
63  struct OPENMS_DLLAPI NoiseEstimator
64  {
68  double mz_start;
70  double window_length;
72  std::vector<double> result_windows_even;
74  std::vector<double> result_windows_odd;
75 
78 
80  NoiseEstimator(double nr_windows_, double mz_start_, double win_len_) :
81  nr_windows(nr_windows_),
82  mz_start(mz_start_),
83  window_length(win_len_),
84  result_windows_even(nr_windows_),
85  result_windows_odd(nr_windows_+1)
86  {}
87 
95  double get_noise_value (double mz)
96  {
97  // Take the average of the two stored values
98  // Avoid division by 0 (since most clients will divide by the noise value)
99  return std::max(1.0, (get_noise_even(mz)+get_noise_odd(mz))/2.0 );
100  }
101 
102  double get_noise_even (double mz)
103  {
104  // PRECONDITION
105  int window_nr = (int)((mz - mz_start)/window_length);
106  assert(window_nr >= 0);
107  assert(window_nr < (int)result_windows_even.size());
108 
109  double noise = result_windows_even[window_nr];
110  return noise;
111  }
112 
113  double get_noise_odd (double mz)
114  {
115  // PRECONDITION
116  int window_nr = (int)((mz - mz_start + window_length/2.0)/window_length);
117  assert(window_nr >= 0);
118  assert(window_nr < (int)result_windows_odd.size());
119 
120  double noise = result_windows_odd[window_nr];
121  return noise;
122  }
123  };
124 
126  SignalToNoiseEstimatorMedianRapid(double window_length) :
127  window_length_(window_length)
128  {
129  }
130 
136  {
137  return estimateNoise(spectrum->getMZArray()->data, spectrum->getIntensityArray()->data);
138  }
139 
145  {
146  return estimateNoise(chrom->getTimeArray()->data, chrom->getIntensityArray()->data);
147  }
148 
153  NoiseEstimator estimateNoise(const std::vector<double>& mz_array, const std::vector<double>& int_array)
154  {
155  // PRECONDITION
156  assert(mz_array.size() == int_array.size());
157  assert(mz_array.size() > 2);
158 
159  int nr_windows = (int)((mz_array[mz_array.size()-1] - mz_array[0])/window_length_) + 1;
160  NoiseEstimator eval(nr_windows, mz_array[0], window_length_);
161 
162  // Compute even windows
163  computeNoiseInWindows_(mz_array, int_array, eval.result_windows_even, mz_array[0]);
164  // Compute odd windows
165  computeNoiseInWindows_(mz_array, int_array, eval.result_windows_odd, mz_array[0] - window_length_/2.0);
166 
167  return eval;
168  }
169 
170 private:
171 
177  void computeNoiseInWindows_(const std::vector<double>& mz_array, std::vector<double> int_array, std::vector<double> & result, double mz_start);
178 
185  double computeMedian_(std::vector<double>::iterator & first, std::vector<double>::iterator & last);
186 
187  };
188 
189 } // namespace OpenMS
190 
191 
Estimates the signal/noise (S/N) ratio of each data point in a scan by using the median (window based...
Definition: SignalToNoiseEstimatorMedianRapid.h:45
double window_length_
Window length parameter.
Definition: SignalToNoiseEstimatorMedianRapid.h:47
NoiseEstimator estimateNoise(OpenMS::Interfaces::SpectrumPtr spectrum)
Compute noise estimator for an m/z and intensity array using windows.
Definition: SignalToNoiseEstimatorMedianRapid.h:135
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:153
SignalToNoiseEstimatorMedianRapid(double window_length)
default constructor
Definition: SignalToNoiseEstimatorMedianRapid.h:126
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:144
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: FeatureDeconvolution.h:22
Class to compute the noise value at a given position.
Definition: SignalToNoiseEstimatorMedianRapid.h:64
NoiseEstimator(double nr_windows_, double mz_start_, double win_len_)
Constructor.
Definition: SignalToNoiseEstimatorMedianRapid.h:80
double window_length
Length of the window in m/z direction.
Definition: SignalToNoiseEstimatorMedianRapid.h:70
double get_noise_even(double mz)
Definition: SignalToNoiseEstimatorMedianRapid.h:102
double get_noise_value(double mz)
Return the noise value at a given m/z position.
Definition: SignalToNoiseEstimatorMedianRapid.h:95
NoiseEstimator()
Constructor.
Definition: SignalToNoiseEstimatorMedianRapid.h:77
double mz_start
Start of m/z domain.
Definition: SignalToNoiseEstimatorMedianRapid.h:68
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:74
std::vector< double > result_windows_even
Noise values for window starting at mz_start (length = nr_windows)
Definition: SignalToNoiseEstimatorMedianRapid.h:72
double get_noise_odd(double mz)
Definition: SignalToNoiseEstimatorMedianRapid.h:113
int nr_windows
Number of windows in m/z direction for which noise values are stored.
Definition: SignalToNoiseEstimatorMedianRapid.h:66