# Load modules we'll need.
from scipy.io import loadmat
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import spectrogram
Coherence Part 1 (Two noise signals)
Make two noise signals
= 1000;
N = 0.001;
dt= N*dt;
T = np.random.randn(N)
x = np.random.randn(N)
y = np.arange(0,N)*dt
t
plt.plot(t,x)
plt.plot(t,y)'Time [s]'); plt.xlabel(
Compute the cross-covariance
= "SOMETHING" # Compute the covariance.
cc_xy = np.arange(-N + 1, N) # Create a lag axis,
lags * dt, cc_xy) # ... and plot the result.
plt.plot(lags -0.1, 1])
plt.ylim(['Lag [s]')
plt.xlabel('Cross-covariance'); plt.ylabel(
Compute the coherence
= np.fft.fft(x - x.mean()) # Compute Fourier transform of x
Xf = np.fft.fft(y - y.mean()) # Compute Fourier transform of y
Yf
# Compute the spectra
= "SOMETHING" # Spectrum of x
Sxx = "SOMETHING" # ... and y
Syy = "SOMETHING" # ... and the cross spectrum
Sxy
# Compute the coherence.
= "SOMETHING"
cohr
# Define a frequency axis.
= np.fft.fftfreq(N, dt)
f
# Plot the result.
plt.plot(f, cohr.real)