Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirsum.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 = mirsum(orig,varargin) | |
2 % s = mirsum(f) sums the envelope of the multi-channel object f. | |
3 % | |
4 % Optional arguments: | |
5 % mirsum(f,'Centered') centers the resulting envelope. | |
6 % mirsum(f,'Mean') divides the summation by the number of channels. | |
7 | |
8 % mirsum(f,'Weights')... | |
9 | |
10 c.key = 'Centered'; | |
11 c.type = 'Boolean'; | |
12 c.default = 0; | |
13 c.when = 'After'; | |
14 option.c = c; | |
15 | |
16 m.key = 'Mean'; | |
17 m.type = 'Boolean'; | |
18 m.default = 0; | |
19 m.when = 'After'; | |
20 option.m = m; | |
21 | |
22 adj.key = 'Adjacent'; | |
23 adj.type = 'Integer'; | |
24 adj.default = 1; | |
25 option.adj = adj; | |
26 | |
27 weights.key = 'Weights'; | |
28 weights.type = 'Integer'; | |
29 weights.default = []; | |
30 option.weights = weights; | |
31 | |
32 specif.option = option; | |
33 | |
34 if isamir(orig,'mirtemporal') | |
35 specif.eachchunk = @eachtemporalchunk; | |
36 specif.combinechunk = 'Concat'; %@combinetemporalchunk; | |
37 else | |
38 specif.combinechunk = @combinechunk; | |
39 end | |
40 varargout = mirfunction(@mirsum,orig,varargin,nargout,specif,@init,@main); | |
41 | |
42 | |
43 function [x type] = init(x,option) | |
44 type = mirtype(x); | |
45 | |
46 | |
47 function s = main(x,option,postoption) | |
48 x = purgedata(x); | |
49 if iscell(x) | |
50 x = x{1}; | |
51 end | |
52 d = get(x,'Data'); | |
53 pp = get(x,'PeakPos'); | |
54 pv = get(x,'PeakVal'); | |
55 pm = get(x,'PeakMode'); | |
56 p = get(x,'Pos'); | |
57 sc = cell(1,length(d)); | |
58 spp = cell(1,length(d)); | |
59 spv = cell(1,length(d)); | |
60 spm = cell(1,length(d)); | |
61 for h = 1:length(d) | |
62 dh = d{h}; | |
63 sch = cell(1,length(dh)); | |
64 spph = cell(1,length(dh)); | |
65 spvh = cell(1,length(dh)); | |
66 spmh = cell(1,length(dh)); | |
67 for i = 1:length(dh) | |
68 % Summation of signal | |
69 s3 = size(dh{i},3); | |
70 if not(isempty(option.weights)) | |
71 weights = reshape(option.weights,1,1,length(option.weights)); | |
72 if length(weights)~=s3 | |
73 %warning('WARNING in MIRSUM..'); | |
74 weights = weights(1,1,1:s3); | |
75 end | |
76 dh{i} = dh{i}.*repmat(weights,[size(dh{i},1),size(dh{i},2),1]); | |
77 end | |
78 if option.adj < 2 | |
79 sch{i} = sum(dh{i},3); | |
80 else | |
81 m0 = option.adj; | |
82 nc1 = size(dh{i},3); | |
83 nc2 = ceil(nc1/m0); | |
84 sch{i} = zeros(size(dh{i},1),size(dh{i},2),nc2); | |
85 for j = 1:nc2 | |
86 sch{i}(:,:,j) = sum(dh{i}(:,:,(j-1)*m0+1:min(j*m0,nc1))... | |
87 ,3); | |
88 end | |
89 end | |
90 % Summation of peaks | |
91 if not(isempty(pp)) && not(isempty(pp{1})) | |
92 ppi = pp{h}{i}; | |
93 pvi = pv{h}{i}; | |
94 pmi = pm{h}{i}; | |
95 nfr = size(ppi,2); | |
96 nbd = size(ppi,3); | |
97 sppi = cell(1,nfr,1); | |
98 spvi = cell(1,nfr,1); | |
99 spmi = cell(1,nfr,1); | |
100 for j = 1:nfr | |
101 sppj = []; | |
102 spvj = []; | |
103 spmj = []; | |
104 for k = 1:nbd | |
105 ppk = ppi{1,j,k}; | |
106 pvk = pvi{1,j,k}; | |
107 pmk = pmi{1,j,k}; | |
108 for l = 1:length(ppk) | |
109 fp = find(ppk(l) == sppj); | |
110 if fp | |
111 spvj(fp) = spvj(fp) + pvk(l); | |
112 else | |
113 sppj(end+1) = ppk(l); | |
114 spvj(end+1) = pvk(l); | |
115 spmj(end+1) = pmk(l); | |
116 end | |
117 end | |
118 end | |
119 sppi{1,j,1} = sppj; | |
120 spvi{1,j,1} = spvj; | |
121 spmi{1,j,1} = spmj; | |
122 end | |
123 spph{i} = sppi; | |
124 spvh{i} = spvi; | |
125 spmh{i} = spmi; | |
126 else | |
127 spph{i} = []; | |
128 spvh{i} = []; | |
129 spmh{i} = []; | |
130 end | |
131 if not(isempty(p)) && not(isempty(p{h})) | |
132 p{h}{i} = p{h}{i}(:,:,1); | |
133 end | |
134 end | |
135 sc{h} = sch; | |
136 spp{h} = spph; | |
137 spv{h} = spvh; | |
138 spm{h} = spmh; | |
139 end | |
140 s = set(x,'Data',sc,'Pos',p,'PeakPos',spp,'PeakVal',spv,'PeakMode',spm); | |
141 if not(isempty(postoption)) | |
142 s = post(s,postoption); | |
143 end | |
144 | |
145 | |
146 function s = post(s,option) | |
147 if option.c | |
148 d = get(s,'Data'); | |
149 for k = 1:length(d) | |
150 for i = 1:length(d{k}) | |
151 d{k}{i} = center(d{k}{i}); | |
152 end | |
153 end | |
154 s = set(s,'Data',d); | |
155 end | |
156 if option.m | |
157 d = get(s,'Data'); | |
158 ch = get(s,'Channels'); | |
159 if not(isempty(ch)) | |
160 for k = 1:length(d) | |
161 for i = 1:length(d{k}) | |
162 d{k}{i} = d{k}{i}/length(ch{k}); | |
163 end | |
164 ch{k} = [1]; | |
165 end | |
166 s = set(s,'Data',d,'Channels',ch); | |
167 end | |
168 end | |
169 | |
170 | |
171 function [y orig] = eachtemporalchunk(orig,option,missing) | |
172 [y orig] = mirsum(orig,option); | |
173 | |
174 | |
175 %function y = combinetemporalchunk(old,new) | |
176 %do = get(old,'Data'); | |
177 %to = get(old,'Time'); | |
178 %dn = get(new,'Data'); | |
179 %tn = get(new,'Time'); | |
180 %y = set(old,'Data',{{[do{1}{1};dn{1}{1}]}},... | |
181 % 'Time',{{[to{1}{1};tn{1}{1}]}}); | |
182 % | |
183 | |
184 function y = combinechunk(old,new) | |
185 if isa(old,'mirspectrum') | |
186 warning('WARNING IN MIRSUM: not yet fully generalized to mirspectrum') | |
187 end | |
188 do = get(old,'Data'); | |
189 do = do{1}{1}; | |
190 dn = get(new,'Data'); | |
191 dn = dn{1}{1}; | |
192 if length(dn) < length(do) | |
193 dn(length(do),:,:) = 0; % Zero-padding | |
194 end | |
195 y = set(old,'ChunkData',do+dn); |