Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

SavitzkyGolayFilter.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Alexandra Zerck $
00032 // $Authors: Eva Lange $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H
00036 #define OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H
00037 
00038 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
00039 #include <OpenMS/CONCEPT/ProgressLogger.h>
00040 #include <OpenMS/KERNEL/MSExperiment.h>
00041 
00042 #include <gsl/gsl_vector.h>
00043 #include <gsl/gsl_matrix.h>
00044 #include <gsl/gsl_linalg.h>
00045 #include <gsl/gsl_permutation.h>
00046 #include <gsl/gsl_pow_int.h>
00047 
00048 namespace OpenMS
00049 {
00107   class OPENMS_DLLAPI SavitzkyGolayFilter :
00108     public ProgressLogger,
00109     public DefaultParamHandler
00110   {
00111 public:
00113     SavitzkyGolayFilter();
00114 
00116     virtual ~SavitzkyGolayFilter();
00117 
00121     template <typename PeakType>
00122     void filter(MSSpectrum<PeakType> & spectrum)
00123     {
00124       UInt n = (UInt)spectrum.size();
00125 
00126       typename MSSpectrum<PeakType>::iterator first = spectrum.begin();
00127       typename MSSpectrum<PeakType>::iterator last = spectrum.end();
00128 
00129       //copy the data AND META DATA to the output container
00130       MSSpectrum<PeakType> output = spectrum;
00131 
00132       if (frame_size_ > n)
00133       {
00134         return;
00135       }
00136 
00137       int i;
00138       UInt j;
00139       int mid = (frame_size_ / 2);
00140       double help;
00141 
00142       typename MSSpectrum<PeakType>::iterator it_forward;
00143       typename MSSpectrum<PeakType>::iterator it_help;
00144       typename MSSpectrum<PeakType>::iterator out_it = output.begin();
00145 
00146       // compute the transient on
00147       for (i = 0; i <= mid; ++i)
00148       {
00149         it_forward = (first - i);
00150         help = 0;
00151 
00152         for (j = 0; j < frame_size_; ++j)
00153         {
00154           help += it_forward->getIntensity() * coeffs_[(i + 1) * frame_size_ - 1 - j];
00155           ++it_forward;
00156         }
00157 
00158 
00159         out_it->setPosition(first->getPosition());
00160         out_it->setIntensity(std::max(0.0, help));
00161         ++out_it;
00162         ++first;
00163       }
00164 
00165       // compute the steady state output
00166       it_help = (last - mid);
00167       while (first != it_help)
00168       {
00169         it_forward = (first - mid);
00170         help = 0;
00171 
00172         for (j = 0; j < frame_size_; ++j)
00173         {
00174           help += it_forward->getIntensity() * coeffs_[mid * frame_size_ + j];
00175           ++it_forward;
00176         }
00177 
00178 
00179         out_it->setPosition(first->getPosition());
00180         out_it->setIntensity(std::max(0.0, help));
00181         ++out_it;
00182         ++first;
00183       }
00184 
00185       // compute the transient off
00186       for (i = (mid - 1); i >= 0; --i)
00187       {
00188         it_forward = (first - (frame_size_ - i - 1));
00189         help = 0;
00190 
00191         for (j = 0; j < frame_size_; ++j)
00192         {
00193           help += it_forward->getIntensity() * coeffs_[i * frame_size_ + j];
00194           ++it_forward;
00195         }
00196 
00197         out_it->setPosition(first->getPosition());
00198         out_it->setIntensity(std::max(0.0, help));
00199         ++out_it;
00200         ++first;
00201       }
00202 
00203       spectrum = output;
00204     }
00205 
00209     template <typename PeakType>
00210     void filterExperiment(MSExperiment<PeakType> & map)
00211     {
00212       startProgress(0, map.size(), "smoothing data");
00213       for (Size i = 0; i < map.size(); ++i)
00214       {
00215         filter(map[i]);
00216         setProgress(i);
00217       }
00218       endProgress();
00219     }
00220 
00221 protected:
00223     std::vector<DoubleReal> coeffs_;
00225     UInt frame_size_;
00227     UInt order_;
00228     // Docu in base class
00229     virtual void updateMembers_();
00230 
00231   };
00232 
00233 } // namespace OpenMS
00234 #endif // OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:44 using doxygen 1.7.1