function pcov=cov(nf1,nf2,nn,fs,x,mpv_S,sigma2,DD,D,mshape,beta,d,dof)
z=x(2);
mpv_fi=mshape;
nf=nf2-nf1+1;
for k=nf1:nf2
j=k-nf1+1;
fk(j)=(k-1)/nn*fs;
gama(j)=mpv_S*DD(j)/sigma2;%signal-to-noise factor
Dnf(j)=4*(beta(j)^3-beta(j)*(1-2*z^2))/fk(j);%eq(43)
Dnz(j)=8*z*beta(j)^2;%eq(44)
Dnff(j)=4*(3*beta(j)^2-1+2*z^2)/(fk(j)^2);%eq(45)
Dnzz(j)=8*beta(j)^2;%eq(46)
Dnfz(j)=16*z*beta(j)/fk(j);%eq(47)
Df(j)=-Dnf(j)*DD(j)^2;
Dz(j)=-Dnz(j)*DD(j)^2;
lnDf(j)=-4*DD(j)/fk(j)*(beta(j)^3-beta(j)*(1-2*z^2));%eq(48)
lnDz(j)=-8*z*beta(j)^2*DD(j);%eq(49)
lnDff(j)=4*fk(j)^(-2)*(4*DD(j)^2*(beta(j)^3-beta(j)*(1-2*z^2))^2-DD(j)*(3*beta(j)^2-1+2*z^2));%eq(50)
lnDzz(j)=8*beta(j)^2*DD(j)*(8*z^2*beta(j)^2*DD(j)-1);%eq(51)
lnDfz=16*fk(j)^(-1)*z*beta(j)*(beta(j)^4-1)*DD(j)^2;%eq(52)
Dff(j)=lnDff(j)*DD(j)+Df(j)^2/DD(j);
Dzz(j)=lnDzz(j)*DD(j)+Dz(j)^2/DD(j);
Dfz(j)=2*Dz(j)*Df(j)/DD(j)-Dnfz(j)*DD(j)^2;
B(j)=1/(1+1/gama(j));%eq(55)
Bnf(j)=-gama(j)^(-1)/DD(j)*Df(j);%eq(58)
Bnz(j)=-gama(j)^(-1)/DD(j)*Dz(j);
BnS(j)=-1/gama(j)*mpv_S^(-1);%eq(59)
Bnsigma(j)=1/gama(j)/sigma2;%eq(60)
Bnff(j)=1/gama(j)*DD(j)*Dnff(j);%eq(61)
Bnzz(j)=1/gama(j)*DD(j)*Dnzz(j);
BnSS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A*mpv_fi*mpv_fi'-mpv_fi'*A*mpv_fi*eye(dof))/sigma2;%eq73
Ldfz=sum(Ldfz(l),2);
Ldfs=sum(Ldfs(l),2)/mpv_S;
Ldzs=sum(Ldzs(l),2)/mpv_S;
Ldfsig=-sum(Ldfsig(l),2)/sigma2;
Ldzsig=-sum(Ldzsig(l),2)/sigma2;
Ldssig=-sum(Ldssig(l),2)/mpv_S/sigma2;%eq77
Lqfsig=sum(Bf(l).*q(l),2)/sigma2^2-sum(Bfsig(l).*q(l),2)/sigma2;
Lqzsig=sum(Bz(l).*q(l),2)/sigma2^2-sum(Bzsig(l).*q(l),2)/sigma2;
Lqssig=sum(Bs(l).*q(l),2)/sigma2^2-sum(Bssig(l).*q(l),2)/sigma2;
Lqffi=2*(sum(Bf(l).*q(l),2)*mpv_fi'-mpv_fi'*reshape(sum(Lqffi2(:,l),2),dof,dof))/sigma2;
Lqzfi=2*(sum(Bz(l).*q(l),2)*mpv_fi'-mpv_fi'*reshape(sum(Lqzfi2(:,l),2),dof,dof))/sigma2;
Lqsfi=2*(sum(Bs(l).*q(l),2)*mpv_fi'-mpv_fi'*reshape(sum(Lqsfi2(:,l),2),dof,dof))/sigma2;
Lqsigfi=2*(mpv_fi'*A-sum(B(l).*q(l),2)*mpv_fi')/sigma2^2+2/sigma2*(sum(Bsigma(l).*q(l),2)*mpv_fi'-mpv_fi'*reshape(sum(Lqsigfi2(:,l),2),dof,dof));
Lqfz=-1/sigma2*sum(Bfz(l).*q(l),2);
Lqfs=-1/sigma2*sum(Bfs(l).*q(l),2);
Lqzs=-1/sigma2*sum(Bzs(l).*q(l),2);
Lff=Ldff+Lqff;
Lfz=Ldfz+Lqfz;
Lfs=Ldfs+Lqfs;
Lfsig=Ldfsig+Lqfsig;
Lffi=Lqffi;
Lzz=Ldzz+Lqzz;
Lss=Ldss+Lqss;
Lsig2=Ldsig2+Lqsig2;
Lfi2=Lqfi2;
Lzsig=Ldzsig+Lqzsig;
Lzs=Ldzs+Lqzs;
Lzfi=Lqzfi;
Lssig=Ldssig+Lqssig;
Lsfi=Lqsfi;
Lsigfi=Lqsigfi;
H=[Lff Lfz Lfs Lfsig Lffi;Lfz Lzz Lzs Lzsig Lzfi;Lfs Lzs Lss Lssig Lsfi;Lfsig Lzsig Lssig Lsig2 Lsigfi;Lffi' Lzfi' Lsfi' Lsigfi' Lfi2];
[basis,kv]=eig(H);
c=0;
for j=1:5
c=c+1/kv(j,j)*basis(:,j)*basis(:,j)';
end
f_cov=sqrt(c(1,1))/x(1)*100;
z_cov=sqrt(c(2,2))/x(2)*100;
s_cov=sqrt(c(3,3))/mpv_S*100;
sig_cov=sqrt(c(4,4))/sigma2*100;
pcov=[f_cov;z_cov;s_cov;sig_cov]';