]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/Mori.h
...
[FrontAlgorithms.git] / lib / fpa / Base / Mori.h
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 // https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data
7
8
9 #ifndef __fpa__Base__Mori__h__
10 #define __fpa__Base__Mori__h__
11
12 #include <deque>
13 #include <set>
14 #include <vector>
15 #include <itkConceptChecking.h>
16 #include <itkFunctionBase.h>
17
18 #include <fpa/Generic/PeakDetector.h>
19 #include <fpa/Base/Functors/RegionGrow/BinaryThreshold.h>
20
21 namespace fpa
22 {
23   namespace Base
24   {
25     /**
26      */
27     template< class _TAlgorithm >
28     class Mori
29       : public _TAlgorithm
30     {
31     public:
32       typedef Mori                            Self;
33       typedef _TAlgorithm                     Superclass;
34       typedef itk::SmartPointer< Self >       Pointer;
35       typedef itk::SmartPointer< const Self > ConstPointer;
36
37       typedef typename _TAlgorithm::TNode        TNode;
38       typedef typename _TAlgorithm::TNodes       TNodes;
39       typedef typename _TAlgorithm::TInputValue  TInputValue;
40       typedef typename _TAlgorithm::TOutputValue TOutputValue;
41       typedef typename _TAlgorithm::TFrontId     TFrontId;
42       typedef typename _TAlgorithm::TVertex      TVertex;
43
44       typedef std::deque< TNode >     TQueue;
45       typedef std::set< TInputValue > TThresholds;
46       typedef std::pair< TInputValue, unsigned long > TSignalData;
47       typedef std::vector< TSignalData >              TSignal;
48
49       typedef fpa::Generic::PeakDetector TPeakDetector;
50       typedef fpa::Base::Functors::RegionGrow::BinaryThreshold< TInputValue > TPredicate;
51
52     public:
53       itkConceptMacro(
54         Check_TOutputValue,
55         ( itk::Concept::IsUnsignedInteger< TOutputValue > )
56         );
57
58       itkGetConstMacro( InsideValue, TOutputValue );
59       itkGetConstMacro( MinimumThreshold, TInputValue );
60
61       itkSetMacro( InsideValue, TOutputValue );
62       itkSetMacro( MinimumThreshold, TInputValue );
63
64       itkGetConstReferenceMacro( Signal, TSignal );
65       itkGetConstMacro( SignalLag, unsigned long );
66       itkGetConstMacro( SignalThreshold, double );
67       itkGetConstMacro( SignalInfluence, double );
68
69       itkSetMacro( SignalLag, unsigned long );
70       itkSetMacro( SignalThreshold, double );
71       itkSetMacro( SignalInfluence, double );
72
73     public:
74       virtual itk::ModifiedTimeType GetMTime( ) const override;
75
76       TOutputValue GetOutsideValue( ) const;
77       void SetOutsideValue( const TOutputValue& v );
78
79       void ClearThresholds( );
80       void AddThreshold( const TInputValue& thr );
81       void SetThresholds(
82         const TInputValue& init,
83         const TInputValue& end,
84         const TInputValue& delta
85         );
86       unsigned long GetNumberOfEvaluatedThresholds( ) const;
87       TInputValue GetOptimumThreshold( ) const;
88
89     protected:
90       Mori( );
91       virtual ~Mori( );
92
93       virtual void _BeforeGenerateData( ) override;
94       virtual void _FinishOneLoop( ) override;
95       virtual void _ComputeOutputValue( TNode& n ) override;
96       virtual void _UpdateOutputValue( TNode& n ) override;
97       virtual void _QueueClear( ) override;
98       virtual TNode _QueuePop( ) override;
99       virtual void _QueuePush( const TNode& node ) override;
100       virtual unsigned long _QueueSize( ) const override;
101       virtual void _PrepareSeeds( TNodes& nodes ) override;
102
103     private:
104       // Purposely not implemented.
105       Mori( const Self& other );
106       Self& operator=( const Self& other );
107
108     protected:
109       typename TPredicate::Pointer m_Predicate;
110       TThresholds m_Thresholds;
111       typename TThresholds::const_iterator m_CurrentThreshold;
112       TQueue m_Queues[ 2 ];
113       unsigned int m_CurrentQueue;
114       unsigned long m_Count;
115
116       TPeakDetector m_PeakDetector;
117
118       TSignal m_Signal;
119       unsigned long m_SignalLag;
120       double m_SignalThreshold;
121       double m_SignalInfluence;
122       std::vector< double > m_FilteredSignal;
123       std::vector< double > m_SignalAverages;
124       std::vector< double > m_SignalDeviations;
125       std::vector< short >  m_SignalPeaks;
126       double m_CurrentAverage;
127       double m_CurrentVariance;
128
129       TInputValue  m_MinimumThreshold;
130       TOutputValue m_InsideValue;
131     };
132
133   } // ecapseman
134
135 } // ecapseman
136
137 #ifndef ITK_MANUAL_INSTANTIATION
138 #  include <fpa/Base/Mori.hxx>
139 #endif // ITK_MANUAL_INSTANTIATION
140
141 #endif // __fpa__Base__Mori__h__
142
143 // eof - $RCSfile$