diff --git a/HW3.ipynb b/HW3.ipynb new file mode 100644 index 0000000..eeeea96 --- /dev/null +++ b/HW3.ipynb @@ -0,0 +1,113 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Homework \\#3 - Python exercise" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%pylab inline\n", + "import matplotlib\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## DTFT,DFT, and numerical computations\n", + "\n", + "Consider the following infinite non-periodic discrete time signal, where $M\\in\\mathbb{N}$:\n", + "$$\n", + "x[n]=\\begin{cases}\n", + " 0 & n<0,\\\\\n", + " 1 & 0\\leq n< M,\\\\\n", + " 0 & n\\geq M.\n", + " \\end{cases}\n", + "$$\n", + "\n", + " The goal is to plot the magnitude of $X(e^{j\\omega})$ using a numerical package. Using a numerical package implies that we will only obtain an approximation of the true value.\n", + "* Compute $X(e^{j\\omega})$ analytically\n", + "* Using a numerical package plot $|X(e^{j\\omega})|$ over $[-\\pi, \\pi]$ for $M=20$ using 10,000 points.\n", + "* Now generate a finite sequence $\\hat{x}[n]$ of length $N=30$ such that $\\hat{x}[n]=x[n]$ for $n=0,1, \\ldots, N-1$. Compute its DFT using the numerical package's FFT algorithm and plot its magnitude. Compare it with the plot obtained previously.\n", + "* Repeat now for different values of $N = 50, 100, 1000$. What can you conclude?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution\n", + "The analytical expression for the DTFT of the signal is \n", + "$$\n", + " X(e^{j\\omega}) = \\ldots\n", + "$$" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# we want to plot the analytic DTFT over 10,000 points: create the values for \\omega\n", + "w = ...\n", + "\n", + "# analytic expression for the DTFT\n", + "M = 20\n", + "X = ...\n", + "\n", + "# plotting\n", + "plt.plot(w, X);\n", + "plt.xlim([-np.pi, np.pi]); # fit the x-axis" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# now compute the FFT-based approximations\n", + "N = [30, 50, 100, 1000]\n", + "for pts in N:\n", + " # build the signal\n", + " x = ...\n", + " \n", + " # compute the DFT and shift it to align it with the DTFT\n", + " X_a = ...\n", + " plt.plot(np.abs(X_a));\n", + " plt.show();" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.7" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}