1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17 
18 
19 //! \addtogroup running_stat_vec
20 //! @{
21 
22 
23 template<typename obj_type, bool> struct rsv_get_elem_type_worker                  { };
24 template<typename obj_type>       struct rsv_get_elem_type_worker<obj_type, false> { typedef          obj_type            result; };
25 template<typename obj_type>       struct rsv_get_elem_type_worker<obj_type, true>  { typedef typename obj_type::elem_type result; };
26 
27 template<typename obj_type> struct rsv_get_elem_type { typedef typename rsv_get_elem_type_worker<obj_type, is_Mat<obj_type>::value>::result elem_type; };
28 
29 
30 template<typename obj_type, bool> struct rsv_get_return_type1_worker                  { };
31 template<typename obj_type>       struct rsv_get_return_type1_worker<obj_type, false> { typedef Mat<obj_type>  result; };
32 template<typename obj_type>       struct rsv_get_return_type1_worker<obj_type, true>  { typedef obj_type       result; };
33 
34 template<typename obj_type> struct rsv_get_return_type1 { typedef typename rsv_get_return_type1_worker<obj_type, is_Mat<obj_type>::value>::result return_type1; };
35 
36 
37 template<typename return_type1> struct rsv_get_return_type2            { };
38 template<typename eT>           struct rsv_get_return_type2< Mat<eT> > { typedef Mat<typename get_pod_type<eT>::result> return_type2; };
39 template<typename eT>           struct rsv_get_return_type2< Row<eT> > { typedef Row<typename get_pod_type<eT>::result> return_type2; };
40 template<typename eT>           struct rsv_get_return_type2< Col<eT> > { typedef Col<typename get_pod_type<eT>::result> return_type2; };
41 
42 
43 //! Class for keeping statistics of a continuously sampled process / signal.
44 //! Useful if the storage of individual samples is not necessary or desired.
45 //! Also useful if the number of samples is not known beforehand or exceeds
46 //! available memory.
47 template<typename obj_type>
48 class running_stat_vec
49   {
50   public:
51 
52   // voodoo for compatibility with old user code
53   typedef typename rsv_get_elem_type<obj_type>::elem_type eT;
54 
55   typedef typename get_pod_type<eT>::result T;
56 
57   typedef typename rsv_get_return_type1<obj_type    >::return_type1 return_type1;
58   typedef typename rsv_get_return_type2<return_type1>::return_type2 return_type2;
59 
60   inline ~running_stat_vec();
61   inline  running_stat_vec(const bool in_calc_cov = false);  // TODO: investigate char* overload, eg. "calc_cov", "no_calc_cov"
62 
63   inline running_stat_vec(const running_stat_vec& in_rsv);
64 
65   inline running_stat_vec& operator=(const running_stat_vec& in_rsv);
66 
67   template<typename T1> arma_hot inline void operator() (const Base<              T, T1>& X);
68   template<typename T1> arma_hot inline void operator() (const Base<std::complex<T>, T1>& X);
69 
70   inline void reset();
71 
72   inline const return_type1&  mean() const;
73 
74   inline const return_type2&  var   (const uword norm_type = 0);
75   inline       return_type2   stddev(const uword norm_type = 0) const;
76   inline const Mat<eT>&       cov   (const uword norm_type = 0);
77 
78   inline const return_type1& min()   const;
79   inline const return_type1& max()   const;
80   inline       return_type1  range() const;
81 
82   inline T count() const;
83 
84   //
85   //
86 
87   private:
88 
89   const bool calc_cov;
90 
91   arma_aligned arma_counter<T> counter;
92 
93   arma_aligned return_type1 r_mean;
94   arma_aligned return_type2 r_var;
95   arma_aligned Mat<eT>      r_cov;
96 
97   arma_aligned return_type1 min_val;
98   arma_aligned return_type1 max_val;
99 
100   arma_aligned Mat< T> min_val_norm;
101   arma_aligned Mat< T> max_val_norm;
102 
103   arma_aligned return_type2 r_var_dummy;
104   arma_aligned Mat<eT>      r_cov_dummy;
105 
106   arma_aligned Mat<eT> tmp1;
107   arma_aligned Mat<eT> tmp2;
108 
109   friend class running_stat_vec_aux;
110   };
111 
112 
113 
114 class running_stat_vec_aux
115   {
116   public:
117 
118   template<typename obj_type>
119   inline static void
120   update_stats
121     (
122     running_stat_vec<obj_type>& x,
123     const                  Mat<typename running_stat_vec<obj_type>::eT>& sample,
124     const typename arma_not_cx<typename running_stat_vec<obj_type>::eT>::result* junk = nullptr
125     );
126 
127   template<typename obj_type>
128   inline static void
129   update_stats
130     (
131     running_stat_vec<obj_type>& x,
132     const          Mat<std::complex< typename running_stat_vec<obj_type>::T > >& sample,
133     const typename       arma_not_cx<typename running_stat_vec<obj_type>::eT>::result* junk = nullptr
134     );
135 
136   template<typename obj_type>
137   inline static void
138   update_stats
139     (
140     running_stat_vec<obj_type>& x,
141     const                  Mat< typename running_stat_vec<obj_type>::T >& sample,
142     const typename arma_cx_only<typename running_stat_vec<obj_type>::eT>::result* junk = nullptr
143     );
144 
145   template<typename obj_type>
146   inline static void
147   update_stats
148     (
149     running_stat_vec<obj_type>& x,
150     const                   Mat<typename running_stat_vec<obj_type>::eT>& sample,
151     const typename arma_cx_only<typename running_stat_vec<obj_type>::eT>::result* junk = nullptr
152     );
153   };
154 
155 
156 
157 //! @}
158