Changeset 36222 for trunk/psLib/src/math/psStats.c
- Timestamp:
- Oct 16, 2013, 5:49:11 PM (13 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/math/psStats.c (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/math/psStats.c
r36121 r36222 141 141 142 142 // Debug information 143 #define CZW 0143 #define CZW 1 144 144 145 145 /*****************************************************************************/ … … 232 232 } 233 233 count++; 234 234 235 } 235 236 if (errors) { … … 424 425 for (long i = 0; i < myVector->n; i++) { 425 426 // Check if the data is with the specified range 427 426 428 if (!isfinite(data[i])) 427 429 continue; … … 1043 1045 1044 1046 1045 #if (CZW )1046 printf("CZW (%d): median %f sigma %f delta: %f \ t %f %f %f %f %f %f %f\t %f %f %f %f %f %f %f\n",1047 #if (CZW && 0) 1048 printf("CZW (%d): median %f sigma %f delta: %f \n\t %f %f %f %f %f %f %f \n\t %f %f %f %f %f %f %f\n", 1047 1049 iterate, 1048 1050 stats->robustMedian,stats->robustStdev, … … 1060 1062 cumulative->nums->data.F32[binMedian+1], 1061 1063 cumulative->nums->data.F32[binMedian+2],cumulative->nums->data.F32[binMedian+3]); 1062 PS_VECTOR_PRINT_F32(histogram->bounds);1063 PS_VECTOR_PRINT_F32(histogram->nums);1064 PS_VECTOR_PRINT_F32(cumulative->bounds);1065 PS_VECTOR_PRINT_F32(cumulative->nums);1064 // PS_VECTOR_PRINT_F32(histogram->bounds); 1065 // PS_VECTOR_PRINT_F32(histogram->nums); 1066 // PS_VECTOR_PRINT_F32(cumulative->bounds); 1067 // PS_VECTOR_PRINT_F32(cumulative->nums); 1066 1068 #endif 1067 1069 … … 1276 1278 return true; 1277 1279 } 1278 // printf("BINS: %ld %f %f %f %f\n",stats->robustN50,guessStdev,binSize,min,max); 1280 1279 1281 // Calculate the number of bins. 1280 1282 // XXX can we calculate the binMin, binMax **before** building this histogram? … … 1286 1288 psTrace(TRACE, 6, "The numBins is %ld\n", numBins); 1287 1289 1288 // printf("BINS2: %ld %f %f %f %f %ld\n",stats->robustN50,guessStdev,binSize,min,max,numBins);1289 1290 1290 1291 psHistogram *histogram = psHistogramAlloc(min, max, numBins); // A new histogram (without outliers) … … 1385 1386 } 1386 1387 y->n = x->n = j; 1387 1388 1388 1389 // fit 2nd order polynomial to ln(y) = -(x-xo)^2/2sigma^2 1389 1390 // XXX this fit may fail with an error for an ill-conditioned matrix (bad data) … … 1397 1398 psErrorClear(); 1398 1399 COUNT_WARNING(10, 100, "Failed to fit a gaussian to the robust histogram.\n"); 1400 1399 1401 psFree(poly); 1400 1402 psFree(histogram); … … 1478 1480 psPolynomial1D *poly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, 2); 1479 1481 bool status = psVectorFitPolynomial1D (poly, NULL, 0, y, NULL, x); 1482 #if (CZW && 0) 1483 for (long i = 0; i < x->n; i++) { 1484 printf("CZW: Dcheck: %ld %f %f %f\n", 1485 i,x->data.F32[i],y->data.F32[i], 1486 poly->coeff[0] + poly->coeff[1] * x->data.F32[i] + 1487 poly->coeff[2] * pow(x->data.F32[i],2)); 1488 } 1489 #endif 1480 1490 psFree(x); 1481 1491 psFree(y); … … 1493 1503 fullfitStdev = sqrt(-0.5/poly->coeff[2]); 1494 1504 fullfitMean = poly->coeff[1]*PS_SQR(fullfitStdev); 1505 1495 1506 #ifndef PS_NO_TRACE 1496 1507 psTrace(TRACE, 6, "Parabolic Symmetric fit results: %f + %f x + %f x^2\n", poly->coeff[0], poly->coeff[1], poly->coeff[2]); … … 1515 1526 } 1516 1527 1528 1517 1529 psFree (poly); 1518 1530 } … … 1537 1549 done = true; 1538 1550 } 1539 #if (CZW) 1540 printf("CZW IN FITTED? low %f %f full %f %f robust %f %f final %f %f\n", 1541 lowfitMean,lowfitStdev,fullfitMean,fullfitStdev,stats->robustMedian,stats->robustStdev, 1551 1552 1553 #if (CZW && 0) 1554 printf("CZW IN FITTED: iter %d %f \n" 1555 " low %f %f \n" 1556 " full %f %f \n" 1557 " robust %f %f \n" 1558 " final %f %f\n", 1559 iteration,minValueSym, 1560 lowfitMean,lowfitStdev, 1561 fullfitMean,fullfitStdev, 1562 stats->robustMedian,stats->robustStdev, 1542 1563 guessMean,guessStdev); 1543 1564 #endif … … 2423 2444 psTrace(TRACE, 6, "y data is (%f %f %f)\n", y->data.F64[0], y->data.F64[1], y->data.F64[2]); 2424 2445 2425 #if (CZW)2426 printf(" polyin: %f %f %f == %f %f %f\n",2427 x->data.F64[0], x->data.F64[1], x->data.F64[2],2428 y->data.F64[0], y->data.F64[1], y->data.F64[2]);2429 printf(" rawpolyin: %f %f %f == %f %f %f\n",2430 xVec->data.F32[binNum - 1], xVec->data.F32[binNum], xVec->data.F32[binNum + 1],2431 y->data.F64[0], y->data.F64[1], y->data.F64[2]);2432 #endif2433 2446 2434 2447 // Ensure that the y value lies within range of the y values. … … 2469 2482 (psF32) psPolynomial1DEval(myPoly, (psF64) x->data.F64[1]), 2470 2483 (psF32) psPolynomial1DEval(myPoly, (psF64) x->data.F64[2])); 2471 #if (CZW)2472 printf(" poly: %f %f %f fit: %f %f %f\n",2473 myPoly->coeff[0],myPoly->coeff[1],myPoly->coeff[2],2474 (psF32) psPolynomial1DEval(myPoly, (psF64) x->data.F64[0]),2475 (psF32) psPolynomial1DEval(myPoly, (psF64) x->data.F64[1]),2476 (psF32) psPolynomial1DEval(myPoly, (psF64) x->data.F64[2]));2477 #endif2478 2484 2479 2485 psTrace(TRACE, 6, "We fit the polynomial, now find x such that f(x) equals %f\n", yVal); … … 2498 2504 // tmpFloat = xVec->data.F32[fitBin] + dY * dX; 2499 2505 tmpFloat = binValue; 2500 2501 #if (CZW)2502 printf(" internal median: %f %f\n",tmpFloat,binValue);2503 #endif2504 2506 2505 2507 } else { … … 2556 2558 ) 2557 2559 { 2560 2558 2561 # if (1) 2559 double Sx = (yVec->data.F32[binNum - 2] + yVec->data.F32[binNum - 1] + 2560 yVec->data.F32[binNum - 0] + 2561 yVec->data.F32[binNum + 1] + yVec->data.F32[binNum + 2]); 2562 2563 double YM = 0.5*(xVec->data.F32[binNum - 2] + xVec->data.F32[binNum - 1]); 2564 double Ym = 0.5*(xVec->data.F32[binNum - 1] + xVec->data.F32[binNum - 0]); 2565 double Yo = 0.5*(xVec->data.F32[binNum - 0] + xVec->data.F32[binNum + 1]); 2566 double Yp = 0.5*(xVec->data.F32[binNum + 1] + xVec->data.F32[binNum + 2]); 2567 double YP = 0.5*(xVec->data.F32[binNum + 2] + xVec->data.F32[binNum + 3]); 2568 2569 double Sy = YM + Ym + Yo + Yp + YP; 2570 2571 double Sxx = (PS_SQR(yVec->data.F32[binNum - 2]) + PS_SQR(yVec->data.F32[binNum - 1]) + 2572 PS_SQR(yVec->data.F32[binNum - 0]) + 2573 PS_SQR(yVec->data.F32[binNum + 1]) + PS_SQR(yVec->data.F32[binNum + 2])); 2574 2575 double Sxy = (yVec->data.F32[binNum - 2] * YM + 2576 yVec->data.F32[binNum - 1] * Ym + 2577 yVec->data.F32[binNum - 0] * Yo + 2578 yVec->data.F32[binNum + 1] * Yp + 2579 yVec->data.F32[binNum + 2] * YP); 2580 2581 double Det = 5*Sxx - Sx*Sx; 2582 if (Det == 0.0) return NAN; 2583 2584 double C0 = (Sy*Sxx - Sx*Sxy) / Det; 2585 double C1 = (Sxy*5 - Sx*Sy) / Det; 2586 2587 double value = C0 + yVal*C1; 2588 2589 return value; 2562 # define HALF_SIZE 2 2563 double Sx = 0.0; 2564 2565 double Sy = 0.0; 2566 double Sxx = 0.0; 2567 double Sxy = 0.0; 2568 double deltaY = 0.0; 2569 int N = 0; 2570 2571 for (int u = binNum - HALF_SIZE; u <= binNum + HALF_SIZE; u++) { 2572 if ((u >= 0)&&(u < yVec->n)) { 2573 if (u+1 < xVec->n) { 2574 Sx += yVec->data.F32[u]; 2575 Sxx += PS_SQR(yVec->data.F32[u]); 2576 2577 deltaY = 0.5 * (xVec->data.F32[u] + xVec->data.F32[u+1]); 2578 Sy += deltaY; 2579 Sxy += yVec->data.F32[u] * deltaY; 2580 N += 1; 2581 } 2582 } 2583 } 2584 double Det = N * Sxx - Sx * Sx; 2585 if (Det == 0.0) return NAN; 2586 if (N == 0) return NAN; 2587 2588 double C0 = (Sy*Sxx - Sx*Sxy) / Det; 2589 double C1 = (Sxy*N - Sx*Sy) / Det; 2590 2591 double value = C0 + yVal*C1; 2592 return value; 2593 2594 2590 2595 # else 2591 2596 psTrace(TRACE, 5, "binNum, yVal is (%d, %f)\n", binNum, yVal); … … 2682 2687 tmpFloat = binValue; 2683 2688 2684 #if (CZW)2685 printf(" internal median: %f %f\n",tmpFloat,binValue);2686 #endif2687 2689 2688 2690 } else {
Note:
See TracChangeset
for help on using the changeset viewer.
