From f0ac9294681f08ffc872e69594d80083c6a3ac91 Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Tue, 15 Feb 2022 14:17:31 +0100 Subject: [PATCH] 05: Task 3 is done, and runs quite quickly. --- ...Discrete and Fast Fourier Transforms.ipynb | 57 +++++++++++++++++-- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/Week 2/5 Discrete and Fast Fourier Transforms.ipynb b/Week 2/5 Discrete and Fast Fourier Transforms.ipynb index c7e604a..d8945f4 100644 --- a/Week 2/5 Discrete and Fast Fourier Transforms.ipynb +++ b/Week 2/5 Discrete and Fast Fourier Transforms.ipynb @@ -84,7 +84,8 @@ "outputs": [], "source": [ "import numpy as np\n", - "from matplotlib import pyplot as plt" + "from matplotlib import pyplot as plt\n", + "import timeit" ] }, { @@ -229,6 +230,10 @@ "ax[1].plot(np.abs(np.fft.fft(yk1)), label=\"numpy.fft.fft\")\n", "ax[1].legend(loc=\"upper right\")\n", "\n", + "# TODO: So the graphs overlap completely. Is this good enough?\n", + "# To make it more clear, we could mirror one of the graphs (multiply by -1),\n", + "# like what is often done in spectroscopy, or we could add the difference.\n", + "\n", "fig.show()" ] }, @@ -266,7 +271,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "deletable": false, "nbgrader": { @@ -281,10 +286,52 @@ "task": false } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "M = 2 gives\n", + "tOut = [0.0010804971680045128, 0.0005500782281160355, 0.0005198698490858078, 0.0005232971161603928, 0.0006225360557436943]\n", + "\n", + "M = 3 gives\n", + "tOut = [0.0005145492032170296, 0.00047505367547273636, 0.000535009428858757, 0.0003488045185804367, 0.00028009340167045593]\n", + "\n", + "M = 4 gives\n", + "tOut = [0.0002853143960237503, 0.00024075806140899658, 0.00021690316498279572, 0.00021761376410722733, 0.00023322459310293198]\n", + "\n", + "M = 5 gives\n", + "tOut = [0.000910954549908638, 0.0005088187754154205, 0.0005268435925245285, 0.0005177054554224014, 0.0005334652960300446]\n", + "\n", + "M = 6 gives\n", + "tOut = [0.002634277567267418, 0.0023930883035063744, 0.0024030981585383415, 0.002396835945546627, 0.0023911641910672188]\n", + "\n", + "M = 7 gives\n", + "tOut = [0.0092502785846591, 0.008834738284349442, 0.00884521659463644, 0.008864735253155231, 0.008844294585287571]\n", + "\n", + "M = 8 gives\n", + "tOut = [0.034397597424685955, 0.03395726904273033, 0.03396071586757898, 0.03401851560920477, 0.03401240427047014]\n", + "\n", + "M = 9 gives\n", + "tOut = [0.14702877961099148, 0.148259031586349, 0.14664556831121445, 0.14639647398144007, 0.14646969363093376]\n", + "\n", + "M = 10 gives\n", + "tOut = [0.5762987565249205, 0.5738314474001527, 0.5741532389074564, 0.5737893972545862, 0.574077476747334]\n", + "\n" + ] + } + ], "source": [ - "# YOUR CODE HERE\n", - "raise NotImplementedError()" + "for M in range(2, 10+1):\n", + " N = 2**M\n", + " xk = 2*np.pi*np.arange(N)/N\n", + " # Using the first equation for yk from the previous exercise.\n", + " yk = np.exp(20j*xk) + np.exp(40j*xk)\n", + " tOut = timeit.repeat(stmt=lambda: DFT(yk), number=10, repeat=5)\n", + " tMean = np.mean(tOut)\n", + " print(\"M =\", M, \"gives\")\n", + " print(\"tOut =\", tOut)\n", + " print()" ] }, {