ProteoWizard
gauss.hpp
Go to the documentation of this file.
1//
2// $Id$
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 GAUSS_H
23#define GAUSS_H
24
28
29namespace ralab{
30 namespace base{
31 namespace filter{
32 namespace utilities{
33
34 /*! \brief Gauss function
35
36 \f[
37 f(x) = \frac{1}{\sigma \sqrt{2 \pi}} \cdot e^{-0.5 \cdot \frac{x - \mu }{\sigma}^2 }
38 \f]
39 \ingroup FILTER
40 */
41 template<typename TReal>
42 struct Gauss : std::unary_function <TReal, TReal> {
43
44 Gauss(TReal mu, TReal sigma)
45 :mu_(mu),
46 sigma_(sigma)
47 {}
48
49 TReal operator()(TReal x)
50 {
51 return( 1/(sigma_ * sqrt(2. * ralab::constants::PI) ) * exp(-0.5 * ( pow( (x - mu_ )/sigma_, TReal(2.) ) ) ));
52 }
53 public:
54 TReal mu_;
55 TReal sigma_;
56 };
57
58 /*! \brief First derivative of Gaussian
59
60 \f$
61 T_1 = -\frac{(x-\mu)}{ \sqrt{2 \pi}* \sigma^2 |\sigma| },\\
62
63 T_2 = e^{-0.5 \frac{x-\mu}{\sigma}^2 },\\
64
65 f'(x) = T_1 \cdot T_2
66 \f$
67
68 \ingroup FILTER
69 */
70 template<typename TReal>
71 struct Gauss_1deriv : std::unary_function< TReal ,TReal > {
72
74 TReal mu//!< mean
75 , TReal sigma //!< sigma
76 )
77 :mu_(mu),
78 sigma_(sigma)
79 {}
80 /*!\brief returns f'(x), with f - Gaussian.*/
81 TReal operator()( TReal x )
82 {
83 TReal T1 = - (x - mu_) / ( sqrt(TReal(2.) * ralab::constants::PI) * pow(sigma_ , TReal(2.)) * abs(sigma_) );
84 TReal T2 = exp( -0.5 * pow( ( ( x-mu_ ) / sigma_ ) , TReal(2.) ) );
85 return( T1 * T2 );
86 }
87 protected:
88 TReal mu_;
89 TReal sigma_;
90 };
91
92
93 template<typename TReal>
94 TReal getGaussWorker( TReal sigma,
95 std::vector<TReal> &gauss,
96 std::vector<TReal> &x )
97 {
98 //generate response
99 Gauss<TReal> g(0.,sigma);
100 gauss.resize(x.size());
101 std::transform(x.begin(),x.end(),gauss.begin(),g);
102
103 //ensure that are of gaussian is one...
104 TReal sum = std::accumulate(gauss.begin() , gauss.end() , 0.);
105 std::transform(gauss.begin(),gauss.end(),gauss.begin(),std::bind2nd(std::divides<TReal>(),sum )) ;
106 TReal sumfilter = std::accumulate(gauss.begin(),gauss.end(),0.);
107 return sumfilter;
108 }
109
110
111 template<typename TReal>
113 std::vector<TReal> &mh //the wavelet to scale
114 )
115 {
116 //do this so that the sum of wavelet equals zero
117 TReal sum = std::accumulate(mh.begin() , mh.end() , 0.);
118 sum /= mh.size();
119 std::transform(mh.begin(),mh.end(),mh.begin(),std::bind2nd(std::minus<TReal>(), sum )) ;
120 //compute sum of square...
121 TReal sumAbs = 0;
122 for(typename std::vector<TReal>::iterator it = mh.begin() ;it != mh.end(); ++it)
123 {
124 sumAbs += fabs(*it);
125 }
126 std::transform(mh.begin(),mh.end(),mh.begin(),std::bind2nd(std::divides<TReal>(), sumAbs )) ;
127 }
128
129 template<typename TReal>
130 TReal getGaussian1DerWorker( TReal sigma, std::vector<TReal> &gauss1d, std::vector<TReal> &x )
131 {
132 Gauss_1deriv<TReal> g(0.,sigma);
133 gauss1d.resize(x.size());
134 std::transform(x.begin(),x.end(),gauss1d.begin(),g);
135 scaleDerivative(gauss1d);
136 TReal sum = std::accumulate(gauss1d.begin(),gauss1d.end(),0.);
137 return sum;
138 }
139
140 }//utilities
141 }//base
142 }//filter
143}//ralab
144#endif
KernelTraitsBase< Kernel >::space_type::abscissa_type x
TReal getGaussWorker(TReal sigma, std::vector< TReal > &gauss, std::vector< TReal > &x)
Definition gauss.hpp:94
TReal getGaussian1DerWorker(TReal sigma, std::vector< TReal > &gauss1d, std::vector< TReal > &x)
Definition gauss.hpp:130
void scaleDerivative(std::vector< TReal > &mh)
Definition gauss.hpp:112
void filter(const TContainer &data, const TContainer &filter, TContainer &result, bool circular=false, uint32_t sides=2)
Applies linear convolution (filtering) to a univariate time series.
Definition filter.hpp:112
const double PI(3.14159265358979323846264338327950288)
the ratio of the circumference of a circle to its diameter;
EQUISPACEINTERPOL Interpolation on a equidistantly spaced grid.
Definition base.hpp:40
First derivative of Gaussian.
Definition gauss.hpp:71
TReal operator()(TReal x)
returns f'(x), with f - Gaussian.
Definition gauss.hpp:81
Gauss(TReal mu, TReal sigma)
Definition gauss.hpp:44