]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Common/PeakDetector.cxx
...
[FrontAlgorithms.git] / lib / fpa / Common / PeakDetector.cxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #include <fpa/Common/PeakDetector.h>
6 #include <cmath>
7
8 // -------------------------------------------------------------------------
9 fpa::Common::PeakDetector::
10 PeakDetector( )
11   : m_K( 3 ),
12     m_T( 3.5 ),
13     m_I( 0.5 )
14 {
15 }
16
17 // -------------------------------------------------------------------------
18 fpa::Common::PeakDetector::
19 ~PeakDetector( )
20 {
21 }
22
23 // -------------------------------------------------------------------------
24 unsigned long fpa::Common::PeakDetector::
25 GetKernelSize( ) const
26 {
27   return( this->m_K );
28 }
29
30 // -------------------------------------------------------------------------
31 double fpa::Common::PeakDetector::
32 GetThreshold( ) const
33 {
34   return( this->m_T );
35 }
36
37 // -------------------------------------------------------------------------
38 double fpa::Common::PeakDetector::
39 GetInfluence( ) const
40 {
41   return( this->m_I );
42 }
43
44 // -------------------------------------------------------------------------
45 void fpa::Common::PeakDetector::
46 SetKernelSize( unsigned long k )
47 {
48   this->m_K = k;
49   this->Clear( );
50 }
51
52 // -------------------------------------------------------------------------
53 void fpa::Common::PeakDetector::
54 SetThreshold( double t )
55 {
56   this->m_T = t;
57   this->Clear( );
58 }
59
60 // -------------------------------------------------------------------------
61 void fpa::Common::PeakDetector::
62 SetInfluence( double i )
63 {
64   this->m_I = i;
65   this->Clear( );
66 }
67
68 // -------------------------------------------------------------------------
69 void fpa::Common::PeakDetector::
70 Clear( )
71 {
72   this->m_X.clear( );
73   this->m_Y.clear( );
74   this->m_YF.clear( );
75   this->m_Avg.clear( );
76   this->m_STD.clear( );
77   this->m_Peaks.clear( );
78   this->m_MeanAndVar.Clear( );
79 }
80
81 // -------------------------------------------------------------------------
82 const std::vector< double >& fpa::Common::PeakDetector::
83 GetXValues( ) const
84 {
85   return( this->m_X );
86 }
87
88 // -------------------------------------------------------------------------
89 const std::vector< double >& fpa::Common::PeakDetector::
90 GetYValues( ) const
91 {
92   return( this->m_Y );
93 }
94
95 // -------------------------------------------------------------------------
96 const std::vector< double >& fpa::Common::PeakDetector::
97 GetFilteredYValues( ) const
98 {
99   return( this->m_YF );
100 }
101
102 // -------------------------------------------------------------------------
103 const std::vector< double >& fpa::Common::PeakDetector::
104 GetAverages( ) const
105 {
106   return( this->m_Avg );
107 }
108
109 // -------------------------------------------------------------------------
110 const std::vector< double >& fpa::Common::PeakDetector::
111 GetDeviations( ) const
112 {
113   return( this->m_STD );
114 }
115
116 // -------------------------------------------------------------------------
117 const std::vector< fpa::Common::PeakDetector::TPeak >& fpa::Common::
118 PeakDetector::GetPeaks( ) const
119 {
120   return( this->m_Peaks );
121 }
122
123 // -------------------------------------------------------------------------
124 unsigned long fpa::Common::PeakDetector::
125 GetNumberOfSamples( ) const
126 {
127   return( this->m_X.size( ) );
128 }
129
130 // -------------------------------------------------------------------------
131 fpa::Common::PeakDetector::
132 TPeak fpa::Common::PeakDetector::
133 AddValue( double x, double y )
134 {
135   this->m_X.push_back( x );
136   this->m_Y.push_back( y );
137
138   if( this->m_YF.size( ) < this->m_K )
139   {
140     this->m_YF.push_back( y );
141     this->m_Avg.push_back( double( 0 ) );
142     this->m_STD.push_back( double( 0 ) );
143     this->m_Peaks.push_back( Self::NoPeak );
144
145     this->m_MeanAndVar.AddValue( y );
146     if( this->m_YF.size( ) == this->m_K )
147     {
148       this->m_Avg.push_back( this->m_MeanAndVar.GetMean( ) );
149       this->m_STD.push_back( this->m_MeanAndVar.GetDeviation( ) );
150
151     } // fi
152   }
153   else
154   {
155     unsigned long i = this->m_X.size( ) - 1;
156     if(
157       ( std::fabs( y - this->m_Avg[ i - 1 ] ) ) >
158       ( this->m_T * this->m_STD[ i - 1 ] )
159       )
160     {
161       this->m_Peaks.push_back(
162         ( y > this->m_Avg[ i - 1 ] )? Self::PosPeak: Self::NegPeak
163         );
164       this->m_YF.push_back(
165         ( this->m_I * y ) +
166         ( ( double( 1 ) - this->m_I ) * this->m_YF[ i - 1 ] )
167         );
168     }
169     else
170     {
171       this->m_Peaks.push_back( Self::NoPeak );
172       this->m_YF.push_back( y );
173
174     } // fi
175
176     this->m_MeanAndVar.Clear( );
177     unsigned long k = 0;
178     for( unsigned long j = i - this->m_K; j <= i; ++j, ++k )
179       this->m_MeanAndVar.AddValue( this->m_YF[ j ] );
180     this->m_Avg.push_back( this->m_MeanAndVar.GetMean( ) );
181     this->m_STD.push_back( this->m_MeanAndVar.GetDeviation( ) );
182
183   } // fi
184   return( this->m_Peaks.back( ) );
185 }
186
187 // eof - $RCSfile$