MATLAB: compute mean of each 1-minute interval of a time-series

前端 未结 6 1280
小鲜肉
小鲜肉 2020-12-09 20:16

I have a bunch of times-series each described by two components, a timestamp vector (in seconds), and a vector of values measured. The time vector is non-uniform (i.e. sampl

6条回答
  •  渐次进展
    2020-12-09 20:55

    Here's a way that uses binary search. It is 6-10x faster for 9900 elements and about 64x times faster for 99900 elements. It was hard to get reliable times using only 900 elements so I'm not sure which is faster at that size. It uses almost no extra memory if you consider making tx directly from the generated data. Other than that it just has four extra float variables (prevind, first, mid, and last).

    % Sort the data so that we can use binary search (takes O(N logN) time complexity).
    tx = sortrows([t x]);
    
    prevind = 1;
    
    for i=1:N
        % First do a binary search to find the end of this section
        first = prevind;
        last = length(tx);
        while first ~= last
            mid = floor((first+last)/2);
            if tt(i+1) > tx(mid,1)
                first = mid+1;
            else
                last = mid;
            end;
        end;
        mu(i) = mean( tx(prevind:last-1,2) );
        sd(i) = std( tx(prevind:last-1,2) );
        prevind = last;
    end;
    

    It uses all of the variables that you had originally. I hope that it suits your needs. It is faster because it takes O(log N) to find the indices with binary search, but O(N) to find them the way you were doing it.

提交回复
热议问题