source: trunk/src/pdf2stat.m @ 1112

Last change on this file since 1112 was 880, checked in by sommeria, 10 years ago

various bug fixes

File size: 3.6 KB
RevLine 
[880]1%'pdf2stat': calculate statistics from pdf
2%
3%OUTPUT:
4% VarVal(13,nb_component):statistics for each of the input components
5% line 1: 'SampleNbre': nbe of samples (sum of pdf_val)
6% line 2: 'BinSize'
7% line 3: 'Mean': mean value
8% line 4: 'RMS': root mean square deviation from mean
9% line 5: 'Skewness': third order moment normalized by (rms)^3
10% line 6: 'Kurtosis': : fourth order moment normalized by (rms)^4 (= 3 for a Gaussian)
11% line 7: 'Min': min value
12%  statistics for the centered variable (x-mean)
13% line 8: 'FirstCentile': first centile
14% line 9: 'FirstDecile'
15% line 10: 'Median'
16% line 11: 'LastDecile'
17% line 12: 'LastCentile'
18% line 13: 'Max'
19%
20% INPUT:
21% x(nb_bin): column vector representing the set of variable values for each bin of the histogram
22% pdf_val(nb_bin,nb_component): values of the histogram at each bin x,
23%              possibly with different components (e.g. rgb color image)
24%
25
26function VarVal=pdf2stat(x,pdf_val)
27if ~exist('x','var')% list the stat names
28    VarVal={'SampleNbre';'BinSize';'Mean';'RMS';'Skewness';'Kurtosis';...
29        'Min';'FirstCentile';'FirstDecile';'Median';'LastDecile';'LastCentile';'Max'};
30else
31    nbvar=size(pdf_val,2);
32    VarVal=zeros(13,nbvar);
33    for ivar=1:nbvar
34    VarVal(1,ivar)=sum(pdf_val(:,ivar));% total sample number
35    VarVal(7,ivar)=min(x);
36    VarVal(13,ivar)=max(x);
37    VarVal(2,ivar)=(VarVal(13,ivar)-VarVal(7,ivar))/(numel(x)-1);%bin size
38    pdf_val(:,ivar)=pdf_val(:,ivar)/VarVal(1,ivar);% normalised pdf
39    VarVal(3,ivar)=sum(x.*pdf_val(:,ivar));%Mean
40    x=x-VarVal(3,ivar); %centered variable
41    Variance=sum(x.*x.*pdf_val(:,ivar));
42    VarVal(4,ivar)=sqrt(Variance);
43    VarVal(5,ivar)=(sum(x.*x.*x.*pdf_val(:,ivar)))/(Variance*VarVal(4,ivar));%skewness
44    VarVal(6,ivar)=(sum(x.*x.*x.*x.*pdf_val(:,ivar)))/(Variance*Variance);%kurtosis
45    cumpdf=cumsum(pdf_val(:,ivar));% sum of pdf
46    ind_centile=find(cumpdf>=0.01,1);% first index with cumsum >=0.01
47    VarVal(8,ivar)=x(ind_centile)+VarVal(2,ivar)/2;%
48    if ind_centile>1
49        VarVal(8,ivar)=(cumpdf(ind_centile)-0.01)*x(ind_centile-1)+(0.01-cumpdf(ind_centile-1))*x(ind_centile);
50        VarVal(8,ivar)=VarVal(8,ivar)/(cumpdf(ind_centile)-cumpdf(ind_centile-1))+VarVal(2,ivar)/2;%linear interpolation near ind_centile
51    end
52    ind_decile=find(cumpdf>=0.1,1);
53    if ind_decile>1
54        VarVal(9,ivar)=x(ind_decile)+VarVal(2,ivar)/2;%
55        VarVal(9,ivar)=(cumpdf(ind_decile)-0.1)*x(ind_decile-1)+(0.1-cumpdf(ind_decile-1))*x(ind_decile);
56        VarVal(9,ivar)=VarVal(9,ivar)/(cumpdf(ind_decile)-cumpdf(ind_decile-1))+VarVal(2,ivar)/2;%linear interpolation near ind_decile;
57    end
58    ind_median=find(cumpdf>= 0.5,1);
59    if ind_median<=1 % not enough data
60        return
61    end
62    VarVal(10,ivar)=(cumpdf(ind_median)-0.5)*x(ind_median-1)+(0.5-cumpdf(ind_median-1))*x(ind_median);
63    VarVal(10,ivar)=VarVal(10,ivar)/(cumpdf(ind_median)-cumpdf(ind_median-1))+VarVal(2,ivar)/2;%linear interpolation near ind_median;
64    %     VarVal(9)=x(ind_median);
65    ind_decile=find(cumpdf>=0.9,1);
66    VarVal(11,ivar)=(cumpdf(ind_decile)-0.9)*x(ind_decile-1)+(0.9-cumpdf(ind_decile-1))*x(ind_decile);
67    VarVal(11,ivar)=VarVal(11,ivar)/(cumpdf(ind_decile)-cumpdf(ind_decile-1))+VarVal(2,ivar)/2;%linear interpolation near ind_median;
68    ind_centile=find(cumpdf>=0.99,1);
69    VarVal(12,ivar)=(cumpdf(ind_centile)-0.99)*x(ind_centile-1)+(0.99-cumpdf(ind_centile-1))*x(ind_centile);
70    VarVal(12,ivar)=VarVal(12,ivar)/(cumpdf(ind_centile)-cumpdf(ind_centile-1))+VarVal(2,ivar)/2;%linear interpolation near ind_centile;
71    end
72end
Note: See TracBrowser for help on using the repository browser.