Vespucci  1.0.0
nonlinear.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 *******************************************************************************/
22 
30 arma::vec Vespucci::Math::Smoothing::MedianFilter(const arma::vec &X, arma::uword window_size)
31 {
32  arma::uword k = (window_size - 1) / 2;
33 
34  arma::vec filtered = X; //copy the whole thing, then add in the bit we actually filter
35  arma::vec buffer;
36  arma::uvec sorted;
37  //The armadillo median function results in a crash on some mingw compilers
38  //This method might not be as fast, but it always works.
39  try{
40  //sort the window then pick the middle value
41  for (arma::uword i = k; i < (X.n_rows - k); ++i){
42  buffer = X.subvec(i-k, i+k);
43  sorted = stable_sort_index(buffer);
44  filtered(i) = buffer(sorted(k));
45  //filtered(i) = median(buffer);
46  }
47 
48  }catch(std::exception e){
49  std::cout << e.what();
50  }
51 
52  return filtered;
53 }
54 
61 arma::mat Vespucci::Math::Smoothing::MedianFilterMat(const arma::mat &X, arma::uword window_size)
62 {
63  arma::mat filtered;
64  filtered.set_size(X.n_rows, X.n_cols);
65  for(arma::uword i = 0; i < X.n_cols; ++i)
66  filtered.col(i) = MedianFilter(X.col(i), window_size);
67  return filtered;
68 }
VESPUCCI_EXPORT arma::vec MedianFilter(const arma::vec &X, arma::uword window_size)
Vespucci::Math::Smoothing::MedianFilter.
Definition: nonlinear.cpp:30
VESPUCCI_EXPORT arma::mat MedianFilterMat(const arma::mat &X, arma::uword window_size)
Vespucci::Math::Smoothing::MedianFilterMat.
Definition: nonlinear.cpp:61