function [SWT,POWER,Pj]=waventropy_scales(Y,wtype,wetype,scaleBegin,scaleEnd,scalesCount,plotOn)
% wtype == 2,3

if (nargin<7), plotOn=0; end
if (nargin<6), scalesCount=10; end
if (nargin<5), scaleEnd=200; end
if (nargin<4), scaleBegin=1; end
if (nargin<3), return; end

[m,n]=size(Y);
if (m<n)
    Y=Y'; [m,n]=size(Y);
end
Y=Y(:,1); [m,n]=size(Y);
%SCALE=[scaleBegin:scaleEnd];
SCALE=[1:scaleEnd];

if (wtype==2)
    Yb=zeros(scaleEnd*4,1); Yb(:,:)=Y(1);
    Ye=zeros(scaleEnd*4,1); Ye(:,:)=Y(m);
    Y2=[Yb; Y; Ye];
    WAVE=cwt(Y2,SCALE,'gaus8');

    WAVE=WAVE(:,scaleEnd*4+1:scaleEnd*4+m);
    title_add2='CWT';    
else
    Yb=Y; Yb(:,:)=Y(1);
    Ye=Y; Ye(:,:)=Y(m);
    Y2=[Yb; Y; Ye];
    [WAVE,PERIOD,SCALE,COI]=wavelet_cs(Y2,SCALE,'DOG',8);
    
    WAVE=WAVE(:,m+1:m+m);
    title_add2='DWT_c_s';    
end

POWER=abs(WAVE).^2;
STD=std(Y);
POWER=POWER/STD;

% розрахунок по масштабам
iscaleBegin=scaleBegin;
iscaleEnd=scaleBegin+scalesCount-1;
Pj=[]; SWT=[];
Smax=log2(m);

if (wetype==1)
    Etot=sum(sum(POWER));
    Pij=POWER/Etot;
    
    while (iscaleEnd<=scaleEnd)
        tPj=sum(Pij(iscaleBegin:iscaleEnd,:));
        SWT=[SWT -sum(tPj.*log2(tPj)/Smax)];
        Pj=[Pj; tPj];
        iscaleBegin=iscaleBegin+1;
        iscaleEnd=iscaleEnd+1;
    end
    
    title_add1='Relative wavelet energy method on scales';
else
    [ms,ns]=size(SCALE);
    [mw,nw]=size(POWER);
    SM=sum(POWER');
    Pij=POWER;
    for i=1:ns
        Pij(i,:)=Pij(i,:)/SM(i);
    end

    while (iscaleEnd<=scaleEnd)
        tPj=sum(Pij(iscaleBegin:iscaleEnd,:));
        SWT=[SWT -sum(tPj.*log2(tPj)/Smax)];
        Pj=[Pj; tPj];
        iscaleBegin=iscaleBegin+1;
        iscaleEnd=iscaleEnd+1;
    end

    title_add1='Points wavelet energy method on scales';
end

if (plotOn==1)
    f=figure; mesh(POWER); title(['Power of C (' title_add2 ')']); kpfun(f);
    f=figure;
    subplot(2,1,1); mesh(Pj); title([title_add1 ' (' title_add2 ')']);
    subplot(2,1,2); plot(Y); title('Source TS');
    kpfun(f);
    f=figure; plot(SWT); title(['Entropy on scales (' title_add2 ')']); kpfun(f);
end
