
% This calculates uniformly spaced means and medians for a random data
% series with monotonic time vector. Calculation is done row-wise
% independently for each row 
% (this is just my own convention --- in my brains, the time goes always in "x"-axis)
% The routine is not fast, but handy for a non-uniformly spaced
% data (such as gaps). 

function [timevec,data_mean,data_median,data_std,data_N]=simplemeans(data,time,timewinlength,timestep)


timevec=time(1):timestep:time(end);
Ntimes=length(timevec);

Nrows=length(data(:,1));

data_median=nan(Nrows,Ntimes);
data_mean=nan(Nrows,Ntimes);
data_std=nan(Nrows,Ntimes);
data_N=zeros(1,Ntimes);

for tind=1:length(timevec)
 
    tind/length(timevec);
    
%     inds=(time>=timevec(tind) & time<timevec(tind)+timewinlength); 
% 
%   
% 
%     
%     data_median(:,tind)=median(data(inds),2);
%     data_mean(:,tind)=mean(data(inds),2);
%     data_std(:,tind)=std(data(inds),0,2);
%     
%     data_N=length(time(inds));
    
    
    ind0=find(time>=timevec(tind),1,'first');
    ind1=find(time<timevec(tind)+timewinlength,1,'last');
    
    data_median(:,tind)=median(data(:,ind0:ind1),2);
    data_mean(:,tind)=mean(data(:,ind0:ind1),2);
    data_std(:,tind)=std(data(:,ind0:ind1),0,2);
    
    data_N=length(time(ind0:ind1));
  
    
end







