source: tags/release-1.2.2/src/Utils/getStats.cc

Last change on this file was 889, checked in by MatthewWhiting, 12 years ago

Making our choice of template in the statistics functions explicit

File size: 6.7 KB
Line 
1// -----------------------------------------------------------------------
2// getStats.cc: Basic functions to calculate statistical parameters
3//              such as mean, median, rms, madfm, min, max.
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#include <iostream>
30#include <algorithm>
31#include <math.h>
32#include <duchamp/Utils/utils.hh>
33
34template <class T> T absval(T value)
35{
36  /// Type-independent way of getting the absolute value.
37  if( value > 0) return value;
38  else return 0-value;
39}
40template int absval<int>(int value);
41template long absval<long>(long value);
42template float absval<float>(float value);
43template double absval<double>(double value);
44//--------------------------------------------------------------------
45
46template <class T> void findMinMax(const T *array, const size_t size,
47                                   T &min, T &max)
48{
49  /// @details
50  /// A function to find the minimum and maximum values of a set of numbers.
51  /// \param array The array of data values. Type independent.
52  /// \param size The length of the array
53  /// \param min The returned value of the minimum value in the array.
54  /// \param max The returned value of the maximum value in the array.
55  min = max = array[0];
56  for(size_t i=1;i<size;i++) {
57    if(array[i]<min) min=array[i];
58    if(array[i]>max) max=array[i];
59  }
60}
61template void findMinMax<int>(const int *array, const size_t size,
62                                 int &min, int &max);
63template void findMinMax<long>(const long *array, const size_t size,
64                                  long &min, long &max);
65template void findMinMax<float>(const float *array, const size_t size,
66                                   float &min, float &max);
67template void findMinMax<double>(const double *array, const size_t size,
68                                    double &min, double &max);
69//--------------------------------------------------------------------
70
71template <class T> void findAllStats(T *array, size_t size,
72                                     float &mean, float &stddev,
73                                     T &median, T &madfm)
74{
75  /// @details
76  /// Find the mean,rms (or standard deviation), median AND madfm of an
77  /// array of numbers. Type independent.
78  ///
79  /// \param array The array of numbers.
80  /// \param size The length of the array.
81  /// \param mean The mean value of the array, returned as a float.
82  /// \param stddev The rms or standard deviation of the array,
83  /// returned as a float.
84  /// \param median The median value of the array, returned as the same
85  /// type as the array.
86  /// \param madfm The median absolute deviation from the median value
87  /// of the array, returned as the same type as the array.
88
89  if(size==0){
90    std::cerr << "Error in findAllStats: zero sized array!\n";
91    return;
92  }
93
94  T *newarray = new T[size];
95
96  for(size_t i=0;i<size;i++) newarray[i] = array[i];
97
98  mean = findMean<T>(newarray,size);
99  stddev = findStddev<T>(newarray,size);
100  median = findMedian<T>(newarray,size,true);
101  //  madfm = findMADFM<T>(newarray,size,true);
102  madfm = findMADFM<T>(newarray,size,median,true);
103
104  delete [] newarray;
105
106}
107template void findAllStats<int>(int *array, size_t size,
108                                float &mean, float &stddev,
109                                int &median, int &madfm);
110template void findAllStats<long>(long *array, size_t size,
111                                 float &mean, float &stddev,
112                                 long &median, long &madfm);
113template void findAllStats<float>(float *array, size_t size,
114                                  float &mean, float &stddev,
115                                  float &median, float &madfm);
116template void findAllStats<double>(double *array, size_t size,
117                                   float &mean, float &stddev,
118                                   double &median, double &madfm);
119//--------------------------------------------------------------------
120
121template <class T> void findAllStats(T *array, size_t size, bool *mask,
122                                     float &mean, float &stddev,
123                                     T &median, T &madfm)
124{
125  /// @details
126  /// Find the mean,rms (or standard deviation), median AND madfm of a
127  /// subset of an array of numbers. Type independent.The subset is
128  /// defined by an array of bool variables. Type independent.
129  ///
130  /// \param array The array of numbers.
131  /// \param size The length of the array.
132  /// \param mask An array of the same length that says whether to
133  /// include each member of the array in the calculations. Only look
134  /// at values where mask=true.
135  /// \param mean The mean value of the array, returned as a float.
136  /// \param stddev The rms or standard deviation of the array,
137  /// returned as a float.
138  /// \param median The median value of the array, returned as the same
139  /// type as the array.
140  /// \param madfm The median absolute deviation from the median value
141  /// of the array, returned as the same type as the array.
142
143  int goodSize=0;
144  for(size_t i=0;i<size;i++) if(mask[i]) goodSize++;
145  if(goodSize==0){
146    std::cerr << "Error in findAllStats: no good values!\n";
147    return;
148  }
149
150  T *newarray = new T[goodSize];
151  goodSize=0;
152  for(size_t i=0;i<size;i++) if(mask[i]) newarray[goodSize++] = array[i];
153
154  mean = findMean<T>(newarray,goodSize);
155  stddev = findStddev<T>(newarray,goodSize);
156  median = findMedian<T>(newarray,goodSize,true);
157  //madfm = findMADFM<T>(newarray,goodSize,true);
158  madfm = findMADFM<T>(newarray,goodSize,median,true);
159
160  delete [] newarray;
161
162}
163template void findAllStats<int>(int *array, size_t size, bool *mask,
164                                float &mean, float &stddev,
165                                int &median, int &madfm);
166template void findAllStats<long>(long *array, size_t size, bool *mask,
167                                 float &mean, float &stddev,
168                                 long &median, long &madfm);
169template void findAllStats<float>(float *array, size_t size, bool *mask,
170                                  float &mean, float &stddev,
171                                  float &median, float &madfm);
172template void findAllStats<double>(double *array, size_t size, bool *mask,
173                                   float &mean, float &stddev,
174                                   double &median, double &madfm);
175//--------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.