-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4df55ef
commit b272b70
Showing
30 changed files
with
2,112 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"title: Coherence Part 1 (Two noise signals)\n", | ||
"project:\n", | ||
" type: website\n", | ||
"format:\n", | ||
" html:\n", | ||
" code-fold: false\n", | ||
" code-tools: true\n", | ||
"jupyter: python 3\n", | ||
"number-sections: false\n", | ||
"---" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Load modules we'll need.\n", | ||
"from scipy.io import loadmat\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import numpy as np\n", | ||
"from scipy.signal import spectrogram" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Make two noise signals" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"N = 1000;\n", | ||
"dt= 0.001;\n", | ||
"T = N*dt;\n", | ||
"x = np.random.randn(N)\n", | ||
"y = np.random.randn(N)\n", | ||
"t = np.arange(0,N)*dt\n", | ||
"\n", | ||
"plt.plot(t,x)\n", | ||
"plt.plot(t,y)\n", | ||
"plt.xlabel('Time [s]');" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Compute the cross-covariance" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cc_xy = \"SOMETHING\" # Compute the covariance.\n", | ||
"lags = np.arange(-N + 1, N) # Create a lag axis,\n", | ||
"plt.plot(lags * dt, cc_xy) # ... and plot the result.\n", | ||
"plt.ylim([-0.1, 1])\n", | ||
"plt.xlabel('Lag [s]')\n", | ||
"plt.ylabel('Cross-covariance');\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Compute the coherence" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"Xf = np.fft.fft(x - x.mean()) # Compute Fourier transform of x\n", | ||
"Yf = np.fft.fft(y - y.mean()) # Compute Fourier transform of y\n", | ||
"\n", | ||
"# Compute the spectra\n", | ||
"Sxx = ??? # Spectrum of E1 trials\n", | ||
"Syy = ??? # ... and E2 trials\n", | ||
"Sxy = ??? # ... and the cross spectrum\n", | ||
"\n", | ||
"# Compute the coherence.\n", | ||
"cohr = ???\n", | ||
"\n", | ||
"# Define a frequency axis.\n", | ||
"f = np.fft.fftfreq(N, dt)\n", | ||
"\n", | ||
"# Plot the result.\n", | ||
"plt.plot(f, cohr.real)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"title: Coherence Part 2 (Two noise signals, again)\n", | ||
"project:\n", | ||
" type: website\n", | ||
"format:\n", | ||
" html:\n", | ||
" code-fold: false\n", | ||
" code-tools: true\n", | ||
"jupyter: python 3\n", | ||
"number-sections: false\n", | ||
"---" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Load modules we'll need.\n", | ||
"\n", | ||
"from scipy.io import loadmat\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import numpy as np\n", | ||
"from scipy.signal import spectrogram" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Make two noise signals, with multiple trials" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"N = 1000;\n", | ||
"K = 100;\n", | ||
"dt= 0.001;\n", | ||
"T = N*dt;\n", | ||
"x = np.random.randn(K,N)\n", | ||
"y = np.random.randn(K,N)\n", | ||
"t = np.arange(0,N)*dt\n", | ||
"\n", | ||
"plt.plot(t,x[0,:])\n", | ||
"plt.plot(t,y[0,:])\n", | ||
"plt.xlabel('Time [s]');" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Visualize the data across all trials" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plt.imshow(x, # ... and show the image,\n", | ||
" extent=[min(t), max(t), K, 1], # ... with meaningful axes,\n", | ||
" aspect='auto') # ... and a nice aspect ratio\n", | ||
"plt.xlabel('Time [s]')\n", | ||
"plt.ylabel('Trial #');\n", | ||
"plt.title('All trials from E1');" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Compute the cross-covariance, averaged across trials" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cc_xy = \"SOMETHING\" # Compute cc for each trial, \n", | ||
"cc_xy = np.mean(cc_xy,0) # ... average over trials,\n", | ||
"lags = np.arange(-N + 1, N) # ... create a lag axis,\n", | ||
"plt.plot(lags * dt, cc_xy) # ... and plot the result.\n", | ||
"plt.xlabel('Lag [s]')\n", | ||
"plt.ylabel('Trial averaged cross-covariance');\n", | ||
"plt.ylim([-0.1, 1]);" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Compute the coherence" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Fourier transforms.\n", | ||
"Xf = \"SOMETHING\" # Compute Fourier transform of x for each trial\n", | ||
"Yf = \"SOMETHING\" # Compute Fourier transform of y for each trial\n", | ||
"\n", | ||
"# Auto- and cross-spectra.\n", | ||
"Sxx = \"SOMETHING\" # Spectrum of E1 trials\n", | ||
"Syy = \"SOMETHING\" # ... and E2 trials\n", | ||
"Sxy = \"SOMETHING\" # ... and the cross spectrum\n", | ||
"\n", | ||
"# Trial average.\n", | ||
"Sxx = np.mean(Sxx,0)\n", | ||
"Syy = np.mean(Syy,0)\n", | ||
"Sxy = np.mean(Sxy,0)\n", | ||
"\n", | ||
"# Calculate coherence.\n", | ||
"cohr_squared = \"SOMETHING\"\n", | ||
"\n", | ||
"f = np.fft.fftfreq(N, dt) # Define a frequency axis.\n", | ||
"plt.plot(f, cohr_squared.real) # Plot the coherence.\n", | ||
"plt.ylim([0, 1.1]) # ... with y-axis scaled,\n", | ||
"plt.xlabel('Frequency [Hz]') # ... and with axes labeled.\n", | ||
"plt.ylabel('Coherence')\n", | ||
"plt.title('Trial averaged coherence between two electrodes');" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.