Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
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
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
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
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
00229 virtual void updateMembers_();
00230
00231 };
00232
00233 }
00234 #endif // OPENMS_FILTERING_SMOOTHING_SAVITZKYGOLAYFILTER_H