% cardio topograms
% 3D
clear all
clc
scrsz = get(0,'ScreenSize');

file2load='a1nn.dat';

f2l=load(file2load);
t=f2l(:,2);
t=t-t(1);
rr=f2l(:,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T=1200.0;        % sec time interval to display (10min)
Tshift=300.0;  % sec time shift (overlap) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
k1=1;
k2=find(t>=T,1);

Kmax=10000000;
% Kmax=1;
for k=1:Kmax

    t1=t(k1);
    t2=t(k2);

    t_disp=t(k1:k2);
    rr_disp=rr(k1:k2);

    y1=rr_disp(1:end-1);
    y2=rr_disp(2:end);

    figure
    [ctrs1, ctrs2, F, hAxes] = dscatter2(y1,y2,'PLOTTYPE','contour');
    close

    fig1=figure('Position',...
    [0.05*scrsz(3) 0.05*scrsz(4) 0.8*scrsz(3) 0.89*scrsz(4)],...
    'Color',[0 0 0]);
    
    colormap jet
       
    subplot(4,1,[1 2])
%     contour(ctrs1, ctrs2, F,'LineWidth',2,'LevelStep',0.00015)
%     meshc(ctrs1, ctrs2, F)
    surf(ctrs1, ctrs2, F, 'FaceColor','interp',...
'EdgeColor','none',...
'FaceLighting','phong')
    xlim([0.3 1.0])
    ylim([0.3 1.0])
    colormap jet
view(5,70)
axis off
%     mesh(ctrs1, ctrs2, F+1.0)
    subplot(4,1, 3)
    contour(ctrs1, ctrs2, F,'LineWidth',2,'LevelStep',0.0001)
 view(10,90)
% axis off
    colormap jet

    %     waterfall(ctrs1, ctrs2, F)
    xlim([0.3 1.0])
    ylim([0.3 1.0])
    zlim([0.0  0.00001])
%     grid on
axis off
%     axis square
%     axis normal
%     title([file2load '     ' num2str(min(t_disp)/3600, 3) ' - ' num2str(max(t_disp)/3600, 3) ' hours']);
   
    subplot(4,1,4)
    plot(t_disp,rr_disp,'r','LineWidth',1.8)
%     grid on
    xlim([min(t_disp) max(t_disp)])
%     xlim([(min(t_disp)-70) (max(t_disp)+120)])
    ylim([0.2 2.0])
 axis off
 
    % capture frame here 
    Fr1=getframe(fig1);
    [Im1, Im1_map]=frame2im(Fr1);

    im_file=['a1nn_3D_2\' int2str(k) '.bmp'];
    imwrite(Im1,im_file);

%     fr_file=['jet3\' 'fr_' int2str(k) '.dat'];
%     save(fr_file,'Fr1')  
    
    clear Fr1 Im1 Im1_map im_file fr_file;
    close(fig1);
    
    k1=find(t>=t1+Tshift,1);
    k2=find(t>=t2+Tshift,1);

    k
end

