ProteoWizard
simplepicker.hpp
Go to the documentation of this file.
1 //
2 // $Id: simplepicker.hpp 5313 2013-12-17 18:06:54Z chambm $
3 //
4 //
5 // Original author: Witold Wolski <wewolski@gmail.com>
6 //
7 // Copyright : ETH Zurich
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 #ifndef SIMPLEPICKER_H
23 #define SIMPLEPICKER_H
24 
25 #include <iterator>
26 #include <vector>
27 #include <stdexcept>
28 #include <boost/lexical_cast.hpp>
29 
30 namespace ralab
31 {
32  namespace base
33  {
34  namespace ms
35  {
36  /*! computes first derivative of a sequence, looks for zero crossings
37  */
38  template<class TReal>
39  struct SimplePicker{
40  std::vector<TReal> worker_;
41  double epsilon_;
42  bool problem_; //indicates if not the whole signal was picked.
43 
44  SimplePicker(TReal epsilon = 1e-3):epsilon_(epsilon),problem_(false){}
45  /*!
46  *returns number of zero crossings found
47  */
48  template<typename Tit, typename Outit>
49  size_t operator()(Tit beg, Tit end ,
50  Outit zerocrossings, //! picked peaks
51  size_t nzercross,
52  std::ptrdiff_t lag = 2 //must be even (leave odd out)
53  )
54  {
55  if((lag % 2 ) == 1){
56  return -1;
57  }
58  worker_.resize(std::distance(beg,end) - lag);
59  TReal * pworkerBeg = &worker_[0];
60  TReal * pworkerEnd = &worker_[0] + worker_.size();
61 
62  Tit tbegin = beg;
63  Tit tbeginm1 = tbegin + ( lag);
64  for(;tbeginm1 != end ; ++tbeginm1, ++tbegin, ++pworkerBeg )
65  {
66  *pworkerBeg = (*tbeginm1 - *tbegin);
67  }
68 
69  //reset worker
70  pworkerBeg = &worker_[0];
71  std::size_t crosscount = 0;
72  for( int i = 0 ; (pworkerBeg != pworkerEnd-1) ; ++pworkerBeg , ++i )
73  {
74  if(crosscount >= nzercross){
75  problem_ = true;
76  return crosscount; // protect against memmory violations
77  std::string x = "nzerocross:";
78  x+=boost::lexical_cast<std::string>(nzercross);
79  x+=" crosscount:";
80  x+=boost::lexical_cast<std::string>(crosscount);
81  x+=" i: ";
82  x+= boost::lexical_cast<std::string>(i);
83  x+=" worker size ";
84  x+= boost::lexical_cast<std::string>( worker_.size() );
85  x+=" : ";
86  x+=boost::lexical_cast<std::string>(__LINE__);
87  x+=" : ";
88  x+= __FILE__;
89  throw std::length_error(x.c_str());
90  }
91  TReal v1 = (*pworkerBeg);
92  TReal v2 = *(pworkerBeg + 1);
93  //peak detected ... detect a zero crossing
94  if((v1 > 0 && v2 < 0) && ((v1 - v2) > epsilon_))
95  {
96  //determine zero crossing....
97  double frac = v1 / ( v1 - v2 );
98  double idx = static_cast<float>(i + lag/2) + frac;
99  *zerocrossings = ( idx );
100  ++zerocrossings;
101  ++crosscount;
102  }else if( v1 > 0 && v2 == 0 ){
103  TReal v3 = *(pworkerBeg + 2);
104  if((v3 < 0) && ((v1 - v3) > epsilon_)){
105  *zerocrossings = (i + lag/2 + 1.);
106  }
107  }else{
108  //just continue, nothing to handle...
109  }
110  }
111  return crosscount;
112  }
113 
114  bool getProblem() const{
115  return problem_;
116  }
117 
118  };
119  }//ms
120  }//base
121 }//ralab
122 
123 #endif // SIMPLEPICKER_H
const double epsilon
Definition: DiffTest.cpp:41
std::vector< TReal > worker_
float lexical_cast(const std::string &str)
SimplePicker(TReal epsilon=1e-3)
size_t operator()(Tit beg, Tit end, Outit zerocrossings, size_t nzercross, std::ptrdiff_t lag=2)
EQUISPACEINTERPOL Interpolation on a equidistantly spaced grid.
Definition: base.hpp:39
KernelTraitsBase< Kernel >::space_type::abscissa_type x