libpappsomspp
Library for mass spectrometry
savgolfilter.h
Go to the documentation of this file.
1 /* BEGIN software license
2  *
3  * msXpertSuite - mass spectrometry software suite
4  * -----------------------------------------------
5  * Copyright(C) 2009,...,2018 Filippo Rusconi
6  *
7  * http://www.msxpertsuite.org
8  *
9  * This file is part of the msXpertSuite project.
10  *
11  * The msXpertSuite project is the successor of the massXpert project. This
12  * project now includes various independent modules:
13  *
14  * - massXpert, model polymer chemistries and simulate mass spectrometric data;
15  * - mineXpert, a powerful TIC chromatogram/mass spectrum viewer/miner;
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  *
30  * END software license
31  */
32 
33 
34 #pragma once
35 
36 
37 #include <QObject>
38 
39 #include "../../trace/trace.h"
40 #include "../../exportinmportconfig.h"
41 
42 
43 namespace pappso
44 {
45 
46 
47 //! Parameters for the Savitzky-Golay filter
49 {
50  int nL = 15;
51  //!< number of data points on the left of the filtered point
52  int nR = 15;
53  //!< number of data points on the right of the filtered point
54  int m = 4;
55  //!< order of the polynomial to use in the regression analysis leading to the
56  //! Savitzky-Golay coefficients (typically between 2 and 6)
57  int lD = 0;
58  //!< specifies the order of the derivative to extract from the Savitzky-Golay
59  //! smoothing algorithm (for regular smoothing, use 0)
60  bool convolveWithNr = false;
61  //!< set to false for best results
62 
64 
65  SavGolParams(const SavGolParams &other)
66  : nL{other.nL},
67  nR{other.nR},
68  m{other.m},
69  lD{other.lD},
70  convolveWithNr{other.convolveWithNr}
71  {
72  }
73 
75  int nLParam, int nRParam, int mParam, int lDParam, bool convolveWithNrParam)
76  {
77  nL = nLParam;
78  nR = nRParam;
79  m = mParam;
80  lD = lDParam;
81  convolveWithNr = convolveWithNrParam;
82  }
83 
84  void
86  int nLParam, int nRParam, int mParam, int lDParam, bool convolveWithNrParam)
87  {
88  nL = nLParam;
89  nR = nRParam;
90  m = mParam;
91  lD = lDParam;
92  convolveWithNr = convolveWithNrParam;
93  }
94 
95  void
96  initialize(const SavGolParams &other)
97  {
98  nL = other.nL;
99  nR = other.nR;
100  m = other.m;
101  lD = other.lD;
102  convolveWithNr = other.convolveWithNr;
103  }
104 };
105 
106 
107 /**
108  * @brief uses Savitsky-Golay filter on trace
109  */
111 {
112  public:
113  /** Construct a FilterSavitzkyGolay instance using the Savitzky-Golay
114  parameters \param nL number of data point left of the point being filtered
115  \param nR number of data point right of the point being filtered
116  \param m order of the polynomial to use in the regression analysis
117  \param lD order of the derivative to extract
118  \param convolveWithNr set to false
119  */
121  int nL, int nR, int m, int lD, bool convolveWithNr = false);
122 
123  /**
124  * Copy constructor
125  *
126  * @param other TODO
127  */
129 
130  /**
131  * Destructor
132  */
133  virtual ~FilterSavitzkyGolay();
134 
135  FilterSavitzkyGolay &operator=(const FilterSavitzkyGolay &other);
136 
137  Trace &filter(Trace &data_points) const override;
138 
139  SavGolParams getParameters() const;
140 
141  char runFilter(double *y_data_p,
142  double *y_filtered_data_p,
143  int data_point_count) const;
144 
145  void filteredData(std::vector<pappso_double> &data);
146 
147  private:
148  ///// Parameters to configure the Savitzky-Golay filter algorithm
149 
150  int m_nL = 15;
151  //!< number of data points on the left of the filtered point
152  int m_nR = 15;
153  //!< number of data points on the right of the filtered point
154  int m_m = 4;
155  //!< order of the polynomial to use in the regression analysis leading to the
156  //! Savitzky-Golay coefficients (typically between 2 and 6)
157  int m_lD = 0;
158  //!< specifies the order of the derivative to extract from the Savitzky-Golay
159  //! smoothing algorithm (for regular smoothing, use 0)
160  bool m_convolveWithNr = false;
161  //!< set to false for best results
162 
163  ///// Data used for running the algorithm.
164 
165  //! C array of keys of the Trace
167 
168  //! C array of raw values of the Trace
170 
171  //! C array of filtered values after the computation has been performed
173 
174 
175  ///// Functions that actually implement the algorithm.
176  int *ivector(long nl, long nh) const;
177  pappso_double *dvector(long nl, long nh) const;
178  pappso_double **dmatrix(long nrl, long nrh, long ncl, long nch) const;
179  void free_ivector(int *v, long nl, long nh) const;
180  void free_dvector(pappso_double *v, long nl, long nh) const;
181  void
182  free_dmatrix(pappso_double **m, long nrl, long nrh, long ncl, long nch) const;
183  void lubksb(pappso_double **a, int n, int *indx, pappso_double b[]) const;
184  void ludcmp(pappso_double **a, int n, int *indx, pappso_double *d) const;
185  void four1(pappso_double data[], unsigned long nn, int isign);
186  void twofft(pappso_double data1[],
187  pappso_double data2[],
188  pappso_double fft1[],
189  pappso_double fft2[],
190  unsigned long n);
191  void realft(pappso_double data[], unsigned long n, int isign);
192  char convlv(pappso_double data[],
193  unsigned long n,
194  pappso_double respns[],
195  unsigned long m,
196  int isign,
197  pappso_double ans[]);
198  char sgcoeff(pappso_double c[], int np, int nl, int nr, int ld, int m) const;
199 
200  // Utility function
201 
202  QString toString() const;
203 };
204 } // namespace pappso
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:48
pappso::FilterSavitzkyGolay::m_x
pappso_double * m_x
C array of keys of the Trace.
Definition: savgolfilter.h:166
pappso::FilterSavitzkyGolay::m_yf
pappso_double * m_yf
C array of filtered values after the computation has been performed.
Definition: savgolfilter.h:172
pappso::SavGolParams::m
int m
Definition: savgolfilter.h:54
pappso::SavGolParams::SavGolParams
SavGolParams()
Definition: savgolfilter.h:63
PMSPP_LIB_DECL
#define PMSPP_LIB_DECL
Definition: exportinmportconfig.h:14
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
pappso::SavGolParams::initialize
void initialize(const SavGolParams &other)
Definition: savgolfilter.h:96
pappso::FilterInterface
generic interface to apply a filter on a trace
Definition: filterinterface.h:40
pappso::SavGolParams::SavGolParams
SavGolParams(const SavGolParams &other)
Definition: savgolfilter.h:65
pappso::Trace
A simple container of DataPoint instances.
Definition: trace.h:132
pappso::SavGolParams
Parameters for the Savitzky-Golay filter.
Definition: savgolfilter.h:49
pappso::SavGolParams::lD
int lD
Definition: savgolfilter.h:57
pappso::FilterSavitzkyGolay::m_yr
pappso_double * m_yr
C array of raw values of the Trace.
Definition: savgolfilter.h:169
pappso::SavGolParams::nR
int nR
number of data points on the right of the filtered point
Definition: savgolfilter.h:52
pappso::SavGolParams::nL
int nL
number of data points on the left of the filtered point
Definition: savgolfilter.h:50
pappso::SavGolParams::initialize
void initialize(int nLParam, int nRParam, int mParam, int lDParam, bool convolveWithNrParam)
Definition: savgolfilter.h:85
pappso::SavGolParams::convolveWithNr
bool convolveWithNr
set to false for best results
Definition: savgolfilter.h:60
pappso::SavGolParams::SavGolParams
SavGolParams(int nLParam, int nRParam, int mParam, int lDParam, bool convolveWithNrParam)
Definition: savgolfilter.h:74
pappso::FilterSavitzkyGolay
uses Savitsky-Golay filter on trace
Definition: savgolfilter.h:111
pappso::FilterSavitzkyGolay::filteredData
void filteredData(std::vector< pappso_double > &data)