Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
SavitzkyGolayFilter.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2017.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Eva Lange $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H
36 #define OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H
37 
42 
43 namespace OpenMS
44 {
102  class OPENMS_DLLAPI SavitzkyGolayFilter :
103  public ProgressLogger,
104  public DefaultParamHandler
105  {
106 public:
109 
111  virtual ~SavitzkyGolayFilter();
112 
113  // low level template to filters spectra and chromatograms
114  // raw data and meta data needs to be copied to the output container before calling this function
115  template<class InputIt, class OutputIt>
116  void filter(InputIt first, InputIt last, OutputIt d_first)
117  {
118  size_t n = std::distance(first, last);
119 
120  if (frame_size_ > n) { return; }
121 
122  int i;
123  UInt j;
124  int mid = (frame_size_ / 2);
125  double help;
126 
127  // compute the transient on
128  OutputIt out_it = d_first;
129 
130  for (i = 0; i <= mid; ++i)
131  {
132  InputIt it_forward = (first - i);
133  help = 0;
134  for (j = 0; j < frame_size_; ++j)
135  {
136  help += it_forward->getIntensity() * coeffs_[(i + 1) * frame_size_ - 1 - j];
137  ++it_forward;
138  }
139 
140  out_it->setPosition(first->getPosition());
141  out_it->setIntensity(std::max(0.0, help));
142  ++out_it;
143  ++first;
144  }
145 
146  // compute the steady state output
147  InputIt it_help = (last - mid);
148 
149  while (first != it_help)
150  {
151  InputIt it_forward = (first - mid);
152  help = 0;
153 
154  for (j = 0; j < frame_size_; ++j)
155  {
156  help += it_forward->getIntensity() * coeffs_[mid * frame_size_ + j];
157  ++it_forward;
158  }
159 
160  out_it->setPosition(first->getPosition());
161  out_it->setIntensity(std::max(0.0, help));
162  ++out_it;
163  ++first;
164  }
165 
166  // compute the transient off
167  for (i = (mid - 1); i >= 0; --i)
168  {
169  InputIt it_forward = (first - (frame_size_ - i - 1));
170  help = 0;
171 
172  for (j = 0; j < frame_size_; ++j)
173  {
174  help += it_forward->getIntensity() * coeffs_[i * frame_size_ + j];
175  ++it_forward;
176  }
177 
178  out_it->setPosition(first->getPosition());
179  out_it->setIntensity(std::max(0.0, help));
180  ++out_it;
181  ++first;
182  }
183 
184  }
185 
189  void filter(MSSpectrum & spectrum)
190  {
191  // copy the data AND META DATA to the output container
192  MSSpectrum output = spectrum;
193  // filter
194  filter(spectrum.begin(), spectrum.end(), output.begin());
195  // swap back
196  std::swap(spectrum, output);
197  }
198 
202  void filter(MSChromatogram & chromatogram)
203  {
204  // copy the data AND META DATA to the output container
205  MSChromatogram output = chromatogram;
206  // filter
207  filter(chromatogram.begin(), chromatogram.end(), output.begin());
208  // swap back
209  std::swap(chromatogram, output);
210  }
211 
216  {
217  Size progress = 0;
218  startProgress(0, map.size() + map.getChromatograms().size(), "smoothing data");
219  for (Size i = 0; i < map.size(); ++i)
220  {
221  filter(map[i]);
222  setProgress(++progress);
223  }
224  for (Size i = 0; i < map.getChromatograms().size(); ++i)
225  {
226  filter(map.getChromatogram(i));
227  setProgress(++progress);
228  }
229  endProgress();
230  }
231 
232 protected:
234  std::vector<double> coeffs_;
235 
238 
241 
242  // Docu in base class
243  virtual void updateMembers_();
244  };
245 
246 } // namespace OpenMS
247 #endif // OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H
void filter(InputIt first, InputIt last, OutputIt d_first)
Definition: SavitzkyGolayFilter.h:116
const std::vector< MSChromatogram > & getChromatograms() const
returns the chromatogram list
Definition: MSExperiment.h:861
void filter(MSSpectrum &spectrum)
Removed the noise from an MSSpectrum containing profile data.
Definition: SavitzkyGolayFilter.h:189
The representation of a chromatogram.
Definition: MSChromatogram.h:55
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
UInt frame_size_
UInt of the filter kernel (number of pre-tabulated coefficients)
Definition: SavitzkyGolayFilter.h:237
Size size() const
Definition: MSExperiment.h:132
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void filterExperiment(PeakMap &map)
Removed the noise from an MSExperiment containing profile data.
Definition: SavitzkyGolayFilter.h:215
UInt order_
The order of the smoothing polynomial.
Definition: SavitzkyGolayFilter.h:240
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
Computes the Savitzky-Golay filter coefficients using QR decomposition.
Definition: SavitzkyGolayFilter.h:102
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:82
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
std::vector< double > coeffs_
Coefficients.
Definition: SavitzkyGolayFilter.h:234
MSChromatogram & getChromatogram(Size id)
returns a single chromatogram
Definition: MSExperiment.h:875
void filter(MSChromatogram &chromatogram)
Removed the noise from an MSChromatogram.
Definition: SavitzkyGolayFilter.h:202

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:03 using doxygen 1.8.13