]> 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 #include <fpa/Base/Functors/RegionGrow/BinaryThreshold.h>
18
19 namespace fpa
20 {
21   namespace Base
22   {
23     /**
24      */
25     template< class _TAlgorithm >
26     class Mori
27       : public _TAlgorithm
28     {
29     public:
30       typedef Mori                            Self;
31       typedef _TAlgorithm                     Superclass;
32       typedef itk::SmartPointer< Self >       Pointer;
33       typedef itk::SmartPointer< const Self > ConstPointer;
34
35       typedef typename _TAlgorithm::TNode        TNode;
36       typedef typename _TAlgorithm::TNodes       TNodes;
37       typedef typename _TAlgorithm::TInputValue  TInputValue;
38       typedef typename _TAlgorithm::TOutputValue TOutputValue;
39       typedef typename _TAlgorithm::TFrontId     TFrontId;
40       typedef typename _TAlgorithm::TVertex      TVertex;
41
42       typedef std::deque< TNode >     TQueue;
43       typedef std::set< TInputValue > TThresholds;
44       typedef std::pair< TInputValue, unsigned long > TSignalData;
45       typedef std::vector< TSignalData >              TSignal;
46       typedef fpa::Base::Functors::RegionGrow::BinaryThreshold< TInputValue > TPredicate;
47
48     public:
49       itkConceptMacro(
50         Check_TOutputValue,
51         ( itk::Concept::IsUnsignedInteger< TOutputValue > )
52         );
53
54       itkGetConstMacro( InsideValue, TOutputValue );
55       itkSetMacro( InsideValue, TOutputValue );
56
57       itkGetConstReferenceMacro( Signal, TSignal );
58       itkGetConstMacro( SignalLag, unsigned long );
59       itkGetConstMacro( SignalThreshold, double );
60       itkGetConstMacro( SignalInfluence, double );
61
62       itkSetMacro( SignalLag, unsigned long );
63       itkSetMacro( SignalThreshold, double );
64       itkSetMacro( SignalInfluence, double );
65
66     public:
67       virtual itk::ModifiedTimeType GetMTime( ) const override;
68
69       TOutputValue GetOutsideValue( ) const;
70       void SetOutsideValue( const TOutputValue& v );
71
72       void ClearThresholds( );
73       void AddThreshold( const TInputValue& thr );
74       void SetThresholds(
75         const TInputValue& init,
76         const TInputValue& end,
77         const TInputValue& delta
78         );
79       unsigned long GetNumberOfEvaluatedThresholds( ) const;
80       TInputValue GetOptimumThreshold( ) const;
81
82     protected:
83       Mori( );
84       virtual ~Mori( );
85
86       virtual void _BeforeGenerateData( ) override;
87       virtual void _FinishOneLoop( ) override;
88       virtual void _ComputeOutputValue( TNode& n ) override;
89       virtual void _UpdateOutputValue( TNode& n ) override;
90       virtual void _QueueClear( ) override;
91       virtual TNode _QueuePop( ) override;
92       virtual void _QueuePush( const TNode& node ) override;
93       virtual unsigned long _QueueSize( ) const override;
94       virtual void _PrepareSeeds( TNodes& nodes ) override;
95
96     private:
97       // Purposely not implemented.
98       Mori( const Self& other );
99       Self& operator=( const Self& other );
100
101     protected:
102       typename TPredicate::Pointer m_Predicate;
103       TThresholds m_Thresholds;
104       typename TThresholds::const_iterator m_CurrentThreshold;
105       TQueue m_Queues[ 2 ];
106       unsigned int m_CurrentQueue;
107       unsigned long m_Count;
108
109       TSignal m_Signal;
110       unsigned long m_SignalLag;
111       double m_SignalThreshold;
112       double m_SignalInfluence;
113       std::vector< double > m_FilteredSignal;
114       std::vector< double > m_SignalAverages;
115       std::vector< double > m_SignalDeviations;
116       std::vector< short >  m_SignalPeaks;
117       double m_CurrentAverage;
118       double m_CurrentVariance;
119
120       TOutputValue m_InsideValue;
121     };
122
123   } // ecapseman
124
125 } // ecapseman
126
127 #ifndef ITK_MANUAL_INSTANTIATION
128 #  include <fpa/Base/Mori.hxx>
129 #endif // ITK_MANUAL_INSTANTIATION
130
131 #endif // __fpa__Base__Mori__h__
132
133 // eof - $RCSfile$