-
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
088b9c3
commit 531acd3
Showing
19 changed files
with
2,039 additions
and
161 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,129 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"title: Analyzing Rhythms Part 2a (Autocovariance)\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": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Load modules we'll need." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"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 the noise signal." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"N = 1000;\n", | ||
"dt= 0.001;\n", | ||
"T = \"SOMETHING\"\n", | ||
"x = \"SOMETHING\"\n", | ||
"t = \"SOMETHING\"\n", | ||
"\n", | ||
"plt.plot(t,x)\n", | ||
"plt.xlabel('Time [s]');" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Compute the auto-covariance." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ac_xx = \"SOMETHING\"\n", | ||
"lags = \"SOMETHING\" # Create a lag axis,\n", | ||
"plt.plot(lags, ac_xx) # ... and plot the result.\n", | ||
"plt.xlabel('Lag [s]')\n", | ||
"plt.ylabel('Autocovariance');\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Compute the spectrum." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"Xf = np.fft.fft(x - x.mean()) # Compute Fourier transform of x\n", | ||
"\n", | ||
"# Compute the spectrum\n", | ||
"Sxx = \"SOMETHING\"\n", | ||
"\n", | ||
"# Define a frequency axis.\n", | ||
"f = np.fft.fftfreq(N, dt)\n", | ||
"\n", | ||
"# Plot the result.\n", | ||
"plt.plot(f, Sxx)" | ||
] | ||
} | ||
], | ||
"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.8.18" | ||
} | ||
}, | ||
"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,208 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"title: Analyzing Rhythms Part 2b (Autocovariance)\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": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Load the data.\n", | ||
"data = loadmat('AC_Example.mat') # Load the data,\n", | ||
"# Data available here\n", | ||
"# https://github.com/Mark-Kramer/BU-MA665-MA666/tree/master/Data\n", | ||
"#\n", | ||
"d = data['d'][0] # ... from the first electrode.\n", | ||
"t = data['t'][0] # Load the time axis\n", | ||
"N = np.size(d,0) # Store number of observations.\n", | ||
"dt = t[1]-t[0] # Store sampling interval." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Load the data and look at it.\n", | ||
"\n", | ||
"**Q.** Do you see rhythms?\n", | ||
"\n", | ||
"### Conclusions\n", | ||
"\n", | ||
"* \n", | ||
"* " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Code to compute the spectrum." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Compute the spectrum.\n", | ||
"\n", | ||
"**Q.** What do you find? What rhythms are present in the data?\n", | ||
"\n", | ||
"### Conclusions\n", | ||
"\n", | ||
"* \n", | ||
"* " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Code to compute the autocovariance." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Compute the autocovariance.\n", | ||
"\n", | ||
"**Q.** What do you find? Is it consistent with the spectrum?\n", | ||
"\n", | ||
"### Conclusions\n", | ||
"\n", | ||
"* \n", | ||
"* " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Example: Spectrum = FT{Autocovariance}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Make a simple signal." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dt = 0.001\n", | ||
"N = 1000\n", | ||
"t = np.arange(0,N)*dt\n", | ||
"d = np.sin(2*np.pi*10*t) + np.random.randn(N)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Compute the autocovariance with a modifications: circular-shift the data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"rxx = [];\n", | ||
"lags = np.arange(-int(N/2),int(N/2));\n", | ||
"for idx,L in enumerate(lags):\n", | ||
" rxx = np.append(rxx, 1/N*np.sum(np.roll(d,L) * d))\n", | ||
"plt.plot(lags, rxx)\n", | ||
"plt.xlabel('Lags [indices]');\n", | ||
"plt.ylabel('Autocovariance rxx');" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Compute the spectrum via FT and directly from the data." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Compute the spectrum from the FT{rxx}\n", | ||
"Sxx_via_rxx = 2*dt*np.fft.fft(rxx)\n", | ||
"\n", | ||
"# Compute the spectrum from the data.\n", | ||
"T = t[-1];\n", | ||
"xf = np.fft.fft(d);\n", | ||
"Sxx = 2 * dt ** 2 / T * (xf * xf.conj()) \n", | ||
"\n", | ||
"plt.plot(10*np.log10(Sxx))\n", | ||
"plt.plot(10*np.log10(Sxx_via_rxx), 'o')\n", | ||
"plt.xlabel('Freq Index')\n", | ||
"plt.legend({'Direct Sxx', 'Sxx via rxx'})\n", | ||
"plt.xlim([0,150]);" | ||
] | ||
} | ||
], | ||
"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.8.18" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.