{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n",
"
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n",
"
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Imports\n",
"The tutorial below imports [NumPy](http://www.numpy.org/), [Pandas](https://plotly.com/pandas/intro-to-pandas-tutorial/), [SciPy](https://www.scipy.org/) and [Plotly](https://plotly.com/python/getting-started/)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"import plotly.figure_factory as ff\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import scipy\n",
"\n",
"from scipy import signal"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Import Data\n",
"Let us import some stock data to apply convolution on."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stock_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/stockdata.csv')\n",
"df = stock_data[0:15]\n",
"\n",
"table = ff.create_table(df)\n",
"py.iplot(table, filename='stockdata-peak-fitting')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Convolve Two Signals\n",
"`Convolution` is a type of transform that takes two functions `f` and `g` and produces another function via an integration. In particular, the convolution $(f*g)(t)$ is defined as:\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"\\int_{-\\infty}^{\\infty} {f(\\tau)g(t - \\tau)d\\tau}\n",
"\\end{align*}\n",
"$$\n",
"\n",
"\n",
"We can use convolution in the discrete case between two n-dimensional arrays."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sample = range(15)\n",
"saw = signal.sawtooth(t=sample)\n",
"\n",
"data_sample = list(stock_data['SBUX'][0:100])\n",
"data_sample2 = list(stock_data['AAPL'][0:100])\n",
"x = list(range(len(data_sample)))\n",
"y_convolve = signal.convolve(saw, data_sample2)\n",
"x_convolve = list(range(len(y_convolve)))\n",
"\n",
"trace1 = go.Scatter(\n",
" x = x,\n",
" y = data_sample,\n",
" mode = 'lines',\n",
" name = 'SBUX'\n",
")\n",
"\n",
"trace2 = go.Scatter(\n",
" x = x,\n",
" y = data_sample2,\n",
" mode = 'lines',\n",
" name = 'AAPL'\n",
")\n",
"\n",
"trace3 = go.Scatter(\n",
" x = x_convolve,\n",
" y = y_convolve,\n",
" mode = 'lines',\n",
" name = 'Convolution'\n",
")\n",
"\n",
"data = [trace1, trace2, trace3]\n",
"py.iplot(data, filename='convolution-of-two-signals')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/k_/zf24qrfn2kg710j9pdrxzrz40000gn/T/pip-07Be7Z-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
" Running setup.py install for publisher ... \u001b[?25ldone\n",
"\u001b[?25hSuccessfully installed publisher-0.11\n",
"\u001b[33mYou are using pip version 9.0.3, however version 10.0.1 is available.\n",
"You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/chelsea/venv/venv2/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/Users/chelsea/venv/venv2/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
" warnings.warn('Did you \"Save\" this notebook before running this command? '\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'python-Convolution.ipynb', 'python/convolution/', 'Convolution | plotly',\n",
" 'Learn how to perform convolution between two signals in Python.',\n",
" title='Convolution in Python | plotly',\n",
" name='Convolution',\n",
" language='python',\n",
" page_type='example_index', has_thumbnail='false', display_as='signal-analysis', order=4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 1
}