%%%JORGENSEN et 2015 ADDITIONAL FILES - Salvador Jorgensen (first coded January 2011)
%%%DETERMINE TIME WINDOW OVER WHICH MEAN POSITION CAN BE ASSUMED 'PRONE'
%%%Use the depth infoto figure out over what time window we can find an
%%%average zero change in depth, and variance is minimized

%% LOAD DATA
%%% USER EDIT Load depth data vector in centimeters and note time step 
depth_cm = load('ENTER PATHWAY HERE');

%%% Determine change in depth at each time step
delta_depth = diff(depth_cm); %%%unsmoothed change in depth

%% Define sampling rate and window range



%%% USER DEFINE range of bins to run coorresponding to data sampling
%%% interval

%%%   Example for 5 Hz data:  
%%%   one minute = 5 * 60 = 300 measurements
%%%   30 measurements = 6 seconds
%%%   10000 measurements = 33.3 minutes

%%% USER INPUT
strt= 30;   %% starting bin size
stp = 30;   %% step size
mx = 10000; %% 

%%% define windos in data
mx_tm  = (stp * floor((mx-strt)/stp)) +strt; % largest bin size value in for-loop range given above
min_wins = floor((length(delta_depth)-mx_tm)/mx_tm); % minimum number of windows given larges bin size (mx_tm)


    for tm = strt:stp:mx %%% step through increasing window size by 6 sec steps 

        wins =  floor((length(delta_depth)-tm)/tm); %%% number of windows
        aa = reshape(delta_depth(1:tm*wins), tm, wins); %% make delta matrix
        mu = mean(aa);
        mu_mu (tm)  =  mean(mu(1:min_wins)); %%%mean mean change in depth among windows
        std_mu (tm) =  std(mu(1:min_wins));  %% standard deviation among window means                             

    end

    
%%  Plot results for visual assement

%%% Subsample for clear plotting
std_mu1 = std_mu(30:30:end);
mu_mu1 = mu_mu(30:30:end);
 
close all

 %%% Plot mean change in depth
 subplot(2,1,1); plot((6:6:length(std_mu1)*6)/60,mu_mu1,'o')
 xlim([0 15])
 ylabel ('Mean Change in Depth (cm)','fontsize', 16)
 ylim( [-.025 .025])
 set (gca, 'fontsize', 16)
  
 %%% Plot standard deviation
 subplot(2,1,2); plot((6:6:length(std_mu1)*6)/60,std_mu1,'o')
 xlim([0 15])
 ylabel ('Standard Deviation','fontsize', 16)
 xlabel ('Observation window (min)','fontsize', 16)
 set (gca, 'fontsize', 16)








