| 1 |
// |
| 2 |
// $Id$ |
| 3 |
// |
| 4 |
// |
| 5 |
// Original author: Witold Wolski <[email protected]> |
| 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 |
#ifndef DETERMINEBINWIDTHUTILITIES_H |
| 22 |
#define DETERMINEBINWIDTHUTILITIES_H |
| 23 |
|
| 24 |
#include <functional> |
| 25 |
#include <boost/cstdint.hpp> |
| 26 |
#include <algorithm> |
| 27 |
|
| 28 |
namespace ralab |
| 29 |
{ |
| 30 |
namespace base |
| 31 |
{ |
| 32 |
namespace resample |
| 33 |
{ |
| 34 |
|
| 35 |
typedef boost::int32_t int32_t; |
| 36 |
namespace utilities{ |
| 37 |
|
| 38 |
template<class T> |
| 39 |
struct meanfunctor{ |
| 40 |
T operator()(const T & x, const T& y){ |
| 41 |
return (x+y)/2.; |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
template < |
| 47 |
typename InputIterator, |
| 48 |
typename OutputIterator, |
| 49 |
typename TN //= int32_t |
| 50 |
> |
| 51 |
OutputIterator summ |
| 52 |
( |
| 53 |
InputIterator begin, //!< [in] begin |
| 54 |
InputIterator end, //!< [in] end |
| 55 |
OutputIterator destBegin, //!< [out] dest begin |
| 56 |
TN lag = 1//!< [in] an integer indicating which lag to use. |
| 57 |
) |
| 58 |
{ |
| 59 |
return( std::transform(begin + lag |
| 60 |
, end |
| 61 |
, begin |
| 62 |
, destBegin |
| 63 |
, meanfunctor<typename InputIterator::value_type>()) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
template<typename TRealI> |
| 68 |
double determine(TRealI begin, TRealI end,double maxj=5.){ |
| 69 |
//BOOST_ASSERT(!boost::range::is_sorted(begin,end)); |
| 70 |
double j = 1.; |
| 71 |
double average = *begin; |
| 72 |
double sum = average; |
| 73 |
int32_t i = 1; |
| 74 |
for(; begin != end ; ++begin, ++i){ |
| 75 |
while(*begin > (j+0.5) *average){ |
| 76 |
++j; |
| 77 |
} |
| 78 |
if(j > maxj){ |
| 79 |
break; |
| 80 |
} |
| 81 |
sum += *begin/j; |
| 82 |
average = sum/static_cast<double>(i); |
| 83 |
} |
| 84 |
return average; |
| 85 |
|
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
#endif |