// ----------------------------------------------------------------------- // getStats.cc: Basic functions to calculate statistical parameters // such as mean, median, rms, madfm, min, max. // ----------------------------------------------------------------------- // Copyright (C) 2006, Matthew Whiting, ATNF // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your // option) any later version. // // Duchamp is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // for more details. // // You should have received a copy of the GNU General Public License // along with Duchamp; if not, write to the Free Software Foundation, // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA // // Correspondence concerning Duchamp may be directed to: // Internet email: Matthew.Whiting [at] atnf.csiro.au // Postal address: Dr. Matthew Whiting // Australia Telescope National Facility, CSIRO // PO Box 76 // Epping NSW 1710 // AUSTRALIA // ----------------------------------------------------------------------- #include #include #include #include template float findMean(T *array, size_t size) { /// @details /// Find the mean of an array of numbers. Type independent. /// \param array The array of numbers. /// \param size The length of the array. /// \return The mean value of the array, returned as a float double mean = 0; for(size_t i=0;i0) mean /= double(size); return float(mean); } template float findMean(int *array, size_t size); template float findMean(long *array, size_t size); template float findMean(float *array, size_t size); template float findMean(double *array, size_t size); //-------------------------------------------------------------------- template float findMeanDiff(T *first, T *second, size_t size) { /// @details /// Find the mean of an array of numbers. Type independent. /// \param array The array of numbers. /// \param size The length of the array. /// \return The mean value of the array, returned as a float double mean = 0; for(size_t i=0;i0) mean /= double(size); return float(mean); } template float findMeanDiff(int *first, int *second, size_t size); template float findMeanDiff(long *first, long *second, size_t size); template float findMeanDiff(float *first, float *second, size_t size); template float findMeanDiff(double *first, double *second, size_t size); //-------------------------------------------------------------------- template float findMean(T *array, std::vector mask, size_t size) { /// @details /// Find the mean of an array of numbers. Type independent. /// \param array The array of numbers. /// \param mask An array of the same length that says whether to /// include each member of the array in the calculations. Only use /// values where mask=true. /// \param size The length of the array. /// \return The mean value of the array, returned as a float double mean = 0.; int ct=0; for(size_t i=0;i0) mean /= double(ct); return float(mean); } template float findMean(int *array, std::vector mask, size_t size); template float findMean(long *array, std::vector mask, size_t size); template float findMean(float *array, std::vector mask, size_t size); template float findMean(double *array, std::vector mask, size_t size); //-------------------------------------------------------------------- template float findMeanDiff(T *first, T *second, std::vector mask, size_t size) { /// @details /// Find the mean of an array of numbers. Type independent. /// \param array The array of numbers. /// \param mask An array of the same length that says whether to /// include each member of the array in the calculations. Only use /// values where mask=true. /// \param size The length of the array. /// \return The mean value of the array, returned as a float double mean = 0.; int ct=0; for(size_t i=0;i0) mean /= double(ct); return float(mean); } template float findMeanDiff(int *first, int *second, std::vector mask, size_t size); template float findMeanDiff(long *first, long *second, std::vector mask, size_t size); template float findMeanDiff(float *first, float *second, std::vector mask, size_t size); template float findMeanDiff(double *first, double *second, std::vector mask, size_t size); //-------------------------------------------------------------------- template float findStddev(T *array, size_t size) { /// @details Find the rms or standard deviation of an array of /// numbers. Type independent. Calculated by iterating only once, /// using \sum x and \sum x^2 (no call to findMean) /// \param array The array of numbers. /// \param size The length of the array. /// \return The rms value of the array, returned as a float double sumx=0.,sumxx=0.; double stddev=0.; double dsize=double(size); for(size_t i=0;i0) stddev = sqrt(sumxx/dsize - mean*mean); // std::cerr << stddev << " " << float(stddev) << "\n"; return float(stddev); } template float findStddev(int *array, size_t size); template float findStddev(long *array, size_t size); template float findStddev(float *array, size_t size); template float findStddev(double *array, size_t size); //-------------------------------------------------------------------- template float findStddevDiff(T *first, T *second, size_t size) { /// @details Find the rms or standard deviation of an array of /// numbers. Type independent. Calculated by iterating only once, /// using \sum x and \sum x^2 (no call to findMean) /// \param array The array of numbers. /// \param size The length of the array. /// \return The rms value of the array, returned as a float double sumx=0.,sumxx=0.; double stddev=0.; double dsize=double(size); for(size_t i=0;i0) stddev = sqrt(sumxx/dsize - mean*mean); return float(stddev); } template float findStddevDiff(int *first, int *second, size_t size); template float findStddevDiff(long *first, long *second, size_t size); template float findStddevDiff(float *first, float *second, size_t size); template float findStddevDiff(double *first, double *second, size_t size); //-------------------------------------------------------------------- template float findStddev(T *array, std::vector mask, size_t size) { /// @details Find the rms or standard deviation of an array of /// numbers. Type independent. Calculated by iterating only once, /// using \sum x and \sum x^2 (no call to findMean) /// \param array The array of numbers. /// \param mask An array of the same length that says whether to /// include each member of the array in the calculations. Only use /// values where mask=true. /// \param size The length of the array. /// \return The rms value of the array, returned as a float double sumx=0.,sumxx=0.; double stddev=0.; int ct=0; for(size_t i=0;i0) stddev = sqrt(sumxx/dct - mean*mean); return float(stddev); } template float findStddev(int *array, std::vector mask, size_t size); template float findStddev(long *array, std::vector mask, size_t size); template float findStddev(float *array, std::vector mask, size_t size); template float findStddev(double *array, std::vector mask, size_t size); //-------------------------------------------------------------------- template float findStddevDiff(T *first, T *second, std::vector mask, size_t size) { /// @details Find the rms or standard deviation of an array of /// numbers. Type independent. Calculated by iterating only once, /// using \sum x and \sum x^2 (no call to findMean) /// \param array The array of numbers. /// \param mask An array of the same length that says whether to /// include each member of the array in the calculations. Only use /// values where mask=true. /// \param size The length of the array. /// \return The rms value of the array, returned as a float double sumx=0,sumxx=0; double stddev=0; int ct=0; for(size_t i=0;i0) stddev = sqrt(sumxx/dct - mean*mean); return float(stddev); } template float findStddevDiff(int *first, int *second, std::vector mask, size_t size); template float findStddevDiff(long *first, long *second, std::vector mask, size_t size); template float findStddevDiff(float *first, float *second, std::vector mask, size_t size); template float findStddevDiff(double *first, double *second, std::vector mask, size_t size); //-------------------------------------------------------------------- template void findNormalStats(T *array, size_t size, float &mean, float &stddev) { /// @details /// Find the mean and rms or standard deviation of an array of /// numbers. Type independent. /// /// \param array The array of numbers. /// \param size The length of the array. /// \param mean The mean value of the array, returned as a float. /// \param stddev The rms or standard deviation of the array, /// returned as a float. if(size==0){ std::cerr << "Error in findNormalStats: zero sized array!\n"; return; } mean = array[0]; for(size_t i=1;i(int *array, size_t size, float &mean, float &stddev); template void findNormalStats(long *array, size_t size, float &mean, float &stddev); template void findNormalStats(float *array, size_t size, float &mean, float &stddev); template void findNormalStats(double *array, size_t size, float &mean, float &stddev); //-------------------------------------------------------------------- template void findNormalStats(T *array, size_t size, std::vector mask, float &mean, float &stddev) { /// @details /// Find the mean and rms or standard deviation of a subset of an /// array of numbers. The subset is defined by an array of bool /// variables. Type independent. /// /// \param array The array of numbers. /// \param size The length of the array. /// \param mask An array of the same length that says whether to /// include each member of the array in the calculations. Only look /// at values where mask=true. /// \param mean The mean value of the array, returned as a float. /// \param stddev The rms or standard deviation of the array, /// returned as a float. int goodSize=0; for(size_t i=0;i(int *array, size_t size, std::vector mask, float &mean, float &stddev); template void findNormalStats(long *array, size_t size, std::vector mask, float &mean, float &stddev); template void findNormalStats(float *array, size_t size, std::vector mask, float &mean, float &stddev); template void findNormalStats(double *array, size_t size, std::vector mask, float &mean, float &stddev); //-------------------------------------------------------------------- template void findNormalStatsDiff(T *first, T *second, size_t size, float &mean, float &stddev) { /// @details /// Find the mean and rms or standard deviation of the difference between two arrays of /// numbers. The difference is defined as first - second. Type independent. /// /// \param first The first array /// \param second The second array /// \param size The length of the array. /// \param mean The mean value of the array, returned as a float. /// \param stddev The rms or standard deviation of the array, /// returned as a float. if(size==0){ std::cerr << "Error in findNormalStats: zero sized array!\n"; return; } mean = first[0]-second[0]; for(size_t i=1;i(int *first, int *second, size_t size, float &mean, float &stddev); template void findNormalStatsDiff(long *first, long *second, size_t size, float &mean, float &stddev); template void findNormalStatsDiff(float *first, float *second, size_t size, float &mean, float &stddev); template void findNormalStatsDiff(double *first, double *second, size_t size, float &mean, float &stddev); //-------------------------------------------------------------------- template void findNormalStatsDiff(T *first, T *second, size_t size, std::vector mask, float &mean, float &stddev) { /// @details Find the mean and rms or standard deviation of the /// difference between two arrays of numbers, where some elements /// are masked out. The mask is defined by an array of bool /// variables, and the difference is defined as first - second. Type /// independent. /// /// \param first The first array /// \param second The second array /// \param size The length of the array. /// \param mask An array of the same length that says whether to /// include each member of the array in the calculations. Only look /// at values where mask=true. /// \param mean The mean value of the array, returned as a float. /// \param stddev The rms or standard deviation of the array, /// returned as a float. int goodSize=0; for(size_t i=0;i(int *first, int *second, size_t size, std::vector mask, float &mean, float &stddev); template void findNormalStatsDiff(long *first, long *second, size_t size, std::vector mask, float &mean, float &stddev); template void findNormalStatsDiff(float *first, float *second, size_t size, std::vector mask, float &mean, float &stddev); template void findNormalStatsDiff(double *first, double *second, size_t size, std::vector mask, float &mean, float &stddev); //--------------------------------------------------------------------