Mercurial > hg > segmenter-vamp-plugin
comparison armadillo-3.900.4/include/armadillo_bits/running_stat_meat.hpp @ 49:1ec0e2823891
Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author | Chris Cannam |
---|---|
date | Thu, 13 Jun 2013 10:25:24 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
48:69251e11a913 | 49:1ec0e2823891 |
---|---|
1 // Copyright (C) 2009-2011 NICTA (www.nicta.com.au) | |
2 // Copyright (C) 2009-2011 Conrad Sanderson | |
3 // | |
4 // This Source Code Form is subject to the terms of the Mozilla Public | |
5 // License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | |
8 | |
9 //! \addtogroup running_stat | |
10 //! @{ | |
11 | |
12 | |
13 | |
14 template<typename eT> | |
15 inline | |
16 arma_counter<eT>::~arma_counter() | |
17 { | |
18 arma_extra_debug_sigprint_this(this); | |
19 } | |
20 | |
21 | |
22 | |
23 template<typename eT> | |
24 inline | |
25 arma_counter<eT>::arma_counter() | |
26 : d_count( eT(0)) | |
27 , i_count(uword(0)) | |
28 { | |
29 arma_extra_debug_sigprint_this(this); | |
30 } | |
31 | |
32 | |
33 | |
34 template<typename eT> | |
35 inline | |
36 const arma_counter<eT>& | |
37 arma_counter<eT>::operator++() | |
38 { | |
39 if(i_count < ARMA_MAX_UWORD) | |
40 { | |
41 i_count++; | |
42 } | |
43 else | |
44 { | |
45 d_count += eT(ARMA_MAX_UWORD); | |
46 i_count = 1; | |
47 } | |
48 | |
49 return *this; | |
50 } | |
51 | |
52 | |
53 | |
54 template<typename eT> | |
55 inline | |
56 void | |
57 arma_counter<eT>::operator++(int) | |
58 { | |
59 operator++(); | |
60 } | |
61 | |
62 | |
63 | |
64 template<typename eT> | |
65 inline | |
66 void | |
67 arma_counter<eT>::reset() | |
68 { | |
69 d_count = eT(0); | |
70 i_count = uword(0); | |
71 } | |
72 | |
73 | |
74 | |
75 template<typename eT> | |
76 inline | |
77 eT | |
78 arma_counter<eT>::value() const | |
79 { | |
80 return d_count + eT(i_count); | |
81 } | |
82 | |
83 | |
84 | |
85 template<typename eT> | |
86 inline | |
87 eT | |
88 arma_counter<eT>::value_plus_1() const | |
89 { | |
90 if(i_count < ARMA_MAX_UWORD) | |
91 { | |
92 return d_count + eT(i_count + 1); | |
93 } | |
94 else | |
95 { | |
96 return d_count + eT(ARMA_MAX_UWORD) + eT(1); | |
97 } | |
98 } | |
99 | |
100 | |
101 | |
102 template<typename eT> | |
103 inline | |
104 eT | |
105 arma_counter<eT>::value_minus_1() const | |
106 { | |
107 if(i_count > 0) | |
108 { | |
109 return d_count + eT(i_count - 1); | |
110 } | |
111 else | |
112 { | |
113 return d_count - eT(1); | |
114 } | |
115 } | |
116 | |
117 | |
118 | |
119 // | |
120 | |
121 | |
122 | |
123 template<typename eT> | |
124 running_stat<eT>::~running_stat() | |
125 { | |
126 arma_extra_debug_sigprint_this(this); | |
127 } | |
128 | |
129 | |
130 | |
131 template<typename eT> | |
132 running_stat<eT>::running_stat() | |
133 : r_mean ( eT(0)) | |
134 , r_var (typename running_stat<eT>::T(0)) | |
135 , min_val ( eT(0)) | |
136 , max_val ( eT(0)) | |
137 , min_val_norm(typename running_stat<eT>::T(0)) | |
138 , max_val_norm(typename running_stat<eT>::T(0)) | |
139 { | |
140 arma_extra_debug_sigprint_this(this); | |
141 } | |
142 | |
143 | |
144 | |
145 //! update statistics to reflect new sample | |
146 template<typename eT> | |
147 inline | |
148 void | |
149 running_stat<eT>::operator() (const typename running_stat<eT>::T sample) | |
150 { | |
151 arma_extra_debug_sigprint(); | |
152 | |
153 if( arma_isfinite(sample) == false ) | |
154 { | |
155 arma_warn(true, "running_stat: sample ignored as it is non-finite" ); | |
156 return; | |
157 } | |
158 | |
159 running_stat_aux::update_stats(*this, sample); | |
160 } | |
161 | |
162 | |
163 | |
164 //! update statistics to reflect new sample (version for complex numbers) | |
165 template<typename eT> | |
166 inline | |
167 void | |
168 running_stat<eT>::operator() (const std::complex< typename running_stat<eT>::T >& sample) | |
169 { | |
170 arma_extra_debug_sigprint(); | |
171 | |
172 arma_type_check(( is_same_type<eT, std::complex< typename running_stat<eT>::T > >::value == false )); | |
173 | |
174 if( arma_isfinite(sample) == false ) | |
175 { | |
176 arma_warn(true, "running_stat: sample ignored as it is non-finite" ); | |
177 return; | |
178 } | |
179 | |
180 running_stat_aux::update_stats(*this, sample); | |
181 } | |
182 | |
183 | |
184 | |
185 //! set all statistics to zero | |
186 template<typename eT> | |
187 inline | |
188 void | |
189 running_stat<eT>::reset() | |
190 { | |
191 arma_extra_debug_sigprint(); | |
192 | |
193 // typedef typename running_stat<eT>::T T; | |
194 | |
195 counter.reset(); | |
196 | |
197 r_mean = eT(0); | |
198 r_var = T(0); | |
199 | |
200 min_val = eT(0); | |
201 max_val = eT(0); | |
202 | |
203 min_val_norm = T(0); | |
204 max_val_norm = T(0); | |
205 } | |
206 | |
207 | |
208 | |
209 //! mean or average value | |
210 template<typename eT> | |
211 inline | |
212 eT | |
213 running_stat<eT>::mean() const | |
214 { | |
215 arma_extra_debug_sigprint(); | |
216 | |
217 return r_mean; | |
218 } | |
219 | |
220 | |
221 | |
222 //! variance | |
223 template<typename eT> | |
224 inline | |
225 typename running_stat<eT>::T | |
226 running_stat<eT>::var(const uword norm_type) const | |
227 { | |
228 arma_extra_debug_sigprint(); | |
229 | |
230 const T N = counter.value(); | |
231 | |
232 if(N > T(1)) | |
233 { | |
234 if(norm_type == 0) | |
235 { | |
236 return r_var; | |
237 } | |
238 else | |
239 { | |
240 const T N_minus_1 = counter.value_minus_1(); | |
241 return (N_minus_1/N) * r_var; | |
242 } | |
243 } | |
244 else | |
245 { | |
246 return T(0); | |
247 } | |
248 } | |
249 | |
250 | |
251 | |
252 //! standard deviation | |
253 template<typename eT> | |
254 inline | |
255 typename running_stat<eT>::T | |
256 running_stat<eT>::stddev(const uword norm_type) const | |
257 { | |
258 arma_extra_debug_sigprint(); | |
259 | |
260 return std::sqrt( (*this).var(norm_type) ); | |
261 } | |
262 | |
263 | |
264 | |
265 //! minimum value | |
266 template<typename eT> | |
267 inline | |
268 eT | |
269 running_stat<eT>::min() const | |
270 { | |
271 arma_extra_debug_sigprint(); | |
272 | |
273 return min_val; | |
274 } | |
275 | |
276 | |
277 | |
278 //! maximum value | |
279 template<typename eT> | |
280 inline | |
281 eT | |
282 running_stat<eT>::max() const | |
283 { | |
284 arma_extra_debug_sigprint(); | |
285 | |
286 return max_val; | |
287 } | |
288 | |
289 | |
290 | |
291 //! number of samples so far | |
292 template<typename eT> | |
293 inline | |
294 typename get_pod_type<eT>::result | |
295 running_stat<eT>::count() const | |
296 { | |
297 arma_extra_debug_sigprint(); | |
298 | |
299 return counter.value(); | |
300 } | |
301 | |
302 | |
303 | |
304 //! update statistics to reflect new sample | |
305 template<typename eT> | |
306 inline | |
307 void | |
308 running_stat_aux::update_stats(running_stat<eT>& x, const eT sample) | |
309 { | |
310 arma_extra_debug_sigprint(); | |
311 | |
312 typedef typename running_stat<eT>::T T; | |
313 | |
314 const T N = x.counter.value(); | |
315 | |
316 if(N > T(0)) | |
317 { | |
318 if(sample < x.min_val) | |
319 { | |
320 x.min_val = sample; | |
321 } | |
322 | |
323 if(sample > x.max_val) | |
324 { | |
325 x.max_val = sample; | |
326 } | |
327 | |
328 const T N_plus_1 = x.counter.value_plus_1(); | |
329 const T N_minus_1 = x.counter.value_minus_1(); | |
330 | |
331 // note: variance has to be updated before the mean | |
332 | |
333 const eT tmp = sample - x.r_mean; | |
334 | |
335 x.r_var = N_minus_1/N * x.r_var + (tmp*tmp)/N_plus_1; | |
336 | |
337 x.r_mean = x.r_mean + (sample - x.r_mean)/N_plus_1; | |
338 //x.r_mean = (N/N_plus_1)*x.r_mean + sample/N_plus_1; | |
339 //x.r_mean = (x.r_mean + sample/N) * N/N_plus_1; | |
340 } | |
341 else | |
342 { | |
343 x.r_mean = sample; | |
344 x.min_val = sample; | |
345 x.max_val = sample; | |
346 | |
347 // r_var is initialised to zero | |
348 // in the constructor and reset() | |
349 } | |
350 | |
351 x.counter++; | |
352 } | |
353 | |
354 | |
355 | |
356 //! update statistics to reflect new sample (version for complex numbers) | |
357 template<typename T> | |
358 inline | |
359 void | |
360 running_stat_aux::update_stats(running_stat< std::complex<T> >& x, const T sample) | |
361 { | |
362 arma_extra_debug_sigprint(); | |
363 | |
364 running_stat_aux::update_stats(x, std::complex<T>(sample)); | |
365 } | |
366 | |
367 | |
368 | |
369 //! alter statistics to reflect new sample (version for complex numbers) | |
370 template<typename T> | |
371 inline | |
372 void | |
373 running_stat_aux::update_stats(running_stat< std::complex<T> >& x, const std::complex<T>& sample) | |
374 { | |
375 arma_extra_debug_sigprint(); | |
376 | |
377 const T sample_norm = std::norm(sample); | |
378 const T N = x.counter.value(); | |
379 | |
380 if(N > T(0)) | |
381 { | |
382 if(sample_norm < x.min_val_norm) | |
383 { | |
384 x.min_val_norm = sample_norm; | |
385 x.min_val = sample; | |
386 } | |
387 | |
388 if(sample_norm > x.max_val_norm) | |
389 { | |
390 x.max_val_norm = sample_norm; | |
391 x.max_val = sample; | |
392 } | |
393 | |
394 const T N_plus_1 = x.counter.value_plus_1(); | |
395 const T N_minus_1 = x.counter.value_minus_1(); | |
396 | |
397 x.r_var = N_minus_1/N * x.r_var + std::norm(sample - x.r_mean)/N_plus_1; | |
398 | |
399 x.r_mean = x.r_mean + (sample - x.r_mean)/N_plus_1; | |
400 //x.r_mean = (N/N_plus_1)*x.r_mean + sample/N_plus_1; | |
401 //x.r_mean = (x.r_mean + sample/N) * N/N_plus_1; | |
402 } | |
403 else | |
404 { | |
405 x.r_mean = sample; | |
406 x.min_val = sample; | |
407 x.max_val = sample; | |
408 x.min_val_norm = sample_norm; | |
409 x.max_val_norm = sample_norm; | |
410 | |
411 // r_var is initialised to zero | |
412 // in the constructor and reset() | |
413 } | |
414 | |
415 x.counter++; | |
416 } | |
417 | |
418 | |
419 | |
420 //! @} |