annotate mirex2012-matlab/cplcaMT.m @ 116:91bb029a847a timing

Reorder the calculations to match the series of vector operations in the most recent bqvec code, just in case it's the order of vector calculations that is saving the time rather than the avoidance of std::vector
author Chris Cannam
date Wed, 07 May 2014 09:57:19 +0100
parents 8017dd4a650d
children
rev   line source
Chris@2 1 function [w,h,z,u,xa] = cplcaMT( x, K, T, R, w, h, z, u, iter, sw, sh, sz, su, lw, lh, lz, lu, pa)
Chris@2 2 % function [w,h,xa2] = cplcaMT( x, K, T, R, w, h, z, u, iter, sw, sh, sz, su, lw, lh, lz, lu)
Chris@2 3 %
Chris@2 4 % Perform multiple-source, multiple-template SIPLCA for transcription
Chris@2 5 %
Chris@2 6 % Inputs:
Chris@2 7 % x input distribution
Chris@2 8 % K number of components
Chris@2 9 % T size of components
Chris@2 10 % R size of sources
Chris@2 11 % w initial value of p(w) [default = random]
Chris@2 12 % h initial value of p(h) [default = random]
Chris@2 13 % z initial value of p(z) [default = random]
Chris@2 14 % iter number of EM iterations [default = 10]
Chris@2 15 % sw sparsity parameter for w [default = 1]
Chris@2 16 % sh sparsity parameter for h [default = 1]
Chris@2 17 % sz sparsity parameter for z [default = 1]
Chris@2 18 % lw flag to update w [default = 1]
Chris@2 19 % lh flag to update h [default = 1]
Chris@2 20 % lh flag to update h [default = 1]
Chris@2 21 % pa source-component activity range [Rx2]
Chris@2 22 %
Chris@2 23 % Outputs:
Chris@2 24 % w p(w) - spectral bases
Chris@2 25 % h p(h) - pitch impulse
Chris@2 26 % z p(z) - mixing matrix for p(h)
Chris@2 27 % xa approximation of input
Chris@2 28
Chris@2 29 % Emmanouil Benetos 2011, based on cplca code by Paris Smaragdis
Chris@2 30
Chris@2 31
Chris@2 32 % Sort out the sizes
Chris@2 33 wc = 2*size(x)-T;
Chris@2 34 hc = size(x)+T-1;
Chris@2 35
Chris@2 36 % Default training iterations
Chris@2 37 if ~exist( 'iter')
Chris@2 38 iter = 10;
Chris@2 39 end
Chris@2 40
Chris@2 41
Chris@2 42 % Initialize
Chris@2 43 sumx = sum(x);
Chris@2 44
Chris@2 45 if ~exist( 'w') || isempty( w)
Chris@2 46 w = cell(R, K);
Chris@2 47 for k = 1:K
Chris@2 48 for r=1:R
Chris@2 49 w{r,k} = rand( T);
Chris@2 50 w{r,k} = w{r,k} / sum( w{r,k}(:));
Chris@2 51 end
Chris@2 52 end
Chris@2 53 end
Chris@2 54 if ~exist( 'h') || isempty( h)
Chris@2 55 h = cell(1, K);
Chris@2 56 for k = 1:K
Chris@2 57 h{k} = rand( size(x)-T+1);
Chris@2 58 h{k} = h{k};
Chris@2 59 end
Chris@2 60 end
Chris@2 61 if ~exist( 'z') || isempty( z)
Chris@2 62 z = cell(1, K);
Chris@2 63 for k = 1:K
Chris@2 64 z{k} = rand( size(x,2),1);
Chris@2 65 z{k} = z{k};
Chris@2 66 end
Chris@2 67 end
Chris@2 68 if ~exist( 'u') || isempty( u)
Chris@2 69 u = cell(R, K);
Chris@2 70 for k = 1:K
Chris@2 71 for r=1:R
Chris@2 72 if( (pa(r,1) <= k && k <= pa(r,2)) )
Chris@2 73 u{r,k} = ones( size(x,2),1);
Chris@2 74 else
Chris@2 75 u{r,k} = zeros( size(x,2),1);
Chris@2 76 end
Chris@2 77 end;
Chris@2 78 end
Chris@2 79 end
Chris@2 80
Chris@2 81 fh = cell(1, K);
Chris@2 82 fw = cell(R, K);
Chris@2 83 for k = 1:K
Chris@2 84 fh{k} = ones(wc) + 1i*ones(wc);
Chris@2 85 for r=1:R
Chris@2 86 fw{r,k} = ones(wc) + 1i*ones(wc);
Chris@2 87 end;
Chris@2 88 end;
Chris@2 89
Chris@2 90
Chris@2 91
Chris@2 92 % Make commands for subsequent multidim operations and initialize fw
Chris@2 93 fnh = 'c(hc(1)-(T(1)+(1:size(h{k},1))-2),hc(2)-(T(2)+(1:size(h{k},2))-2))';
Chris@2 94 xai = 'xa(1:size(x,1),1:size(x,2))';
Chris@2 95 flz = 'xbar(end:-1:1,end:-1:1)';
Chris@2 96
Chris@2 97 for k = 1:K
Chris@2 98 for r=1:R
Chris@2 99 if( (pa(r,1) <= k && k <= pa(r,2)) )
Chris@2 100 fw{r,k} = fftn( w{r,k}, wc);
Chris@2 101 end;
Chris@2 102 end;
Chris@2 103 end;
Chris@2 104
Chris@2 105 % Iterate
Chris@2 106 for it = 1:iter
Chris@2 107
Chris@2 108 %disp(['Iteration: ' num2str(it)]);
Chris@2 109
Chris@2 110 % E-step
Chris@2 111 xa = eps;
Chris@2 112 for k = 16:73
Chris@2 113 fh{k} = fftn( h{k}, wc);
Chris@2 114 for r=1:R
Chris@2 115 if( (pa(r,1) <= k && k <= pa(r,2)) )
Chris@2 116 xa1 = abs( real( ifftn( fw{r,k} .* fh{k})));
Chris@2 117 xa = xa + xa1(1:size(x,1),1:size(x,2)) .*repmat(z{k},1,size(x,1))'.*repmat(u{r,k},1,size(x,1))';
Chris@2 118 clear xa1;
Chris@2 119 end
Chris@2 120 end
Chris@2 121 end
Chris@2 122
Chris@2 123 xbar = x ./ xa;
Chris@2 124 xbar = eval( flz);
Chris@2 125 fx = fftn( xbar, wc);
Chris@2 126
Chris@2 127
Chris@2 128 % M-step
Chris@2 129 for k = 16:73
Chris@2 130
Chris@2 131
Chris@2 132 % Update h, z, u
Chris@2 133 nh=eps;
Chris@2 134 for r=1:R
Chris@2 135 if( (pa(r,1) <= k && k <= pa(r,2)) )
Chris@2 136 c = abs( real( ifftn( fx .* fw{r,k} )));
Chris@2 137 nh1 = eval( fnh);
Chris@2 138 nh1 = nh1 .*repmat(u{r,k},1,size(h{k},1))';
Chris@2 139 nh = nh + nh1;
Chris@2 140
Chris@2 141 nhu = eval( fnh);
Chris@2 142 nhu = nhu .* h{k};
Chris@2 143 nu = sum(nhu)';
Chris@2 144 nu = u{r,k} .* nu + eps;
Chris@2 145 if lu
Chris@2 146 u{r,k} = nu;
Chris@2 147 end;
Chris@2 148
Chris@2 149 end;
Chris@2 150 end
Chris@2 151 nh = h{k} .* (nh.^sh);
Chris@2 152 nz = sum(nh)';
Chris@2 153 nz = z{k} .* nz + eps;
Chris@2 154
Chris@2 155
Chris@2 156 % Assign and normalize
Chris@2 157 if lh
Chris@2 158 h{k} = nh;
Chris@2 159 end
Chris@2 160 if lz
Chris@2 161 z{k} = nz;
Chris@2 162 end
Chris@2 163
Chris@2 164
Chris@2 165 end
Chris@2 166
Chris@2 167 % Normalize z over t
Chris@2 168 if lz
Chris@2 169 Z=[]; for i=1:K Z=[Z z{i}]; end;
Chris@2 170 Z = Z.^sz;
Chris@2 171 Z(1:end,1:15)=0;
Chris@2 172 Z(1:end,74:88)=0;
Chris@2 173 Z = Z./repmat(sum(Z,2),1,K); z = num2cell(Z,1); %figure; imagesc(imrotate(Z,90));
Chris@2 174 end
Chris@2 175
Chris@2 176 % Normalize u over z,t
Chris@2 177 if lu
Chris@2 178 U=[]; for r=1:R U(r,:,:) = cell2mat(u(r,:)); end;
Chris@2 179 for i=1:size(U,2) for j=1:size(U,3) U(:,i,j) = U(:,i,j).^su; U(:,i,j) = U(:,i,j) ./ sum(U(:,i,j)); end; end;
Chris@2 180 U0 = permute(U,[2 1 3]); u = squeeze(num2cell(U0,1));
Chris@2 181 end
Chris@2 182
Chris@2 183 % Normalize h over z,t
Chris@2 184 H=[]; for k=1:K H(k,:,:) = cell2mat(h(k)); end; H0 = permute(H,[2 1 3]);
Chris@2 185 for i=1:size(H0,2) for j=1:size(H0,3) H0(:,i,j) = sumx(j)* (H0(:,i,j) ./ sum(H0(:,i,j))); end; end;
Chris@2 186 h = squeeze(num2cell(squeeze(H0),[1 3])); for k=1:K h{k} = squeeze(h{k}); end;
Chris@2 187
Chris@2 188 %figure; imagesc(imrotate(xa',90));
Chris@2 189
Chris@2 190 end
Chris@2 191
Chris@2 192 %figure; imagesc(imrotate(xa',90));