Created using Colaboratory

This commit is contained in:
udlbook
2023-11-06 13:10:09 +00:00
parent 7a07cba349
commit 0c5aacbd8b

View File

@@ -4,7 +4,7 @@
"metadata": { "metadata": {
"colab": { "colab": {
"provenance": [], "provenance": [],
"authorship_tag": "ABX9TyPHUNRkJMI5LujaxIXNV60m", "authorship_tag": "ABX9TyPTidpnPhn4O5QF011gt0cz",
"include_colab_link": true "include_colab_link": true
}, },
"kernelspec": { "kernelspec": {
@@ -85,10 +85,10 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"# Now let's define a zero-padded convolution operation\n", "# Now let's define a zero-padded convolution operation\n",
"# with a convolution kernel size of 3, a stride of 1, and a dilation of 0\n", "# with a convolution kernel size of 3, a stride of 1, and a dilation of 1\n",
"# as in figure 10.2a-c. Write it yourself, don't call a library routine!\n", "# as in figure 10.2a-c. Write it yourself, don't call a library routine!\n",
"# Don't forget that Python arrays are indexed from zero, not from 1 as in the book figures\n", "# Don't forget that Python arrays are indexed from zero, not from 1 as in the book figures\n",
"def conv_3_1_0_zp(x_in, omega):\n", "def conv_3_1_1_zp(x_in, omega):\n",
" x_out = np.zeros_like(x_in)\n", " x_out = np.zeros_like(x_in)\n",
" # TODO -- write this function\n", " # TODO -- write this function\n",
" # replace this line\n", " # replace this line\n",
@@ -119,7 +119,7 @@
"source": [ "source": [
"\n", "\n",
"omega = [0.33,0.33,0.33]\n", "omega = [0.33,0.33,0.33]\n",
"h = conv_3_1_0_zp(x, omega)\n", "h = conv_3_1_1_zp(x, omega)\n",
"\n", "\n",
"# Check that you have computed this correctly\n", "# Check that you have computed this correctly\n",
"print(f\"Sum of output is {np.sum(h):3.3}, should be 71.1\")\n", "print(f\"Sum of output is {np.sum(h):3.3}, should be 71.1\")\n",
@@ -155,7 +155,7 @@
"source": [ "source": [
"\n", "\n",
"omega = [-0.5,0,0.5]\n", "omega = [-0.5,0,0.5]\n",
"h2 = conv_3_1_0_zp(x, omega)\n", "h2 = conv_3_1_1_zp(x, omega)\n",
"\n", "\n",
"# Draw the signal\n", "# Draw the signal\n",
"fig,ax = plt.subplots()\n", "fig,ax = plt.subplots()\n",
@@ -187,9 +187,9 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"# Now let's define a zero-padded convolution operation\n", "# Now let's define a zero-padded convolution operation\n",
"# with a convolution kernel size of 3, a stride of 2, and a dilation of 0\n", "# with a convolution kernel size of 3, a stride of 2, and a dilation of 1\n",
"# as in figure 10.2a-c. Write it yourself, don't call a library routine!\n", "# as in figure 10.3a-b. Write it yourself, don't call a library routine!\n",
"def conv_3_2_0_zp(x_in, omega):\n", "def conv_3_2_1_zp(x_in, omega):\n",
" x_out = np.zeros(int(np.ceil(len(x_in)/2)))\n", " x_out = np.zeros(int(np.ceil(len(x_in)/2)))\n",
" # TODO -- write this function\n", " # TODO -- write this function\n",
" # replace this line\n", " # replace this line\n",
@@ -209,7 +209,7 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"omega = [0.33,0.33,0.33]\n", "omega = [0.33,0.33,0.33]\n",
"h3 = conv_3_2_0_zp(x, omega)\n", "h3 = conv_3_2_1_zp(x, omega)\n",
"\n", "\n",
"# If you have done this right, the output length should be six and it should\n", "# If you have done this right, the output length should be six and it should\n",
"# contain every other value from the original convolution with stride 1\n", "# contain every other value from the original convolution with stride 1\n",
@@ -226,9 +226,9 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"# Now let's define a zero-padded convolution operation\n", "# Now let's define a zero-padded convolution operation\n",
"# with a convolution kernel size of 5, a stride of 1, and a dilation of 0\n", "# with a convolution kernel size of 5, a stride of 1, and a dilation of 1\n",
"# as in figure 10.2a-c. Write it yourself, don't call a library routine!\n", "# as in figure 10.3c. Write it yourself, don't call a library routine!\n",
"def conv_5_1_0_zp(x_in, omega):\n", "def conv_5_1_1_zp(x_in, omega):\n",
" x_out = np.zeros_like(x_in)\n", " x_out = np.zeros_like(x_in)\n",
" # TODO -- write this function\n", " # TODO -- write this function\n",
" # replace this line\n", " # replace this line\n",
@@ -249,7 +249,7 @@
"source": [ "source": [
"\n", "\n",
"omega2 = [0.2, 0.2, 0.2, 0.2, 0.2]\n", "omega2 = [0.2, 0.2, 0.2, 0.2, 0.2]\n",
"h4 = conv_5_1_0_zp(x, omega2)\n", "h4 = conv_5_1_1_zp(x, omega2)\n",
"\n", "\n",
"# Check that you have computed this correctly\n", "# Check that you have computed this correctly\n",
"print(f\"Sum of output is {np.sum(h4):3.3}, should be 69.6\")\n", "print(f\"Sum of output is {np.sum(h4):3.3}, should be 69.6\")\n",
@@ -273,10 +273,10 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"# Finally let's define a zero-padded convolution operation\n", "# Finally let's define a zero-padded convolution operation\n",
"# with a convolution kernel size of 3, a stride of 1, and a dilation of 1\n", "# with a convolution kernel size of 3, a stride of 1, and a dilation of 2\n",
"# as in figure 10.2a-c. Write it yourself, don't call a library routine!\n", "# as in figure 10.3d. Write it yourself, don't call a library routine!\n",
"# Don't forget that Python arrays are indexed from zero, not from 1 as in the book figures\n", "# Don't forget that Python arrays are indexed from zero, not from 1 as in the book figures\n",
"def conv_3_1_1_zp(x_in, omega):\n", "def conv_3_1_2_zp(x_in, omega):\n",
" x_out = np.zeros_like(x_in)\n", " x_out = np.zeros_like(x_in)\n",
" # TODO -- write this function\n", " # TODO -- write this function\n",
" # replace this line\n", " # replace this line\n",
@@ -295,7 +295,7 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"omega = [0.33,0.33,0.33]\n", "omega = [0.33,0.33,0.33]\n",
"h5 = conv_3_1_1_zp(x, omega)\n", "h5 = conv_3_1_2_zp(x, omega)\n",
"\n", "\n",
"# Check that you have computed this correctly\n", "# Check that you have computed this correctly\n",
"print(f\"Sum of output is {np.sum(h5):3.3}, should be 68.3\")\n", "print(f\"Sum of output is {np.sum(h5):3.3}, should be 68.3\")\n",
@@ -328,7 +328,7 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"# Compute matrix in figure 10.4 d\n", "# Compute matrix in figure 10.4 d\n",
"def get_conv_mat_3_1_0_zp(n_out, omega):\n", "def get_conv_mat_3_1_1_zp(n_out, omega):\n",
" omega_mat = np.zeros((n_out,n_out))\n", " omega_mat = np.zeros((n_out,n_out))\n",
" # TODO Fill in this matix\n", " # TODO Fill in this matix\n",
" # Replace this line:\n", " # Replace this line:\n",
@@ -349,11 +349,11 @@
"source": [ "source": [
"# Run original convolution\n", "# Run original convolution\n",
"omega = np.array([-1.0,0.5,-0.2])\n", "omega = np.array([-1.0,0.5,-0.2])\n",
"h6 = conv_3_1_0_zp(x, omega)\n", "h6 = conv_3_1_1_zp(x, omega)\n",
"print(h6)\n", "print(h6)\n",
"\n", "\n",
"# If you have done this right, you should get the same answer\n", "# If you have done this right, you should get the same answer\n",
"omega_mat = get_conv_mat_3_1_0_zp(len(x), omega)\n", "omega_mat = get_conv_mat_3_1_1_zp(len(x), omega)\n",
"h7 = np.matmul(omega_mat, x)\n", "h7 = np.matmul(omega_mat, x)\n",
"print(h7)\n" "print(h7)\n"
], ],