%function [] = wlegandr(data)
%WLEGANDR Summary of this function goes here
%  Detailed explanation goes here
win=256; step=15; sn=1; pointmin =257; pointmax=757;
data=data((pointmin-win):(pointmax),:);
[rw,cw]=size(data);
wdata=zeros(win,(rw-win));
for ii=1:step:rw-win;
    wdata(1:win,ii)=data(ii:ii+win-1,sn);
end
 jj=1; [rw,cw]=size(wdata);
for ii=1:cw
   if wdata(1,ii)~=0 
      wdata1(1:rw,jj)=wdata(1:rw,ii);
        jj=jj+1;
    end
end
wdata=wdata1; %wdata2=wdata3;
clear ii jj rw cw wdata1
[rw,cw]=size(wdata);
xxx=zeros(1,cw); xxy=zeros(50,cw); xxz=zeros(50,cw);
for ii=1:cw
    clear xy xz f_alpha1 alpha1
    x=wdata(:,ii);
N = length(x) ;
Nscale = max(2,round(log2(N/16))) ; 
[wt,scale] = contwtmir(x,2^(-Nscale),2^(-1),2*Nscale,8*i) ;
Q = linspace(-4,4,50) ;
[alpha,f_alpha] = contwtspec(wt,scale,Q,1,0);
xy=length(alpha); alpha1(1:(50-xy))=alpha(xy); alpha=[alpha alpha1]; 
xz=length(f_alpha); f_alpha1(1:(50-xz))=f_alpha(xz); f_alpha=[f_alpha f_alpha1];
xxy(1:length(alpha),ii)=alpha(:);
xxz(1:length(f_alpha),ii)=f_alpha(:);
xxx(1,ii)=ii;
end
xxx=repmat(xxx,50,1);

clear xy xz f_alpha1 alpha1 N Nscale Q f_alpha alpha scale
clear wdata x xy xz wt ii


[rw,cw]=size(data);
wdata=zeros(win,(rw-win));
wdata2=zeros(win,(rw-win));
for ii=1:step:rw-win;
    wdata(1:win,ii)=data(ii:ii+win-1,sn);
    wdata2(1:win,ii)=data(ii:ii+win-1,1);
end
 jj=1; [rw,cw]=size(wdata);
for ii=1:cw
   if wdata(1,ii)~=0
      wdata1(1:rw,jj)=wdata(1:rw,ii);
      wdata3(1:rw,jj)=wdata2(1:rw,ii);
      jj=jj+1;
    end
end

wdata=wdata1; wdata2=wdata3;
clear ii jj rw cw step wdata1 

wdata2=max(wdata2); wdata31=wdata2; [rw,cw]=size(wdata2);

wdata2=wdata2';
xxt=num2str(wdata2);

jj=1;
for ii=1:cw-1
 if strncmpi(xxt(ii,:),xxt(ii+1,:),4)==0 
  xxt1(jj,1:4)=xxt(ii+1,1:4);
  xxt2(jj)=ii+1;
  jj=jj+1;
end
end


figure(1)
subplot(2,1,1); 
contourf(xxx,xxy,xxz);colorbar;grid on
set(gca,'XTick',xxt2)
set(gca,'XTickLabel',xxt1)
xlabel('Time, years','FontSize',14,'FontName','Times New Roman');
ylabel('\alpha','FontSize',14,'FontName','Times New Roman');
zlabel('f(\alpha)','FontSize',14,'FontName','Times New Roman');
clear XMIN XMAX YMIN YMAX ZMIN ZMAX

wind=30; 
[rw,cw]=size(xxy);
for jj=1:cw-wind
    a=length(xxy)-wind
    n=jj;
    k=n+wind-1;
 for ii=n:k
    if xxy(1,ii)>0 & xxy(50,ii)>0
        S(ii-n+1)=xxy(1,ii)-xxy(50,ii);
    elseif xxy(1,ii)>0 & xxy(50,ii)<0
        S(ii-n+1)=xxy(1,ii)-abs(xxy(50,ii));
    elseif xxy(1,ii)<0 & xxy(50,ii)<0
        S(ii-n+1)=abs(xxy(1,ii))-abs(xxy(50,ii));
    end   
  end
SS(jj)=mean(S); 
end

data11=data(win+wind+1:length(data),sn);

x = 1:1:(length(data)-win-wind);


%jj=1
%f=1+wind;
%for ii=f:cw-1
% if strncmpi(xxt(ii,:),xxt(ii+1,:),4)==0 
%  xxt11(jj,1:4)=xxt(ii+1,1:4);
%  xxt22(jj)=ii+1-wind;
%  jj=jj+1;
%end
%end

subplot(2,1,2);

plot(SS);

grid on

%set(get((1),'Ylabel'),'String','Left Y-axis')
xlabel('Time, years','FontSize',14,'FontName','Times New Roman');
ylabel('\Delta \','FontSize',14,'FontName','Times New Roman');
%plot(data11,'b')
%hold on
