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-2015.
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: Alexandra Zerck $
32 // $Authors: Eva Lange $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H
36 #define OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H
37 
41 
42 namespace OpenMS
43 {
101  class OPENMS_DLLAPI SavitzkyGolayFilter :
102  public ProgressLogger,
103  public DefaultParamHandler
104  {
105 public:
108 
110  virtual ~SavitzkyGolayFilter();
111 
115  template <typename PeakType>
116  void filter(MSSpectrum<PeakType> & spectrum)
117  {
118  UInt n = (UInt)spectrum.size();
119 
120  typename MSSpectrum<PeakType>::iterator first = spectrum.begin();
121  typename MSSpectrum<PeakType>::iterator last = spectrum.end();
122 
123  //copy the data AND META DATA to the output container
124  MSSpectrum<PeakType> output = spectrum;
125 
126  if (frame_size_ > n)
127  {
128  return;
129  }
130 
131  int i;
132  UInt j;
133  int mid = (frame_size_ / 2);
134  double help;
135 
136  typename MSSpectrum<PeakType>::iterator it_forward;
137  typename MSSpectrum<PeakType>::iterator it_help;
138  typename MSSpectrum<PeakType>::iterator out_it = output.begin();
139 
140  // compute the transient on
141  for (i = 0; i <= mid; ++i)
142  {
143  it_forward = (first - i);
144  help = 0;
145 
146  for (j = 0; j < frame_size_; ++j)
147  {
148  help += it_forward->getIntensity() * coeffs_[(i + 1) * frame_size_ - 1 - j];
149  ++it_forward;
150  }
151 
152 
153  out_it->setPosition(first->getPosition());
154  out_it->setIntensity(std::max(0.0, help));
155  ++out_it;
156  ++first;
157  }
158 
159  // compute the steady state output
160  it_help = (last - mid);
161  while (first != it_help)
162  {
163  it_forward = (first - mid);
164  help = 0;
165 
166  for (j = 0; j < frame_size_; ++j)
167  {
168  help += it_forward->getIntensity() * coeffs_[mid * frame_size_ + j];
169  ++it_forward;
170  }
171 
172 
173  out_it->setPosition(first->getPosition());
174  out_it->setIntensity(std::max(0.0, help));
175  ++out_it;
176  ++first;
177  }
178 
179  // compute the transient off
180  for (i = (mid - 1); i >= 0; --i)
181  {
182  it_forward = (first - (frame_size_ - i - 1));
183  help = 0;
184 
185  for (j = 0; j < frame_size_; ++j)
186  {
187  help += it_forward->getIntensity() * coeffs_[i * frame_size_ + j];
188  ++it_forward;
189  }
190 
191  out_it->setPosition(first->getPosition());
192  out_it->setIntensity(std::max(0.0, help));
193  ++out_it;
194  ++first;
195  }
196 
197  spectrum = output;
198  }
199 
200  template <typename PeakType>
201  void filter(MSChromatogram<PeakType> & chromatogram)
202  {
203 
204  MSSpectrum<PeakType> filter_spectra;
205  for (typename MSChromatogram<PeakType>::const_iterator it = chromatogram.begin(); it != chromatogram.end(); ++it)
206  {
207  filter_spectra.push_back(*it);
208  }
209  filter(filter_spectra);
210  chromatogram.clear(false);
211  for (typename MSSpectrum<PeakType>::const_iterator it = filter_spectra.begin(); it != filter_spectra.end(); ++it)
212  {
213  chromatogram.push_back(*it);
214  }
215 
216  }
217 
221  template <typename PeakType>
223  {
224  Size progress = 0;
225  startProgress(0, map.size() + map.getChromatograms().size(), "smoothing data");
226  for (Size i = 0; i < map.size(); ++i)
227  {
228  filter(map[i]);
229  setProgress(++progress);
230  }
231  for (Size i = 0; i < map.getChromatograms().size(); ++i)
232  {
233  filter(map.getChromatogram(i));
234  setProgress(++progress);
235  }
236  endProgress();
237  }
238 
239 protected:
241  std::vector<double> coeffs_;
246  // Docu in base class
247  virtual void updateMembers_();
248 
249  };
250 
251 } // namespace OpenMS
252 #endif // OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H
Size size() const
Definition: MSExperiment.h:117
The representation of a chromatogram.
Definition: MSChromatogram.h:52
unsigned int UInt
Unsigned integer type.
Definition: Types.h:88
UInt frame_size_
UInt of the filter kernel (number of pre-tabulated coefficients)
Definition: SavitzkyGolayFilter.h:243
void filter(MSSpectrum< PeakType > &spectrum)
Removed the noise from an MSSpectrum containing profile data.
Definition: SavitzkyGolayFilter.h:116
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
UInt order_
The order of the smoothing polynomial.
Definition: SavitzkyGolayFilter.h:245
Computes the Savitzky-Golay filter coefficients using QR decomposition.
Definition: SavitzkyGolayFilter.h:101
MSChromatogram< ChromatogramPeakType > & getChromatogram(Size id)
returns a single chromatogram
Definition: MSExperiment.h:796
void filter(MSChromatogram< PeakType > &chromatogram)
Definition: SavitzkyGolayFilter.h:201
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:69
void filterExperiment(MSExperiment< PeakType > &map)
Removed the noise from an MSExperiment containing profile data.
Definition: SavitzkyGolayFilter.h:222
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:241
void clear(bool clear_meta_data)
Clears all data and meta data.
Definition: MSChromatogram.h:587
const std::vector< MSChromatogram< ChromatogramPeakType > > & getChromatograms() const
returns the chromatogram list
Definition: MSExperiment.h:788

OpenMS / TOPP release 2.0.0 Documentation generated on Fri May 29 2015 17:20:29 using doxygen 1.8.9.1