diff --git a/HW7.ipynb b/HW7.ipynb new file mode 100644 index 0000000..a791543 --- /dev/null +++ b/HW7.ipynb @@ -0,0 +1,170 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Homework \\#7 - 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": [ + "## Sampling and aliasing\n", + " \n", + "Consider the linear chirp defined as\n", + "\n", + "$$\n", + " x(t) = \\cos(\\varphi(t)) = \\cos(\\pi at^2 + 2\\pi bt)\n", + "$$\n", + "\n", + "where $a$ and $b$ are real-valued parameters.\n", + "\n", + "* What can you say about the frequency of the signal $x(t)$? In particular, if we sample the signal with a given sampling rate, what do you think is going to happen?\n", + "* Write a function `chirp(a, b, Fs, len)` that computes the sampled version of a chirp at a sampling frequency `Fs` for `len` seconds\n", + "* Look at a typical shape of a chirp before aliasing by plotting `chirp(40, 4, 400, 1)`. Now plot `chirp(400, 4, 400, 1)` and explain what you observe\n", + "* Consider the case where $a=50$ and $b=600$. What is the minimum sampling frequency so that aliasing does not occur before $t=1$ second? %\n", + "* Play a chirp signal computed using $a=40$, $b=4$ and $F_s=900$ for a duration of 10 seconds. Repeat the operation with $F_s=700$. What do you observe?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution\n", + "\n", + "The instantaneous angular frequency of the chirp at time $t$ is ..." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def chirp(a, b, sf, len=1):\n", + " ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For the plots, it's best to use wider viewports:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.rcParams[\"figure.figsize\"] = (14,4)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(chirp(40, 4, 400, 1));" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can see from this plot that when $a=40$, a sampling frequency of ..." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(chirp(400, 4, 400, 1));" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "When $a=400$, the sampling frequency ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The minimum sampling frequency ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can play the chirp for a duration of 10 seconds by executing the following code." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import IPython\n", + "IPython.display.Audio(chirp(40, 4, 900, 10), rate=900)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "IPython.display.Audio(chirp(40, 4, 700, 10), rate=700)" + ] + } + ], + "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.6.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}