Path: news.mathworks.com!not-for-mail
From: "GG " <ggkmath@comcast.net>
Newsgroups: comp.soft-sys.matlab
Subject: Re: help vectorizing a FOR loop
Date: Fri, 5 Sep 2008 15:18:02 +0000 (UTC)
Organization: Jittertime Consulting
Lines: 36
Message-ID: <g9rija$ece$1@fred.mathworks.com>
References: <g9oskl$st1$1@fred.mathworks.com> <cPSvk.66035$hx.50905@pd7urf3no> <g9qa7d$s9f$1@fred.mathworks.com>
Reply-To: "GG " <ggkmath@comcast.net>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1220627882 14734 172.30.248.38 (5 Sep 2008 15:18:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 5 Sep 2008 15:18:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1432308
Xref: news.mathworks.com comp.soft-sys.matlab:488815



I have another loop maybe someone can help me with as well. I'm not too optimistic that it can be vectorized but I thought I'd post it as I wouldn't have expected the previous loop above to have been vectorized and I was wrong.

It is a variation of what's above... instead of a centered-sliding window average, this loop is a backward-looking sliding window average. The window size for averaging increases with frequency. The tricky thing is this data has a lot of noise (some noise shoots up quite high, which I call spurs). I identify the spurs (if they're above some threshold level) and don't use them when I calculate the mean_line as I step through the data. The data is in array 'd'.

dx=1:20000;
bin_width = round(0.2*sqrt(dx)) - 2;  % makes bin_width
                                      % increase w/freq
bin_width(1:2)=0;   % prevents negative bin_width
first_data = 1:1:length(bin_width); % initialize array
first_data = first_data - bin_width +1; % index of 1st 
                          % datapt in window for avg'g
mean_line(4) = 1e-5;      %give starting pt
    
for i = 5:1:10000  
    threshold(i) = 2 * mean_line(i-1);   
    if (d(i) > threshold(i))  % identifies spurs
        spurs = [spurs, i];                  
    end

% Next, calc the avg by summing all data pts in 
% window (including spurs), then subtract the spur 
% data, then divide this by the number of non-spur 
% data points (which were used to compute this avg).

running_spurs = spurs(find(spurs >= first_data(i))); 
% finds spurs in window for avg'g

mean_line(i) = (sum(d(first_data(i):i)) -  ...
  sum(d(running_spurs))) /(bin_width(i)- ...
  sum(running_spurs>=first_data(i)));  %% computes avg 
    
end

Thanks in advance, -GG