{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework \\#4 - Python exercise" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Gibbs Phenomenon\n", " \n", "In this exercise you will verify by yourself the existence of the Gibbs phenomenon using Python (or any other numerical package). The idea is to plot a zoomed-in version of the frequency response of a truncated ideal lowpass filter with cutoff frequency $\\pi/2$:\n", "$$\n", " \\hat H(e^{j\\omega}) = \\sum_{n=-N}^{N} (1/2)\\mbox{sinc}(n/2) \\; e^{-j\\omega n}\n", "$$\n", "where we are interested in plotting the transform over a small interval around the cutoff frequency.\n", " \n", "* Plot $\\hat H(e^{j\\omega})$ over 2000 points in the interval $1.4 \\leq \\omega \\leq 1.7$ for $N = 20$. \n", "* Repeat the above point for $N = 100$ and $N=200$ and verify that the peak of the magnitude is still approximately $9\\%$ of the value of the discontinuity.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution\n", "\n", "First let's define a function that computes the DTFT $\\hat H(e^{j\\omega})$ over a given set of values $\\{\\omega_0, \\ldots, \\omega_M\\}$. To do so, we will define a matrix $\\mathbf{W}$ such that $W_{m,n} = e^{-j\\omega_m n}$ and, with this, we can obtain all $M$ DTFT values as the matrix-vector multiplication $\\mathbf{Wx = W}[h[-N], \\ldots, h[N]]^T$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# this function computes H(e^jw) for a given set of values of omega\n", "def H(omega: np.ndarray, N: int):\n", " # Create the transform matrix W\n", " ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "wlim = [1.4, 1.7]\n", "omega = np.linspace(wlim[0], wlim[1], 2000)\n", "\n", "fig, ax = plt.subplots()\n", "for N in (20, 100, 200, 1000):\n", " ax.plot(omega, H(omega, N), label=f\"$N={N}$\")\n", "ax.plot(wlim, [1.09, 1.09], 'r--')\n", "ax.set_xlim(wlim)\n", "ax.set_xlabel(r'$\\omega$', fontsize=12)\n", "ax.set_ylabel(r'$\\left|\\hat{H}(e^{j\\omega})\\right|$', fontsize=12)\n", "ax.grid()\n", "ax.legend()" ] } ], "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.11.8" } }, "nbformat": 4, "nbformat_minor": 4 }