子程序 function Coh = SCoh_W(y,x,alpha,nfft,Noverlap,Window,opt,P) if length(Window) == 1 Window = hanning(Window); end Window = Window(:); n = length(x); nwind = length(Window); % check inputs if (alpha>1)||(alpha<0),error('alpha must be in [0,1] !!'),end if nwind <= Noverlap,error('Window length must be > Noverlap');end if nfft < nwind,error('Window length must be <= nfft');end if (nargin>7) && (P>=1 || P<=0),error('P must be in ]0,1[ !!'),end y = y(:); x = x(:); t = (0:n-1)'; if strcmp(opt,'sym') == 1 y = y.*exp(-1i*pi*alpha*t); x = x.*exp(1i*pi*alpha*t); else x = x.*exp(2i*pi*alpha*t); end Syx = CPS_W(y,x,0,nfft,Noverlap,Window,opt); Sy = CPS_W(y,y,0,nfft,Noverlap,Window,opt); Sx = CPS_W(x,x,0,nfft,Noverlap,Window,opt); Coh.K = Sx.K; Coh.f = Sx.f; Coh.Syx = Syx.S; Coh.Sy = Sy.S; Coh.Sx = Sx.S; Coh.C = Syx.S./sqrt(Sy.S.*Sx.S); % variance reduction factor Window = Window(:)/norm(Window); Delta = nwind - Noverlap; R2w = xcorr(Window); k = nwind+Delta:Delta:min(2*nwind-1,nwind+Delta*(Coh.K-1)); if length(k) >1 Coh.Var_Reduc = R2w(nwind)^2/Coh.K + 2/Coh.K*(1-(1:length(k))/Coh.K)*(R2w(k).^2); else Coh.Var_Reduc = R2w(nwind)^2/Coh.K; end % threshold on square magnitude at P significance level under H0 if nargin > 7 Coh.thres = chi2inv(1-P,2)*Coh.Var_Reduc/2; end % set up output parameters if nargout == 0 figure,newplot; subplot(211),plot(Coh.f(1:nfft/2+1),abs(Coh.C(1:nfft/2+1)).^2), grid on if nargin > 7, hold on,plot(Coh.f(1:nfft/2+1),Coh.thres,':r'), title(['Spectral Coherence (squared magnitude) and threshold at ',num2str(100*P),'% level of significance']) else title('Spectral Coherence (squared magnitude)') end subplot(212),plot(Coh.f(1:nfft/2+1),angle(Coh.C(1:nfft/2+1))), grid on xlabel('[Hz]'),title('Phase') end |