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 #include "filternameinterface.h"
42 
43 
44 namespace pappso
45 {
46 
47 
48 //! Parameters for the Savitzky-Golay filter
50 {
51  int nL = 15;
52  //!< number of data points on the left of the filtered point
53  int nR = 15;
54  //!< number of data points on the right of the filtered point
55  int m = 4;
56  //!< order of the polynomial to use in the regression analysis leading to the
57  //! Savitzky-Golay coefficients (typically between 2 and 6)
58  int lD = 0;
59  //!< specifies the order of the derivative to extract from the Savitzky-Golay
60  //! smoothing algorithm (for regular smoothing, use 0)
61  bool convolveWithNr = false;
62  //!< set to false for best results
63 
65 
66  SavGolParams(const SavGolParams &other)
67  : nL{other.nL},
68  nR{other.nR},
69  m{other.m},
70  lD{other.lD},
71  convolveWithNr{other.convolveWithNr}
72  {
73  }
74 
76  int nLParam, int nRParam, int mParam, int lDParam, bool convolveWithNrParam)
77  {
78  nL = nLParam;
79  nR = nRParam;
80  m = mParam;
81  lD = lDParam;
82  convolveWithNr = convolveWithNrParam;
83  }
84 
85  void
87  int nLParam, int nRParam, int mParam, int lDParam, bool convolveWithNrParam)
88  {
89  nL = nLParam;
90  nR = nRParam;
91  m = mParam;
92  lD = lDParam;
93  convolveWithNr = convolveWithNrParam;
94  }
95 
96  void
97  initialize(const SavGolParams &other)
98  {
99  nL = other.nL;
100  nR = other.nR;
101  m = other.m;
102  lD = other.lD;
103  convolveWithNr = other.convolveWithNr;
104  }
105 
106  QString
107  toString() const
108  {
109  return QString("%1;%2;%3;%4;%5")
110  .arg(QString::number(nL))
111  .arg(QString::number(nR))
112  .arg(QString::number(m))
113  .arg(QString::number(lD))
114  .arg(convolveWithNr ? "true" : "false");
115  }
116 };
117 
118 
119 class FilterSavitzkyGolay;
120 
121 typedef std::shared_ptr<FilterSavitzkyGolay> FilterSavitzkyGolaySPtr;
122 typedef std::shared_ptr<const FilterSavitzkyGolay> FilterSavitzkyGolayCstSPtr;
123 
124 
125 /**
126  * @brief uses Savitsky-Golay filter on trace
127  */
129 {
130  public:
131  /** Construct a FilterSavitzkyGolay instance using the Savitzky-Golay
132  parameters \param nL number of data point left of the point being filtered
133  \param nR number of data point right of the point being filtered
134  \param m order of the polynomial to use in the regression analysis
135  \param lD order of the derivative to extract
136  \param convolveWithNr set to false
137  */
139  int nL, int nR, int m, int lD, bool convolveWithNr = false);
140 
141  FilterSavitzkyGolay(const SavGolParams sav_gol_params);
142 
143  FilterSavitzkyGolay(const QString &parameters);
144 
145  /**
146  * Copy constructor
147  *
148  * @param other TODO
149  */
151 
152  /**
153  * Destructor
154  */
155  virtual ~FilterSavitzkyGolay();
156 
157  FilterSavitzkyGolay &operator=(const FilterSavitzkyGolay &other);
158 
159  Trace &filter(Trace &data_points) const override;
160 
161  SavGolParams getParameters() const;
162 
163  char runFilter(double *y_data_p,
164  double *y_filtered_data_p,
165  int data_point_count) const;
166 
167  void filteredData(std::vector<pappso_double> &data);
168 
169  QString name() const override;
170 
171  // Utility function
172 
173  QString toString() const override;
174 
175  protected:
176  void buildFilterFromString(const QString &strBuildParams) override;
177 
178  private:
179  ///// Parameters to configure the Savitzky-Golay filter algorithm
180 
182 
183  ///// Data used for running the algorithm.
184 
185  //! C array of keys of the Trace
187 
188  //! C array of raw values of the Trace
190 
191  //! C array of filtered values after the computation has been performed
193 
194 
195  ///// Functions that actually implement the algorithm.
196  int *ivector(long nl, long nh) const;
197  pappso_double *dvector(long nl, long nh) const;
198  pappso_double **dmatrix(long nrl, long nrh, long ncl, long nch) const;
199  void free_ivector(int *v, long nl, long nh) const;
200  void free_dvector(pappso_double *v, long nl, long nh) const;
201  void
202  free_dmatrix(pappso_double **m, long nrl, long nrh, long ncl, long nch) const;
203  void lubksb(pappso_double **a, int n, int *indx, pappso_double b[]) const;
204  void ludcmp(pappso_double **a, int n, int *indx, pappso_double *d) const;
205  void four1(pappso_double data[], unsigned long nn, int isign);
206  void twofft(pappso_double data1[],
207  pappso_double data2[],
208  pappso_double fft1[],
209  pappso_double fft2[],
210  unsigned long n);
211  void realft(pappso_double data[], unsigned long n, int isign);
212  char convlv(pappso_double data[],
213  unsigned long n,
214  pappso_double respns[],
215  unsigned long m,
216  int isign,
217  pappso_double ans[]);
218  char sgcoeff(pappso_double c[], int np, int nl, int nr, int ld, int m) const;
219 };
220 } // namespace pappso
Interface that allows to build filter objects from strings.
uses Savitsky-Golay filter on trace
Definition: savgolfilter.h:129
pappso_double * m_yr
C array of raw values of the Trace.
Definition: savgolfilter.h:189
void filteredData(std::vector< pappso_double > &data)
pappso_double * m_x
C array of keys of the Trace.
Definition: savgolfilter.h:186
pappso_double * m_yf
C array of filtered values after the computation has been performed.
Definition: savgolfilter.h:192
A simple container of DataPoint instances.
Definition: trace.h:148
#define PMSPP_LIB_DECL
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
std::shared_ptr< FilterSavitzkyGolay > FilterSavitzkyGolaySPtr
Definition: savgolfilter.h:119
std::shared_ptr< const FilterSavitzkyGolay > FilterSavitzkyGolayCstSPtr
Definition: savgolfilter.h:122
double pappso_double
A type definition for doubles.
Definition: types.h:49
Parameters for the Savitzky-Golay filter.
Definition: savgolfilter.h:50
SavGolParams(int nLParam, int nRParam, int mParam, int lDParam, bool convolveWithNrParam)
Definition: savgolfilter.h:75
SavGolParams(const SavGolParams &other)
Definition: savgolfilter.h:66
void initialize(int nLParam, int nRParam, int mParam, int lDParam, bool convolveWithNrParam)
Definition: savgolfilter.h:86
QString toString() const
Definition: savgolfilter.h:107
int nR
number of data points on the right of the filtered point
Definition: savgolfilter.h:53
int nL
number of data points on the left of the filtered point
Definition: savgolfilter.h:51
bool convolveWithNr
set to false for best results
Definition: savgolfilter.h:61
void initialize(const SavGolParams &other)
Definition: savgolfilter.h:97