Statistics.c

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2005 Badri Krishnan, Alicia Sintes  
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with with program; see the file COPYING. If not, write to the 
00016  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
00017  *  MA  02111-1307  USA
00018  */
00019 
00020 /**
00021  *
00022  *  \file Statistics.c
00023  *  \author Krishnan, B
00024  *  \ingroup pulsarHough
00025  *  \brief Module for calculating statistics and histogram of a total Hough map.
00026  *
00027  *  $Id: Statistics.c,v 1.9 2008/06/16 09:47:56 badri Exp $
00028  *
00029  *  Bug in variance corrected -- Feb 01, 2004
00030  *  Generalized to handle REAL4 number counts -- Nov 01, 2005
00031 
00032 \par Description
00033 
00034 The fuction LALHoughStatistics() calculates the maximum number count, minimum
00035 number count, average and standard deviation of a given total Hough map.
00036 The input HOUGHMapTotal *in is  a total Hough map and the output is a 
00037 structure HoughStats *out.
00038 
00039 LALHoughHistogram@ produces a histogram of the number counts in a total Hough map. 
00040 The input is of type HOUGHMapTotal *in and the output UINT4Vector *out.
00041 
00042 */
00043 
00044 /************************************<lalVerbatim file="StatisticsCV">
00045 Authors: Krishnan, B., Sintes, A.M.
00046 $Id: Statistics.c,v 1.9 2008/06/16 09:47:56 badri Exp $
00047 *************************************</lalVerbatim> */
00048 
00049 /* <lalLaTeX>  **********************************************
00050 
00051 \subsection{Module \texttt{Statistics.c}}
00052 \label{ss:Statistics.c}
00053 Calculation of statistical quantities of the Hough map and a histogram of the
00054 number counts.
00055 
00056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00057 \subsubsection*{Prototypes}
00058 \vspace{0.1in}
00059 \input{StatisticsD}
00060 \index{\verb&LALHoughStatistics()&}
00061 \index{\verb&LALHoughHistogram()&}
00062 
00063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00064 \subsubsection*{Description}
00065 The fuction \verb@LALHoughStatistics@ calculates the maximum number count, minimum
00066    number count, average and standard deviation of a given total Hough map.
00067 The input \verb@HOUGHMapTotal *in@ is  a total Hough map
00068 and the output is a structure \verb@HoughStats *out@.
00069 
00070 The \verb@LALHoughHistogram@ produces a histogram of
00071    the number counts in a total Hough map. The input is of type \verb@HOUGHMapTotal *in@
00072 and the output \verb@UINT4Vector *out@.
00073 
00074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00075 \subsubsection*{Uses}
00076 %\begin{verbatim}
00077 %LALZDestroyVector()
00078 %\end{verbatim}
00079 %%%%%%%%%%%%%%%%%%%
00080 \subsubsection*{Notes}
00081 %%
00082 \vfill{\footnotesize\input{StatisticsCV}}
00083 ********************************************** </lalLaTeX> */
00084 
00085 
00086 #include <lal/Statistics.h> 
00087 /* #include "./Statistics.h" */
00088 
00089 NRCSID (STATISTICSC, "$Id: Statistics.c,v 1.9 2008/06/16 09:47:56 badri Exp $");
00090 
00091 /*
00092  * The functions that make up the guts of this module
00093  */
00094 
00095 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
00096 /** Given a total hough map, this calculates the maximum number count, minimum
00097    number count, average and standard deviation */
00098 
00099 /* *******************************  <lalVerbatim file="StatisticsD"> */
00100 void LALHoughStatistics( LALStatus     *status,
00101                          HoughStats    *out,
00102                          HOUGHMapTotal *in)
00103 { /*-------------------------------------------------</lalVerbatim> */
00104 
00105   INT4   i,j, xSide, ySide, mObsCoh; 
00106   INT4   maxIndexX, maxIndexY, minIndexX, minIndexY;
00107   REAL8  average, nPixel, variance, ep, temp, sum; 
00108   HoughTT max, min;
00109   /*--------------------------------------------------------------- */
00110         
00111   INITSTATUS (status, "LALHoughStatistics", STATISTICSC);
00112   ATTATCHSTATUSPTR (status);
00113 
00114   /* make sure arguments are not null */
00115   ASSERT (in, status, STATISTICSH_ENULL, STATISTICSH_MSGENULL);
00116   ASSERT (out, status, STATISTICSH_ENULL, STATISTICSH_MSGENULL);
00117 
00118   /* make sure input hough map is ok*/
00119   ASSERT (in->xSide > 0, status, STATISTICSH_EVAL, STATISTICSH_MSGEVAL);
00120   ASSERT (in->ySide > 0, status, STATISTICSH_EVAL, STATISTICSH_MSGEVAL);
00121   ASSERT (in->mObsCoh > 0, status, STATISTICSH_EVAL, STATISTICSH_MSGEVAL);
00122   
00123   /* read input parameters */
00124   xSide = in->xSide;
00125   ySide = in->ySide;
00126   mObsCoh = in->mObsCoh;
00127 
00128   /* first find maximum, minimum and average */
00129   maxIndexX = maxIndexY = minIndexX = minIndexY = 0;
00130   sum = 0;
00131   max = min = in->map[0];
00132   /* loop over hough map and calculate statistics */
00133   for (i = 0; i < ySide; i++){
00134     for (j = 0; j < xSide; j++){
00135       /* read the current number count */
00136       temp = (REAL4) in->map[i*xSide + j];
00137 
00138       /* add number count to sum */
00139       sum += temp;
00140 
00141       /* look for maximum */
00142       if (temp > (REAL4)max){
00143         max = in->map[i*xSide + j];
00144         maxIndexX = j;
00145         maxIndexY = i;
00146       }
00147 
00148       /* look for minimum */
00149       if (temp < (REAL4)min){
00150         min = in->map[i*xSide + j];
00151         minIndexX = j;
00152         minIndexY = i;
00153       }
00154     }
00155   }
00156  
00157   nPixel = 1.0 * xSide * ySide;
00158   average = sum / nPixel;
00159 
00160   /* now do another loop to find the variance and standard deviation */
00161   /* look at numerical recipes in C for calculation of variance */
00162   variance = 0.0;
00163   ep = 0.0;
00164   for (i = 0; i < ySide; i++){
00165     for (j = 0; j < xSide; j++){
00166       temp = (REAL4) (in->map[i*xSide + j]) - average;
00167       ep += temp;
00168       variance += temp*temp;
00169     }
00170   }
00171   /* the ep is supposed to reduce the rounding off errors */ 
00172   variance = (variance - ep*ep/nPixel)/(nPixel-1);  
00173 
00174 
00175   /* copy results to output structure */
00176   out->maxCount = max;
00177   out->maxIndex[0] = maxIndexX;
00178   out->maxIndex[1] = maxIndexY;
00179   out->minCount = min;
00180   out->minIndex[0] = minIndexX;
00181   out->minIndex[1] = minIndexY;
00182   out->avgCount = average;
00183   out->stdDev = sqrt(variance);
00184 
00185 
00186   DETATCHSTATUSPTR (status);
00187         
00188   /* normal exit */     
00189   RETURN (status);
00190 }
00191 
00192 
00193 
00194 void LALHoughmapMeanVariance( LALStatus     *status,
00195                               REAL8         *mean,
00196                               REAL8         *variance,
00197                               HOUGHMapTotal *in)
00198 { /*-------------------------------------------------</lalVerbatim> */
00199 
00200   INT4   i,j, xSide, ySide, mObsCoh, nPixel; 
00201   REAL8  sum, tempVariance, tempMean, ep, temp;
00202   /*--------------------------------------------------------------- */
00203         
00204   INITSTATUS (status, "LALHoughmapMeanVariance", STATISTICSC);
00205   ATTATCHSTATUSPTR (status);
00206 
00207 
00208   xSide = in->xSide;
00209   ySide = in->ySide;
00210   nPixel = 1.0 * xSide * ySide;
00211 
00212 
00213   sum = 0.0;
00214   /* loop over hough map and calculate statistics */
00215   for (i = 0; i < ySide; i++){
00216     for (j = 0; j < xSide; j++){
00217       /* read the current number count */
00218       sum += in->map[i*xSide + j];
00219     }
00220   }
00221 
00222   tempMean = sum/nPixel;
00223 
00224   tempVariance = 0.0;
00225   ep = 0.0;
00226   for (i = 0; i < ySide; i++){
00227     for (j = 0; j < xSide; j++){
00228       temp = (REAL4) (in->map[i*xSide + j]) - tempMean;
00229       ep += temp;
00230       tempVariance += temp*temp;
00231     }
00232   }
00233   /* the ep is supposed to reduce the rounding off errors */ 
00234   tempVariance = (tempVariance - ep*ep/nPixel)/(nPixel-1);  
00235 
00236   *mean = tempMean;
00237   *variance = tempVariance;
00238 
00239   DETATCHSTATUSPTR (status);
00240         
00241   /* normal exit */     
00242   RETURN (status);
00243 }
00244 
00245 
00246 
00247 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
00248 /** given a total hough map, this function produces a histogram of
00249    the number counts */
00250 /* *******************************  <lalVerbatim file="StatisticsD"> */
00251 void LALHoughHistogram(LALStatus      *status,
00252                        UINT8Vector    *out,
00253                        HOUGHMapTotal  *in)
00254 { /* *********************************************  </lalVerbatim> */
00255 
00256 
00257   INT4   i, j, length, xSide, ySide, temp;
00258 
00259   INITSTATUS (status, "LALHoughHistogram", STATISTICSC);
00260   ATTATCHSTATUSPTR (status);
00261 
00262   /* make sure arguments are not null */
00263   ASSERT (in, status, STATISTICSH_ENULL, STATISTICSH_MSGENULL);
00264   ASSERT (in->map, status, STATISTICSH_ENULL, STATISTICSH_MSGENULL);
00265   ASSERT (out, status, STATISTICSH_ENULL, STATISTICSH_MSGENULL);
00266 
00267   /* make sure input hough map is ok*/
00268   ASSERT (in->xSide > 0, status, STATISTICSH_EVAL, STATISTICSH_MSGEVAL);
00269   ASSERT (in->ySide > 0, status, STATISTICSH_EVAL, STATISTICSH_MSGEVAL);
00270   ASSERT (in->mObsCoh > 0, status, STATISTICSH_EVAL, STATISTICSH_MSGEVAL);
00271 
00272   /* make sure output length is same as mObsCoh+1 */
00273   ASSERT (out->length == in->mObsCoh+1, status, STATISTICSH_EVAL, STATISTICSH_MSGEVAL);
00274 
00275   length = out->length;
00276   xSide = in->xSide;
00277   ySide = in->ySide;
00278 
00279   /* initialize histogram vector*/
00280   for (i=0; i < length; i++) out->data[i] = 0;
00281 
00282   /* loop over hough map and find histogram */
00283   for (i = 0; i < ySide; i++){
00284     for (j = 0; j < xSide; j++){
00285       /* read the current number count and round it off to the
00286          nearest integer -- useful when the number counts are 
00287          floats as when we use weights */
00288       temp = (INT4)(in->map[i*xSide + j]);
00289 
00290       if ( (temp > length) || (temp < 0) ) {
00291         ABORT ( status, STATISTICSH_EVAL, STATISTICSH_MSGEVAL);
00292       }
00293       /* add to relevant entry in histogram */
00294       out->data[temp] += 1;
00295     }
00296   }
00297 
00298   DETATCHSTATUSPTR (status);
00299         
00300   /* normal exit */     
00301   RETURN (status);
00302 }
00303 
00304 
00305 
00306 
00307 

Generated on Fri Sep 5 03:07:30 2008 for LAL by  doxygen 1.5.2