source: tags/release-1.1.12/src/Devel/trimStats.cc

Last change on this file was 393, checked in by MatthewWhiting, 17 years ago

Fixed up headers for trunk as well.

File size: 4.9 KB
Line 
1// -----------------------------------------------------------------------
2// trimStats.cc: Find estimates of the mean & rms by looking at the
3//               histogram of pixel values trimmed of outliers.
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#include <iostream>
30#include <math.h>
31#include <duchamp/Utils/utils.hh>
32
33void findTrimmedHistStatsOLD(float *array, const int size,
34                             float &tmean, float &tsigma)
35{
36
37  const int nbin = 100;
38  float *num = new float[nbin];
39  bool *keep = new bool[nbin];
40
41  // HOW MANY VALUES IN EACH BIN?
42  float min,max;
43  findMinMax(array,size,min,max);
44  for(int i=0; i<nbin; i++) num[i]=0;
45  for(int i=0; i<size; i++){
46    float fraction = (array[i] - min) / (max - min);
47    int bin = (int)( floor(fraction*nbin) );
48    if((bin>=0)&&(bin<nbin)) num[bin]++;
49  }
50  int binmax = 0;
51  for(int i=1; i<nbin; i++)  if(num[i]>num[binmax]) binmax = i;
52  for(int i=0; i<nbin; i++) keep[i] = (num[i]>=num[binmax]/2);
53  float *newarray = new float[size];
54  int newsize = 0;
55  for(int i=0; i<size; i++){
56    float fraction = (array[i] - min) / (max - min);
57    int bin = (int)( floor(fraction*nbin) );
58    if(keep[bin]) newarray[newsize++] = array[i];
59  }
60
61  findNormalStats(newarray,newsize,tmean,tsigma);
62  delete [] num;
63  delete [] keep;
64  delete [] newarray;
65
66}
67//--------------------------------------------------------------------
68
69void findTrimmedHistStats2(float *array, const int size,
70                           float &tmean, float &tsigma)
71{
72
73  const int nbin = 200;
74  float *num = new float[nbin];
75  bool *keep = new bool[nbin];
76
77  // HOW MANY VALUES IN EACH BIN?
78  float min,max;
79  findMinMax(array,size,min,max);
80  for(int i=0; i<nbin; i++) num[i]=0;
81  for(int i=0; i<size; i++){
82    float fraction = (array[i] - min) / (max - min);
83    int bin = (int)( floor(fraction*nbin) );
84    if((bin>=0)&&(bin<nbin)) num[bin]++;
85  }
86  int binmax = 0;
87  for(int i=1; i<nbin; i++)  if(num[i]>num[binmax]) binmax = i;
88  for(int i=0; i<nbin; i++) keep[i] = (num[i]>=num[binmax]/2);
89  float *newarray = new float[size];
90  int newsize = 0;
91  for(int i=0; i<size; i++){
92    float fraction = (array[i] - min) / (max - min);
93    int bin = (int)( floor(fraction*nbin) );
94    if(keep[bin]) newarray[newsize++] = array[i];
95  }
96
97  tmean = findMean(newarray,newsize);
98
99  tsigma = newsize * (max-min)/float(nbin) /
100    (num[binmax] * erf(sqrt(M_LN2)) * sqrt(2.*M_PI));
101
102}
103//--------------------------------------------------------------------
104
105void findTrimmedHistStats(float *array, const int size,
106                          float &tmean, float &tsigma)
107{
108  float datamin,datamax;
109  findMinMax(array,size,datamin,datamax);
110  float *sorted = new float[size];
111  float *cumul  = new float[size];
112  float *angle  = new float[size];
113  for(int i=0;i<size;i++) sorted[i] = array[i]/(datamax-datamin);
114  std::sort(sorted,sorted+size);
115  for(int i=0;i<size;i++) cumul[i] = (float)i/(float)size;
116  int width =(int)( 20. * log10((float)size));
117  for(int i=0;i<size;i++){
118    int beg,end;
119    float slope,eSlope,inter,eInter,r;
120    if(i<width/2) beg=0; else beg=i-width/2;
121    if(i>=size-width/2) end=size-1; else end=i+width/2;
122    if(linear_regression(size,sorted,cumul,beg,end,slope,eSlope,inter,eInter,r)==0)
123      angle[i] = atan( fabs(slope) ) * 180. / M_PI;
124    else angle[i] = 90.;
125  }
126
127//   int start = 0;
128//   while(angle[start] < 45.) start++;
129//   int finish = size-1;
130//   while(angle[finish] < 45.) finish--;
131
132  int maxpt = 0;
133  for(int i = 1; i<size; i++) if(angle[i]>angle[maxpt]) maxpt=i;
134  int start = maxpt;
135  while((start>0)&&(angle[start]>45.)) start--;
136  int finish = maxpt;
137  while((finish < size-1)&&(angle[finish]>45.)) finish++;
138
139  float *newarray = new float[finish-start+1];
140  for(int i=0;i<finish-start+1;i++)
141    newarray[i] = sorted[i+start]*(datamax-datamin);
142
143  findNormalStats(newarray,finish-start+1,tmean,tsigma);
144 
145}
Note: See TracBrowser for help on using the repository browser.