source: trunk/src/Utils/getStats.cc

Last change on this file was 1393, checked in by MatthewWhiting, 10 years ago

A large changeset that moves all use of bool arrays (ie. bool *) to std::vector<bool>. This will be a bit safer, plus potentially cheaper in memory usage. Interfaces to all stats functions with masks have changed accordingly, as well as the Gaussian smoothing functions and some spectral utility functions.

File size: 6.8 KB
RevLine 
[301]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// -----------------------------------------------------------------------
[3]29#include <iostream>
[204]30#include <algorithm>
[3]31#include <math.h>
[393]32#include <duchamp/Utils/utils.hh>
[3]33
[190]34template <class T> T absval(T value)
[70]35{
[528]36  /// Type-independent way of getting the absolute value.
[190]37  if( value > 0) return value;
38  else return 0-value;
39}
[222]40template int absval<int>(int value);
41template long absval<long>(long value);
42template float absval<float>(float value);
43template double absval<double>(double value);
[190]44//--------------------------------------------------------------------
45
[847]46template <class T> void findMinMax(const T *array, const size_t size,
[190]47                                   T &min, T &max)
48{
[528]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.
[70]55  min = max = array[0];
[847]56  for(size_t i=1;i<size;i++) {
[70]57    if(array[i]<min) min=array[i];
58    if(array[i]>max) max=array[i];
59  }
60}
[847]61template void findMinMax<int>(const int *array, const size_t size,
[222]62                                 int &min, int &max);
[847]63template void findMinMax<long>(const long *array, const size_t size,
[222]64                                  long &min, long &max);
[847]65template void findMinMax<float>(const float *array, const size_t size,
[222]66                                   float &min, float &max);
[847]67template void findMinMax<double>(const double *array, const size_t size,
[222]68                                    double &min, double &max);
[190]69//--------------------------------------------------------------------
[70]70
[847]71template <class T> void findAllStats(T *array, size_t size,
[275]72                                     float &mean, float &stddev,
73                                     T &median, T &madfm)
74{
[528]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
[275]89  if(size==0){
[285]90    std::cerr << "Error in findAllStats: zero sized array!\n";
[275]91    return;
92  }
93
94  T *newarray = new T[size];
95
[847]96  for(size_t i=0;i<size;i++) newarray[i] = array[i];
[285]97
[889]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);
[285]103
[275]104  delete [] newarray;
105
106}
[847]107template void findAllStats<int>(int *array, size_t size,
[275]108                                float &mean, float &stddev,
109                                int &median, int &madfm);
[847]110template void findAllStats<long>(long *array, size_t size,
[275]111                                 float &mean, float &stddev,
112                                 long &median, long &madfm);
[847]113template void findAllStats<float>(float *array, size_t size,
[275]114                                  float &mean, float &stddev,
115                                  float &median, float &madfm);
[847]116template void findAllStats<double>(double *array, size_t size,
[275]117                                   float &mean, float &stddev,
118                                   double &median, double &madfm);
119//--------------------------------------------------------------------
120
[1393]121template <class T> void findAllStats(T *array, size_t size, std::vector<bool> mask,
[275]122                                     float &mean, float &stddev,
123                                     T &median, T &madfm)
124{
[528]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
[275]143  int goodSize=0;
[847]144  for(size_t i=0;i<size;i++) if(mask[i]) goodSize++;
[275]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;
[847]152  for(size_t i=0;i<size;i++) if(mask[i]) newarray[goodSize++] = array[i];
[275]153
[889]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);
[275]159
[282]160  delete [] newarray;
161
[275]162}
[1393]163template void findAllStats<int>(int *array, size_t size, std::vector<bool> mask,
[275]164                                float &mean, float &stddev,
165                                int &median, int &madfm);
[1393]166template void findAllStats<long>(long *array, size_t size, std::vector<bool> mask,
[275]167                                 float &mean, float &stddev,
168                                 long &median, long &madfm);
[1393]169template void findAllStats<float>(float *array, size_t size, std::vector<bool> mask,
[275]170                                  float &mean, float &stddev,
171                                  float &median, float &madfm);
[1393]172template void findAllStats<double>(double *array, size_t size, std::vector<bool> mask,
[275]173                                   float &mean, float &stddev,
174                                   double &median, double &madfm);
175//--------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.