Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirstat.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function varargout = mirstat(f,varargin) | |
2 % stat = mirstat(f) returns basic statistics of the feature f as a | |
3 % structure array stat such that: | |
4 % stat.Mean is the mean of the feature; | |
5 % stat.Std is the standard deviation; | |
6 % stat.Slope is the linear slope of the curve; | |
7 % stat.PeriodFreq is the frequency (in Hz) of the main periodicity; | |
8 % stat.PeriodAmp is the normalized amplitude of the main periodicity; | |
9 % stat.PeriodEntropy is the entropy of the periodicity curve. | |
10 % The main periodicity and periodicity curve are evaluated through the | |
11 % computation of the autocorrelation sequence related to f. | |
12 % | |
13 % f can be itself a structure array composed of features. In this case, | |
14 % stat will be structured the same way. | |
15 % | |
16 % mirstat does not work for multi-channels objects. | |
17 | |
18 if isa(f,'mirstruct') | |
19 varargout = {set(f,'Stat',1)}; | |
20 elseif isstruct(f) | |
21 if isdesign(f) | |
22 f.Stat = 1; | |
23 varargout = {f}; | |
24 else | |
25 fields = fieldnames(f); | |
26 for i = 1:length(fields) | |
27 field = fields{i}; | |
28 ff = f.(field); | |
29 if iscell(ff) && isa(ff{1},'mirdata') | |
30 ff = ff{1}; | |
31 end | |
32 if isa(ff,'mirdata') | |
33 if i == 1 | |
34 la = get(ff,'Label'); | |
35 if not(isempty(la) || isempty(la{1})) | |
36 stat.Class = la; | |
37 end | |
38 end | |
39 ff = set(ff,'Label',''); | |
40 end | |
41 stat.(field) = mirstat(ff,'FileNames',0); | |
42 end | |
43 if isempty(varargin) | |
44 f0 = f; | |
45 while isstruct(f0) | |
46 fields = fieldnames(f0); | |
47 f0 = f0.(fields{1}); | |
48 end | |
49 if iscell(f0) | |
50 f0 = f0{1}; | |
51 end | |
52 stat.FileNames = get(f0,'Name'); | |
53 end | |
54 varargout = {stat}; | |
55 end | |
56 elseif iscell(f) | |
57 if 0 %ischar(f{1}) | |
58 varargout = {f}; | |
59 else | |
60 res = zeros(length(f),1); | |
61 for i = 1:length(f) | |
62 res(i) = mirstat(f{i}); | |
63 end | |
64 varargout = {res}; | |
65 end | |
66 elseif isnumeric(f) | |
67 f(isnan(f)) = []; | |
68 varargout = {mean(f)}; | |
69 else | |
70 alongfiles.key = 'AlongFiles'; | |
71 alongfiles.type = 'Boolean'; | |
72 alongfiles.default = 0; | |
73 option.alongfiles = alongfiles; | |
74 | |
75 filenames.key = 'FileNames'; | |
76 filenames.type = 'Boolean'; | |
77 filenames.default = 1; | |
78 option.filenames = filenames; | |
79 | |
80 specif.option = option; | |
81 | |
82 specif.nochunk = 1; | |
83 varargout = mirfunction(@mirstat,f,varargin,nargout,specif,@init,@main); | |
84 end | |
85 | |
86 | |
87 function [x type] = init(x,option) | |
88 type = ''; | |
89 | |
90 | |
91 function stat = main(f,option,postoption) | |
92 if iscell(f) | |
93 f = f{1}; | |
94 end | |
95 if isa(f,'mirhisto') | |
96 warning('WARNING IN MIRSTAT: histograms are not taken into consideration yet.') | |
97 stat = struct; | |
98 return | |
99 end | |
100 fp = get(f,'FramePos'); | |
101 if haspeaks(f) | |
102 ppp = get(f,'PeakPrecisePos'); | |
103 if not(isempty(ppp)) && not(isempty(ppp{1})) | |
104 stat = addstat(struct,ppp,fp,'PeakPos'); | |
105 stat = addstat(stat,get(f,'PeakPreciseVal'),fp,'PeakMag'); | |
106 else | |
107 if isa(f,'mirkeystrength') || (isa(f,'mirchromagram') && get(f,'Wrap')) | |
108 stat = struct; % This needs to be defined using some kind of circular statistics.. | |
109 else | |
110 stat = addstat(struct,get(f,'PeakPosUnit'),fp,'PeakPos'); | |
111 end | |
112 stat = addstat(stat,get(f,'PeakVal'),fp,'PeakMag'); | |
113 end | |
114 else | |
115 stat = addstat(struct,get(f,'Data'),fp,'',option.alongfiles,... | |
116 get(f,'Name'),get(f,'Label')); | |
117 end | |
118 if option.filenames | |
119 stat.FileNames = get(f,'Name'); | |
120 end | |
121 | |
122 | |
123 function stat = addstat(stat,d,fp,field,alongfiles,filename,labels) | |
124 l = length(d); | |
125 if nargin<5 | |
126 alongfiles = 0; | |
127 end | |
128 if nargin<7 | |
129 labels = {}; | |
130 end | |
131 if not(alongfiles) || l == 1 | |
132 anyframe = 0; | |
133 for i = 1:l | |
134 if not(iscell(d{i})) | |
135 d{i} = {d{i}}; | |
136 end | |
137 for k = 1:length(d{i}) | |
138 dd = d{i}{k}; | |
139 if iscell(dd) | |
140 if length(dd)>1 | |
141 anyframe = 1; | |
142 end | |
143 dn = cell2array(dd); | |
144 [m{i}{k},s{i}{k},sl{i}{k},pa{i}{k},pf{i}{k},pe{i}{k}] ... | |
145 = computestat(dn,fp{i}{k}); | |
146 elseif size(dd,2) < 2 | |
147 nonan = find(not(isnan(dd))); | |
148 dn = dd(nonan); | |
149 if isempty(dn) | |
150 m{i}{k} = NaN; | |
151 else | |
152 m{i}{k} = mean(dn,2); | |
153 end | |
154 s{i}{k} = NaN; | |
155 sl{i}{k} = NaN; | |
156 pa{i}{k} = NaN; | |
157 pf{i}{k} = NaN; | |
158 pe{i}{k} = NaN; | |
159 else | |
160 anyframe = 1; | |
161 s1 = size(dd,1); | |
162 s3 = size(dd,3); | |
163 dd = mean(dd,4); %mean(dd,3),4); | |
164 m{i}{k} = NaN(s1,1,s3); | |
165 s{i}{k} = NaN(s1,1,s3); | |
166 sl{i}{k} = NaN(s1,1,s3); | |
167 pa{i}{k} = NaN(s1,1,s3); | |
168 pf{i}{k} = NaN(s1,1,s3); | |
169 pe{i}{k} = NaN(s1,1,s3); | |
170 for h = 1:s3 | |
171 for j = 1:s1 | |
172 [m{i}{k}(j,1,h),s{i}{k}(j,1,h),sl{i}{k}(j,1,h),... | |
173 pa{i}{k}(j,1,h),pf{i}{k}(j,1,h),pe{i}{k}(j,1,h)] = ... | |
174 computestat(dd(j,:,h),fp{i}{k}); | |
175 end | |
176 end | |
177 end | |
178 end | |
179 end | |
180 if anyframe | |
181 fields = {'Mean','Std','Slope','PeriodFreq','PeriodAmp','PeriodEntropy'}; | |
182 stats = {m,s,sl,pf,pa,pe}; | |
183 else | |
184 fields = {'Mean'}; | |
185 stats = {m}; | |
186 end | |
187 for i = 1:length(stats) | |
188 data = stats{i}; | |
189 data = uncell(data,NaN); | |
190 stat.(strcat(field,fields{i})) = data; | |
191 end | |
192 else | |
193 if iscell(d{1}{1}) | |
194 slash = strfind(filename{1},'/'); | |
195 nbfolders = 1; | |
196 infolder = 0; | |
197 foldername{1} = filename{1}(1:slash(end)-1); | |
198 for i = 1:length(d) | |
199 slash = strfind(filename{i},'/'); | |
200 if not(strcmpi(filename{i}(1:slash(end)-1),foldername)) | |
201 nbfolders = nbfolders + 1; | |
202 infolder = 0; | |
203 foldername{nbfolders} = filename{i}(1:slash(end)-1); | |
204 end | |
205 infolder = infolder+1; | |
206 dd{nbfolders}(infolder,:) = cell2array(d{i}{1}); | |
207 end | |
208 for i = 1:length(dd) | |
209 figure | |
210 plot(dd{i}') | |
211 stat.Mean{i} = mean(dd{i}); | |
212 stat.Std{i} = std(dd{i}); | |
213 end | |
214 end | |
215 end | |
216 if not(isempty(labels) || isempty(labels{1})) | |
217 stat.Class = labels; | |
218 end | |
219 | |
220 | |
221 function dn = cell2array(dd) | |
222 dn = zeros(1,length(dd)); | |
223 for j = 1:length(dd) | |
224 if isempty(dd{j}) | |
225 dn(j) = NaN; | |
226 else | |
227 dn(j) = dd{j}(1); | |
228 end | |
229 end | |
230 | |
231 | |
232 function [m,s,sl,pa,pf,pe] = computestat(d,fp) | |
233 m = NaN; | |
234 s = NaN; | |
235 sl = NaN; | |
236 pa = NaN; | |
237 pf = NaN; | |
238 pe = NaN; | |
239 diffp = fp(1,2:end) - fp(1,1:end-1); | |
240 if isempty(diffp) || sum(round((diffp(2:end)-diffp(1:end-1))*1000)) | |
241 % Not regular sampling (in mirattacktime for instance) | |
242 framesampling = NaN; | |
243 else | |
244 framesampling = fp(1,2)-fp(1,1); | |
245 end | |
246 nonan = find(not(isnan(d))); | |
247 if not(isempty(nonan)) | |
248 dn = d(nonan); | |
249 m = mean(dn,2); | |
250 s = std(dn,0,2); | |
251 if not(isnan(s)) | |
252 if s | |
253 dk = (dn-m)/s; | |
254 tk = linspace(0,1,size(d,2)); | |
255 sl = dk(:)'/tk(nonan); | |
256 elseif size(s,2) == 1 | |
257 s = NaN; | |
258 end | |
259 end | |
260 if length(dn)>1 | |
261 cor = xcorr(dn',dn','coeff'); | |
262 cor = cor(ceil(length(cor)/2):end); | |
263 % let's zero the first descending slope of the | |
264 % autocorrelation function | |
265 firstmin = find(cor(2:end)>cor(1:end-1)); | |
266 if not(isempty(firstmin) || isnan(framesampling)) | |
267 cor2 = cor; | |
268 cor2(1:firstmin(1)) = 0; | |
269 [pa,pfk] = max(cor2); | |
270 if pfk > 1 | |
271 pf = 1/(pfk-1)/framesampling; | |
272 end | |
273 end | |
274 cor = abs(cor); | |
275 cor = cor/sum(cor); | |
276 pe = -sum(cor.*log(cor+1e-12))./log(length(cor)); | |
277 end | |
278 end | |
279 | |
280 | |
281 function b = isdesign(f) | |
282 if iscell(f) | |
283 f = f{1}; | |
284 end | |
285 if isa(f,'mirdesign') || isa(f,'mirstruct') | |
286 b = 1; | |
287 elseif isa(f,'mirdata') || not(isstruct(f)) | |
288 b = 0; | |
289 else | |
290 fields = fieldnames(f); | |
291 b = isdesign(f.(fields{1})); | |
292 end |