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