Vespucci  1.0.0
bandwidth.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 *******************************************************************************/
20 
22 
32 double Vespucci::Math::Quantification::FindBandwidth(const arma::vec &X, arma::uword min_index, arma::uword max_index, arma::vec &midline, arma::vec &baseline, double abscissa_step)
33 {
34  arma::vec region = X.subvec(min_index, max_index);
35  arma::uword size = region.n_elem;
36  double maximum, half_maximum;
37  double start_value, end_value;
38  midline.set_size(size);
39  max_index = 0;
40  arma::uword left_index = 0;
41  arma::uword right_index = 0;
42  start_value = X(min_index);
43  end_value = X(max_index);
44  baseline = arma::linspace(start_value, end_value, size);
45  region -= baseline;
46  maximum = region.max();
47  half_maximum = maximum / 2.0;
48  arma::uvec max_indices = find(region == maximum);
49  max_index = max_indices(0);
50 
51  //search for left inflection point
52  for (arma::uword i = max_index; i > 0; --i){
53  if (X(i) - half_maximum < 0){
54  left_index = i;
55  break;
56  }
57  }
58 
59  //search for right inflection point
60  for (arma::uword i = max_index; i < size; ++i){
61  if (X(i) - half_maximum < 0){
62  right_index = i;
63  break;
64  }
65  }
66 
67  //check to make sure the values on the other side of the inflection point aren't better
68  if (left_index > 0 && right_index < size - 1){
69  if(std::fabs(X(left_index) - half_maximum) > std::fabs(X(left_index - 1) - half_maximum)){
70  --left_index;
71  }
72 
73  if (std::fabs(X(right_index) - half_maximum) > std::fabs(X(right_index + 1) - half_maximum)){
74  ++right_index;
75  }
76  }
77 
78  double region_size = region.subvec(left_index, right_index).n_elem;
79 
80  return abscissa_step * region_size;
81 }
82 
93 arma::vec Vespucci::Math::Quantification::FindBandwidthMat(const arma::mat &X, arma::vec abscissa, double &min, double &max, arma::mat &midlines, arma::mat &baselines, arma::uvec &boundaries)
94 {
95  double delta = std::abs(abscissa(1) - abscissa(0));
96  arma::uvec left_bound = find(((min-delta) <= abscissa) && (abscissa <= (min+delta)));
97  arma::uvec right_bound = find(((max-delta) <= abscissa) && (abscissa <= (max+delta)));
98 
99  arma::uword min_index = left_bound(0);
100  arma::uword max_index = right_bound(0);
101  boundaries << min_index << arma::endr << max_index;
102 
103  min = abscissa(min_index);
104  max = abscissa(max_index);
105 
106  arma::uword size = abscissa.subvec(min_index, max_index).n_elem;
107  arma::vec results(X.n_cols);
108  midlines.set_size(X.n_cols, size);
109  baselines.set_size(X.n_cols, size);
110  arma::vec midline;
111  arma::vec baseline;
112  for (arma::uword i = 0; i < X.n_cols; ++i){
113  results(i) = FindBandwidth(X.col(i), min_index, max_index, midline, baseline, delta);
114  midlines.row(i) = midline;
115  baselines.row(i) = baseline;
116  }
117 
118  return results;
119 }
VESPUCCI_EXPORT arma::vec FindBandwidthMat(const arma::mat &X, arma::vec abscissa, double &min, double &max, arma::mat &midlines, arma::mat &baselines, arma::uvec &boundaries)
Vespucci::Math::Quantification::FindBandwidthMat.
Definition: bandwidth.cpp:93
VESPUCCI_EXPORT arma::uword max(arma::uword a, arma::uword b)
Vespucci::Math::max.
Definition: accessory.cpp:237
VESPUCCI_EXPORT arma::uword min(arma::uword a, arma::uword b)
Vespucci::Math::min.
Definition: accessory.cpp:249
VESPUCCI_EXPORT double FindBandwidth(const arma::vec &X, arma::uword min_index, arma::uword max_index, arma::vec &midline, arma::vec &baseline, double abscissa_step)
Vespucci::Math::Quantification::FindBandwidth.
Definition: bandwidth.cpp:32