{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from numpy import loadtxt\n", "import matplotlib.pyplot as plt\n", "from numpy import zeros,exp,pi\n", "from numpy.fft import rfft\n", "\n", "def dft(y):\n", " N = len(y)\n", " c = zeros(N//2+1,complex) # only sums over the first half of the series assuming the data is real\n", " for k in range(N//2+1):\n", " for n in range(N):\n", " c[k] += y[n]*exp(-2j*pi*k*n/N)\n", " return c\n", "\n", "x = loadtxt(\"pitch.txt\",float) # load the waveform we are looking at\n", "%timeit c1 = dft(x)\n", "%timeit c2 = rfft(x)\n", "\n", "plt.figure(figsize=(16,8)) # set the figsize\n", "plt.subplot(1,2,1) # now plot the two histograms as separate figures on 2 rows with 1 column, and this is row one.\n", "plt.xlabel(\"$t$\")\n", "plt.ylabel(\"$f(t)$\")\n", "plt.plot(x)\n", "\n", "plt.subplot(1,2,2) # now plot the two histograms as separate figures on 2 rows with 1 column, and this is row one.\n", "plt.xlim(0,500)\n", "plt.xlabel(\"$k$\")\n", "plt.ylabel(\"$c_k$\")\n", "plt.plot(abs(c1),'r') # plot the absolute values, since the coefficients are generally complex\n", "plt.plot(abs(c2),'y--') # plot the absolute values, since the coefficients are generally complex\n", "\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.6" } }, "nbformat": 4, "nbformat_minor": 4 }