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

SignalToNoiseEstimator.h (Maintainer: Chris Bielow)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Chris Bielow $
00025 // --------------------------------------------------------------------------
00026 //
00027 
00028 #ifndef OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATOR_H
00029 #define OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATOR_H
00030 
00031 #include <OpenMS/KERNEL/MSSpectrum.h>
00032 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
00033 #include <OpenMS/CONCEPT/ProgressLogger.h>
00034 
00035 #include <iostream>
00036 #include <vector>
00037 #include <cmath>
00038 #include <map>
00039 
00040 namespace OpenMS
00041 {
00049   template < typename Container = MSSpectrum< > >
00050   class SignalToNoiseEstimator: public DefaultParamHandler, public ProgressLogger
00051   {
00052   public:
00053 
00057     typedef typename Container::const_iterator PeakIterator;
00058     typedef typename PeakIterator::value_type PeakType;
00059     
00060     
00062     
00064     inline SignalToNoiseEstimator()
00065         : DefaultParamHandler("SignalToNoiseEstimator"),
00066           ProgressLogger(),
00067           first_(0),
00068           last_(0),
00069           is_result_valid_(false)
00070     {
00071     }
00072 
00074     inline SignalToNoiseEstimator(const SignalToNoiseEstimator&  source)
00075         : DefaultParamHandler(source),
00076           ProgressLogger(source),
00077         stn_estimates_( source.stn_estimates_),
00078         first_(source.first_),
00079         last_(source.last_),
00080         is_result_valid_(source.is_result_valid_)
00081     {}
00082 
00084     inline SignalToNoiseEstimator& operator=(const SignalToNoiseEstimator& source)
00085     {
00086       if(&source == this) return *this; 
00087       
00088       DefaultParamHandler::operator=(source);
00089       ProgressLogger::operator=(source);
00090       stn_estimates_ = source.stn_estimates_;
00091       first_ = source.first_;
00092       last_  = source.last_;
00093       return *this;
00094     }
00095     
00097     virtual ~SignalToNoiseEstimator()
00098     {}
00099 
00100      
00102     virtual void init(const PeakIterator& it_begin, const PeakIterator& it_end)
00103     {
00104       first_=it_begin;
00105       last_=it_end;
00106       computeSTN_(first_, last_);
00107       is_result_valid_ = true;
00108     }
00109       
00110           
00112     virtual void init(const Container& c)
00113     {
00114       init(c.begin(), c.end() ); 
00115     }
00116     
00122     virtual double getSignalToNoise(const PeakIterator& data_point)
00123     {
00124       if (!is_result_valid_)
00125       { 
00126         // recompute ...
00127         init(first_, last_);
00128       }
00129 
00130       return stn_estimates_[*data_point];
00131     }
00132     
00133     virtual double getSignalToNoise(const PeakType& data_point)
00134     {
00135       if (!is_result_valid_)
00136       { 
00137         // recompute ...
00138         init(first_, last_);
00139       }
00140 
00141       return stn_estimates_[data_point];
00142     }
00143 
00144   protected:
00145 
00146     virtual void computeSTN_(const PeakIterator& scan_first_, const PeakIterator& scan_last_) throw(Exception::InvalidValue) = 0;
00147         
00148 
00149 
00155     struct GaussianEstimate
00156     {
00157       double mean;   
00158       double variance; 
00159     };  
00160 
00161 
00163     inline GaussianEstimate estimate_(const PeakIterator& scan_first_, const PeakIterator& scan_last_) const
00164     {
00165       int size = 0;
00166       // add up
00167       double v = 0;
00168       double m = 0;
00169       PeakIterator run = scan_first_;
00170       while (run != scan_last_)
00171       {
00172         m += (*run).getIntensity();
00173         ++size;
00174         ++run;
00175       }
00176       //average
00177       m = m/size;
00178 
00179       //determine variance
00180       run = scan_first_;
00181       while (run != scan_last_)
00182       {
00183         v += std::pow(m - (*run).getIntensity(), 2);
00184         ++run;
00185       }
00186       v = v / ((double)size); // divide by n
00187 
00188       GaussianEstimate value = {m, v};
00189       return value;
00190     }
00191 
00192     //MEMBERS:
00193 
00195     std::map< PeakType, double, typename PeakType::PositionLess > stn_estimates_;
00196   
00198     PeakIterator first_;
00200     PeakIterator last_;
00202     mutable bool is_result_valid_;    
00203   };
00204 
00205 }// namespace OpenMS
00206 
00207 #endif //OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATOR_H

Generated Tue Apr 1 15:36:37 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1