Daniel@0: function varargout = mirsum(orig,varargin) Daniel@0: % s = mirsum(f) sums the envelope of the multi-channel object f. Daniel@0: % Daniel@0: % Optional arguments: Daniel@0: % mirsum(f,'Centered') centers the resulting envelope. Daniel@0: % mirsum(f,'Mean') divides the summation by the number of channels. Daniel@0: Daniel@0: % mirsum(f,'Weights')... Daniel@0: Daniel@0: c.key = 'Centered'; Daniel@0: c.type = 'Boolean'; Daniel@0: c.default = 0; Daniel@0: c.when = 'After'; Daniel@0: option.c = c; Daniel@0: Daniel@0: m.key = 'Mean'; Daniel@0: m.type = 'Boolean'; Daniel@0: m.default = 0; Daniel@0: m.when = 'After'; Daniel@0: option.m = m; Daniel@0: Daniel@0: adj.key = 'Adjacent'; Daniel@0: adj.type = 'Integer'; Daniel@0: adj.default = 1; Daniel@0: option.adj = adj; Daniel@0: Daniel@0: weights.key = 'Weights'; Daniel@0: weights.type = 'Integer'; Daniel@0: weights.default = []; Daniel@0: option.weights = weights; Daniel@0: Daniel@0: specif.option = option; Daniel@0: Daniel@0: if isamir(orig,'mirtemporal') Daniel@0: specif.eachchunk = @eachtemporalchunk; Daniel@0: specif.combinechunk = 'Concat'; %@combinetemporalchunk; Daniel@0: else Daniel@0: specif.combinechunk = @combinechunk; Daniel@0: end Daniel@0: varargout = mirfunction(@mirsum,orig,varargin,nargout,specif,@init,@main); Daniel@0: Daniel@0: Daniel@0: function [x type] = init(x,option) Daniel@0: type = mirtype(x); Daniel@0: Daniel@0: Daniel@0: function s = main(x,option,postoption) Daniel@0: x = purgedata(x); Daniel@0: if iscell(x) Daniel@0: x = x{1}; Daniel@0: end Daniel@0: d = get(x,'Data'); Daniel@0: pp = get(x,'PeakPos'); Daniel@0: pv = get(x,'PeakVal'); Daniel@0: pm = get(x,'PeakMode'); Daniel@0: p = get(x,'Pos'); Daniel@0: sc = cell(1,length(d)); Daniel@0: spp = cell(1,length(d)); Daniel@0: spv = cell(1,length(d)); Daniel@0: spm = cell(1,length(d)); Daniel@0: for h = 1:length(d) Daniel@0: dh = d{h}; Daniel@0: sch = cell(1,length(dh)); Daniel@0: spph = cell(1,length(dh)); Daniel@0: spvh = cell(1,length(dh)); Daniel@0: spmh = cell(1,length(dh)); Daniel@0: for i = 1:length(dh) Daniel@0: % Summation of signal Daniel@0: s3 = size(dh{i},3); Daniel@0: if not(isempty(option.weights)) Daniel@0: weights = reshape(option.weights,1,1,length(option.weights)); Daniel@0: if length(weights)~=s3 Daniel@0: %warning('WARNING in MIRSUM..'); Daniel@0: weights = weights(1,1,1:s3); Daniel@0: end Daniel@0: dh{i} = dh{i}.*repmat(weights,[size(dh{i},1),size(dh{i},2),1]); Daniel@0: end Daniel@0: if option.adj < 2 Daniel@0: sch{i} = sum(dh{i},3); Daniel@0: else Daniel@0: m0 = option.adj; Daniel@0: nc1 = size(dh{i},3); Daniel@0: nc2 = ceil(nc1/m0); Daniel@0: sch{i} = zeros(size(dh{i},1),size(dh{i},2),nc2); Daniel@0: for j = 1:nc2 Daniel@0: sch{i}(:,:,j) = sum(dh{i}(:,:,(j-1)*m0+1:min(j*m0,nc1))... Daniel@0: ,3); Daniel@0: end Daniel@0: end Daniel@0: % Summation of peaks Daniel@0: if not(isempty(pp)) && not(isempty(pp{1})) Daniel@0: ppi = pp{h}{i}; Daniel@0: pvi = pv{h}{i}; Daniel@0: pmi = pm{h}{i}; Daniel@0: nfr = size(ppi,2); Daniel@0: nbd = size(ppi,3); Daniel@0: sppi = cell(1,nfr,1); Daniel@0: spvi = cell(1,nfr,1); Daniel@0: spmi = cell(1,nfr,1); Daniel@0: for j = 1:nfr Daniel@0: sppj = []; Daniel@0: spvj = []; Daniel@0: spmj = []; Daniel@0: for k = 1:nbd Daniel@0: ppk = ppi{1,j,k}; Daniel@0: pvk = pvi{1,j,k}; Daniel@0: pmk = pmi{1,j,k}; Daniel@0: for l = 1:length(ppk) Daniel@0: fp = find(ppk(l) == sppj); Daniel@0: if fp Daniel@0: spvj(fp) = spvj(fp) + pvk(l); Daniel@0: else Daniel@0: sppj(end+1) = ppk(l); Daniel@0: spvj(end+1) = pvk(l); Daniel@0: spmj(end+1) = pmk(l); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: sppi{1,j,1} = sppj; Daniel@0: spvi{1,j,1} = spvj; Daniel@0: spmi{1,j,1} = spmj; Daniel@0: end Daniel@0: spph{i} = sppi; Daniel@0: spvh{i} = spvi; Daniel@0: spmh{i} = spmi; Daniel@0: else Daniel@0: spph{i} = []; Daniel@0: spvh{i} = []; Daniel@0: spmh{i} = []; Daniel@0: end Daniel@0: if not(isempty(p)) && not(isempty(p{h})) Daniel@0: p{h}{i} = p{h}{i}(:,:,1); Daniel@0: end Daniel@0: end Daniel@0: sc{h} = sch; Daniel@0: spp{h} = spph; Daniel@0: spv{h} = spvh; Daniel@0: spm{h} = spmh; Daniel@0: end Daniel@0: s = set(x,'Data',sc,'Pos',p,'PeakPos',spp,'PeakVal',spv,'PeakMode',spm); Daniel@0: if not(isempty(postoption)) Daniel@0: s = post(s,postoption); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: function s = post(s,option) Daniel@0: if option.c Daniel@0: d = get(s,'Data'); Daniel@0: for k = 1:length(d) Daniel@0: for i = 1:length(d{k}) Daniel@0: d{k}{i} = center(d{k}{i}); Daniel@0: end Daniel@0: end Daniel@0: s = set(s,'Data',d); Daniel@0: end Daniel@0: if option.m Daniel@0: d = get(s,'Data'); Daniel@0: ch = get(s,'Channels'); Daniel@0: if not(isempty(ch)) Daniel@0: for k = 1:length(d) Daniel@0: for i = 1:length(d{k}) Daniel@0: d{k}{i} = d{k}{i}/length(ch{k}); Daniel@0: end Daniel@0: ch{k} = [1]; Daniel@0: end Daniel@0: s = set(s,'Data',d,'Channels',ch); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: function [y orig] = eachtemporalchunk(orig,option,missing) Daniel@0: [y orig] = mirsum(orig,option); Daniel@0: Daniel@0: Daniel@0: %function y = combinetemporalchunk(old,new) Daniel@0: %do = get(old,'Data'); Daniel@0: %to = get(old,'Time'); Daniel@0: %dn = get(new,'Data'); Daniel@0: %tn = get(new,'Time'); Daniel@0: %y = set(old,'Data',{{[do{1}{1};dn{1}{1}]}},... Daniel@0: % 'Time',{{[to{1}{1};tn{1}{1}]}}); Daniel@0: % Daniel@0: Daniel@0: function y = combinechunk(old,new) Daniel@0: if isa(old,'mirspectrum') Daniel@0: warning('WARNING IN MIRSUM: not yet fully generalized to mirspectrum') Daniel@0: end Daniel@0: do = get(old,'Data'); Daniel@0: do = do{1}{1}; Daniel@0: dn = get(new,'Data'); Daniel@0: dn = dn{1}{1}; Daniel@0: if length(dn) < length(do) Daniel@0: dn(length(do),:,:) = 0; % Zero-padding Daniel@0: end Daniel@0: y = set(old,'ChunkData',do+dn);