# Why the spectral coherence is unity for all frequencies between single-frequency time series and itself

In the example below, I am plotting the coherence between time series and itself. The time series do has one frequency.The coherence magnitude was one for all frequencies. I wonder why it is not zero at all frequencies except at the time series frequency. I found that if I include random variable, I get zero coherence at most frequencies except the time series and its surrounding frequencies. Here is my questions:

1. Why the coherence is unit for all frequencies however the time series have only single frequency.
2. Why I got more realistic (coherence values at the time series frequency, and zero else where) values when I add noise to the time series.
3. Why the coherence (with the noise added case) show more unity for more that one frequency. It looks like that the coherence have unit at the time series frequency and the surrounding frequencies.
import matplotlib.pyplot as plt
from scipy import signal

nt=10000
w1=0.02

xp_pre=np.zeros((nt))
yp_pre=np.zeros((nt))

for t in np.arange(nt):
xp_pre[t]=np.cos(2*np.pi*w1*t)
yp_pre[t]=np.cos(2*np.pi*w1*t)

#xp_pre=xp_pre+np.random.randn(nt)*0.03
#yp_pre=yp_pre+np.random.randn(nt)*0.03

plt.figure();plt.plot(xp_pre);plt.plot(yp_pre);plt.show()

#f,coh = signal.coherence(xp_pre,yp_pre,window=signal.get_window('hanning',200),noverlap=100)
f,coh = signal.coherence(xp_pre,yp_pre)
plt.figure();plt.plot(f,coh,'*')
plt.show()


Thanks

According to the documentation, the coherence between two signals $$x(t)$$ and $$y(t)$$ is defined as $$C_{xy}(w) = \frac{|P_{xy}(w)|^2}{P_x(w) P_y(w)}$$, where $$P_x(w)$$ and $$P_y(w)$$ are power spectral density estimates of $$x$$ and $$y$$ and $$P_{xy}(w)$$ is an estimate of the cross-spectral density.

• In your first experiment, you put in two identical signals $$x(t) = y(t) = \cos(2\pi w_1 t)$$. Since $$x(t)=y(t)$$, what you should expect is that $$P_x (w)= P_y(w) = P_{xy}(w)$$ for all $$w$$. Therefore, $$C_{xy} = \frac{|P_{xy}(w)|^2}{P_x(w) P_y(w)}= 1$$ for all $$w$$.
• You might want to argue that the signal(s) only contains a single frequency. In this case we would expect that $$P_x(w) =P_y(w) = P_{xy}(w) =0$$ for $$w \neq w_1$$. However:
• This would mean that $$C_{xy}(w) = \frac{0}{0} =$$undefined for $$w\neq w_1$$.
• This wouldn't happen numerically, as due to rounding errors you'd have some epsilons left that could still make this fraction equal to one
• You're not dividing power densities but (Welch) estimates of the power densities of a finite window of the signal, hence $$P(w)$$ will not be exactly zero anywhere. It will contain estimation artifacts and the truncation effects. Since $$x(t)$$ and $$y(t)$$ are exactly equal, the artifacts are exactly equal and the coherence is one.
• When you add noise to $$x(t)$$ and $$y(t)$$ in your second experiment you're adding "signal content" to the frequencies outside $$w=w_1$$. Since the noise contributions are drawn independently, they are uncorrelated which means that $$P_{xy}(w)$$ will indeed be zero there, while $$P_x(w)$$ as well as $$P_y(w)$$ will not be. Hence the coherence does go to zero.
• Thanks a lot for your helpful answer. Any idea why after I add the noise, I get coherence for frequencies around the signal frequency as show in the second figure. – Ahmed Sep 10 '19 at 16:06
• @Ahmed: That's due to the fact that your power estimate is not perfect. You've only considered a finite window, so the power does leak from $w_1$ to its neighborhood as a result of the effect of the window function. Let's also not forget that the function uses the Welch method to estimate the spectrum, which divides the signal into even smaller windows. This could pronounce the windowing effect further. – Florian Sep 10 '19 at 16:10
• Thanks, any ideas how to get sharper coherence? do you think that trying different windows might be helpful. – Ahmed Sep 10 '19 at 16:19
• @Ahmed: Sure, you can control the way the function does the Welch estimate. In particular, the parameters nperseg and noverlap can be used to control the window size. Try increasing nperseg a bit, that should give wider windows and thus sharper peaks. As for the windowing functions: the rectangular window gives the sharpest peaks possible (at the price of higher sidelobes). – Florian Sep 11 '19 at 8:18
• Thanks for the spectrum coherence recipes (wider + rectangular window). – Ahmed Sep 11 '19 at 17:21