% Pernille Tønnesen
% 2019 Oct 2019

clear

load('C:\MATLAB_pt\SW_AcousticScene\Data\sw17_196a_buzz_old.mat')
load('C:\MATLAB_pt\SW_AcousticScene\Data\sw17_196a_data.mat')
buzz = rmfield(buzz, {'win10med', 'win20med', 'win30med', 'win40med', 'win50med', 'win60med', 'win10total', 'win20total', 'win30total', 'win40total', 'win50total', 'win60total', 'handoff_near', 'handoff_near_60s_dist', 'handoff_near_60s_time', 'handoff_2med_60s_dist', 'handoff_2med_60s_time', 'handoff_2med_5s_dist', 'handoff_2med_5s_time', 'handoff_2med_10s_dist', 'handoff_2med_10s_time', 'handoff_2med_15s_dist', 'handoff_2med_15s_time', 'handoff_2med_20s_dist', 'handoff_2med_20s_time', 'handoff_2med_25s_dist', 'handoff_2med_25s_time', 'handoff_2med_30s_dist', 'handoff_2med_30s_time', 'handoff_allmed_60s_dist', 'handoff_allmed_60s_time', 'handoff_allmed_5s_dist', 'handoff_allmed_5s_time', 'handoff_allmed_10s_dist', 'handoff_allmed_10s_time', 'handoff_allmed_15s_dist', 'handoff_allmed_15s_time', 'handoff_allmed_20s_dist', 'handoff_allmed_20s_time', 'handoff_allmed_25s_dist', 'handoff_allmed_25s_time', 'handoff_allmed_30s_dist', 'handoff_allmed_30s_time', 'ccf_echoinbuzz', 'ccf_school', 'ccf_echonearestbuzz_60s', 'ccf_echorestinwin_60s', 'ccf_echonearestbuzz_5s', 'ccf_echorestinwin_5s', 'ccf_echonearestbuzz_10s', 'ccf_echorestinwin_10s', 'ccf_echonearestbuzz_15s', 'ccf_echorestinwin_15s', 'ccf_echonearestbuzz_20s', 'ccf_echorestinwin_20s', 'ccf_echonearestbuzz_25s', 'ccf_echorestinwin_25s', 'ccf_echonearestbuzz_30s', 'ccf_echorestinwin_30s', 'ccf_echorestindive_60s', 'ccf_echorestindive_5s', 'ccf_echorestindive_10s', 'ccf_echorestindive_15s', 'ccf_echorestindive_20s', 'ccf_echorestindive_25s', 'ccf_echorestindive_30s', 'handoff_near_5s_dist', 'handoff_near_5s_time', 'handoff_near_10s_dist', 'handoff_near_10s_time', 'handoff_near_15s_dist', 'handoff_near_15s_time', 'handoff_near_20s_dist', 'handoff_near_20s_time', 'handoff_near_25s_dist', 'handoff_near_25s_time', 'handoff_near_30s_dist', 'handoff_near_30s_time'});

% using the closest echo within 5:30 and 60 sec

win = 5:5:30;

% Nearest
for jwin = 1:length(win)
    k = 1;
    for jdive = 1:size(whale,2)
        if jdive ~= 16 & jdive ~= 17 & jdive ~= 21 & jdive ~= 23
            for jec = 1:length(whale(jdive).echo_all)
                eend(jec) = whale(jdive).echo_all{1,jec}(end,1);
            end
            
            for jbz = 1:length(whale(jdive).buzz_start)
                idx = find(eend > whale(jdive).buzz_start(jbz)-win(jwin) & eend < whale(jdive).buzz_start(jbz));
                if isempty(idx)
                    buzz(k).(['handoff_near_' num2str(win(jwin)) 's_dist'])= NaN;
                    buzz(k).(['handoff_near_' num2str(win(jwin)) 's_time'])= NaN;
                     k = k+1;
                elseif ~isempty(idx)
                    [v ie] = min(abs(eend(idx)-whale(jdive).buzz_start(jbz)));
                    ee = whale(jdive).echo_all{1,idx(ie)};
                    [ dist ] = echo_exterpolation( ee,whale(jdive).buzz_start(jbz) );
                    buzz(k).(['handoff_near_' num2str(win(jwin)) 's_dist'])= dist;
                    buzz(k).(['handoff_near_' num2str(win(jwin)) 's_time'])= whale(jdive).buzz_start(jbz)-eend(idx(ie));
                    k = k+1;
                end
                idx = [];
                clear v idxe dist
            end
        elseif jdive == 17 || jdive == 21 || jdive == 23
            buzz(k).(['handoff_near_' num2str(win(jwin)) 's_dist'])= NaN;
            buzz(k).(['handoff_near_' num2str(win(jwin)) 's_time'])= NaN;
            k = k+1;
        end
    end
end
        
pas = 'C:\MATLAB_pt\SW_AcousticScene\Data\';
save([pas 'sw17_196a_buzz.mat'],'buzz')
            
            
% median of 2
for jwin = 1:length(win)
    k = 1;
    for jdive = 1:size(whale,2)
        if jdive ~= 16 & jdive ~= 17 & jdive ~= 21 & jdive ~= 23
            for jec = 1:length(whale(jdive).echo_all)
                eend(jec) = whale(jdive).echo_all{1,jec}(end,1);
            end
            
            for jbz = 1:length(whale(jdive).buzz_start)
                idx = find(eend > whale(jdive).buzz_start(jbz)-win(jwin) & eend < whale(jdive).buzz_start(jbz));
                if isempty(idx)
                    buzz(k).(['handoff_2med_' num2str(win(jwin)) 's_dist'])= NaN;
                    buzz(k).(['handoff_2med_' num2str(win(jwin)) 's_time'])= NaN;
                     k = k+1;
                elseif ~isempty(idx)
                    if length(idx) == 1
                        [v ie] = min(abs(eend(idx)-whale(jdive).buzz_start(jbz)));
                        ee = whale(jdive).echo_all{1,idx(ie)};
                        [ dist ] = echo_exterpolation( ee,whale(jdive).buzz_start(jbz) );
                        buzz(k).(['handoff_2med_' num2str(win(jwin)) 's_dist'])= dist;
                        buzz(k).(['handoff_2med_' num2str(win(jwin)) 's_time'])= whale(jdive).buzz_start(jbz)-eend(idx(ie));
                        k = k+1;
                    elseif length(idx) > 1
                        [v ie1] = min(abs(eend(idx)-whale(jdive).buzz_start(jbz)));
                        ee1 = whale(jdive).echo_all{1,idx(ie1)};
                        [ dist1 ] = echo_exterpolation( ee1,whale(jdive).buzz_start(jbz) );
                        [ ie2 ] = find2echo(eend(idx),eend(idx(ie1)));
                        if ~isempty(ie2)
                            ee2 = whale(jdive).echo_all{1,idx(ie2)};
                            [ dist2 ] = echo_exterpolation( ee2,whale(jdive).buzz_start(jbz) );
                        elseif isempty(ie2)
                            dist2 = dist1;
                        end
                        buzz(k).(['handoff_2med_' num2str(win(jwin)) 's_dist'])= [dist1 dist2];
                        buzz(k).(['handoff_2med_' num2str(win(jwin)) 's_time'])= [whale(jdive).buzz_start(jbz)-eend(idx(ie1)) whale(jdive).buzz_start(jbz)-eend(idx(ie2))];
                        k = k+1;                        
                    end
                end
                idx = [];
                clear v idxe dist1 dist2 ie1 ee1 ie2 ee2
            end
        elseif jdive == 17 || jdive == 21 || jdive == 23
            buzz(k).(['handoff_2med_' num2str(win(jwin)) 's_dist'])= NaN;
            buzz(k).(['handoff_2med_' num2str(win(jwin)) 's_time'])= NaN;
            k = k+1;
        end
    end
end
        
pas = 'C:\MATLAB_pt\SW_AcousticScene\Data\';
save([pas 'sw17_196a_buzz.mat'],'buzz')            
            
            
% % median of all positive in window
% for jwin = 1:length(win)
%     k = 1;
%     for jdive = 1:size(whale,2)
%         if jdive ~= 16 & jdive ~= 17 & jdive ~= 21 & jdive ~= 23
%             for jec = 1:length(whale(jdive).echo_all)
%                 eend(jec) = whale(jdive).echo_all{1,jec}(end,1);
%             end
%             
%             for jbz = 1:length(whale(jdive).buzz_start)
%                 idx = find(eend > whale(jdive).buzz_start(jbz)-win(jwin) & eend < whale(jdive).buzz_start(jbz));
%                 if isempty(idx)
%                     buzz(k).(['handoff_allmed_' num2str(win(jwin)) 's_dist'])= NaN;
%                     buzz(k).(['handoff_allmed_' num2str(win(jwin)) 's_time'])= NaN;
%                     k = k+1;
%                 elseif ~isempty(idx)
%                     for j = 1:length(idx)
%                         ee = whale(jdive).echo_all{1,idx(j)};
%                         [ dist(j) ] = echo_exterpolation( ee,whale(jdive).buzz_start(jbz) );
%                         tim(j) = whale(jdive).buzz_start(jbz)-eend(idx(j));
%                         clear ee
%                     end
%                     id = find(dist<0);
%                     dist(id)=[];
%                     tim(id)=[];
%                     if isempty(dist)
%                         buzz(k).(['handoff_allmed_' num2str(win(jwin)) 's_dist'])= NaN;
%                         buzz(k).(['handoff_allmed_' num2str(win(jwin)) 's_time'])= NaN;
%                         k = k+1;
%                     elseif ~isempty(dist)    
%                         buzz(k).(['handoff_allmed_' num2str(win(jwin)) 's_dist'])= dist;
%                         buzz(k).(['handoff_allmed_' num2str(win(jwin)) 's_time'])= tim;
%                         k = k+1;
%                     end
%                     clear ee dist tim id
%                 end
%                 idx = [];
%                 
%             end
%         elseif jdive == 17 || jdive == 21 || jdive == 23
%             buzz(k).(['handoff_allmed_' num2str(win(jwin)) 's_dist'])= NaN;
%             buzz(k).(['handoff_allmed_' num2str(win(jwin)) 's_time'])= NaN;
%             k = k+1;
%         end
%     end
% end
% 
% pas = 'C:\MATLAB_pt\SW_AcousticScene\Data\';
% save([pas 'sw17_196a_buzz.mat'],'buzz')            
%                         
            
% Get overview of data
% near
xx = [buzz.handoff_near_25s_dist];

idx = find(xx > 0);
x = xx(idx);
x(isnan(x))=[];
median(x)
length(x)
median(x)*750

% average of 2
n2 = 0;
n1 = 0;
ff=[];
for j = 1:size(buzz,2)
    dist = buzz(j).handoff_2med_25s_dist;
    ff = find(dist>0);
    if length(ff)==2
        dd(j)= median(dist);
        n2 = n2+1;
    elseif length(ff)==1
        dd(j)= dist(ff);
        n1 = n1+1;
    elseif length(ff)==0
        dd(j)= NaN;
    end
end

dd(isnan(dd))=[];
median(dd)
n2
n1
median(dd)*750 

% average of all
dd=[];
for j = 1:size(buzz,2)
    dist = buzz(j).handoff_allmed_25s_dist;
    if isnan(dist)
        dd(j) = NaN;
    elseif ~isnan(dist)
        dd(j) = median(dist);
    end
end
dd(isnan(dd))=[];
median(dd)
length(dd)
median(dd)*750



% plot
near = [26.7 24.7 23.9 23.7 23.7 23.7 23.7];
med2 = [27.7 24.7 23.6 23.2 23.1 23.1 23.06];
medall = [27.7 24.7 22.6 20.8 20.1 18.5 17.1];
x = [5:5:30 60];

f = figure(57), clf
plot(x,near,'bo-'),hold on
plot(x,med2,'go-')
plot(x,medall,'ro-')
plot(xlim,[1 1]*23.4,'k--')
legend({'nearest','med 2','med all','suggested HOD'},'location','northeast')
ylabel('handoff distance (m)')
xlabel('tiem window (s)')
title('sw196a - hand off distance')

pas = 'C:\MATLAB_pt\SW_AcousticScene\Meetings\Oct 17\';
saveas(f,[pas 'hand_off_distance.fig'])






