|
Hi,
I'm trying to weight a signal using an exponential window; the signal is three-dimensional. The code I'm currently using is forming a major bottleneck in my script. So I've narrowed down the line responsible and the two methods I've tried so far to elimiate the bottleneck, shown here in a minimal working example (MWE) as the first line in the for loops (it's not entirely accurate to what I'm doing but is the same in essence):
tau = 480;
t = 160;
MyLength = 33;
MyWidth = 41;
E = zeros(t,MyLength,MyWidth);
E_dash = zeros(t+tau,MyLength,MyWidth);
%% Method 1
expwin = ((1-1/(tau)).^(1:tau))';
expwin = expwin(:,ones(MyLength,1));
E_prev = 0;
tic
for k = 1:t
E(k,:,:) = E_prev + (1/tau).*sum(bsxfun(@times,expwin,E_dash(k:k+tau-1,:,:)));
E_prev = E(k,:,:);
end
toc
%% Method 2
expwin = repmat(((1-1/(tau)).^(1:tau))',[1 MyLength MyWidth]); % exponential window function
E_prev = 0;
tic
for k = 1:160
E(k,:,:) = E_prev + (1/tau).*sum(expwin.*E_dash(k:k+tau-1,:,:));
E_prev = E(k,:,:);
end
toc
In my script, it takes about 140 secs to run (this is part of a bigger loop), but when I remove the line in question the time drops to 25 secs. I cannot vectorise it any further as it is simply too much data. Anyone any ideas on how I might optimise this?
Cheers,
Chris
|