% Для запуска программы надо написать wlegandr и в скобочках указать через
%запятую все перечисленные ниже параметры

%- название исходного массива
%- ширина окна для Лежандра
%- шаг перемещения окна
%- номер столбца, который анализируется в исходном массиве
%- ширина окна сглаживания дельта альфа
%- заголовок рисунка (указать в кавычках)
% например wlegandr(Developed,1024,1,2,5,'Developed') or wlegandr(DowJones(12833:15614,:),1024,10,2,10,'DJ')

function [] = wlegandr_Ser(data,win,step,nomer,win_s,textdata)
%function [] = wlegandr(data,point)
%WLEGANDR Summary of this function goes here
%  Detailed explanation goes here

sn=nomer;

[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); surf(xxx,xxy,xxz);
XMIN=min(min(xxx)); YMIN=min(min(xxy)); ZMIN=min(min(xxz));
XMAX=max(max(xxx)); YMAX=max(max(xxz)); ZMAX=max(max(xxz));
axis([XMIN XMAX YMIN YMAX ZMIN ZMAX]) 
%shading interp;
set(gca,'XTick',xxt2)
set(gca,'XTickLabel',xxt1)
title(textdata,'FontSize',24,'FontName','Times New Roman');
ylabel('\alpha','FontSize',14,'FontName','Times New Roman');
zlabel('f(\alpha)','FontSize',14,'FontName','Times New Roman');
subplot(2,1,2); 
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=win_s; 
[rw,cw]=size(xxy);
for jj=1:cw-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


for ii=1:length(SS)
    Ser(ii)=mean(SS);
end    
data11=data(win+wind+1:length(data),sn);

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


figure(2)
subplot(2,1,1); 
contourf(xxx,xxy,xxz);colorbar;grid on
set(gca,'XTick',xxt2)
set(gca,'XTickLabel',xxt1)
title(textdata,'FontSize',24,'FontName','Times New Roman');
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');


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);

[AX,H1,H2] = plotyy(x,SS,x,data11,'plot');

set(H1,'Color','b','LineWidth',2)
set(H2,'Color',[ 0.000 0.502 0.000 ],'LineWidth',2)
grid on

%set(get((1),'Ylabel'),'String','Left Y-axis')

set(AX(2),'XTickLabel',xxt11)
set(AX(2),'XTick',xxt22)
xlim([0 length(SS)])
set(AX(1),'XTick',[])
xlim([0 length(SS)])
set(get(AX(2),'Ylabel'),'String','Price')
set(get(AX(1),'Ylabel'),'String','\Delta S')
set(AX(1),'YTick',[-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1])
xlim([0 length(SS)])
xlabel('Time, years','FontSize',14,'FontName','Times New Roman');

%plot(data11,'b')
hold on
plot(Ser,'r') 