Skip to content

Commit

Permalink
+ rhythms
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Kramer committed Oct 21, 2024
1 parent a62b83f commit 2a13bc5
Show file tree
Hide file tree
Showing 16 changed files with 2,007 additions and 145 deletions.
2 changes: 1 addition & 1 deletion Regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
"version": "3.8.18"
}
},
"nbformat": 4,
Expand Down
87 changes: 51 additions & 36 deletions Rhythms_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
"cells": [
{
"cell_type": "markdown",
"id": "2f3b69be-50b4-41db-a74d-1e2c2983436a",
"id": "71df1f5c-097b-4d06-9c56-4d05110772ac",
"metadata": {},
"source": [
"---\n",
"title: Rhythms 1\n",
"title: Rhythms\n",
"project:\n",
" type: website\n",
"format:\n",
" html:\n",
" code-fold: true\n",
" code-tools: true\n",
"jupyter: python 3\n",
"number-sections: true\n",
"number-sections: false\n",
"filters:\n",
" - pyodide\n",
"---"
]
},
Expand All @@ -27,16 +29,17 @@
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "90b0e4f7-34de-4e79-bca3-35cf6ddd41e2",
"cell_type": "markdown",
"id": "28b90ecd-3fa8-47f6-9a6c-137592ddd80f",
"metadata": {},
"outputs": [],
"source": [
"```{pyodide-python}\n",
"# Load modules\n",
"import scipy.io as sio\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"```"
]
},
{
Expand All @@ -56,13 +59,13 @@
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c4ce8627-f4a3-43a1-804f-6ff06100234e",
"cell_type": "markdown",
"id": "79b206fe-b22c-4a69-ba3e-feebe45cfd72",
"metadata": {},
"outputs": [],
"source": [
"t = np.arange(0, 2, 0.001)"
"```{pyodide-python}\n",
"t = np.arange(0, 2, 0.001)\n",
"```"
]
},
{
Expand All @@ -84,13 +87,13 @@
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4f0b6d21-41a7-4321-9850-47279dbf714b",
"cell_type": "markdown",
"id": "665c6619-b67d-4be6-9af5-2cca406cd09e",
"metadata": {},
"outputs": [],
"source": [
"x = np.sin(2*np.pi*t)"
"```{pyodide-python}\n",
"x = np.sin(2*np.pi*t)\n",
"```"
]
},
{
Expand All @@ -99,7 +102,7 @@
"metadata": {},
"source": [
"::: {.callout}\n",
"**Q:** Is \"x\" a sinusoid? What is the frequency of the oscillation?\n",
"**Q:** Is `x` a sinusoid? What is the frequency of the oscillation?\n",
":::"
]
},
Expand All @@ -109,7 +112,7 @@
"metadata": {},
"source": [
"::: {.callout}\n",
"**Q:** How would you update \"x\" to create a sine function that oscillates at 10 Hz?\n",
"**Q:** How would you update `x` to create a sine function that oscillates at 10 Hz?\n",
":::"
]
},
Expand Down Expand Up @@ -144,17 +147,17 @@
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "78327266-8bcf-4120-aa9e-87fde9065025",
"cell_type": "markdown",
"id": "a85fda84-c332-4bad-bb7f-c2599b01bdc6",
"metadata": {},
"outputs": [],
"source": [
"```{pyodide-python}\n",
"f1 = 10;\n",
"x1 = np.cos(2*np.pi*t*f1);\n",
"\n",
"f2 = 2;\n",
"x2 = np.cos(2*np.pi*t*f2);"
"x2 = np.cos(2*np.pi*t*f2);\n",
"```"
]
},
{
Expand Down Expand Up @@ -195,6 +198,16 @@
":::"
]
},
{
"cell_type": "markdown",
"id": "6a90bf16-6497-4919-bc08-cdbb18bdd178",
"metadata": {},
"source": [
"::: {.callout}\n",
"**Q:** How does this result change if you use sine?\n",
":::"
]
},
{
"cell_type": "markdown",
"id": "22041f5b-7b0e-46c4-9fbb-89ab11a1740f",
Expand All @@ -208,19 +221,21 @@
"id": "c1c144f9-507e-4862-9c53-d8d99b6c9fff",
"metadata": {},
"source": [
"We'll now consider a more complicated signal. Please download the data [Rhythms_1.mat](/Data/Rhythms_1.mat) and load the data,"
"We'll now consider a more complicated signal. Please download the data [Rhythms_1.csv](/Data/Rhythms_1.csv) and load the data,"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "45d9ba16-cb28-4969-972c-28e817300ff2",
"cell_type": "markdown",
"id": "d380a618-5d8b-4c65-837e-e03e1018d4e5",
"metadata": {},
"outputs": [],
"source": [
"data = sio.loadmat('Data/Rhythms_1.mat') # Load the experimental data\n",
"d1 = data['d1']\n",
"t = data['t']"
"```{pyodide-python}\n",
"df = pd.read_csv(\"https://raw.githubusercontent.com/Mark-Kramer/BU-MA665-MA666/master/Data/Rhythms_1.csv\")\n",
"\n",
"# Extract the variables from the loaded data\n",
"data = np.array(df.iloc[:,0]) #Get the values associated with the first column of the dataframe\n",
"t = np.array(df.iloc[:,1]) #Get the values associated with the second column of the dataframe\n",
"```"
]
},
{
Expand All @@ -229,7 +244,7 @@
"metadata": {},
"source": [
"::: {.callout}\n",
"**Q:** What variables appear? What are the dimensions of each? What is the sampling interval? What is the total duration of the data?\n",
"**Q:** What are the dimensions of each variable? What is the sampling interval? What is the total duration of the data?\n",
":::"
]
},
Expand All @@ -248,9 +263,9 @@
"id": "5b83e12b-15bd-4509-a215-e087c209711c",
"metadata": {},
"source": [
" Now, we'd like to \"compare\" sinusoids with known frequency to the data.\n",
" Now, we'd like to compare sinusoids with known frequency to the data.\n",
"\n",
"In doing so, our goal is to identify those sinusoids that \"best match\" the data; this will reveal the rhythms that appear in the signal, which we will ultimately display graphically in a power spectrum.\n",
"In doing so, our goal is to identify those sinusoids that \"best match\" the data; this will reveal the rhythms that appear in the signal, which we will ultimately display graphically in a power spectrum.\n",
"\n",
"To begin this process, we'll specify a handful of frequencies to consider.\n",
"\n",
Expand Down
Binary file added Slides/Rhythms_1.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions docs/Backpropagation.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>

<meta charset="utf-8">
<meta name="generator" content="quarto-1.5.57">
<meta name="generator" content="quarto-1.5.56">

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">

Expand Down Expand Up @@ -228,7 +228,7 @@
}

// Store cell data
globalThis.qpyodideCellDetails = [{"options":{"context":"interactive","autorun":"","fig-height":5,"out-width":"700px","output":"true","message":"true","read-only":"false","label":"","out-height":"","results":"markup","classes":"","fig-width":7,"comment":"","warning":"true","fig-cap":"","dpi":72},"id":1,"code":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd"},{"options":{"context":"interactive","autorun":"","fig-height":5,"out-width":"700px","output":"true","message":"true","read-only":"false","label":"","out-height":"","results":"markup","classes":"","fig-width":7,"comment":"","warning":"true","fig-cap":"","dpi":72},"id":2,"code":"df = pd.read_csv(\"https://raw.githubusercontent.com/Mark-Kramer/BU-MA665-MA666/master/Data/backpropagation_example_data.csv\")\n\n# Extract the variables from the loaded data\nin_true = np.array(df.iloc[:,0]) #Get the values associated with the first column of the dataframe\nout_true = np.array(df.iloc[:,1]) #Get the values associated with the second column of the dataframe"},{"options":{"context":"interactive","autorun":"","fig-height":5,"out-width":"700px","output":"true","message":"true","read-only":"false","label":"","out-height":"","results":"markup","classes":"","fig-width":7,"comment":"","warning":"true","fig-cap":"","dpi":72},"id":3,"code":"print(np.transpose([in_true, out_true]))"},{"options":{"context":"interactive","autorun":"","fig-height":5,"out-width":"700px","output":"true","message":"true","read-only":"false","label":"","out-height":"","results":"markup","classes":"","fig-width":7,"comment":"","warning":"true","fig-cap":"","dpi":72},"id":4,"code":"def sigmoid(x):\n return 1/(1+np.exp(-x)) # Define the sigmoid anonymous function.\n\ndef feedforward(w, s0): # Define feedforward solution.\n # ... x1 = activity of first neuron,\n # ... s1 = output of first neuron,\n # ... x2 = activity of second neuron,\n # ... s2 = output of second neuron,\n # ... out = output of neural network.\n return out,s1,s2"},{"options":{"context":"interactive","autorun":"","fig-height":5,"out-width":"700px","output":"true","message":"true","read-only":"false","label":"","out-height":"","results":"markup","classes":"","fig-width":7,"comment":"","warning":"true","fig-cap":"","dpi":72},"id":5,"code":"w = [0.5,0.5] # Choose initial values for the weights.\nalpha = 0.01 # Set the learning constant.\n\nK = np.size(in_true);\nresults = np.zeros([K,3]) # Define a variable to hold the results of each iteration. \n\nfor k in np.arange(K):\n s0 = in_true[k] # Define the input,\n target = out_true[k] # ... and the target output.\n \n #Calculate feedforward solution to get output.\n \n #Update the weights.\n w0 = w[0]; w1 = w[1];\n w[1] = \"SOMETHING\"\n w[0] = \"SOMETHING\"\n \n # Save the results of this step. --------------------------------------\n # Here we save the 3 weights, and the neural network output.\n # results[k,:] = [w[0],w[1], out]\n\n# Plot the NN weights and error during training \n# plt.clf()\n# plt.plot(results[:,1], label='w1')\n# plt.plot(results[:,0], label='w0')\n# plt.plot(results[:,2]-target, label='error')\n# plt.legend() #Include a legend,\n# plt.xlabel('Iteration number'); #... and axis label.\n\n# Print the NN weights\n# print(results[-1,0:2])"}];
globalThis.qpyodideCellDetails = [{"code":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd","id":1,"options":{"label":"","read-only":"false","results":"markup","out-height":"","classes":"","output":"true","autorun":"","context":"interactive","dpi":72,"fig-height":5,"comment":"","out-width":"700px","fig-cap":"","warning":"true","fig-width":7,"message":"true"}},{"code":"df = pd.read_csv(\"https://raw.githubusercontent.com/Mark-Kramer/BU-MA665-MA666/master/Data/backpropagation_example_data.csv\")\n\n# Extract the variables from the loaded data\nin_true = np.array(df.iloc[:,0]) #Get the values associated with the first column of the dataframe\nout_true = np.array(df.iloc[:,1]) #Get the values associated with the second column of the dataframe","id":2,"options":{"label":"","read-only":"false","results":"markup","out-height":"","classes":"","output":"true","autorun":"","context":"interactive","dpi":72,"fig-height":5,"comment":"","out-width":"700px","fig-cap":"","warning":"true","fig-width":7,"message":"true"}},{"code":"print(np.transpose([in_true, out_true]))","id":3,"options":{"label":"","read-only":"false","results":"markup","out-height":"","classes":"","output":"true","autorun":"","context":"interactive","dpi":72,"fig-height":5,"comment":"","out-width":"700px","fig-cap":"","warning":"true","fig-width":7,"message":"true"}},{"code":"def sigmoid(x):\n return 1/(1+np.exp(-x)) # Define the sigmoid anonymous function.\n\ndef feedforward(w, s0): # Define feedforward solution.\n # ... x1 = activity of first neuron,\n # ... s1 = output of first neuron,\n # ... x2 = activity of second neuron,\n # ... s2 = output of second neuron,\n # ... out = output of neural network.\n return out,s1,s2","id":4,"options":{"label":"","read-only":"false","results":"markup","out-height":"","classes":"","output":"true","autorun":"","context":"interactive","dpi":72,"fig-height":5,"comment":"","out-width":"700px","fig-cap":"","warning":"true","fig-width":7,"message":"true"}},{"code":"w = [0.5,0.5] # Choose initial values for the weights.\nalpha = 0.01 # Set the learning constant.\n\nK = np.size(in_true);\nresults = np.zeros([K,3]) # Define a variable to hold the results of each iteration. \n\nfor k in np.arange(K):\n s0 = in_true[k] # Define the input,\n target = out_true[k] # ... and the target output.\n \n #Calculate feedforward solution to get output.\n \n #Update the weights.\n w0 = w[0]; w1 = w[1];\n w[1] = \"SOMETHING\"\n w[0] = \"SOMETHING\"\n \n # Save the results of this step. --------------------------------------\n # Here we save the 3 weights, and the neural network output.\n # results[k,:] = [w[0],w[1], out]\n\n# Plot the NN weights and error during training \n# plt.clf()\n# plt.plot(results[:,1], label='w1')\n# plt.plot(results[:,0], label='w0')\n# plt.plot(results[:,2]-target, label='error')\n# plt.legend() #Include a legend,\n# plt.xlabel('Iteration number'); #... and axis label.\n\n# Print the NN weights\n# print(results[-1,0:2])","id":5,"options":{"label":"","read-only":"false","results":"markup","out-height":"","classes":"","output":"true","autorun":"","context":"interactive","dpi":72,"fig-height":5,"comment":"","out-width":"700px","fig-cap":"","warning":"true","fig-width":7,"message":"true"}}];


</script>
Expand Down
Loading

0 comments on commit 2a13bc5

Please sign in to comment.