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

Last change on this file was 889, checked in by MatthewWhiting, 12 years ago

Making our choice of template in the statistics functions explicit

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 <algorithm>
32#include <duchamp/Utils/utils.hh>
33
34void findTrimmedHistStatsOLD(float *array, const int size,
35                             float &tmean, float &tsigma)
36{
37
38  const int nbin = 100;
39  float *num = new float[nbin];
40  bool *keep = new bool[nbin];
41
42  // HOW MANY VALUES IN EACH BIN?
43  float min,max;
44  findMinMax(array,size,min,max);
45  for(int i=0; i<nbin; i++) num[i]=0;
46  for(int i=0; i<size; i++){
47    float fraction = (array[i] - min) / (max - min);
48    int bin = (int)( floor(fraction*nbin) );
49    if((bin>=0)&&(bin<nbin)) num[bin]++;
50  }
51  int binmax = 0;
52  for(int i=1; i<nbin; i++)  if(num[i]>num[binmax]) binmax = i;
53  for(int i=0; i<nbin; i++) keep[i] = (num[i]>=num[binmax]/2);
54  float *newarray = new float[size];
55  int newsize = 0;
56  for(int i=0; i<size; i++){
57    float fraction = (array[i] - min) / (max - min);
58    int bin = (int)( floor(fraction*nbin) );
59    if(keep[bin]) newarray[newsize++] = array[i];
60  }
61
62  findNormalStats(newarray,newsize,tmean,tsigma);
63  delete [] num;
64  delete [] keep;
65  delete [] newarray;
66
67}
68//--------------------------------------------------------------------
69
70void findTrimmedHistStats2(float *array, const int size,
71                           float &tmean, float &tsigma)
72{
73
74  const int nbin = 200;
75  float *num = new float[nbin];
76  bool *keep = new bool[nbin];
77
78  // HOW MANY VALUES IN EACH BIN?
79  float min,max;
80  findMinMax(array,size,min,max);
81  for(int i=0; i<nbin; i++) num[i]=0;
82  for(int i=0; i<size; i++){
83    float fraction = (array[i] - min) / (max - min);
84    int bin = (int)( floor(fraction*nbin) );
85    if((bin>=0)&&(bin<nbin)) num[bin]++;
86  }
87  int binmax = 0;
88  for(int i=1; i<nbin; i++)  if(num[i]>num[binmax]) binmax = i;
89  for(int i=0; i<nbin; i++) keep[i] = (num[i]>=num[binmax]/2);
90  float *newarray = new float[size];
91  int newsize = 0;
92  for(int i=0; i<size; i++){
93    float fraction = (array[i] - min) / (max - min);
94    int bin = (int)( floor(fraction*nbin) );
95    if(keep[bin]) newarray[newsize++] = array[i];
96  }
97
98  tmean = findMean<float>(newarray,newsize);
99
100  tsigma = newsize * (max-min)/float(nbin) /
101    (num[binmax] * erf(sqrt(M_LN2)) * sqrt(2.*M_PI));
102
103}
104//--------------------------------------------------------------------
105
106void findTrimmedHistStats(float *array, const int size,
107                          float &tmean, float &tsigma)
108{
109  float datamin,datamax;
110  findMinMax(array,size,datamin,datamax);
111  float *sorted = new float[size];
112  float *cumul  = new float[size];
113  float *angle  = new float[size];
114  for(int i=0;i<size;i++) sorted[i] = array[i]/(datamax-datamin);
115  std::sort(sorted,sorted+size);
116  for(int i=0;i<size;i++) cumul[i] = (float)i/(float)size;
117  int width =(int)( 20. * log10((float)size));
118  for(int i=0;i<size;i++){
119    int beg,end;
120    float slope,eSlope,inter,eInter,r;
121    if(i<width/2) beg=0; else beg=i-width/2;
122    if(i>=size-width/2) end=size-1; else end=i+width/2;
123    if(linear_regression(size,sorted,cumul,beg,end,slope,eSlope,inter,eInter,r)==0)
124      angle[i] = atan( fabs(slope) ) * 180. / M_PI;
125    else angle[i] = 90.;
126  }
127
128//   int start = 0;
129//   while(angle[start] < 45.) start++;
130//   int finish = size-1;
131//   while(angle[finish] < 45.) finish--;
132
133  int maxpt = 0;
134  for(int i = 1; i<size; i++) if(angle[i]>angle[maxpt]) maxpt=i;
135  int start = maxpt;
136  while((start>0)&&(angle[start]>45.)) start--;
137  int finish = maxpt;
138  while((finish < size-1)&&(angle[finish]>45.)) finish++;
139
140  float *newarray = new float[finish-start+1];
141  for(int i=0;i<finish-start+1;i++)
142    newarray[i] = sorted[i+start]*(datamax-datamin);
143
144  findNormalStats(newarray,finish-start+1,tmean,tsigma);
145 
146}
Note: See TracBrowser for help on using the repository browser.