Homework 1
- Due Apr 13, 2017 by 11:59pm
- Points 10
- Submitting a file upload
IMPORTANT
See these notes on how to turn in assignments and assign credit.
INTRODUCTION
In this homework you will do two things:
- Install python/scipy on a computer
- Write a program to invert a 2d Fourier transform and get a recognizable image
We'll talk about these things in detail below. Using computers for interesting scientific research extremely useful, especially in biophysics. Though languages like C++ can be daunting, python and scipy have become popular because they're a lot easier to use. Here are instruction on how to install the software you'll need, and links to tutorials on python and SciPy. Everyone in this class must get python and SciPy working for them on some computer that they have access to. Otherwise it will be impossible to collaborate effectively on the class projects
Test your installation
As described in the installation page above, you should test your installation on the two examples given. Also notes that different platforms sometimes require different code. If the code here isn't working (e.g. the window is black) then instead, download this alternate zip file Download zip file and see if you have better luck. It also contains a complete set of all the code with slight modifications.
- Every student in the class should take screenshots of the simulations as they're running and upload them along with the rest of their homework.
INVERT AN FFT OF A 2D IMAGE
Now for fun stuff. Open the zip file Download zip file. You'll see a bunch of images in png format. Pick one of these and save it somewhere, (it's pretty small.)
This is the real part of the 2d fourier transform of some image which should be fairly easily recognizable biological organism, if you've decoded it correctly. Decoding involves mostly inverting a 2d fourier transform. To start with, this is the code Download this is the code that I used to encode the original images. If you don't know scipy already, you must read all the comments in this file very carefully in order to be able to understand enough to write the decoding part. The syntax of python is quite neat. White space matters and indentation must be used. It plays the same role as "{}" in C or java. Comments start out with #'s.
Now you're almost ready to write display_image.py. Fill in this code Download this code with the extra code that is alluded to in the comments (# means a comment).
Get an ipython shell, if you're in linux, open a terminal and type
ipython -pylab
(Note for mac users: There seems to be a bug in one of the older installations of ipython and typing the command will use all of your CPU. Instead type
ipython --pylab=tk
You can also just type directly into a terminal:
python program_name.py if you're having trouble with python.)
Then wait and you'll get a prompt. After the prompt enter:
run display_image.py
it'll run python for you. You can use ipython in windows too. To access ipython, if you're using pythonxy, go to "all programs"->Python(x,y)->Python(x,y). Under "interactive consoles" chose Python(x,y) (Qt). Then to the right of this, click on the first icon "open an interactive shell in console ...". Then you'll get a new window with ipython. Change directory (cd) to where your image is:
cd directory_with_image
and then
run display_image.py
There will probably be bugs. The ones involving syntax normally involve googling. The real fun begins when the program runs but gives you the wrong results. Under ipython, typing in the name of a variable gives it's contents. You can fiddle around in ipython by executing python commands. If you don't like this, you can also use eclipse.
When you have it all working, what do you see?
Now that hopefully you see an image, there is an extra step at the bottom of the hints file that will make it look a little better. Comment the imshow line with a # and uncomment the lines below it. This will make the image look a little better. What is that extra bit of code doing?
INTERPRETATION OF RESULT
- 1. Images involve real data, not complex numbers, so in this algorithm,
the real part was taken. To understand this, solve the following problem. Suppose the fourier transform (FT) g(x) = G(k), that is FT{g(x)} = G(k). In general G(k) is complex. If we invert this by applying and inverse fourier transform (IFT), IFT{G(k)} = g(x).
What is IFT{real(G(k)}? Express you answer in terms of the function g.
- 2. What is the (1D) fourier transform of cos(ax)?
Here "a" is a parameter.
plot(real(fft(cos(arange(128)*10*2*pi/128))))
Try changing this to:
plot(real(fft(cos(arange(128)*25*2*pi/128))))
Compare this is with the analytical result for cos(ax) You're really calculating a discrete fourier tranform (DFT)
For a discussion of DFT's see http://texas.math.ttu.edu/~gilliam/ttu/dft.pdf Links to an external site. and http://en.wikipedia.org/wiki/Discrete_Fourier_transform Links to an external site.
Reading on Fourier Transforms
Great intuitive introduction with many links to interesting material:
http://www.ysbl.york.ac.uk/~cowtan/fourier/fourier.html Links to an external site.
This is explained without a lot of math but gives you a visual idea of how Fourier Transforms work. You are expected to understand Fourier Transforms and the Convolution Theorem (also described there) at this level.