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": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPHUNRkJMI5LujaxIXNV60m",
"authorship_tag": "ABX9TyPTidpnPhn4O5QF011gt0cz",
"include_colab_link": true
},
"kernelspec": {
@@ -85,10 +85,10 @@
"cell_type": "code",
"source": [
"# 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",
"# 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",
" # TODO -- write this function\n",
" # replace this line\n",
@@ -119,7 +119,7 @@
"source": [
"\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",
"# Check that you have computed this correctly\n",
"print(f\"Sum of output is {np.sum(h):3.3}, should be 71.1\")\n",
@@ -155,7 +155,7 @@
"source": [
"\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",
"# Draw the signal\n",
"fig,ax = plt.subplots()\n",
@@ -187,9 +187,9 @@
"cell_type": "code",
"source": [
"# 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",
"# as in figure 10.2a-c. Write it yourself, don't call a library routine!\n",
"def conv_3_2_0_zp(x_in, omega):\n",
"# with a convolution kernel size of 3, a stride of 2, and a dilation of 1\n",
"# as in figure 10.3a-b. Write it yourself, don't call a library routine!\n",
"def conv_3_2_1_zp(x_in, omega):\n",
" x_out = np.zeros(int(np.ceil(len(x_in)/2)))\n",
" # TODO -- write this function\n",
" # replace this line\n",
@@ -209,7 +209,7 @@
"cell_type": "code",
"source": [
"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",
"# 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",
@@ -226,9 +226,9 @@
"cell_type": "code",
"source": [
"# 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",
"# as in figure 10.2a-c. Write it yourself, don't call a library routine!\n",
"def conv_5_1_0_zp(x_in, omega):\n",
"# with a convolution kernel size of 5, a stride of 1, and a dilation of 1\n",
"# as in figure 10.3c. Write it yourself, don't call a library routine!\n",
"def conv_5_1_1_zp(x_in, omega):\n",
" x_out = np.zeros_like(x_in)\n",
" # TODO -- write this function\n",
" # replace this line\n",
@@ -249,7 +249,7 @@
"source": [
"\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",
"# Check that you have computed this correctly\n",
"print(f\"Sum of output is {np.sum(h4):3.3}, should be 69.6\")\n",
@@ -273,10 +273,10 @@
"cell_type": "code",
"source": [
"# 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",
"# as in figure 10.2a-c. Write it yourself, don't call a library routine!\n",
"# with a convolution kernel size of 3, a stride of 1, and a dilation of 2\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",
"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",
" # TODO -- write this function\n",
" # replace this line\n",
@@ -295,7 +295,7 @@
"cell_type": "code",
"source": [
"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",
"# Check that you have computed this correctly\n",
"print(f\"Sum of output is {np.sum(h5):3.3}, should be 68.3\")\n",
@@ -328,7 +328,7 @@
"cell_type": "code",
"source": [
"# 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",
" # TODO Fill in this matix\n",
" # Replace this line:\n",
@@ -349,11 +349,11 @@
"source": [
"# Run original convolution\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",
"\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",
"print(h7)\n"
],