LALPulsar  6.1.0.1-89842e6
lib/statistics.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Badri Krishnan, Alicia Sintes
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with with program; see the file COPYING. If not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  */
19 
20 #include <lal/Statistics.h>
21 
22 /*
23  * The functions that make up the guts of this module
24  */
25 
26 /**
27  * \ingroup Statistics_h
28  * This function calculates the maximum number count, minimum
29  * number count, average and standard deviation of a given total Hough map.
30  * The input HOUGHMapTotal *in is a total Hough map and the output is a
31  * structure HoughStats *out.
32  */
34  HoughStats *out,
35  HOUGHMapTotal *in )
36 {
37 
38  INT4 i, j, xSide, ySide;
39  INT4 maxIndexX, maxIndexY, minIndexX, minIndexY;
40  REAL8 average, nPixel, variance, ep, temp, sum;
41  HoughTT max, min;
42  /*--------------------------------------------------------------- */
43 
44  INITSTATUS( status );
46 
47  /* make sure arguments are not null */
50 
51  /* make sure input hough map is ok*/
55 
56  /* read input parameters */
57  xSide = in->xSide;
58  ySide = in->ySide;
59 
60  /* first find maximum, minimum and average */
61  maxIndexX = maxIndexY = minIndexX = minIndexY = 0;
62  sum = 0;
63  max = min = in->map[0];
64  /* loop over hough map and calculate statistics */
65  for ( i = 0; i < ySide; i++ ) {
66  for ( j = 0; j < xSide; j++ ) {
67  /* read the current number count */
68  temp = ( REAL4 ) in->map[i * xSide + j];
69 
70  /* add number count to sum */
71  sum += temp;
72 
73  /* look for maximum */
74  if ( temp > ( REAL4 )max ) {
75  max = in->map[i * xSide + j];
76  maxIndexX = j;
77  maxIndexY = i;
78  }
79 
80  /* look for minimum */
81  if ( temp < ( REAL4 )min ) {
82  min = in->map[i * xSide + j];
83  minIndexX = j;
84  minIndexY = i;
85  }
86  }
87  }
88 
89  nPixel = 1.0 * xSide * ySide;
90  average = sum / nPixel;
91 
92  /* now do another loop to find the variance and standard deviation */
93  /* look at numerical recipes in C for calculation of variance */
94  variance = 0.0;
95  ep = 0.0;
96  for ( i = 0; i < ySide; i++ ) {
97  for ( j = 0; j < xSide; j++ ) {
98  temp = ( REAL4 )( in->map[i * xSide + j] ) - average;
99  ep += temp;
100  variance += temp * temp;
101  }
102  }
103  /* the ep is supposed to reduce the rounding off errors */
104  variance = ( variance - ep * ep / nPixel ) / ( nPixel - 1 );
105 
106 
107  /* copy results to output structure */
108  out->maxCount = max;
109  out->maxIndex[0] = maxIndexX;
110  out->maxIndex[1] = maxIndexY;
111  out->minCount = min;
112  out->minIndex[0] = minIndexX;
113  out->minIndex[1] = minIndexY;
114  out->avgCount = average;
115  out->stdDev = sqrt( variance );
116 
117 
119 
120  /* normal exit */
121  RETURN( status );
122 }
123 
124 
125 
127  REAL8 *mean,
128  REAL8 *variance,
129  HOUGHMapTotal *in )
130 {
131 
132  INT4 i, j, xSide, ySide, nPixel;
133  REAL8 sum, tempVariance, tempMean, ep, temp;
134  /*--------------------------------------------------------------- */
135 
136  INITSTATUS( status );
138 
139 
140  xSide = in->xSide;
141  ySide = in->ySide;
142  nPixel = 1.0 * xSide * ySide;
143 
144 
145  sum = 0.0;
146  /* loop over hough map and calculate statistics */
147  for ( i = 0; i < ySide; i++ ) {
148  for ( j = 0; j < xSide; j++ ) {
149  /* read the current number count */
150  sum += in->map[i * xSide + j];
151  }
152  }
153 
154  tempMean = sum / nPixel;
155 
156  tempVariance = 0.0;
157  ep = 0.0;
158  for ( i = 0; i < ySide; i++ ) {
159  for ( j = 0; j < xSide; j++ ) {
160  temp = ( REAL4 )( in->map[i * xSide + j] ) - tempMean;
161  ep += temp;
162  tempVariance += temp * temp;
163  }
164  }
165  /* the ep is supposed to reduce the rounding off errors */
166  tempVariance = ( tempVariance - ep * ep / nPixel ) / ( nPixel - 1 );
167 
168  *mean = tempMean;
169  *variance = tempVariance;
170 
172 
173  /* normal exit */
174  RETURN( status );
175 }
176 
177 /**
178  * \ingroup Statistics_h
179  * \brief Produces a histogram of the number counts in a total Hough map.
180  * The input is of type <tt>HOUGHMapTotal *in</tt> and the output <tt>UINT4Vector *out</tt>.
181  */
183  UINT8Vector *out,
184  HOUGHMapTotal *in )
185 {
186 
187 
188  INT4 i, j, length, xSide, ySide, temp;
189 
190  INITSTATUS( status );
192 
193  /* make sure arguments are not null */
197 
198  /* make sure input hough map is ok*/
202 
203  /* make sure output length is same as mObsCoh+1 */
204  ASSERT( out->length == in->mObsCoh + 1, status, STATISTICSH_EVAL, STATISTICSH_MSGEVAL );
205 
206  length = out->length;
207  xSide = in->xSide;
208  ySide = in->ySide;
209 
210  /* initialize histogram vector*/
211  for ( i = 0; i < length; i++ ) {
212  out->data[i] = 0;
213  }
214 
215  /* loop over hough map and find histogram */
216  for ( i = 0; i < ySide; i++ ) {
217  for ( j = 0; j < xSide; j++ ) {
218  /* read the current number count and round it off to the
219  nearest integer -- useful when the number counts are
220  floats as when we use weights */
221  temp = ( INT4 )( in->map[i * xSide + j] );
222 
223  if ( ( temp > length ) || ( temp < 0 ) ) {
225  }
226  /* add to relevant entry in histogram */
227  out->data[temp] += 1;
228  }
229  }
230 
232 
233  /* normal exit */
234  RETURN( status );
235 }
236 
237 
238 
239 
240 
#define min(a, b)
int j
#define ABORT(statusptr, code, mesg)
#define ATTATCHSTATUSPTR(statusptr)
#define ASSERT(assertion, statusptr, code, mesg)
#define DETATCHSTATUSPTR(statusptr)
#define INITSTATUS(statusptr)
#define RETURN(statusptr)
REAL8 HoughTT
Total Hough Map pixel type.
Definition: HoughMap.h:113
double REAL8
int32_t INT4
float REAL4
void LALHoughStatistics(LALStatus *status, HoughStats *out, HOUGHMapTotal *in)
This function calculates the maximum number count, minimum number count, average and standard deviati...
#define STATISTICSH_EVAL
#define STATISTICSH_ENULL
void LALHoughHistogram(LALStatus *status, UINT8Vector *out, HOUGHMapTotal *in)
Produces a histogram of the number counts in a total Hough map.
#define STATISTICSH_MSGEVAL
#define STATISTICSH_MSGENULL
void LALHoughmapMeanVariance(LALStatus *status, REAL8 *mean, REAL8 *variance, HOUGHMapTotal *in)
temp
out
This structure stores the Hough map.
Definition: HoughMap.h:130
UINT2 ySide
number of physical pixels in the y direction
Definition: HoughMap.h:142
UINT2 xSide
number of physical pixels in the x direction
Definition: HoughMap.h:141
HoughTT * map
the pixel counts; the number of elements to allocate is ySide*xSide
Definition: HoughMap.h:143
UINT4 mObsCoh
ratio of observation time and coherent timescale
Definition: HoughMap.h:133
Structure for storing statistical information about a Hough map.
#define max(a, b)