Vespucci  1.0.0
integration.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2014-2016 Wright State University - All Rights Reserved
3  Daniel P. Foose - Maintainer/Lead Developer
4 
5  This file is part of Vespucci.
6 
7  Vespucci is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Vespucci is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Vespucci. If not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************************/
31 double Vespucci::Math::Quantification::IntegratePeak(const arma::vec &X, arma::uword min_index, arma::uword max_index, double abscissa_step, arma::vec &baseline, bool correct_baseline)
32 {
33  arma::vec region = X.subvec(min_index, max_index);
34 
35  double start = X(min_index);
36  double end = X(max_index);
37 
38  if (correct_baseline){baseline = arma::linspace(start, end, region.n_elem);}
39  else{baseline = arma::zeros(region.n_elem);}
40 
41  double baseline_area = sum(baseline) / abscissa_step;
42  double region_area = sum(region) / abscissa_step;
43  double value = (correct_baseline ? region_area - baseline_area : region_area);
44  return value;
45 }
46 
57 arma::vec Vespucci::Math::Quantification::IntegratePeakMat(const arma::mat &X, const arma::vec &abscissa, double &min, double &max, arma::mat &baselines, arma::uvec &boundaries, bool correct_baseline)
58 {
59  double delta = std::abs(abscissa(1) - abscissa(0));
60  arma::uvec left_bound = find(((min-delta) <= abscissa) && (abscissa <= (min+delta)));
61  arma::uvec right_bound = find(((max-delta) <= abscissa) && (abscissa <= (max+delta)));
62 
63  arma::uword min_index = left_bound(0);
64  arma::uword max_index = right_bound(0);
65  min = abscissa(min_index);
66  max = abscissa(max_index);
67  boundaries << min_index << arma::endr << max_index;
68  arma::vec results(X.n_cols);
69  baselines.set_size(X.col(0).subvec(min_index, max_index).n_elem, X.n_cols);
70  arma::vec baseline(baselines.n_cols);
71  for (arma::uword i = 0; i < X.n_cols; ++i){
72  results(i) = IntegratePeak(X.col(i), min_index, max_index, delta, baseline, correct_baseline);
73  baselines.col(i) = baseline;
74  }
75 
76  return results;
77 }
78 
91 arma::mat Vespucci::Math::Quantification::IntegratePeaksMat(const arma::mat &X, const arma::vec &abscissa, double &first_min, double &first_max, double &second_min, double &second_max, arma::mat &first_baselines, arma::mat &second_baselines, arma::uvec &boundaries)
92 {
93  double delta = std::abs(abscissa(1) - abscissa(0));
94  arma::uvec first_left_bound = find(((first_min-delta) <= abscissa) && (abscissa <= (first_min+delta)));
95  arma::uvec first_right_bound = find(((first_max-delta) <= abscissa) && (abscissa <= (first_max+delta)));
96  arma::uvec second_left_bound = find(((second_min-delta) <= abscissa) && (abscissa <= (second_min+delta)));
97  arma::uvec second_right_bound = find(((second_max-delta) <= abscissa) && (abscissa <= (second_max+delta)));
98 
99  arma::uword first_min_index = first_left_bound(0);
100  arma::uword first_max_index = first_right_bound(0);
101  arma::uword second_min_index = second_left_bound(0);
102  arma::uword second_max_index = second_right_bound(0);
103 
104  first_min = abscissa(first_min_index);
105  first_max = abscissa(first_max_index);
106  second_min = abscissa(second_min_index);
107  second_max = abscissa(second_max_index);
108 
109  boundaries.set_size(4);
110  boundaries(0) = first_min_index;
111  boundaries(1) = first_max_index;
112  boundaries(2) = second_min_index;
113  boundaries(3) = second_max_index;
114  //The << operator doesn't seem to work on arma::uvecs...
115  /*
116  boundaries << first_min_index << arma::endr << first_max_index <<
117  second_min_index << arma::endr << second_max_index;
118  */
119  first_baselines.set_size(X.col(0).subvec(first_min_index, first_max_index).n_elem, X.n_cols);
120  arma::vec first_baseline(first_baselines.n_cols);
121  second_baselines.set_size(X.col(0).subvec(second_min_index, second_max_index).n_elem, X.n_cols);
122  arma::vec second_baseline(second_baselines.n_cols);
123 
124  arma::mat results (X.n_cols, 2);
125  for (arma::uword i = 0; i < X.n_cols; ++i){
126  results(i, 0) = IntegratePeak(X.col(i), first_min_index, first_max_index, delta, first_baseline, true);
127  first_baselines.col(i) = first_baseline;
128  }
129  for (arma::uword i = 0; i < X.n_cols; ++i){
130  results(i, 1) = IntegratePeak(X.col(i), second_min_index, second_max_index, delta, second_baseline, true);
131  second_baselines.col(i) = second_baseline;
132  }
133  return results;
134 }
135 
147 arma::vec Vespucci::Math::Quantification::IntegratePeakMat(const arma::mat &X, const arma::vec &abscissa,
148  double &min, double &max,
149  arma::field<arma::vec> &baselines,
150  arma::uvec &boundaries, arma::uword bound_window, bool correct_baseline)
151 {
152  arma::vec results(X.n_cols);
153  double delta = std::abs(abscissa(1) - abscissa(0)); //assumes monotonic to some degree of precision
154  arma::uvec left_bound = find(((min-delta) <= abscissa) && (abscissa <= (min+delta)));
155  arma::uvec right_bound = find(((max-delta) <= abscissa) && (abscissa <= (max+delta)));
156 
157  //initial centers
158  arma::uword min_index = left_bound(0);
159  arma::uword max_index = right_bound(0);
160  min = abscissa(min_index);
161  max = abscissa(max_index);
162 
163  arma::uword min_start = min_index - bound_window;
164  arma::uword min_end = min_index + bound_window;
165  min_end = (min_end >= abscissa.n_rows ? abscissa.n_rows - 1 : min_end);
166 
167  arma::uword max_start = max_index - bound_window;
168  arma::uword max_end = max_index + bound_window;
169  max_end = (max_end >= abscissa.n_rows ? abscissa.n_rows - 1 : max_end);
170 
171  baselines.set_size(X.n_cols);
172  for (arma::uword i = 0; i < X.n_cols; ++i){
173  arma::vec spectrum = X.col(i);
174  arma::vec min_window = spectrum.rows(min_start, min_end);
175  arma::vec max_window = spectrum.rows(max_start, max_end);
176  min_index = Vespucci::Math::LocalMinimum(min_window, min);
177  max_index = Vespucci::Math::LocalMinimum(max_window, max);
178  arma::vec baseline(spectrum.rows(min_index, max_index).n_rows);
179  results(i) = IntegratePeak(spectrum, min_index, max_index, delta, baseline, correct_baseline);
180  baselines(i) = baseline;
181  }
182  return results;
183 }
184 
199 arma::mat Vespucci::Math::Quantification::IntegratePeaksMat(const arma::mat &X, const arma::vec &abscissa,
200  double &first_min, double &first_max,
201  double &second_min, double &second_max,
202  arma::field<arma::vec> &first_baselines, arma::field<arma::vec> &second_baselines,
203  arma::uvec &boundaries, arma::uword bound_window)
204 {
205  arma::mat results(X.n_cols, 2);
206  double delta = std::abs(abscissa(1) - abscissa(0));
207  arma::uvec first_left_bound = find(((first_min-delta) <= abscissa) && (abscissa <= (first_min+delta)));
208  arma::uvec first_right_bound = find(((first_max-delta) <= abscissa) && (abscissa <= (first_max+delta)));
209  arma::uvec second_left_bound = find(((second_min-delta) <= abscissa) && (abscissa <= (second_min+delta)));
210  arma::uvec second_right_bound = find(((second_max-delta) <= abscissa) && (abscissa <= (second_max+delta)));
211 
212  arma::uword first_min_index = first_left_bound(0);
213  arma::uword first_max_index = first_right_bound(0);
214  arma::uword second_min_index = second_left_bound(0);
215  arma::uword second_max_index = second_right_bound(0);
216 
217  first_min = abscissa(first_min_index);
218  first_max = abscissa(first_max_index);
219  second_min = abscissa(second_min_index);
220  second_max = abscissa(second_max_index);
221 
222  arma::uword first_min_start = first_min_index - bound_window;
223  arma::uword first_min_end = first_min_index + bound_window;
224  first_min_end = (first_min_end >= abscissa.n_rows ? abscissa.n_rows - 1 : first_min_end);
225  arma::uword second_min_start = second_min_index - bound_window;
226  arma::uword second_min_end = second_min_index + bound_window;
227  second_min_end = (second_min_end >= abscissa.n_rows ? abscissa.n_rows - 1 : second_min_end);
228 
229  arma::uword first_max_start = first_max_index - bound_window;
230  arma::uword first_max_end = first_max_index + bound_window;
231  first_max_end = (first_max_end >= abscissa.n_rows ? abscissa.n_rows - 1 : first_max_end);
232  arma::uword second_max_start = second_max_index - bound_window;
233  arma::uword second_max_end = second_max_index + bound_window;
234  second_max_end = (second_max_end >= abscissa.n_rows ? abscissa.n_rows - 1 : second_max_end);
235 
236  first_baselines.set_size(X.n_cols);
237  second_baselines.set_size(X.n_cols);
238 
239  for (arma::uword i = 0; i < X.n_cols; ++i){
240  arma::vec spectrum = X.col(i);
241 
242  arma::vec first_min_window = spectrum.rows(first_min_start, first_min_end);
243  arma::vec first_max_window = spectrum.rows(first_max_start, first_max_end);
244  first_min_index = Vespucci::Math::LocalMinimum(first_min_window, first_min);
245  first_max_index = Vespucci::Math::LocalMinimum(first_max_window, first_max);
246  arma::vec first_baseline(spectrum.rows(first_min_index, first_max_index).n_rows);
247  results(i, 0) = IntegratePeak(spectrum, first_min_index, first_max_index, delta, first_baseline, true);
248  first_baselines(i) = first_baseline;
249 
250  arma::vec second_min_window = spectrum.rows(second_min_start, second_min_end);
251  arma::vec second_max_window = spectrum.rows(second_max_start, second_max_end);
252  second_min_index = Vespucci::Math::LocalMinimum(second_min_window, second_min);
253  second_max_index = Vespucci::Math::LocalMinimum(second_max_window, second_max);
254  arma::vec second_baseline(spectrum.rows(second_min_index, second_max_index).n_rows);
255  results(i, 1) = IntegratePeak(spectrum, second_min_index, second_max_index, delta, second_baseline, true);
256  second_baselines(i) = second_baseline;
257  }
258 
259  boundaries.set_size(4);
260  boundaries(0) = first_min_index;
261  boundaries(1) = first_max_index;
262  boundaries(2) = second_min_index;
263  boundaries(3) = second_max_index;
264 
265  return results;
266 }
VESPUCCI_EXPORT arma::vec IntegratePeakMat(const arma::mat &X, const arma::vec &abscissa, double &min, double &max, arma::mat &baselines, arma::uvec &boundaries, bool correct_baseline)
Vespucci::Math::Quantification::IntegratePeakMat.
Definition: integration.cpp:57
VESPUCCI_EXPORT arma::uword max(arma::uword a, arma::uword b)
Vespucci::Math::max.
Definition: accessory.cpp:237
VESPUCCI_EXPORT arma::uword LocalMinimum(const arma::mat &X, double &value)
Vespucci::Math::LocalMinimum.
Definition: accessory.cpp:77
VESPUCCI_EXPORT arma::uword min(arma::uword a, arma::uword b)
Vespucci::Math::min.
Definition: accessory.cpp:249
VESPUCCI_EXPORT arma::mat IntegratePeaksMat(const arma::mat &X, const arma::vec &abscissa, double &first_min, double &first_max, double &second_min, double &second_max, arma::mat &first_baselines, arma::mat &second_baselines, arma::uvec &boundaries)
Vespucci::Math::Quantification::IntegratePeaksMat.
Definition: integration.cpp:91
VESPUCCI_EXPORT double IntegratePeak(const arma::vec &X, arma::uword min_index, arma::uword max_index, double abscissa_step, arma::vec &baseline, bool correct_baseline)
Vespucci::Math::Quantification::IntegratePeak.
Definition: integration.cpp:31