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
44namespace 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
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
98 {
99 nL = other.nL;
100 nR = other.nR;
101 m = other.m;
102 lD = other.lD;
103 convolveWithNr = other.convolveWithNr;
104 }
105
106 void
107 initialize(const QString &parameters)
108 {
109 // Typical string: "15;15;4;0;false"
110
111 QStringList params_list = parameters.split(";");
112
113 nL = params_list.at(0).toInt();
114 nR = params_list.at(1).toInt();
115 m = params_list.at(2).toInt();
116 lD = params_list.at(3).toInt();
117 convolveWithNr = (params_list.at(4) == "true" ? true : false);
118 }
119
120 QString
121 toString() const
122 {
123 // Typical string: "15;15;4;0;false"
124 return QString("%1;%2;%3;%4;%5")
125 .arg(QString::number(nL))
126 .arg(QString::number(nR))
127 .arg(QString::number(m))
128 .arg(QString::number(lD))
129 .arg(convolveWithNr ? "true" : "false");
130 }
131};
132
133
134class FilterSavitzkyGolay;
135
136typedef std::shared_ptr<FilterSavitzkyGolay> FilterSavitzkyGolaySPtr;
137typedef std::shared_ptr<const FilterSavitzkyGolay> FilterSavitzkyGolayCstSPtr;
138
139
140/**
141 * @brief uses Savitsky-Golay filter on trace
142 */
144{
145 public:
147 /** Construct a FilterSavitzkyGolay instance using the Savitzky-Golay
148 parameters \param nL number of data point left of the point being filtered
149 \param nR number of data point right of the point being filtered
150 \param m order of the polynomial to use in the regression analysis
151 \param lD order of the derivative to extract
152 \param convolveWithNr set to false
153 */
155 int nL, int nR, int m, int lD, bool convolveWithNr = false);
156
157 FilterSavitzkyGolay(const SavGolParams sav_gol_params);
158
159 FilterSavitzkyGolay(const QString &parameters);
160
161 /**
162 * Copy constructor
163 *
164 * @param other TODO
165 */
167
168 /**
169 * Destructor
170 */
171 virtual ~FilterSavitzkyGolay();
172
173 FilterSavitzkyGolay &operator=(const FilterSavitzkyGolay &other);
174
175 Trace &filter(Trace &data_points) const override;
176
177 SavGolParams getParameters() const;
178
179 char runFilter(double *y_data_p,
180 double *y_filtered_data_p,
181 int data_point_count) const;
182
183 void filteredData(std::vector<pappso_double> &data);
184
185 QString name() const override;
186
187 // Utility function
188
189 QString toString() const override;
190
191 protected:
192 void buildFilterFromString(const QString &strBuildParams) override;
193
194 private:
195 ///// Parameters to configure the Savitzky-Golay filter algorithm
196 // The default values in m_params work fine for mass spectra.
198
199 ///// Data used for running the algorithm.
200
201 //! C array of keys of the Trace
203
204 //! C array of raw values of the Trace
206
207 //! C array of filtered values after the computation has been performed
209
210
211 ///// Functions that actually implement the algorithm.
212 int *ivector(long nl, long nh) const;
213 pappso_double *dvector(long nl, long nh) const;
214 pappso_double **dmatrix(long nrl, long nrh, long ncl, long nch) const;
215 void free_ivector(int *v, long nl, long nh) const;
216 void free_dvector(pappso_double *v, long nl, long nh) const;
217 void
218 free_dmatrix(pappso_double **m, long nrl, long nrh, long ncl, long nch) const;
219 void lubksb(pappso_double **a, int n, int *indx, pappso_double b[]) const;
220 void ludcmp(pappso_double **a, int n, int *indx, pappso_double *d) const;
221 void four1(pappso_double data[], unsigned long nn, int isign);
222 void twofft(pappso_double data1[],
223 pappso_double data2[],
224 pappso_double fft1[],
225 pappso_double fft2[],
226 unsigned long n);
227 void realft(pappso_double data[], unsigned long n, int isign);
228 char convlv(pappso_double data[],
229 unsigned long n,
230 pappso_double respns[],
231 unsigned long m,
232 int isign,
233 pappso_double ans[]);
234 char sgcoeff(pappso_double c[], int np, int nl, int nr, int ld, int m) const;
235};
236} // namespace pappso
Interface that allows to build filter objects from strings.
uses Savitsky-Golay filter on trace
Definition: savgolfilter.h:144
pappso_double * m_yr
C array of raw values of the Trace.
Definition: savgolfilter.h:205
void filteredData(std::vector< pappso_double > &data)
pappso_double * m_x
C array of keys of the Trace.
Definition: savgolfilter.h:202
pappso_double * m_yf
C array of filtered values after the computation has been performed.
Definition: savgolfilter.h:208
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:136
@ filter
concerning filters (psm, peptide, protein validation)
std::shared_ptr< const FilterSavitzkyGolay > FilterSavitzkyGolayCstSPtr
Definition: savgolfilter.h:137
double pappso_double
A type definition for doubles.
Definition: types.h:52
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:121
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
void initialize(const QString &parameters)
Definition: savgolfilter.h:107