Commit c85ca71b authored by Kuldeep Singh Dhaka's avatar Kuldeep Singh Dhaka

Add Fast Fourier Transform demo

parent b9f745be
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This code will capture, process and show you the FFT of the signal on AIN0.CH0."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFfdJREFUeJzt3X+MXeV95/H3d2Zs89s22FDHNjUR7iq0IoR4KVlWUTd0\nkwDdwB+hgo2CxVJZu8uuEmWlLmylXVXqH8lq1WRRqySoZOus2iaUNsVCtBQBUVNtArUDOBAgdgiB\niQk28Q9+GPCP+e4f97mei7meufbc4cyc5/2Sru45z3nuPc8zc+Zzn/ucc+dGZiJJaq+RphsgSZpd\nBr0ktZxBL0ktZ9BLUssZ9JLUcga9JLWcQS9JLWfQS1LLGfSS1HJjTTcAYNmyZblmzZqmmyFJ88qW\nLVtezszl09WbE0G/Zs0aNm/e3HQzJGleiYifDlLPqRtJarmBgj4inouIH0TEYxGxuZSdGRH3R8S2\ncr+0lEdE3BYR2yNia0RcPJsdkCRN7XhG9P8qMy/KzHVl/RbggcxcCzxQ1gGuANaW2wbgy8NqrCTp\n+M1k6uZqYGNZ3ghc01P+9ez4HrAkIlbMYD+SpBkYNOgT+PuI2BIRG0rZOZn5IkC5P7uUrwRe6Hns\neCmTJDVg0KtuLsvMHRFxNnB/RDw9Rd3oU/aObzcpLxgbAM4999wBmyFJOl4Djegzc0e53wl8C7gE\neKk7JVPud5bq48DqnoevAnb0ec7bM3NdZq5bvnzay0AlSSdo2qCPiFMj4vTuMvBR4AlgE7C+VFsP\n3F2WNwE3lKtvLgX2dad43m0/2/sGDz29c/qKkjQLntyxj+8/v6fpZgw0dXMO8K2I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060zdfmqd9ITN3lPudwLfovAjPx+NrHBjPzIfL\n+l10gn/O9WU+B/1c/xLyTUD37Pl6OnPd3fIbyhn4S4F95a3XfcBHI2JpOYP+UTrzoS8Cr0bEpeWM\n+w1HPVe/fZywso87gKcy8w/na38iYnlELCnLJwO/CTwFPAR88hj96O77k8CD2ZkA3QRcF50rWc4D\n1tI5Qdb3+CuPOdY+Tkhm3pqZqzJzTdnPg5n5qfnYl4g4NSJO7y7TOS6eYJ4dXwCZ+XPghYj4Z6Xo\ncuCHc7IvMz2x0uSNzlnsH9GZe/29BtvxF8CLwEE6r8I30ZnffADYVu7PLHUD+OPS5h8A63qe598B\n28vtxp7ydXT+GH4M/BGTH3Tru48Z9uVf0nl7uBV4rNyunG/9AS4EHi39eAL476X8vXTCbTvwl8Ci\nUn5SWd9etr+357l+r7T1GcpVD1Mdf8fax5COtd9g8qqbedeX8nyPl9uT3X3Nt+OrZ18XAZvLcfY3\ndK6amXN98ZOxktRy83nqRpI0AINeklrOoJekljPoJanlDHpJajmDXpJazqCXpJYz6CWp5f4/Cf29\nKbXtYu8AAAAASUVORK5CYII=\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7fdca072c828>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import box0\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import scipy.fftpack\n",
"\n",
"##### PART 1 - Capture\n",
"\n",
"# Allocate resources\n",
"dev = box0.usb.open_supported()\n",
"ain0 = dev.ain()\n",
"\n",
"# Prepare AIN0 for snapshot mode\n",
"ain0.snapshot_prepare()\n",
"\n",
"# Read data from AIN0\n",
"bitsize, sampling_freq = ain0.bitsize_speed_get()\n",
"data = np.empty(1000, dtype=np.float32)\n",
"ain0.snapshot_start(data)\n",
"\n",
"# Free the resources\n",
"ain0.close()\n",
"dev.close()\n",
"\n",
"##### PART 2 - Process\n",
"\n",
"# perform FFT on captured data\n",
"fft_amp = np.abs(scipy.fftpack.fft(data))\n",
"fft_freq = np.linspace(0, sampling_freq, len(fft_amp))\n",
"\n",
"##### PART 3 - Visualize\n",
"fig, ax = plt.subplots()\n",
"\n",
"# Show the time domain results (unable to show both plot at once - fix later!...never ;)\n",
"#time_x = np.linspace(0, len(data) / sampling_freq, len(data))\n",
"#ax.plot(time_x, data)\n",
"\n",
"# Show the frequency domain results\n",
"ax.plot(fft_freq, fft_amp)\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.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment