{ "metadata": { "name": "", "signature": "sha256:cba062516ac327925ed2d794a00f5995ddce7ee22703ea16d6e799b96193f3c7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib.animation as animation\n", "\n", "xdim = 400\n", "ydim =400\n", "freq = 1. # spatial frequency of the grating\n", "theta = np.pi/3. #orientation of the grating" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "#rectangular mask\n", "\n", "from matplotlib import path\n", "\n", "nx, ny = xdim/2, ydim/2\n", "xoff,yoff = 20,10\n", "\n", "poly_verts = [(xoff,ny-yoff), (xdim-xoff,ny-yoff), (xdim-xoff,ny+yoff),(xoff,ny+yoff)]\n", "\n", "x, y = np.meshgrid(np.arange(xdim), np.arange(ydim))\n", "x, y = x.flatten(), y.flatten()\n", "points = np.vstack((x,y)).T\n", "\n", "p = path.Path(poly_verts)\n", "rectImg = p.contains_points(points)\n", "rectImg = rectImg.reshape((ydim,xdim))\n", "\n", "#print grid" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "#circular mask\n", "\n", "imageSize = xdim;\n", "r = (imageSize-1)/4; cx = cy = imageSize/2; \n", "[X, Y] = np.meshgrid(np.arange(imageSize), np.arange(imageSize))\n", "diskImg = (X-cx)**2 + (Y-cy)**2 <= r**2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "#compare grid = diskImg with grid = rectImg\n", "\n", "grid = diskImg\n", "#grid = rectImg\n", "\n", "fig = plt.figure()\n", "\n", "def grating(x, y):\n", " temp = np.cos((2.*(np.pi)*freq)*(np.cos(theta)*x + np.sin(theta)*y))\n", " return temp\n", "\n", "x = np.linspace(0, 2 * np.pi, xdim)\n", "y = np.linspace(0, 2 * np.pi, ydim).reshape(-1, 1)\n", "\n", "im = plt.imshow(grating(x, y), cmap=plt.get_cmap('Greys'))\n", "\n", "def updatefig(*args):\n", " global x,y\n", " x += np.pi / 15.\n", "# im.set_array(grating(x,y)*diskImg)\n", " im.set_array(grating(x,y)*grid)\n", " return im\n", "\n", "ani = animation.FuncAnimation(fig, updatefig, interval=10, blit=False) # blit needs to be set to False\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Exercise: Try replacing the above diskImg mask with rectImg" ] } ], "metadata": {} } ] }