Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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. sampled at non-regular intervals)

I am trying to compute the mean/SD of each 1-minutes interval of values (take X minute interval, compute its mean, take the next interval, ...).

My current implementation uses loops. This is a sample of what I have so far:

t = (100:999)' + rand(900,1);       %' non-uniform time
x = 5*rand(900,1) + 10;             % x(i) is the value at time t(i)

interval = 1;         % 1-min interval
tt = ( floor(t(1)):interval*60:ceil(t(end)) )';  %' stopping points of each interval
N = length(tt)-1;

mu = zeros(N,1);
sd = zeros(N,1);

for i=1:N
    indices = ( tt(i) <= t & t < tt(i+1) ); % find t between tt(i) and tt(i+1)
    mu(i) = mean( x(indices) );
    sd(i) = std( x(indices) );
end

I am wondering if there a faster vectorized solution. This is important because I have a large number of time-series to process each much longer than the sample shown above..

Any help is welcome.


Thank you all for the feedback.

I corrected the way t is generated to be always monotonically increasing (sorted), this was not really an issue..

Also, I may not have stated this clearly but my intention was to have a solution for any interval length in minutes (1-min was just an example)