# 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 2 (Two noise signals, again)
Make two noise signals, with multiple trials
= 1000;
N = 100;
K = 0.001;
dt= N*dt;
T = np.random.randn(K,N)
x = np.random.randn(K,N)
y = np.arange(0,N)*dt
t
0,:])
plt.plot(t,x[0,:])
plt.plot(t,y['Time [s]'); plt.xlabel(
Visualize the data across all trials
# ... and show the image,
plt.imshow(x, =[min(t), max(t), K, 1], # ... with meaningful axes,
extent='auto') # ... and a nice aspect ratio
aspect'Time [s]')
plt.xlabel('Trial #');
plt.ylabel('All trials from E1'); plt.title(
Compute the cross-covariance, averaged across trials
= "SOMETHING" # Compute cc for each trial,
cc_xy = np.mean(cc_xy,0) # ... average over trials,
cc_xy = np.arange(-N + 1, N) # ... create a lag axis,
lags * dt, cc_xy) # ... and plot the result.
plt.plot(lags 'Lag [s]')
plt.xlabel('Trial averaged cross-covariance');
plt.ylabel(-0.1, 1]); plt.ylim([
Compute the coherence
# Fourier transforms.
= "SOMETHING" # Compute Fourier transform of x for each trial
Xf = "SOMETHING" # Compute Fourier transform of y for each trial
Yf
# Auto- and cross-spectra.
= "SOMETHING" # Spectrum of x trials
Sxx = "SOMETHING" # ... and y trials
Syy = "SOMETHING" # ... and the cross spectrum
Sxy
# Trial average.
= np.mean(Sxx,0)
Sxx = np.mean(Syy,0)
Syy = np.mean(Sxy,0)
Sxy
# Calculate coherence.
= "SOMETHING"
cohr
= np.fft.fftfreq(N, dt) # Define a frequency axis.
f # Plot the coherence.
plt.plot(f, cohr.real) 0, 1.1]) # ... with y-axis scaled,
plt.ylim(['Frequency [Hz]') # ... and with axes labeled.
plt.xlabel('Coherence')
plt.ylabel('Trial averaged coherence between two electrodes'); plt.title(