MATLAB: How to plot the coherence function

signal processing

For my two signals, x and y, I have calculated the auto- and cross-spectral density functions from the fft. So my spectral densities are Sx, Sy and Sxy.
I want to find the coherence-function, but when I just enter the formula for the coherence function directly i a get a problem with the matrix dimensions.
coh=abs(Sxy)/(Sx*Sy)
Iam sure there is a much more elegant way to do it? any suggestions? I want the unit of the frequency axis to be in rad/s.
Andre

Best Answer

If you have the Signal Processing Toolbox, use mscohere()
n = 0:599;
x = cos(pi/4*n)+randn(size(n));
y = 1/2*sin(pi/4*n)+randn(size(n));
[Cxy,W] = mscohere(x,y,160,120,160);
plot(W,Cxy)