Skip to content

Commit

Permalink
+ autocorrelation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Kramer committed Oct 28, 2024
1 parent 088b9c3 commit 531acd3
Show file tree
Hide file tree
Showing 19 changed files with 2,039 additions and 161 deletions.
10 changes: 5 additions & 5 deletions Analyzing_Rhythms_Lab_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"metadata": {},
"source": [
"---\n",
"title: Analyzing Rhtyhms Part 1\n",
"title: Analyzing Rhythms Part 1\n",
"project:\n",
" type: website\n",
"format:\n",
" html:\n",
" code-fold: true\n",
" code-fold: false\n",
" code-tools: true\n",
"jupyter: python 3\n",
"number-sections: false\n",
Expand Down Expand Up @@ -257,7 +257,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot the spectrum versus frequency on a decibel scale.\n",
"Plot the spectrum versus frequency on a **decibel scale**.\n",
"\n",
"**Q.** Now what do you see?\n"
]
Expand All @@ -280,7 +280,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot the spectrum versus frequency on a logarithmic frequency axis.\n",
"Plot the spectrum versus frequency on a **logarithmic frequency axis**.\n",
"\n",
"**Q.** And now what do you see?"
]
Expand Down Expand Up @@ -408,7 +408,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
"version": "3.8.18"
}
},
"nbformat": 4,
Expand Down
129 changes: 129 additions & 0 deletions Analyzing_Rhythms_Lab_2a.ipynb
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
}
208 changes: 208 additions & 0 deletions Analyzing_Rhythms_Lab_2b.ipynb
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 added Data/AC_Example.mat
Binary file not shown.
Binary file added Slides/Analyzing_Rhythms_Lecture_2.pdf
Binary file not shown.
Binary file added docs/.DS_Store
Binary file not shown.
Loading

0 comments on commit 531acd3

Please sign in to comment.