source: branches/pixel-map-branch/src/Utils/trimStats.cc @ 1441

Last change on this file since 1441 was 222, checked in by Matthew Whiting, 17 years ago

Large commit, but mostly documentation-oriented.

Only non-doc-related changes are:

  • To remove two deprecated files and any declarations of the functions in them
  • To move drawBlankEdges to the Cubes/ directory
  • Some small changes to the implementation of the StatsContainer? functions.
  • Creation of Utils/devel.hh to hide functions not used in Duchamp
  • To move the trimmedHist stats functions to their own file, again to hide them.
File size: 3.5 KB
Line 
1#include <iostream>
2#include <math.h>
3#include <Utils/utils.hh>
4
5void findTrimmedHistStatsOLD(float *array, const int size,
6                             float &tmean, float &tsigma)
7{
8
9  const int nbin = 100;
10  float *num = new float[nbin];
11  bool *keep = new bool[nbin];
12
13  // HOW MANY VALUES IN EACH BIN?
14  float min,max;
15  findMinMax(array,size,min,max);
16  for(int i=0; i<nbin; i++) num[i]=0;
17  for(int i=0; i<size; i++){
18    float fraction = (array[i] - min) / (max - min);
19    int bin = (int)( floor(fraction*nbin) );
20    if((bin>=0)&&(bin<nbin)) num[bin]++;
21  }
22  int binmax = 0;
23  for(int i=1; i<nbin; i++)  if(num[i]>num[binmax]) binmax = i;
24  for(int i=0; i<nbin; i++) keep[i] = (num[i]>=num[binmax]/2);
25  float *newarray = new float[size];
26  int newsize = 0;
27  for(int i=0; i<size; i++){
28    float fraction = (array[i] - min) / (max - min);
29    int bin = (int)( floor(fraction*nbin) );
30    if(keep[bin]) newarray[newsize++] = array[i];
31  }
32
33  findNormalStats(newarray,newsize,tmean,tsigma);
34  delete [] num;
35  delete [] keep;
36  delete [] newarray;
37
38}
39//--------------------------------------------------------------------
40
41void findTrimmedHistStats2(float *array, const int size,
42                           float &tmean, float &tsigma)
43{
44
45  const int nbin = 200;
46  float *num = new float[nbin];
47  bool *keep = new bool[nbin];
48
49  // HOW MANY VALUES IN EACH BIN?
50  float min,max;
51  findMinMax(array,size,min,max);
52  for(int i=0; i<nbin; i++) num[i]=0;
53  for(int i=0; i<size; i++){
54    float fraction = (array[i] - min) / (max - min);
55    int bin = (int)( floor(fraction*nbin) );
56    if((bin>=0)&&(bin<nbin)) num[bin]++;
57  }
58  int binmax = 0;
59  for(int i=1; i<nbin; i++)  if(num[i]>num[binmax]) binmax = i;
60  for(int i=0; i<nbin; i++) keep[i] = (num[i]>=num[binmax]/2);
61  float *newarray = new float[size];
62  int newsize = 0;
63  for(int i=0; i<size; i++){
64    float fraction = (array[i] - min) / (max - min);
65    int bin = (int)( floor(fraction*nbin) );
66    if(keep[bin]) newarray[newsize++] = array[i];
67  }
68
69  tmean = findMean(newarray,newsize);
70
71  tsigma = newsize * (max-min)/float(nbin) /
72    (num[binmax] * erf(sqrt(M_LN2)) * sqrt(2.*M_PI));
73
74}
75//--------------------------------------------------------------------
76
77void findTrimmedHistStats(float *array, const int size,
78                          float &tmean, float &tsigma)
79{
80  float datamin,datamax;
81  findMinMax(array,size,datamin,datamax);
82  float *sorted = new float[size];
83  float *cumul  = new float[size];
84  float *angle  = new float[size];
85  for(int i=0;i<size;i++) sorted[i] = array[i]/(datamax-datamin);
86  std::sort(sorted,sorted+size);
87  for(int i=0;i<size;i++) cumul[i] = (float)i/(float)size;
88  int width =(int)( 20. * log10((float)size));
89  for(int i=0;i<size;i++){
90    int beg,end;
91    float slope,eSlope,inter,eInter,r;
92    if(i<width/2) beg=0; else beg=i-width/2;
93    if(i>=size-width/2) end=size-1; else end=i+width/2;
94    if(linear_regression(size,sorted,cumul,beg,end,slope,eSlope,inter,eInter,r)==0)
95      angle[i] = atan( fabs(slope) ) * 180. / M_PI;
96    else angle[i] = 90.;
97  }
98
99//   int start = 0;
100//   while(angle[start] < 45.) start++;
101//   int finish = size-1;
102//   while(angle[finish] < 45.) finish--;
103
104  int maxpt = 0;
105  for(int i = 1; i<size; i++) if(angle[i]>angle[maxpt]) maxpt=i;
106  int start = maxpt;
107  while((start>0)&&(angle[start]>45.)) start--;
108  int finish = maxpt;
109  while((finish < size-1)&&(angle[finish]>45.)) finish++;
110
111  float *newarray = new float[finish-start+1];
112  for(int i=0;i<finish-start+1;i++)
113    newarray[i] = sorted[i+start]*(datamax-datamin);
114
115  findNormalStats(newarray,finish-start+1,tmean,tsigma);
116 
117}
Note: See TracBrowser for help on using the repository browser.