Changeset 682 for trunk/psLib/src/math/psStats.c
- Timestamp:
- May 13, 2004, 4:44:09 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/math/psStats.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/math/psStats.c
r679 r682 3 3 #include <string.h> 4 4 #include <stdarg.h> 5 /******************************************************************************6 5 //#include "pslib.h" 7 6 #include "psMemory.h" … … 11 10 //#include "psError.h" 12 11 //#include "psArray.h" 13 *****************************************************************************/14 12 #include "psStats.h" 13 14 #include "float.h" 15 #include <math.h> 16 15 17 /****************************************************************************** 16 18 psStatsAlloc(): This routine must create a new psStats data structure. … … 19 21 { 20 22 psStats *newStruct = NULL; 23 21 24 newStruct = (psStats *) psAlloc(sizeof(psStats)); 22 newStruct->sampleMean = 0.0;23 newStruct->sampleMedian = 0.0;24 newStruct->sampleStdev = 0.0;25 newStruct->sampleUQ = 0.0;26 newStruct->sampleLQ = 0.0;27 newStruct->robustMean = 0.0;25 newStruct->sampleMean = NAN; 26 newStruct->sampleMedian = NAN; 27 newStruct->sampleStdev = NAN; 28 newStruct->sampleUQ = NAN; 29 newStruct->sampleLQ = NAN; 30 newStruct->robustMean = NAN; 28 31 newStruct->robustMeanNvalues = 0; 29 newStruct->robustMedian = 0.0;32 newStruct->robustMedian = NAN; 30 33 newStruct->robustMedianNvalues = 0; 31 newStruct->robustMode = 0.0;34 newStruct->robustMode = NAN; 32 35 newStruct->robustModeNvalues = 0; 33 newStruct->robustStdev = 0.0;34 newStruct->robustUQ = 0.0;35 newStruct->robustLQ = 0.0;36 newStruct->clippedMean = 0.0;36 newStruct->robustStdev = NAN; 37 newStruct->robustUQ = NAN; 38 newStruct->robustLQ = NAN; 39 newStruct->clippedMean = NAN; 37 40 newStruct->clippedMeanNvalues = 0; 38 newStruct->clippedStdev = 0.0;39 newStruct->clipSigma = 0.0;41 newStruct->clippedStdev = NAN; 42 newStruct->clipSigma = NAN; 40 43 newStruct->clipIter = 0; 41 newStruct->min = 0.0;42 newStruct->max = 0.0;44 newStruct->min = NAN; 45 newStruct->max = NAN; 43 46 newStruct->nValues = 0; 44 47 newStruct->options = options; … … 156 159 MISC STATISTICAL FUNCTIONS 157 160 *****************************************************************************/ 158 float p_psArraySampleMean(const psVector *restrict myVector, 159 const psVector *restrict maskVector, 160 unsigned int maskVal) 161 void p_psArraySampleMean(const psVector *restrict myVector, 162 const psVector *restrict maskVector, 163 unsigned int maskVal, 164 psStats *newStruct) 161 165 { 162 166 int i = 0; … … 178 182 mean/= (float) myVector->n; 179 183 } 180 return(mean); 181 } 182 183 float p_psArrayMax(const psVector *restrict myVector, 184 const psVector *restrict maskVector, 185 unsigned int maskVal) 184 newStruct->sampleMean = mean; 185 } 186 187 void p_psArrayMax(const psVector *restrict myVector, 188 const psVector *restrict maskVector, 189 unsigned int maskVal, 190 psStats *newStruct) 186 191 { 187 192 int i = 0; 188 193 float max = -1e99; 189 194 190 printf("COOL: p_psArrayMax()\n");191 195 if (maskVector != NULL) { 192 196 for (i=0;i<myVector->n;i++) { … … 204 208 } 205 209 } 206 return(max); 207 } 208 209 float p_psArrayMin(const psVector *restrict myVector, /* FLOATS */ 210 const psVector *restrict maskVector, /* INTS */ 211 unsigned int maskVal) 210 newStruct->max = max; 211 } 212 213 void p_psArrayMin(const psVector *restrict myVector, /* FLOATS */ 214 const psVector *restrict maskVector, /* INTS */ 215 unsigned int maskVal, 216 psStats *newStruct) 212 217 { 213 218 int i = 0; … … 229 234 } 230 235 } 231 return(min);236 newStruct->min = min; 232 237 } 233 238 … … 235 240 236 241 *****************************************************************************/ 237 int p_psArrayNValues(const psVector *restrict myVector, // float 238 const psVector *restrict maskVector, // ints 239 unsigned int maskVal) 242 void p_psArrayNValues(const psVector *restrict myVector, 243 const psVector *restrict maskVector, 244 unsigned int maskVal, 245 psStats *newStruct) 240 246 { 241 247 int i = 0; … … 251 257 numData = myVector->n; 252 258 } 253 return(numData);259 newStruct->nValues = numData; 254 260 } 255 261 … … 257 263 258 264 #define MEDIAN_SIZE_THRESHOLD 10000 259 float p_psArraySampleMedian(const psVector *restrict myVector, 260 const psVector *restrict maskVector, 261 unsigned int maskVal) 265 void p_psArraySampleMedian(const psVector *restrict myVector, 266 const psVector *restrict maskVector, 267 unsigned int maskVal, 268 psStats *newStruct) 262 269 { 263 270 psVector *unsortedVector = NULL; … … 268 275 float median = 0.0; 269 276 270 dataSize = p_psArrayNValues(myVector, maskVector, maskVal);277 p_psArrayNValues(myVector, maskVector, maskVal, newStruct); 271 278 272 279 if (dataSize < MEDIAN_SIZE_THRESHOLD) { … … 307 314 psVectorFree(unsortedVector); 308 315 psVectorFree(sortedVector); 309 return(median);310 } 311 312 313 int p_psArrayXXX(const psVector *restrict myVector,314 const psVector *restrict maskVector,315 unsigned int maskVal)316 newStruct->sampleMedian = median; 317 } 318 319 320 float p_psArrayXXX(const psVector *restrict myVector, 321 const psVector *restrict maskVector, 322 unsigned int maskVal) 316 323 { 317 324 printf("ERROR: Don't call me: p_psArrayXXX()\n"); 318 325 exit(1); 326 } 327 328 void p_psArraySampleStdev(const psVector *restrict myVector, 329 const psVector *restrict maskVector, 330 unsigned int maskVal, 331 psStats *newStruct) 332 { 333 int i = 0; 334 int countInt = 0; 335 float countFloat = 0.0; 336 float mean = 0.0; 337 float diff = 0.0; 338 float sumSquares = 0.0; 339 float sumDiffs = 0.0; 340 341 if (0 != isnan(newStruct->sampleMean)) { 342 p_psArraySampleMean(myVector, maskVector, maskVal, newStruct); 343 } 344 mean = newStruct->sampleMean; 345 346 if (maskVector != NULL) { 347 for (i=0;i<myVector->n;i++) { 348 if (!(maskVal & maskVector->vec.ui8[i])) { 349 diff = myVector->vec.f[i] - mean; 350 sumSquares+= (diff * diff); 351 sumDiffs+= diff; 352 countInt++; 353 354 } 355 } 356 } else { 357 for (i=0;i<myVector->n;i++) { 358 diff = myVector->vec.f[i] - mean; 359 sumSquares+= (diff * diff); 360 sumDiffs+= diff; 361 countInt++; 362 } 363 countInt = myVector->n; 364 } 365 countFloat = (float) countInt; 366 newStruct->sampleStdev = sqrtf( (sumSquares-(sumDiffs * 367 sumDiffs/countFloat))/ (countFloat-1)); 319 368 } 320 369 … … 333 382 psStats *newStruct = NULL; 334 383 335 newStruct = psStatsAlloc(stats->options);336 384 if (myVector == NULL) { 337 385 psAbort(__func__, … … 354 402 } 355 403 } 404 newStruct = psStatsAlloc(stats->options); 356 405 357 406 if (stats->options & PS_STAT_SAMPLE_MEAN) { 358 newStruct->sampleMean = p_psArraySampleMean(myVector, 359 maskVector, 360 maskVal); 407 p_psArraySampleMean(myVector, maskVector, maskVal, newStruct); 361 408 } 362 409 363 410 if (stats->options & PS_STAT_MAX) { 364 newStruct->max = p_psArrayMax(myVector, maskVector, maskVal);411 p_psArrayMax(myVector, maskVector, maskVal, newStruct); 365 412 } 366 413 367 414 if (stats->options & PS_STAT_MIN) { 368 newStruct->min = p_psArrayMin(myVector, maskVector, maskVal);415 p_psArrayMin(myVector, maskVector, maskVal, newStruct); 369 416 } 370 417 371 418 if (stats->options & PS_STAT_NVALUES) { 372 newStruct->nValues = p_psArrayNValues(myVector, maskVector, maskVal); 373 } 374 375 if (stats->options & PS_STAT_SAMPLE_MEDIAN) { 376 newStruct->sampleMedian = p_psArrayXXX(myVector, maskVector, maskVal); 377 } 419 p_psArrayNValues(myVector, maskVector, maskVal, newStruct); 420 } 421 422 if (stats->options & PS_STAT_SAMPLE_MEDIAN) {} 378 423 379 424 if (stats->options & PS_STAT_SAMPLE_STDEV) { 380 newStruct->sampleStdev = p_psArrayXXX(myVector, maskVector, maskVal);425 p_psArraySampleStdev(myVector, maskVector, maskVal, newStruct); 381 426 } 382 427
Note:
See TracChangeset
for help on using the changeset viewer.
