

function [q] = framenorm( x, y,FS )
fsx=FS;x=x';
fsy=FS;y=y';
FrameLength=32e-03;
overlap=50;
l=floor(FS*FrameLength/2); %pocet vzoriek v jednom ramci 
delay=finddelay(x,y);
if delay~=0;
    y=circshift(y,[1 -delay]);
end
x=x.*(1/max(x));
y=y.*(1/max(y));
%nx = (x-mean(x)) ./ sqrt(var(x));
%ny = (y-mean(y)) ./ sqrt(var(y));
%s=xcorr(x(:,1), y(:,1));
%r=xcorr(x(:,2), y(:,2));
%t=[s';r']
if (length(x)>length(y))
    y(1,length(x))=0;
else x(1,length(y))=0;
end
nx=x-mean(x);
ny=y-mean(y);
%Doplnenie vektorov o potrebny pocet 0 a vytvorenie matice
a=rem(length(nx),l);
a=(1-(a/l))*l;
a=length(x)+a;
nx(1,a)=0;
rx=reshape (nx,l,[]);rx=rx';
ny(1,a)=0;
ry=reshape (ny,l,[]);ry=ry';
% Overlap pre X,Y
b=round(l*(overlap/100));
nx=circshift(nx,[0 -b]);
ox=reshape (nx,l,[]);ox=ox';
ny=circshift(ny,[0 -b]);
oy=reshape (ny,l,[]);oy=oy';
x=horzcat(rx,ox);
y=horzcat(ry,oy);
w=hann(l*2);w=w';
xwt(length(x),l*2)=0;ywt=xwt;
for i=1:length(x)
    xw=w.*x(i,:); yw=w.*y(i,:);
    xwt(i,:)=abs(fft(xw));ywt(i,:)=abs(fft(yw));
end
%Grafy
%subplot(2,2,1);
%plot(w)
%subplot(2,2,2);
%plot(xw)
%subplot(2,2,3);
%plot(nx)
%subplot(2,2,4);
%plot(ny)
dim=size(xwt);
for i=1:dim(1);
    for j=1:(l*2);
        d=10*log10(xwt(i,j));
        if d<-40;
            xwt(i,j)=-40;
        else xwt(i,j)=d;  
        end 
    end
end
for i=1:dim(1);
    for j=1:(l*2);
        d=10*log10(ywt(i,j));
        if d<-40;
            ywt(i,j)=-40;
        else ywt(i,j)=d;  
        end 
    end
end

ul=64;
ll=2;
uh=128;
lh=65;

xlow=xwt(:,ll:ul);xlow=xlow';
ylow=ywt(:,ll:ul);ylow=ylow';
xhigh=xwt(:,lh:uh);xhigh=xhigh';
yhigh=ywt(:,lh:uh);yhigh=yhigh';
etl=1/(ul-ll+1)*sum(abs(ylow-xlow));
eth=1/(uh-lh+1)*sum(abs(yhigh-xhigh));
ecl=1/(ul-ll+1)*sum(ylow)-1/(ul-ll+1)*sum(xlow);
ech=1/(uh-lh+1)*sum(yhigh)-1/(uh-lh+1)*sum(xhigh);
 for i=1:dim(1);  
    for j=1:uh-lh;
        yl(j,i)=ylow(j,i)-ecl(i);
    end
    for j=1:uh-ul;
        yh(j,i)=yhigh(j,i)-ech(i);
    end
 end
efl=1/(ul-ll+1)*sum(abs(yl-xlow));
efh=1/(uh-lh+1)*sum(abs(yh-xhigh));
p1=mean(ecl);
p2=mean(ech);
p3=mean(efl);
q=(1+exp(-4.29+(p1*0.15)+(p2*0.0243)+(p3*0.449)))^-1;
fid = fopen('q.txt', 'a+');
fseek(fid,0,'eof');
fprintf(fid, '%f ',q);
fclose(fid);
end