{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matascii" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from matplotlib import pylab" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pylab.switch_backend(\"module://matascii\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from random import random\n", "x = range(0, 100)\n", "y = [random() for i in x]\n", "pylab.scatter(x, y, edgecolor=[0., 0., 1., 1.])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Scatter" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
***************************************************************************************************************************************
                                                                                                                                       
                                                                                                                                       
                                                                                                                                       
               1.2********************************************************************************************************             
                *              *              *              *              *              *              *              *             
                *              *              *              *              *              *              *              *             
                *              *              *              *              *              *              *              *             
                *              *              *              *              *              *              *              *             
               1.0**                                                                                                  ****             
                *                **               **        **        **  **       **          **                        *             
                *                ****    *       ***        **        **  ****     **          **                        *             
                *                  **    ** **   *****         **           *                     **                     *             
                *                           **     ***         **        **   **        **        ***                    *             
               0.8**                                      **        ***  ****        ** **      **  **                ****             
                *               **         *   *        *  ***       ***   **        **    **   **      **               *             
                *              **    **    **  *           ****                 **         **       *** **               *             
                *              **                                               **                   **                  *             
                *                                        **                       **                                     *             
               0.6**                                 * ****                       ** **                **             ****             
                *                             **     * **  *                **       **                **                *             
                *                             **  *        *                **                                           *             
                *                     ****        *              *                                    **                 *             
                *                   ******                      ****             **                   **                 *             
               0.4                  ** **                    **   **             ** **      **                           *             
                ****                                 ***      ****                ****  **  ****                      ****             
                *                         **         ***      **** **             **    *     **       **                *             
                *                         **                       **                  *     **    **                    *             
                *               **             **                  **   **            ***  ****    **                    *             
               0.2               *             **                       **     **      **  **    **                      *             
                ****              **                    **                     **        **      **                   ****             
                *                 **     **             **       **    **                **   *  **      **              *             
                *                        **                      **    ****  **               **         *               *             
                *                           *   **                       **  **                                          *             
               0.0                 ** *     *** **                             **         **                             *             
                ****               ** **     **                                **         **                          ****             
                *                                                                                                        *             
                *              *              *              *              *              *              *              *             
                *              *              *              *              *              *              *              *             
                *              *              *              *              *              *              *              *             
               −**********************************************************************************************************             
                −20            0              20             40             60             80             100            120           
                                                                                                                                       
                                                                                                                                       
                                                                                                                                       
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pylab.show()\n", "#pylab.savefig(\"hoge.txt\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Line plot with Legend" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
***************************************************************************************************************************************
                                                                                                                                       
                                                                                                                                       
                                                                                                                                       
               10*********************************************************************************************************             
                *                          *                       ***  ******                 *                         *             
                *                          *                     **  *        *****            *                         *             
                *                          *                   **    *             ******      *                         *             
                *                          *                 **      *                   *******                         *             
                *                                           **                                 ******                    *             
               8****                                      **                                         ******           ****             
                *                                       **                                                 ******        *             
                *                                      *                                                         ******  *             
                *                                    **                                                                ***             
                *                                  **                                                                 ****             
                *                                **                                                                 **   *             
               6****                            *                                                                 **  ****             
                *                             **                                                               ***       *             
                ***********                 **                                                                *          *             
                *         *               **                                                               ***           *             
                *         *              **                                                              **              *             
                *  ***  hoge           **                                                              **                *             
               4****      *          **                                                             ***               ****             
             Nyaaa        *        **                                                              **                    *             
                *  ***  nya       **                                                            ***                      *             
                *         *     **                                                            **                         *             
                * *********   **                                                            **                           *             
                *            *                                                            **                             *             
               2****       **                                                           **                            ****             
                *        **                                                          ***                                 *             
                *      **                                                          **                                    *             
                *     *                                                          **                                      *             
                *   **                                                         ***                                       *             
                * **                                                         **                                          *             
               0****                                                      ***                                         ****             
                *                                                       **                                               *             
                *                                                      *                                                 *             
                *                          *                ***********                        *                         *             
                *                          *****************         *                         *                         *             
                *        *******************                         *                         *                         *             
               −**********************************************************************************************************             
                1.0                       1.5                       2.0                       2.5                        3.0           
                                                                                                                                       
                                                                    Hello, world!                                                      
                                                                                                                                       
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pylab.plot([1,2,3], [10, 9, 1], label='hoge')\n", "pylab.plot([1,2,3], [8, -2, 1], label='nya')\n", "pylab.xlabel(\"Hello, world!\")\n", "pylab.ylabel(\"Nyaaa\")\n", "pylab.legend(prop={'size': 2}, loc='center left')\n", "pylab.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# All lines below are from http://matplotlib.org/api/pyplot_api.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Subplots" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
***************************************************************************************************************************************
                                                                                                                                       
                                                                                                                                       
                                       *********                    A tale of 2 subplots  **********                                   
                **********************************************************************************************************             
               1*0                 ********* ******                  *                  ****************                 *             
                *                 **** **************                *                **************** *                 *             
                *               ********   *   **** *                *               ** ***    *   *******               *             
                ****            ******     *     * ****              *             ******      *     *****            ****             
                *             ******             ******                            * ****             ** ***             *             
               0*5            *****                ******                        **** *                ******            *             
                *           ******                  *****                        * ****                  * ***           *             
                ****        *****                    ** ***                    **** *                    ******       ****             
             Dam*ed oscill***o**                      *****                    ******                      * ***         *             
               0*0        * ***                        ** ***                **** *                        ******        *             
                *       ******                          ******               ******                          ******      *             
                ****   ******                             * ***            ******                            **** *   ****             
                *    **** *                *              *******    *   *******               *               * ****    *             
                *  ********                *                * *****  * *** ****                *               ********  *             
               **********                  *                ************ *****                 *                 **********            
               *************************************************************************************************************           
               *0*************       1                    2     ****** ***     3                    4               *****5*            
               *****  **** ****                                    *****                                              *****            
               1.0    * *  ** ***                                                                                                      
                **********************************************************************************************************             
                ******* *** ******   *    ***********     *        ****        *                    *                 ****             
                *   * ** ****** ***  *    ******** ****   *   **************** ********************************************            
               0.5  *  ********** *  *  ************ *********** ****** ****************** *********************************           
                *  ****  ***********   *** ***************************** **************** ********************************* *          
                **********  ** ** ****** ******************** ************************************************************* *          
                * *  ****    *** ****************** ************************ *************************** ****************** *          
               0.0*  *  *     ************ ****  **** *************************************************** ************** ****          
             Undamped  **      * * **********      ******* ********    *****************************************************           
                ****** *       *** *********         **********               **                                         *             
                ********        ***** *****                                                                           ****             
               0.5* **           *******                                                                                *             
              *** ** **                                                                                                  *             
             ** **** *               *                    *                    *                    *                    *             
             *  ******               *                    *                    *                    *                 ****             
             ** * **                 *                    *                    *                    *                    *             
              *1.0*******************************************************************************************************             
              **0.0                       0.5                       1.0                       1.5                        2.0           
                                                                                                                                       
                                                                    time (s)                                                           
                                                                                                                                       
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "\n", "x1 = np.linspace(0.0, 5.0)\n", "x2 = np.linspace(0.0, 2.0)\n", "\n", "y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)\n", "y2 = np.cos(2 * np.pi * x2)\n", "\n", "plt.subplot(2, 1, 1)\n", "plt.plot(x1, y1, 'yo-')\n", "plt.title('A tale of 2 subplots')\n", "plt.ylabel('Damped oscillation')\n", "\n", "plt.subplot(2, 1, 2)\n", "plt.plot(x2, y2, 'r.-')\n", "plt.xlabel('time (s)')\n", "plt.ylabel('Undamped')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Polar" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/naoki/anaconda/lib/python2.7/site-packages/matplotlib/tri/triangulation.py:110: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n", " self._neighbors)\n", "matascii/__init__.py:108: RuntimeWarning: invalid value encountered in true_divide\n", " dr = v/l\n" ] }, { "data": { "text/html": [ "
***************************************************************************************************************************************
                                                                                                                                       
                                                                                                                                       
                                                                                  Contour plot of Delaunay triangulation               
                                                              1.0************************************   ****1.00                       
                                                                *        *     ***** **    *        *    * *                           
                                                                *        *  *** * * *  *** *        *    * *                           
                                                                *        **  ** * * * **  **        *    * *                           
                                                                *       **    * * * * *    *        *    * *0.75                       
                                                                *      *      ***** ***      *      *   ****                           
                                                                *     **       * *** *       **     *    * *                           
                                                                *    *  *  ***** *** * ***  ** *    *    * *                           
                                                                *    *  *  *  * ***** *  *  *  **   *    * *                           
                                                              0.5**** ** * *   ********  * ****  ****   ****0.50                       
                                                                *   ** *  * *  *******  **** * ***  *    * *                           
                                                                *  *  * *****   ******  *** * **    *    * *                           
                                                                * **** *******  *****  * ***** **** *    * *                           
                                                                * *   ** ****** ***** ****** **   ***    * *0.25                       
                                                                * ***** ******* ************* **** **   ****                           
                                                                ***    *********     *********    ***    * *                           
                                                                *       *******       *******      **    * *                           
                                                                **   **** *****       ***** ****   **    * *                           
                                                              0.0*** *        *        *       * ****   ****0.00                       
                                                                **   **** *****       ***** ****   **    * *                           
                                                                **      *******       *******       *    * *                           
                                                                ***    *********     *********    ***    * *                           
                                                                ** **** ************* ******* **** **    * *−0.25                      
                                                                * *   ** ****** ***** ****** **   * *   ****                           
                                                                * **** *******  *****  * ***** **** *    * *                           
                                                                *     * *****   ******  *** * **  * *    * *                           
                                                                *  *** *  * *  *******  **** * **   *    * *                           
                                                              −0****  ** * *   ********  * **** *****   ****−0.50                      
                                                                *   **  *  *  * ***** *  *  *  *    *    * *                           
                                                                *    *  *  ***** *** * ***  ** *    *    * *                           
                                                                *     **       * *** *       **     *    * *                           
                                                                *      *      ***** ***      **     *    * *−0.75                      
                                                                *        *    * * * * *     *       *   ****                           
                                                                *        *** ** * * * **  **        *    * *                           
                                                                *        * ***  * * *  **  *        *    * *                           
                                                                *        *    ***** ***    *        *    * *                           
                                                              −1*************************************   ****−1.00                      
                                                                −1.0     −0.5     0.0      0.5      1.0                                
                                                                                                                                       
                                                                                                                                       
                                                                                                                                       
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
***************************************************************************************************************************************
                                                                                                                                       
                                                                                                                                       
                                                                                   Contour plot of user-specified triangulation        
                                                                    *********************************   ****1.00                       
                                                                    *  *   *  *   *   *  *   *  *   *    * *                           
                                                                    *  *   *  *   *   *  *   *  *   *    * *                           
                                                                  58*  *****  *   *   *  *   *  *   *    * *                           
                                                                    **** ****** * *   *  *   *  *****    * *0.95                       
                                                                    *      *    **   **             *   ****                           
                                                                    *       ***  *   * *** ** * *   *    * *                           
                                                                    *          ****   *  * *  * *   *    * *                           
                                                                    *            ***  *  * ** * * * *    * *                           
                                                                    *      ******  ** *  *  * * **  *   ****0.90                       
                                                                  56*     ****     *  *  *  * * *****    * *                           
                                                                    ****      **   *  ** *  * * *****    * *                           
                                                                    *          *   *  *  *  * * * * *    * *                           
                                                                    *       ** *  *   *  *  * * * * *    * *0.85                       
                                                                    *        ***  *   *  * *  ***** *   ****                           
                                                                    *         *****  *   * *  *     *    * *                           
                                                                    *             *  *  *  * **     *    * *                           
                                                                    *              ***  * ** *      *    * *                           
                                                                  54****          ***  *  *  *   ****   ****0.80                       
                                                                Latitude (degrees) *  ** ****       *    * *                           
                                                                    *      ****  **   *  **         *    * *                           
                                                                    *      *   **** **  **          *    * *                           
                                                                    *      *****    *  **           *    * *0.75                       
                                                                    *    ****     **  ***           *   ****                           
                                                                    *    * *   ***  ***             *    * *                           
                                                                  52**** *******  **             ****    * *                           
                                                                    *  ***       ****               *    * *                           
                                                                    *  ** *   ****  **              *   ****0.70                       
                                                                    *   ******     **               *    * *                           
                                                                    * ***       ***   *             *    * *                           
                                                                    * **********    ***             *    * *                           
                                                                    *   *       *******             *    * *0.65                       
                                                                  50**** *******                 ****   ****                           
                                                                    *  *   *  *   *   *  *   *  *   *    * *                           
                                                                    *  *   ********   *  *   *  *   *    * *                           
                                                                    *  *   *  *   *   *  *   *  *   *    * *                           
                                                                    *********************************   ****0.60                       
                                                                   −7  −6 −5  −4  −3 −2  −1 0   1   2                                  
                                                                                                                                       
                                                                                   Longitude (degrees)                                 
                                                                                                                                       
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\"\"\"\n", "Contour plots of unstructured triangular grids.\n", "\"\"\"\n", "import matplotlib.pyplot as plt\n", "import matplotlib.tri as tri\n", "import numpy as np\n", "import math\n", "\n", "# Creating a Triangulation without specifying the triangles results in the\n", "# Delaunay triangulation of the points.\n", "\n", "# First create the x and y coordinates of the points.\n", "n_angles = 48\n", "n_radii = 8\n", "min_radius = 0.25\n", "radii = np.linspace(min_radius, 0.95, n_radii)\n", "\n", "angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)\n", "angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)\n", "angles[:, 1::2] += math.pi/n_angles\n", "\n", "x = (radii*np.cos(angles)).flatten()\n", "y = (radii*np.sin(angles)).flatten()\n", "z = (np.cos(radii)*np.cos(angles*3.0)).flatten()\n", "\n", "# Create the Triangulation; no triangles so Delaunay triangulation created.\n", "triang = tri.Triangulation(x, y)\n", "\n", "# Mask off unwanted triangles.\n", "xmid = x[triang.triangles].mean(axis=1)\n", "ymid = y[triang.triangles].mean(axis=1)\n", "mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)\n", "triang.set_mask(mask)\n", "\n", "# pcolor plot.\n", "plt.figure()\n", "plt.gca().set_aspect('equal')\n", "plt.tricontourf(triang, z)\n", "plt.colorbar()\n", "plt.tricontour(triang, z, colors='k')\n", "plt.title('Contour plot of Delaunay triangulation')\n", "\n", "# You can specify your own triangulation rather than perform a Delaunay\n", "# triangulation of the points, where each triangle is given by the indices of\n", "# the three points that make up the triangle, ordered in either a clockwise or\n", "# anticlockwise manner.\n", "\n", "xy = np.asarray([\n", " [-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],\n", " [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898],\n", " [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],\n", " [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949],\n", " [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959],\n", " [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],\n", " [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980],\n", " [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996],\n", " [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021],\n", " [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],\n", " [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987],\n", " [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],\n", " [-0.020, 0.954], [-0.006, 0.947], [ 0.003, 0.935], [ 0.006, 0.926],\n", " [ 0.005, 0.921], [ 0.022, 0.923], [ 0.033, 0.912], [ 0.029, 0.905],\n", " [ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],\n", " [ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],\n", " [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872],\n", " [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933],\n", " [-0.077, 0.990], [-0.059, 0.993]])\n", "x = np.degrees(xy[:, 0])\n", "y = np.degrees(xy[:, 1])\n", "x0 = -5\n", "y0 = 52\n", "z = np.exp(-0.01*((x - x0)*(x - x0) + (y - y0)*(y - y0)))\n", "\n", "triangles = np.asarray([\n", " [67, 66, 1], [65, 2, 66], [ 1, 66, 2], [64, 2, 65], [63, 3, 64],\n", " [60, 59, 57], [ 2, 64, 3], [ 3, 63, 4], [ 0, 67, 1], [62, 4, 63],\n", " [57, 59, 56], [59, 58, 56], [61, 60, 69], [57, 69, 60], [ 4, 62, 68],\n", " [ 6, 5, 9], [61, 68, 62], [69, 68, 61], [ 9, 5, 70], [ 6, 8, 7],\n", " [ 4, 70, 5], [ 8, 6, 9], [56, 69, 57], [69, 56, 52], [70, 10, 9],\n", " [54, 53, 55], [56, 55, 53], [68, 70, 4], [52, 56, 53], [11, 10, 12],\n", " [69, 71, 68], [68, 13, 70], [10, 70, 13], [51, 50, 52], [13, 68, 71],\n", " [52, 71, 69], [12, 10, 13], [71, 52, 50], [71, 14, 13], [50, 49, 71],\n", " [49, 48, 71], [14, 16, 15], [14, 71, 48], [17, 19, 18], [17, 20, 19],\n", " [48, 16, 14], [48, 47, 16], [47, 46, 16], [16, 46, 45], [23, 22, 24],\n", " [21, 24, 22], [17, 16, 45], [20, 17, 45], [21, 25, 24], [27, 26, 28],\n", " [20, 72, 21], [25, 21, 72], [45, 72, 20], [25, 28, 26], [44, 73, 45],\n", " [72, 45, 73], [28, 25, 29], [29, 25, 31], [43, 73, 44], [73, 43, 40],\n", " [72, 73, 39], [72, 31, 25], [42, 40, 43], [31, 30, 29], [39, 73, 40],\n", " [42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38],\n", " [33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]])\n", "\n", "# Rather than create a Triangulation object, can simply pass x, y and triangles\n", "# arrays to tripcolor directly. It would be better to use a Triangulation\n", "# object if the same triangulation was to be used more than once to save\n", "# duplicated calculations.\n", "plt.figure()\n", "plt.gca().set_aspect('equal')\n", "plt.tricontourf(x, y, triangles, z)\n", "plt.colorbar()\n", "plt.title('Contour plot of user-specified triangulation')\n", "plt.xlabel('Longitude (degrees)')\n", "plt.ylabel('Latitude (degrees)')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Grids" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
***************************************************************************************************************************************
                                                                                                                                       
                                                                                                                                       
                                                                                                                                       
               0**********************************************************************************************************             
                *                    *                    *                    *                    *                    *             
               0**********************************************************************************************************             
                *                    *      *           * *                   **   *   *   *    *   *  *              *  *             
                **********************************************************************************************************             
               0*05                *** **  **      *  * **** ** **   *  **** ***  ******* ****** ** **** **   *** ***** **             
                **********************************************************************************************************             
                *    **      ** ** **** ***** * *** **** ** ****   ***** ****  ***       *    *     *   ** *     *       *             
               0**********************************************************************************************************             
                ** **  * *** * *     *                    *                    *                    *                    *             
               −**********************************************************************************************************             
                ****                 *                    *                    *                    *                    *             
                **********************************************************************************************************             
               −*.**                 *                    *                    *                    *                    *             
                **********************************************************************************************************             
                * *                  *                    *                    *                    *                    *             
               −**********************************************************************************************************             
                0                    2                    4                    6                    8                    10            
                                                                                                                                       
               −10                                                                                                                     
                **********************************************************************************************************             
               −20      *            *      ***           *                    *                 ****      **        **  *             
                ****   **         ** *      ***         ***       **        ** *                *****      ***       *****             
               −30     **         ****      * **        ***      ***       *****      ***       * ***      ***       *****             
                *      * *       *  *       *  *      ****       ***       *****      ****      *  *      ** **      *  **             
               −40**   * **      *  *      **  *      ** *      *   *      *  **      *  *      *  *      ** **      *****             
                *      *  *      *  **    **   *      *  **     *   *      *   *     **  *     *    *     *   *     **   *             
               −50    *    *     *   *    *    **    **  **     *   **    *    *    **   **    *    *     *   **    *    *             
             Power Spectral Density (dB/Hz)     *    *    *    **    *    *    *    **    *   **    *    *     *    * ****             
               −60    *    **   *    **   *      *   *    *    *     *   **     *   *     *   *     **   *     *    *    *             
                ***  *      *   *     *   *      *   *    **  **     **  **     *   *     **  *      *   *     **  *     *             
               −70****      *  **     *   *      * ***     * ***      *  *      *  **     **  *      ** **     *** *  ****             
                * ***       *  *      ** **      * **      ****       * **      ** **      ** *      ****       ***      *             
               −80***       ****     * ***       ***      * ***       * **     * ***        **      * ***        **   ****             
                *            **      * **        **       *           ***      * **         **      *  *                 *             
                *            **      *  *                 *           **       *                    *                    *             
               −90********************************************************************************************************             
                0                    10                   20                   30                   40                   50            
                                                                                                                                       
                                                                    Frequency                                                          
                                                                                                                                       
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'\\n% compare with MATLAB\\ndt = 0.01;\\nt = [0:dt:10];\\nnse = randn(size(t));\\nr = exp(-t/0.05);\\ncnse = conv(nse, r)*dt;\\ncnse = cnse(1:length(t));\\ns = 0.1*sin(2*pi*t) + cnse;\\n\\nsubplot(211)\\nplot(t,s)\\nsubplot(212)\\npsd(s, 512, 1/dt)\\n\\n'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "dt = 0.01\n", "t = np.arange(0, 10, dt)\n", "nse = np.random.randn(len(t))\n", "r = np.exp(-t/0.05)\n", "\n", "cnse = np.convolve(nse, r)*dt\n", "cnse = cnse[:len(t)]\n", "s = 0.1*np.sin(2*np.pi*t) + cnse\n", "\n", "plt.subplot(211)\n", "plt.plot(t, s)\n", "plt.subplot(212)\n", "plt.psd(s, 512, 1/dt)\n", "\n", "plt.show()\n", "\n", "\"\"\"\n", "% compare with MATLAB\n", "dt = 0.01;\n", "t = [0:dt:10];\n", "nse = randn(size(t));\n", "r = exp(-t/0.05);\n", "cnse = conv(nse, r)*dt;\n", "cnse = cnse(1:length(t));\n", "s = 0.1*sin(2*pi*t) + cnse;\n", "\n", "subplot(211)\n", "plot(t,s)\n", "subplot(212)\n", "psd(s, 512, 1/dt)\n", "\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
***************************************************************************************************************************************
                                                                                                                                       
                                                                                                                                       
                                                                                                                                       
               0*************************************************        *************************************************             
               0****   ******  * ***    *  *** *      ** **  ****        ********  *        *         *        *      ****             
                ********************************** ** ** *** ****        ********* *        *         *        *      ****             
               0*************************************************        **      ****       *         *        *         *             
                *************************************************        ****     *******                             ****             
               0*************************************************        ****         ****             *              ****             
                *************************************************        ****            *******  ********* ***       ****             
               −*************************************************        ****             ** ********     *******     ****             
               −*********** ** **************** *********** *****        ****      *        *   **    *        ****   ****             
                ********* *  * ***** ********** *  * ****** *****        ****      *        *    *    *        *  ********             
               −*.15      *        *         *        *         *        *         *        *         *        *      ****             
                *************************************************        *************************************************             
               30                                                       30                                                             
                *************************************************        *************************************************             
               25***      *        *         *        *      ****       20***      *    *   *     *  **  *     *      ****             
                **        *        *         *        *         *       10***      *    * * *  *  *  **  **    ***  * ****             
               20***      *        *         *        *      ****       0*       * * *********** ** **********************             
                **                                              *        ****   ******************************************             
               15***                                         ****       −10******************** *****      * *        ****             
             Magnitude (energy)                                 *     Magnitude (dB)*  *                              ****             
                ****      *        *         *        *      ****       −30* *     *        *         *        *         *             
               5**        *        *         *        *         *       −****      *        *         *        *      ****             
                ****      *        *         *        *      ****        ****      *        *         *        *      ****             
               0*************************************************       −*************************************************             
                0        10        20       30        40       50        0        10        20       30        40       50             
               4*************************************************       140                                                            
               3*   *    **   *    *   *Frequency    **   *   ***       120                      Frequency                             
                *****   ***  **   **  ***  ***  *    **   *  ****       100                                                            
               2*  ***  ***  **   **  * *  ***  **   **  ***  ***       80                                                             
               1**** *  ***  **  ***  * *  **   **  ***  * * ****                                                                      
               0*  * * ** * **** * *  * * **** ***  * *  * *  * *       60                                                             
             Angle (radians)*  * * ** * * ** * * ** * ** * * ****     Phase (radians)                                                  
               −1*** * ** * *  ***  *** ***  * *  * * ** * * ****       20                                                             
               −2*** ***  * *  **  **** ***  ***  * * ***  ***  *       0                                                              
               −3***  **  ***  **  ***   **  ***  *** ***   *****       −20                                                            
                ***   **  **    *  **    **  **   **  *     *   *                                                                      
               −4************************************************       −40                                                            
                0        10        20       30        40       50        0        10        20       30        40       50             
                                                                                                                                       
                                        Frequency                                                Frequency                             
                                                                                                                                       
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "dt = 0.01\n", "Fs = 1/dt\n", "t = np.arange(0, 10, dt)\n", "nse = np.random.randn(len(t))\n", "r = np.exp(-t/0.05)\n", "\n", "cnse = np.convolve(nse, r)*dt\n", "cnse = cnse[:len(t)]\n", "s = 0.1*np.sin(2*np.pi*t) + cnse\n", "\n", "plt.subplot(3, 2, 1)\n", "plt.plot(t, s)\n", "\n", "plt.subplot(3, 2, 3)\n", "plt.magnitude_spectrum(s, Fs=Fs)\n", "\n", "plt.subplot(3, 2, 4)\n", "plt.magnitude_spectrum(s, Fs=Fs, scale='dB')\n", "\n", "plt.subplot(3, 2, 5)\n", "plt.angle_spectrum(s, Fs=Fs)\n", "\n", "plt.subplot(3, 2, 6)\n", "plt.phase_spectrum(s, Fs=Fs)\n", "\n", "plt.show()" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }