{
"metadata": {
"name": "",
"signature": "sha256:569e19645f494b174516af445bf98d214069d54c0140047482476fc7965214cc"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.core.display import HTML\n",
"css_file = './example.css'\n",
"HTML(open(css_file, \"r\").read())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
""
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" This example by Aron Ahmadia and David Ketcheson is licensed under a Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)."
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Aliasing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A numerical grid has a limited resolution. If you try to represent a rapidly-oscillating function with relatively few grid points, you will observe an effect known as **aliasing**. This naturally limits the range of frequencies that can be modelled on a given grid. It can also lead to instabilities in pseudospectral simulations, when generation of high frequencies leads to buildup of lower-frequency energy due to aliasing."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The code below plots a sine wave of a given frequency, along with its representation on a grid with $m$ points. Try changing $p$ and notice how for $m<2p$ the function looks like a lower-frequency mode."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from IPython.display import display, HTML\n",
"from IPython.html import widgets\n",
"from IPython.html.widgets import interact, interactive\n",
"\n",
"def plot_sine(wavenumber=4,grid_points=12,plot_sine='On'):\n",
" \"Plot sin(2*pi*p), sampled at m equispaced points.\"\n",
" x = np.linspace(0,1,grid_points+2); # grid\n",
" xf = np.linspace(0,1,1000) # fine grid\n",
" y = np.sin(wavenumber*np.pi*x)\n",
" yf = np.sin(wavenumber*np.pi*xf)\n",
" fig = plt.figure(figsize = (8, 6));\n",
" ax = fig.add_subplot(1,1,1);\n",
" if plot_sine == 'On':\n",
" ax.plot(xf, yf, 'r-', linewidth=2);\n",
" ax.plot(x, y, 'o-', lw=2)\n",
"\n",
"interact(plot_sine, wavenumber=(1,44,1), grid_points=(10, 16, 1), plot_sine=(('On','Off')));"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try to answer the questions below with pencil and paper; then check them by modifying the code above.\n",
"\n",
"1. For a given number of grid points $m$, which wavenumbers $p$ will be aliased to the $p=0$ mode? Which will be aliased to $p=1$? Can you explain why?\n",
"2. What is the highest frequency mode that can be represented on a given grid?"
]
}
],
"metadata": {}
}
]
}